Skip to content

Commit 9b63f0a

Browse files
committed
iss1748 - Really rough ASCII input that uses final line as answer
1 parent 68b3bfa commit 9b63f0a

8 files changed

Lines changed: 19 additions & 160 deletions

File tree

amd/build/stackjsvle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/stackjsvle.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/stackjsvle.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,11 @@ define([
404404
response['input-readonly'] = input.hasAttribute('disabled');
405405
} else if (input.nodeName.toLowerCase() === 'textarea') {
406406
response.value = input.value;
407-
response['input-type'] = 'textarea';
407+
if (input.dataset.stackInputType === 'ascii') {
408+
response['input-type'] = 'ascii';
409+
} else {
410+
response['input-type'] = 'textarea';
411+
}
408412
response['input-readonly'] = input.hasAttribute('disabled');
409413
} else if (input.type === 'checkbox') {
410414
response.value = input.checked;

corsscripts/ascii/stackascii.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default function init(markdownId) {
55
clearTimeout(debounceTimer);
66
debounceTimer = setTimeout(renderMath(), 100); // debounce 100ms
77
});
8+
renderMath();
89
}
910

1011
var markdownContainerId = null;

edit_stack_form.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ protected function definition_input($inputname, MoodleQuickForm $mform, $counts)
760760
$inputname . 'checkanswertype',
761761
$inputname . 'type',
762762
'in',
763-
['radio', 'checkbox', 'dropdown', 'boolean', 'textarea', 'equiv', 'string', 'json', 'notes', 'parsons']
763+
['radio', 'checkbox', 'dropdown', 'boolean', 'textarea',
764+
'ascii', 'equiv', 'string', 'json', 'notes', 'parsons',]
764765
);
765766

766767
$mform->addElement(

lang/en/qtype_stack.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
$string['inputtype_help'] = 'This determines the type of the input element, e.g. form field, true/false, text area.';
190190
$string['inputtype_link'] = '%%WWWROOT%%/question/type/stack/doc/doc.php/Authoring/Inputs.md';
191191
$string['inputtypealgebraic'] = 'Algebraic input';
192+
$string['inputtypeascii'] = 'ASCII input';
192193
$string['inputtypeboolean'] = 'True/False';
193194
$string['inputtypedropdown'] = 'Drop down list';
194195
$string['inputtypecheckbox'] = 'Checkbox';

stack/cas/castext2/blocks/ascii.block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with STACK. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Add description here!
18+
* An ASCIIMaths display block - e.g. [[ascii input="ans1"]][[/ascii]]
1919
* @package qtype_stack
2020
* @copyright 2024 University of Edinburgh.
2121
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.

stack/input/ascii/ascii.class.php

Lines changed: 7 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@
2929
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3030
*/
3131
class 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

Comments
 (0)