PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/calculatedmulti/edit_calculatedmulti_form.php

https://bitbucket.org/moodle/moodle
PHP | 339 lines | 245 code | 44 blank | 50 comment | 41 complexity | a30a554120ec14bc83ca1271d9dde62e 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 calculated multiple-choice questions.
  18. *
  19. * @package qtype
  20. * @subpackage calculatedmulti
  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. /**
  26. * Calculated multiple-choice question editing form.
  27. *
  28. * @copyright 2007 Jamie Pratt me@jamiep.org
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class qtype_calculatedmulti_edit_form extends question_edit_form {
  32. /**
  33. * Handle to the question type for this question.
  34. *
  35. * @var question_calculatedmulti_qtype
  36. */
  37. public $qtypeobj;
  38. public $questiondisplay;
  39. public $initialname = '';
  40. public $reload = false;
  41. public function __construct($submiturl, $question, $category,
  42. $contexts, $formeditable = true) {
  43. $this->question = $question;
  44. $this->qtypeobj = question_bank::get_qtype('calculatedmulti');
  45. $this->reload = optional_param('reload', false, PARAM_BOOL);
  46. if (!$this->reload) {
  47. // Use database data as this is first pass.
  48. if (isset($this->question->id)) {
  49. // Remove prefix #{..}# if exists.
  50. $this->initialname = $question->name;
  51. $question->name = question_bank::get_qtype('calculated')
  52. ->clean_technical_prefix_from_question_name($question->name);
  53. }
  54. }
  55. parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
  56. }
  57. protected function can_preview() {
  58. return false; // Generally not possible for calculated multi-choice questions on this page.
  59. }
  60. public function get_per_answer_fields($mform, $label, $gradeoptions,
  61. &$repeatedoptions, &$answersoption) {
  62. $repeated = array();
  63. $answeroptions = array();
  64. $answeroptions[] = $mform->createElement('text', 'answer',
  65. $label, array('size' => 50));
  66. $answeroptions[] = $mform->createElement('select', 'fraction',
  67. get_string('gradenoun'), $gradeoptions);
  68. $repeated[] = $mform->createElement('group', 'answeroptions',
  69. $label, $answeroptions, null, false);
  70. // Added answeroptions help button in definition_inner() after called to add_per_answer_fields.
  71. $repeatedoptions['answer']['type'] = PARAM_RAW;
  72. $repeatedoptions['fraction']['default'] = 0;
  73. $answersoption = 'answers';
  74. $mform->setType('answer', PARAM_NOTAGS);
  75. $repeated[] = $mform->createElement('hidden', 'tolerance');
  76. $repeated[] = $mform->createElement('hidden', 'tolerancetype', 1);
  77. $repeatedoptions['tolerance']['type'] = PARAM_FLOAT;
  78. $repeatedoptions['tolerance']['default'] = 0.01;
  79. $repeatedoptions['tolerancetype']['type'] = PARAM_INT;
  80. // Create display group.
  81. $answerdisplay = array();
  82. $answerdisplay[] = $mform->createElement('select', 'correctanswerlength',
  83. get_string('answerdisplay', 'qtype_calculated'), range(0, 9));
  84. $repeatedoptions['correctanswerlength']['default'] = 2;
  85. $answerlengthformats = array(
  86. '1' => get_string('decimalformat', 'qtype_numerical'),
  87. '2' => get_string('significantfiguresformat', 'qtype_calculated')
  88. );
  89. $answerdisplay[] = $mform->createElement('select', 'correctanswerformat',
  90. get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
  91. $repeated[] = $mform->createElement('group', 'answerdisplay',
  92. get_string('answerdisplay', 'qtype_calculated'), $answerdisplay, null, false);
  93. // Add feedback.
  94. $repeated[] = $mform->createElement('editor', 'feedback',
  95. get_string('feedback', 'question'), null, $this->editoroptions);
  96. return $repeated;
  97. }
  98. protected function definition_inner($mform) {
  99. $label = get_string('sharedwildcards', 'qtype_calculated');
  100. $mform->addElement('hidden', 'initialcategory', 1);
  101. $mform->addElement('hidden', 'reload', 1);
  102. $mform->setType('initialcategory', PARAM_INT);
  103. $mform->setType('reload', PARAM_BOOL);
  104. $html2 = '';
  105. $mform->insertElementBefore(
  106. $mform->createElement('static', 'listcategory', $label, $html2), 'name');
  107. if (isset($this->question->id)) {
  108. $mform->insertElementBefore($mform->createElement('static', 'initialname',
  109. get_string('questionstoredname', 'qtype_calculated'),
  110. format_string($this->initialname)), 'name');
  111. };
  112. $addfieldsname = 'updatecategory';
  113. $addstring = get_string('updatecategory', 'qtype_calculated');
  114. $mform->registerNoSubmitButton($addfieldsname);
  115. $this->editasmultichoice = 1;
  116. $mform->insertElementBefore(
  117. $mform->createElement('submit', $addfieldsname, $addstring), 'listcategory');
  118. $mform->registerNoSubmitButton('createoptionbutton');
  119. $mform->addElement('hidden', 'multichoice', $this->editasmultichoice);
  120. $mform->setType('multichoice', PARAM_INT);
  121. $menu = array(get_string('answersingleno', 'qtype_multichoice'),
  122. get_string('answersingleyes', 'qtype_multichoice'));
  123. $mform->addElement('select', 'single',
  124. get_string('answerhowmany', 'qtype_multichoice'), $menu);
  125. $mform->setDefault('single', 1);
  126. $mform->addElement('advcheckbox', 'shuffleanswers',
  127. get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
  128. $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
  129. $mform->setDefault('shuffleanswers', 1);
  130. $numberingoptions = question_bank::get_qtype('multichoice')->get_numbering_styles();
  131. $mform->addElement('select', 'answernumbering',
  132. get_string('answernumbering', 'qtype_multichoice'), $numberingoptions);
  133. $mform->setDefault('answernumbering', 'abc');
  134. $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
  135. question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
  136. $mform->addHelpButton('answeroptions[0]', 'answeroptions', 'qtype_calculatedmulti');
  137. $repeated = array();
  138. $nounits = optional_param('nounits', 1, PARAM_INT);
  139. $mform->addElement('hidden', 'nounits', $nounits);
  140. $mform->setType('nounits', PARAM_INT);
  141. $mform->setConstants(array('nounits'=>$nounits));
  142. for ($i = 0; $i < $nounits; $i++) {
  143. $mform->addElement('hidden', 'unit'."[{$i}]",
  144. optional_param("unit[{$i}]", '', PARAM_NOTAGS));
  145. $mform->setType('unit'."[{$i}]", PARAM_NOTAGS);
  146. $mform->addElement('hidden', 'multiplier'."[{$i}]",
  147. optional_param("multiplier[{$i}]", '', PARAM_FLOAT));
  148. $mform->setType("multiplier[{$i}]", PARAM_FLOAT);
  149. }
  150. $this->add_combined_feedback_fields(true);
  151. $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
  152. $this->add_interactive_settings(true, true);
  153. // Hidden elements.
  154. $mform->addElement('hidden', 'synchronize', '');
  155. $mform->setType('synchronize', PARAM_INT);
  156. if (isset($this->question->options) && isset($this->question->options->synchronize)) {
  157. $mform->setDefault('synchronize', $this->question->options->synchronize);
  158. } else {
  159. $mform->setDefault('synchronize', 0);
  160. }
  161. $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
  162. $mform->setType('wizard', PARAM_ALPHA);
  163. }
  164. public function data_preprocessing($question) {
  165. $question = parent::data_preprocessing($question);
  166. $question = $this->data_preprocessing_answers($question, false);
  167. $question = $this->data_preprocessing_combined_feedback($question, true);
  168. $question = $this->data_preprocessing_hints($question, true, true);
  169. if (isset($question->options)) {
  170. $question->synchronize = $question->options->synchronize;
  171. $question->single = $question->options->single;
  172. $question->answernumbering = $question->options->answernumbering;
  173. $question->shuffleanswers = $question->options->shuffleanswers;
  174. }
  175. return $question;
  176. }
  177. protected function data_preprocessing_answers($question, $withanswerfiles = false) {
  178. $question = parent::data_preprocessing_answers($question, $withanswerfiles);
  179. if (empty($question->options->answers)) {
  180. return $question;
  181. }
  182. $key = 0;
  183. foreach ($question->options->answers as $answer) {
  184. // See comment in the parent method about this hack.
  185. unset($this->_form->_defaultValues["tolerance[{$key}]"]);
  186. unset($this->_form->_defaultValues["tolerancetype[{$key}]"]);
  187. unset($this->_form->_defaultValues["correctanswerlength[{$key}]"]);
  188. unset($this->_form->_defaultValues["correctanswerformat[{$key}]"]);
  189. $question->tolerance[$key] = $answer->tolerance;
  190. $question->tolerancetype[$key] = $answer->tolerancetype;
  191. $question->correctanswerlength[$key] = $answer->correctanswerlength;
  192. $question->correctanswerformat[$key] = $answer->correctanswerformat;
  193. $key++;
  194. }
  195. return $question;
  196. }
  197. /**
  198. * Validate the equations in the some question content.
  199. * @param array $errors where errors are being accumulated.
  200. * @param string $field the field being validated.
  201. * @param string $text the content of that field.
  202. * @return array the updated $errors array.
  203. */
  204. protected function validate_text($errors, $field, $text) {
  205. $problems = qtype_calculated_find_formula_errors_in_text($text);
  206. if ($problems) {
  207. $errors[$field] = $problems;
  208. }
  209. return $errors;
  210. }
  211. public function validation($data, $files) {
  212. $errors = parent::validation($data, $files);
  213. // Verifying for errors in {=...} in question text.
  214. $errors = $this->validate_text($errors, 'questiontext', $data['questiontext']['text']);
  215. $errors = $this->validate_text($errors, 'generalfeedback', $data['generalfeedback']['text']);
  216. $errors = $this->validate_text($errors, 'correctfeedback', $data['correctfeedback']['text']);
  217. $errors = $this->validate_text($errors, 'partiallycorrectfeedback', $data['partiallycorrectfeedback']['text']);
  218. $errors = $this->validate_text($errors, 'incorrectfeedback', $data['incorrectfeedback']['text']);
  219. $answers = $data['answer'];
  220. $answercount = 0;
  221. $maxgrade = false;
  222. $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
  223. $mandatorydatasets = array();
  224. foreach ($answers as $key => $answer) {
  225. $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
  226. }
  227. if (count($mandatorydatasets) == 0) {
  228. foreach ($answers as $key => $answer) {
  229. $errors['answeroptions['.$key.']'] =
  230. get_string('atleastonewildcard', 'qtype_calculated');
  231. }
  232. }
  233. $totalfraction = 0;
  234. $maxfraction = -1;
  235. foreach ($answers as $key => $answer) {
  236. $trimmedanswer = trim($answer);
  237. $fraction = (float) $data['fraction'][$key];
  238. if (empty($trimmedanswer) && $trimmedanswer != '0' && empty($fraction)) {
  239. continue;
  240. }
  241. if (empty($trimmedanswer)) {
  242. $errors['answeroptions['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
  243. }
  244. if ($trimmedanswer != '' || $answercount == 0) {
  245. // Verifying for errors in {=...} in answer text.
  246. $errors = $this->validate_text($errors, 'answeroptions[' . $key . ']', $answer);
  247. $errors = $this->validate_text($errors, 'feedback[' . $key . ']',
  248. $data['feedback'][$key]['text']);
  249. }
  250. if ($trimmedanswer != '') {
  251. if ('2' == $data['correctanswerformat'][$key] &&
  252. '0' == $data['correctanswerlength'][$key]) {
  253. $errors['correctanswerlength['.$key.']'] =
  254. get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
  255. }
  256. if (!is_numeric($data['tolerance'][$key])) {
  257. $errors['tolerance['.$key.']'] =
  258. get_string('xmustbenumeric', 'qtype_numerical',
  259. get_string('acceptederror', 'qtype_numerical'));
  260. }
  261. if ($data['fraction'][$key] > 0) {
  262. $totalfraction += $data['fraction'][$key];
  263. }
  264. if ($data['fraction'][$key] > $maxfraction) {
  265. $maxfraction = $data['fraction'][$key];
  266. }
  267. $answercount++;
  268. }
  269. }
  270. if ($answercount == 0) {
  271. $errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
  272. $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
  273. } else if ($answercount == 1) {
  274. $errors['answeroptions[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
  275. }
  276. // Perform sanity checks on fractional grades.
  277. if ($data['single']== 1 ) {
  278. if ($maxfraction != 1) {
  279. $errors['answeroptions[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
  280. $maxfraction * 100);
  281. }
  282. } else {
  283. $totalfraction = round($totalfraction, 2);
  284. if ($totalfraction != 1) {
  285. $totalfraction = $totalfraction * 100;
  286. $errors['answeroptions[0]'] =
  287. get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
  288. }
  289. }
  290. return $errors;
  291. }
  292. public function qtype() {
  293. return 'calculatedmulti';
  294. }
  295. }