2929 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3030 */
3131class stack_ascii_input extends stack_textarea_input {
32- // phpcs:ignore moodle.Commenting.VariableComment.Missing
33- protected $ extraoptions = [
34- 'hideanswer ' => false ,
35- 'allowempty ' => false ,
36- 'nounits ' => true ,
37- 'simp ' => false ,
38- 'consolidatesubscripts ' => false ,
39- ];
40-
4132 // phpcs:ignore moodle.Commenting.MissingDocblock.Function
4233 public function render (stack_input_state $ state , $ fieldname , $ readonly , $ tavalue ) {
4334 // Note that at the moment, $this->boxHeight and $this->boxWidth are only
@@ -80,7 +71,7 @@ public function render(stack_input_state $state, $fieldname, $readonly, $tavalue
8071 }
8172
8273 // Metadata for JS users.
83- $ attributes ['data-stack-input-type ' ] = 'textarea ' ;
74+ $ attributes ['data-stack-input-type ' ] = 'ascii ' ;
8475 if ($ this ->options ->get_option ('decimals ' ) === ', ' ) {
8576 $ attributes ['data-stack-input-decimal-separator ' ] = ', ' ;
8677 $ attributes ['data-stack-input-list-separator ' ] = '; ' ;
@@ -92,54 +83,6 @@ public function render(stack_input_state $state, $fieldname, $readonly, $tavalue
9283 return html_writer::tag ('textarea ' , htmlspecialchars ($ current , ENT_COMPAT ), $ attributes );
9384 }
9485
95- // phpcs:ignore moodle.Commenting.MissingDocblock.Function
96- public function render_api_data ($ tavalue ) {
97- if ($ this ->errors ) {
98- throw new stack_exception ("Error rendering input: " . implode (', ' , $ this ->errors ));
99- }
100-
101- $ data = [];
102-
103- $ data ['type ' ] = 'textarea ' ;
104- $ data ['boxWidth ' ] = $ this ->parameters ['boxWidth ' ];
105- $ data ['syntaxHint ' ] = $ this ->maxima_to_raw_input ($ this ->parameters ['syntaxHint ' ]);
106-
107- return $ data ;
108- }
109-
110- // phpcs:ignore moodle.Commenting.MissingDocblock.Function
111- public function add_to_moodleform_testinput (MoodleQuickForm $ mform ) {
112- $ mform ->addElement ('text ' , $ this ->name , $ this ->name , ['size ' => $ this ->parameters ['boxWidth ' ]]);
113- $ mform ->setDefault ($ this ->name , $ this ->parameters ['syntaxHint ' ]);
114- $ mform ->setType ($ this ->name , PARAM_RAW );
115- }
116-
117- /**
118- * Transforms the student's response input into an array.
119- * Most return the same as went in.
120- *
121- * @param array|string $in
122- * @return string
123- */
124- protected function response_to_contents ($ response ) {
125- $ contents = [];
126- if (array_key_exists ($ this ->name , $ response )) {
127- $ sans = $ response [$ this ->name ];
128- if (trim ($ sans ) == '' && $ this ->get_extra_option ('allowempty ' )) {
129- return ['EMPTYANSWER ' ];
130- }
131- $ rowsin = explode ("\n" , $ sans );
132- $ rowsout = [];
133- foreach ($ rowsin as $ key => $ row ) {
134- $ cleanrow = trim ($ row );
135- if ($ cleanrow !== '' ) {
136- $ contents [] = $ cleanrow ;
137- }
138- }
139- }
140- return $ contents ;
141- }
142-
14386 // phpcs:ignore moodle.Commenting.MissingDocblock.Function
14487 protected function caslines_to_answer ($ caslines , $ secrules = false ) {
14588 $ vals = [];
@@ -154,6 +97,7 @@ protected function caslines_to_answer($caslines, $secrules = false) {
15497 'nounify ' => 1 ,
15598 'nontuples ' => false ,
15699 ];
100+ $ caslines = [end ($ caslines )];
157101 foreach ($ caslines as $ line ) {
158102 $ str = $ line ->ast_to_string (null , $ params );
159103 if ($ line ->get_valid () || $ str === 'EMPTYANSWER ' ) {
@@ -167,62 +111,6 @@ protected function caslines_to_answer($caslines, $secrules = false) {
167111 return stack_ast_container::make_from_student_source ($ s , '' , $ secrules );
168112 }
169113
170- /**
171- * Transforms the contents array into a maxima expression.
172- *
173- * @param array|string $in
174- * @return string
175- */
176- public function contents_to_maxima ($ contents ) {
177- return '[ ' . implode (', ' , $ contents ) . '] ' ;
178- }
179-
180- /**
181- * Transforms a Maxima list into raw input.
182- *
183- * @param string $in
184- * @return string
185- */
186- protected function maxima_to_raw_input ($ in ) {
187- $ delim = ', ' ;
188- if ($ this ->options ->get_option ('decimals ' ) === ', ' ) {
189- $ delim = '; ' ;
190- }
191- $ values = stack_utils::list_to_array ($ in , false , $ delim );
192- foreach ($ values as $ key => $ val ) {
193- if (trim ($ val ) != '' ) {
194- $ cs = stack_ast_container::make_from_teacher_source ($ val );
195- if ($ cs ->get_valid ()) {
196- $ val = $ cs ->get_inputform (false , 0 );
197- }
198- }
199- $ values [$ key ] = $ val ;
200- }
201- return implode ("\n" , $ values );
202- }
203-
204- // phpcs:ignore moodle.Commenting.MissingDocblock.Function
205- protected function ajax_to_response_array ($ in ) {
206- $ in = explode ('<br> ' , $ in );
207- $ in = implode ("\n" , $ in );
208- return [$ this ->name => $ in ];
209- }
210-
211- /**
212- * Transforms a Maxima expression into an array of raw inputs which are part of a response.
213- * Most inputs are very simple, but textarea and matrix need more here.
214- *
215- * @param string $in
216- * @return string
217- */
218- public function maxima_to_response_array ($ in ) {
219- $ response [$ this ->name ] = $ this ->maxima_to_raw_input ($ in );
220- if ($ this ->requires_validation ()) {
221- $ response [$ this ->name . '_val ' ] = $ in ;
222- }
223- return $ response ;
224- }
225-
226114 /**
227115 * This function constructs the display variable for validation.
228116 *
@@ -242,7 +130,10 @@ protected function validation_display(
242130 $ ilines ,
243131 $ notes
244132 ) {
245-
133+ $ caslines = [end ($ caslines )];
134+ $ ilines = [end ($ ilines )];
135+ $ errors = [end ($ errors )];
136+ $ valid = $ caslines [0 ]->get_valid ();
246137 $ rows = [];
247138 foreach ($ caslines as $ index => $ cs ) {
248139 $ row = [];
@@ -312,7 +203,7 @@ public static function get_parameters_defaults() {
312203 return [
313204 'mustVerify ' => true ,
314205 'showValidation ' => 1 ,
315- 'boxWidth ' => 20 ,
206+ 'boxWidth ' => 30 ,
316207 'insertStars ' => 0 ,
317208 'syntaxHint ' => '' ,
318209 'syntaxAttribute ' => 0 ,
@@ -325,43 +216,4 @@ public static function get_parameters_defaults() {
325216 ];
326217 }
327218
328- /**
329- * Each actual extension of this base class must decide what parameter values are valid.
330- * @return array of parameters names.
331- */
332- public function internal_validate_parameter ($ parameter , $ value ) {
333- $ valid = true ;
334- switch ($ parameter ) {
335- case 'boxWidth ' :
336- $ valid = is_int ($ value ) && $ value > 0 ;
337- break ;
338-
339- case 'boxHeight ' :
340- $ valid = is_int ($ value ) && $ value > 0 ;
341- break ;
342- }
343- return $ valid ;
344- }
345-
346- /**
347- * Add description here.
348- * @return string the teacher's answer, displayed to the student in the general feedback.
349- */
350- public function get_teacher_answer_display ($ value , $ display ) {
351- if ($ this ->get_extra_option ('hideanswer ' )) {
352- return '' ;
353- }
354- $ values = stack_utils::list_to_array ($ value , false );
355- foreach ($ values as $ key => $ val ) {
356- if (trim ($ val ) !== '' ) {
357- $ cs = stack_ast_container::make_from_teacher_source ($ val );
358- $ cs ->get_valid ();
359- $ val = '<code> ' . $ cs ->get_inputform (true , 0 , true , $ this ->options ->get_option ('decimals ' )) . '</code> ' ;
360- }
361- $ values [$ key ] = $ val ;
362- }
363- $ value = "<br/> " . implode ("<br/> " , $ values );
364-
365- return stack_string ('teacheranswershow ' , ['value ' => $ value , 'display ' => $ display ]);
366- }
367219}
0 commit comments