PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/calculated/edit_calculated_form.php

https://bitbucket.org/moodle/moodle
PHP | 284 lines | 181 code | 43 blank | 60 comment | 15 complexity | f5fd99035954049f6f58a4b4cb74b69d MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle 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. // Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Defines the editing form for the calculated question type.
  18. *
  19. * @package qtype
  20. * @subpackage calculated
  21. * @copyright 2007 Jamie Pratt me@jamiep.org
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->dirroot . '/question/type/numerical/edit_numerical_form.php');
  26. /**
  27. * Calculated question type editing form definition.
  28. *
  29. * @copyright 2007 Jamie Pratt me@jamiep.org
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qtype_calculated_edit_form extends qtype_numerical_edit_form {
  33. /**
  34. * Handle to the question type for this question.
  35. *
  36. * @var qtype_calculated
  37. */
  38. public $qtypeobj;
  39. public $questiondisplay;
  40. public $activecategory;
  41. public $categorychanged = false;
  42. public $initialname = '';
  43. public $reload = false;
  44. public function __construct($submiturl, $question, $category, $contexts,
  45. $formeditable = true) {
  46. global $CFG, $DB;
  47. $this->question = $question;
  48. $this->reload = optional_param('reload', false, PARAM_BOOL);
  49. if (!$this->reload) { // Use database data as this is first pass.
  50. if (isset($this->question->id)) {
  51. // Remove prefix #{..}# if exists.
  52. $this->initialname = $question->name;
  53. $question->name = question_bank::get_qtype($this->qtype())
  54. ->clean_technical_prefix_from_question_name($question->name);
  55. }
  56. }
  57. parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
  58. }
  59. public function get_per_answer_fields($mform, $label, $gradeoptions,
  60. &$repeatedoptions, &$answersoption) {
  61. $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions,
  62. $repeatedoptions, $answersoption);
  63. // Reorganise answer options group. 0 is the answer. 1 is tolerance. 2 is Grade.
  64. $answeroptions = $repeated[0]->getElements();
  65. // Tolerance field will be part of its own group.
  66. $tolerance = $answeroptions[1];
  67. // Update Answer options group to contain only answer and grade fields.
  68. $answeroptions[0]->setSize(55);
  69. $answeroptions = array($answeroptions[0], $answeroptions[2]);
  70. $repeated[0]->setElements($answeroptions);
  71. // Update answer field and group label.
  72. $repeated[0]->setLabel(get_string('answerformula', 'qtype_calculated', '{no}') . ' =');
  73. $answeroptions[0]->setLabel(get_string('answerformula', 'qtype_calculated', '{no}') . ' =');
  74. // Get feedback field to re append later.
  75. $feedback = array_pop($repeated);
  76. // Create tolerance group.
  77. $answertolerance = array();
  78. $tolerance->setLabel(get_string('tolerance', 'qtype_calculated') . '=');
  79. $answertolerance[] = $tolerance;
  80. $answertolerance[] = $mform->createElement('select', 'tolerancetype',
  81. get_string('tolerancetype', 'qtype_calculated'), $this->qtypeobj->tolerance_types());
  82. $repeated[] = $mform->createElement('group', 'answertolerance',
  83. get_string('tolerance', 'qtype_calculated'), $answertolerance, null, false);
  84. $repeatedoptions['tolerance']['default'] = 0.01;
  85. // Create display group.
  86. $answerdisplay = array();
  87. $answerdisplay[] = $mform->createElement('select', 'correctanswerlength',
  88. get_string('answerdisplay', 'qtype_calculated'), range(0, 9));
  89. $repeatedoptions['correctanswerlength']['default'] = 2;
  90. $answerlengthformats = array(
  91. '1' => get_string('decimalformat', 'qtype_numerical'),
  92. '2' => get_string('significantfiguresformat', 'qtype_calculated')
  93. );
  94. $answerdisplay[] = $mform->createElement('select', 'correctanswerformat',
  95. get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
  96. $repeated[] = $mform->createElement('group', 'answerdisplay',
  97. get_string('answerdisplay', 'qtype_calculated'), $answerdisplay, null, false);
  98. // Add feedback.
  99. $repeated[] = $feedback;
  100. return $repeated;
  101. }
  102. /**
  103. * Add question-type specific form fields.
  104. *
  105. * @param MoodleQuickForm $mform the form being built.
  106. */
  107. protected function definition_inner($mform) {
  108. $this->qtypeobj = question_bank::get_qtype($this->qtype());
  109. $label = get_string('sharedwildcards', 'qtype_calculated');
  110. $mform->addElement('hidden', 'initialcategory', 1);
  111. $mform->addElement('hidden', 'reload', 1);
  112. $mform->setType('initialcategory', PARAM_INT);
  113. $mform->setType('reload', PARAM_BOOL);
  114. $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
  115. $mform->insertElementBefore(
  116. $mform->createElement('static', 'listcategory', $label, $html2), 'name');
  117. if (isset($this->question->id)) {
  118. $mform->insertElementBefore($mform->createElement('static', 'initialname',
  119. get_string('questionstoredname', 'qtype_calculated'),
  120. format_string($this->initialname)), 'name');
  121. };
  122. $addfieldsname = 'updatecategory';
  123. $addstring = get_string('updatecategory', 'qtype_calculated');
  124. $mform->registerNoSubmitButton($addfieldsname);
  125. $mform->insertElementBefore(
  126. $mform->createElement('submit', $addfieldsname, $addstring), 'listcategory');
  127. $mform->registerNoSubmitButton('createoptionbutton');
  128. // Editing as regular question.
  129. $mform->setType('single', PARAM_INT);
  130. $mform->addElement('hidden', 'shuffleanswers', '1');
  131. $mform->setType('shuffleanswers', PARAM_INT);
  132. $mform->addElement('hidden', 'answernumbering', 'abc');
  133. $mform->setType('answernumbering', PARAM_SAFEDIR);
  134. $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
  135. question_bank::fraction_options(), 1, 1);
  136. $repeated = array();
  137. $this->add_unit_options($mform, $this);
  138. $this->add_unit_fields($mform, $this);
  139. $this->add_interactive_settings();
  140. // Hidden elements.
  141. $mform->addElement('hidden', 'synchronize', '');
  142. $mform->setType('synchronize', PARAM_INT);
  143. $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
  144. $mform->setType('wizard', PARAM_ALPHA);
  145. }
  146. protected function can_preview() {
  147. return false; // Generally not possible for calculated questions on this page.
  148. }
  149. public function data_preprocessing($question) {
  150. $question = parent::data_preprocessing($question);
  151. $question = $this->data_preprocessing_answers($question);
  152. $question = $this->data_preprocessing_hints($question);
  153. $question = $this->data_preprocessing_units($question);
  154. $question = $this->data_preprocessing_unit_options($question);
  155. if (isset($question->options->synchronize)) {
  156. $question->synchronize = $question->options->synchronize;
  157. }
  158. return $question;
  159. }
  160. protected function data_preprocessing_answers($question, $withanswerfiles = false) {
  161. $question = parent::data_preprocessing_answers($question, $withanswerfiles);
  162. if (empty($question->options->answers)) {
  163. return $question;
  164. }
  165. $key = 0;
  166. foreach ($question->options->answers as $answer) {
  167. // See comment in the parent method about this hack.
  168. unset($this->_form->_defaultValues["tolerancetype[{$key}]"]);
  169. unset($this->_form->_defaultValues["correctanswerlength[{$key}]"]);
  170. unset($this->_form->_defaultValues["correctanswerformat[{$key}]"]);
  171. $question->tolerancetype[$key] = $answer->tolerancetype;
  172. $question->correctanswerlength[$key] = $answer->correctanswerlength;
  173. $question->correctanswerformat[$key] = $answer->correctanswerformat;
  174. $key++;
  175. }
  176. return $question;
  177. }
  178. public function qtype() {
  179. return 'calculated';
  180. }
  181. /**
  182. * Validate the equations in the some question content.
  183. * @param array $errors where errors are being accumulated.
  184. * @param string $field the field being validated.
  185. * @param string $text the content of that field.
  186. * @return array the updated $errors array.
  187. */
  188. protected function validate_text($errors, $field, $text) {
  189. $problems = qtype_calculated_find_formula_errors_in_text($text);
  190. if ($problems) {
  191. $errors[$field] = $problems;
  192. }
  193. return $errors;
  194. }
  195. public function validation($data, $files) {
  196. $errors = parent::validation($data, $files);
  197. // Verifying for errors in {=...} in question text.
  198. $errors = $this->validate_text($errors, 'questiontext', $data['questiontext']['text']);
  199. $errors = $this->validate_text($errors, 'generalfeedback', $data['generalfeedback']['text']);
  200. // Check that the answers use datasets.
  201. $answers = $data['answer'];
  202. $mandatorydatasets = array();
  203. foreach ($answers as $key => $answer) {
  204. $problems = qtype_calculated_find_formula_errors($answer);
  205. if ($problems) {
  206. $errors['answeroptions['.$key.']'] = $problems;
  207. }
  208. $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
  209. $errors = $this->validate_text($errors, 'feedback[' . $key . ']',
  210. $data['feedback'][$key]['text']);
  211. }
  212. if (empty($mandatorydatasets)) {
  213. foreach ($answers as $key => $answer) {
  214. $errors['answeroptions['.$key.']'] =
  215. get_string('atleastonewildcard', 'qtype_calculated');
  216. }
  217. }
  218. // Validate the answer format.
  219. foreach ($answers as $key => $answer) {
  220. $trimmedanswer = trim($answer);
  221. if (trim($answer)) {
  222. if ($data['correctanswerformat'][$key] == 2 &&
  223. $data['correctanswerlength'][$key] == '0') {
  224. $errors['answerdisplay['.$key.']'] =
  225. get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
  226. }
  227. }
  228. }
  229. return $errors;
  230. }
  231. protected function is_valid_answer($answer, $data) {
  232. return !qtype_calculated_find_formula_errors($answer);
  233. }
  234. protected function valid_answer_message($answer) {
  235. if (!$answer) {
  236. return get_string('mustenteraformulaorstar', 'qtype_numerical');
  237. } else {
  238. return qtype_calculated_find_formula_errors($answer);
  239. }
  240. }
  241. }