|
| 1 | +<?php |
| 2 | +// This file is part of Stack - http://stack.maths.ed.ac.uk/ |
| 3 | +// |
| 4 | +// Stack is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Stack is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Stack. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +defined('MOODLE_INTERNAL') || die(); |
| 18 | + |
| 19 | +require_once(__DIR__ . '/../algebraic/algebraic.class.php'); |
| 20 | +require_once(__DIR__ . '/../string/string.class.php'); |
| 21 | + |
| 22 | +/** |
| 23 | + * A text-field input which is always interpreted as a Maxima string. |
| 24 | + * This is intended for students to type in a complete proof. |
| 25 | + * |
| 26 | + * @package qtype_stack |
| 27 | + * @copyright 2026 University of Edinburgh |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class stack_freetext_input extends stack_string_input { |
| 31 | + // phpcs:ignore moodle.Commenting.VariableComment.Missing |
| 32 | + protected $extraoptions = [ |
| 33 | + 'hideanswer' => false, |
| 34 | + 'allowempty' => false, |
| 35 | + 'validator' => false, |
| 36 | + 'manualgraded' => false, |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * Return the default values for the options. Using this is optional, in this |
| 41 | + * base class implementation, no default options are set. |
| 42 | + * @return array option => default value. |
| 43 | + */ |
| 44 | + public static function get_parameters_defaults() { |
| 45 | + return [ |
| 46 | + 'mustVerify' => true, |
| 47 | + 'showValidation' => 1, |
| 48 | + 'boxWidth' => 80, |
| 49 | + 'insertStars' => 0, |
| 50 | + 'syntaxHint' => '', |
| 51 | + 'syntaxAttribute' => 0, |
| 52 | + 'forbidWords' => '', |
| 53 | + 'allowWords' => '', |
| 54 | + 'forbidFloats' => true, |
| 55 | + 'lowestTerms' => true, |
| 56 | + 'sameType' => true, |
| 57 | + 'options' => '', |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + // phpcs:ignore moodle.Commenting.MissingDocblock.Function |
| 62 | + public function render(stack_input_state $state, $fieldname, $readonly, $tavalue) { |
| 63 | + if ($this->errors) { |
| 64 | + return $this->render_error($this->errors); |
| 65 | + } |
| 66 | + |
| 67 | + // Note that at the moment, $this->boxHeight and $this->boxWidth are only |
| 68 | + // used as minimums. If the current input is bigger, the box is expanded. |
| 69 | + $attributes = [ |
| 70 | + 'name' => $fieldname, |
| 71 | + 'id' => $fieldname, |
| 72 | + 'autocapitalize' => 'none', |
| 73 | + 'spellcheck' => 'false', |
| 74 | + ]; |
| 75 | + |
| 76 | + $value = stack_utils::maxima_string_to_php_string($this->contents_to_maxima($state->contents)); |
| 77 | + if ($this->is_blank_response($state->contents)) { |
| 78 | + $value = stack_utils::maxima_string_to_php_string($this->parameters['syntaxHint']); |
| 79 | + if ($this->parameters['syntaxAttribute'] == '1') { |
| 80 | + $attributes['placeholder'] = $value; |
| 81 | + $value = ''; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: sort out size of text area. |
| 86 | + $attributes['rows'] = 3; |
| 87 | + $attributes['cols'] = $this->parameters['boxWidth']; |
| 88 | + |
| 89 | + if ($readonly) { |
| 90 | + $attributes['readonly'] = 'readonly'; |
| 91 | + } |
| 92 | + |
| 93 | + // Metadata for JS users. |
| 94 | + $attributes['data-stack-input-type'] = 'freetext'; |
| 95 | + |
| 96 | + return html_writer::tag('textarea', htmlspecialchars($value, ENT_COMPAT), $attributes) . |
| 97 | + html_writer::tag('div', "", ['class' => 'clearfix']); |
| 98 | + } |
| 99 | + |
| 100 | + // phpcs:ignore moodle.Commenting.MissingDocblock.Function |
| 101 | + public function render_api_data($tavalue) { |
| 102 | + if ($this->errors) { |
| 103 | + throw new stack_exception("Error rendering input: " . implode(',', $this->errors)); |
| 104 | + } |
| 105 | + |
| 106 | + $data = []; |
| 107 | + |
| 108 | + $data['type'] = 'freetext'; |
| 109 | + $data['boxWidth'] = $this->parameters['boxWidth']; |
| 110 | + $data['syntaxHint'] = $this->parameters['syntaxHint']; |
| 111 | + |
| 112 | + return $data; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * This function constructs the display of variables during validation. |
| 117 | + * |
| 118 | + * @param stack_casstring $answer, the complete answer. |
| 119 | + * @return string any error messages describing validation failures. An empty |
| 120 | + * string if the input is valid - at least according to this test. |
| 121 | + */ |
| 122 | + protected function validation_display( |
| 123 | + $answer, |
| 124 | + $lvars, |
| 125 | + $caslines, |
| 126 | + $additionalvars, |
| 127 | + $valid, |
| 128 | + $errors, |
| 129 | + $castextprocessor, |
| 130 | + $inertdisplayform, |
| 131 | + $ilines, |
| 132 | + $notes |
| 133 | + ) { |
| 134 | + // Always display something sensible. |
| 135 | + $display = htmlentities($this->contents_to_maxima($this->rawcontents)); |
| 136 | + $display = substr($display, 1, strlen($display) - 2); |
| 137 | + if ($answer->is_correctly_evaluated()) { |
| 138 | + $display = stack_utils::maxima_string_to_php_string($answer->get_value()); |
| 139 | + $display = nl2br($display); |
| 140 | + } else { |
| 141 | + $valid = false; |
| 142 | + } |
| 143 | + // TODO: something better here. |
| 144 | + $display = html_writer::tag('p', $display); |
| 145 | + return [$valid, $errors, $display, $notes]; |
| 146 | + } |
| 147 | +} |
0 commit comments