PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/quiz/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 589 lines | 418 code | 83 blank | 88 comment | 58 complexity | 580a5a91fd8712671d7424448879f700 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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 quiz module ettings form.
  18. *
  19. * @package mod
  20. * @subpackage quiz
  21. * @copyright 2006 Jamie Pratt
  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 . '/course/moodleform_mod.php');
  26. require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  27. /**
  28. * Settings form for the quiz module.
  29. *
  30. * @copyright 2006 Jamie Pratt
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class mod_quiz_mod_form extends moodleform_mod {
  34. /** @var array options to be used with date_time_selector fields in the quiz. */
  35. public static $datefieldoptions = array('optional' => true, 'step' => 1);
  36. protected $_feedbacks;
  37. protected static $reviewfields = array(); // Initialised in the constructor.
  38. public function __construct($current, $section, $cm, $course) {
  39. self::$reviewfields = array(
  40. 'attempt' => array('theattempt', 'quiz'),
  41. 'correctness' => array('whethercorrect', 'question'),
  42. 'marks' => array('marks', 'quiz'),
  43. 'specificfeedback' => array('specificfeedback', 'question'),
  44. 'generalfeedback' => array('generalfeedback', 'question'),
  45. 'rightanswer' => array('rightanswer', 'question'),
  46. 'overallfeedback' => array('reviewoverallfeedback', 'quiz'),
  47. );
  48. parent::__construct($current, $section, $cm, $course);
  49. }
  50. protected function definition() {
  51. global $COURSE, $CFG, $DB, $PAGE;
  52. $quizconfig = get_config('quiz');
  53. $mform = $this->_form;
  54. // -------------------------------------------------------------------------------
  55. $mform->addElement('header', 'general', get_string('general', 'form'));
  56. // Name.
  57. $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  58. if (!empty($CFG->formatstringstriptags)) {
  59. $mform->setType('name', PARAM_TEXT);
  60. } else {
  61. $mform->setType('name', PARAM_CLEANHTML);
  62. }
  63. $mform->addRule('name', null, 'required', null, 'client');
  64. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  65. // Introduction.
  66. $this->add_intro_editor(false, get_string('introduction', 'quiz'));
  67. // -------------------------------------------------------------------------------
  68. $mform->addElement('header', 'timing', get_string('timing', 'quiz'));
  69. // Open and close dates.
  70. $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
  71. self::$datefieldoptions);
  72. $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
  73. $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
  74. self::$datefieldoptions);
  75. // Time limit.
  76. $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'),
  77. array('optional' => true));
  78. $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
  79. $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
  80. $mform->setDefault('timelimit', $quizconfig->timelimit);
  81. // What to do with overdue attempts.
  82. $mform->addElement('select', 'overduehandling', get_string('overduehandling', 'quiz'),
  83. quiz_get_overdue_handling_options());
  84. $mform->addHelpButton('overduehandling', 'overduehandling', 'quiz');
  85. $mform->setAdvanced('overduehandling', $quizconfig->overduehandling_adv);
  86. $mform->setDefault('overduehandling', $quizconfig->overduehandling);
  87. // TODO Formslib does OR logic on disableif, and we need AND logic here.
  88. // $mform->disabledIf('overduehandling', 'timelimit', 'eq', 0);
  89. // $mform->disabledIf('overduehandling', 'timeclose', 'eq', 0);
  90. // Grace period time.
  91. $mform->addElement('duration', 'graceperiod', get_string('graceperiod', 'quiz'),
  92. array('optional' => true));
  93. $mform->addHelpButton('graceperiod', 'graceperiod', 'quiz');
  94. $mform->setAdvanced('graceperiod', $quizconfig->graceperiod_adv);
  95. $mform->setDefault('graceperiod', $quizconfig->graceperiod);
  96. $mform->disabledIf('graceperiod', 'overduehandling', 'neq', 'graceperiod');
  97. // -------------------------------------------------------------------------------
  98. // Grade settings.
  99. $this->standard_grading_coursemodule_elements();
  100. $mform->removeElement('grade');
  101. $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
  102. $mform->setType('grade', PARAM_FLOAT);
  103. // Number of attempts.
  104. $attemptoptions = array('0' => get_string('unlimited'));
  105. for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
  106. $attemptoptions[$i] = $i;
  107. }
  108. $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'),
  109. $attemptoptions);
  110. $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
  111. $mform->setDefault('attempts', $quizconfig->attempts);
  112. // Grading method.
  113. $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'),
  114. quiz_get_grading_options());
  115. $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
  116. $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
  117. $mform->setDefault('grademethod', $quizconfig->grademethod);
  118. $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
  119. // -------------------------------------------------------------------------------
  120. $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
  121. // Shuffle questions.
  122. $shuffleoptions = array(
  123. 0 => get_string('asshownoneditscreen', 'quiz'),
  124. 1 => get_string('shuffledrandomly', 'quiz')
  125. );
  126. $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'),
  127. $shuffleoptions, array('id' => 'id_shufflequestions'));
  128. $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
  129. $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
  130. // Questions per page.
  131. $pageoptions = array();
  132. $pageoptions[0] = get_string('neverallononepage', 'quiz');
  133. $pageoptions[1] = get_string('everyquestion', 'quiz');
  134. for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
  135. $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
  136. }
  137. $pagegroup = array();
  138. $pagegroup[] = $mform->createElement('select', 'questionsperpage',
  139. get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
  140. $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
  141. if (!empty($this->_cm)) {
  142. $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
  143. get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
  144. $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
  145. $PAGE->requires->js('/question/qengine.js');
  146. $module = array(
  147. 'name' => 'mod_quiz_edit',
  148. 'fullpath' => '/mod/quiz/edit.js',
  149. 'requires' => array('yui2-dom', 'yui2-event', 'yui2-container'),
  150. 'strings' => array(),
  151. 'async' => false,
  152. );
  153. $PAGE->requires->js_init_call('quiz_settings_init', null, false, $module);
  154. }
  155. $mform->addGroup($pagegroup, 'questionsperpagegrp',
  156. get_string('newpage', 'quiz'), null, false);
  157. $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
  158. $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
  159. // Navigation method.
  160. $mform->addElement('select', 'navmethod', get_string('navmethod', 'quiz'),
  161. quiz_get_navigation_options());
  162. $mform->addHelpButton('navmethod', 'navmethod', 'quiz');
  163. $mform->setAdvanced('navmethod', $quizconfig->navmethod_adv);
  164. $mform->setDefault('navmethod', $quizconfig->navmethod);
  165. // -------------------------------------------------------------------------------
  166. $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
  167. // Shuffle within questions.
  168. $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
  169. $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
  170. $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
  171. $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
  172. // How questions behave (question behaviour).
  173. if (!empty($this->current->preferredbehaviour)) {
  174. $currentbehaviour = $this->current->preferredbehaviour;
  175. } else {
  176. $currentbehaviour = '';
  177. }
  178. $behaviours = question_engine::get_behaviour_options($currentbehaviour);
  179. $mform->addElement('select', 'preferredbehaviour',
  180. get_string('howquestionsbehave', 'question'), $behaviours);
  181. $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
  182. $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour);
  183. // Each attempt builds on last.
  184. $mform->addElement('selectyesno', 'attemptonlast',
  185. get_string('eachattemptbuildsonthelast', 'quiz'));
  186. $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
  187. $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
  188. $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
  189. $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
  190. // -------------------------------------------------------------------------------
  191. $mform->addElement('header', 'reviewoptionshdr',
  192. get_string('reviewoptionsheading', 'quiz'));
  193. $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
  194. // Review options.
  195. $this->add_review_options_group($mform, $quizconfig, 'during',
  196. mod_quiz_display_options::DURING, true);
  197. $this->add_review_options_group($mform, $quizconfig, 'immediately',
  198. mod_quiz_display_options::IMMEDIATELY_AFTER);
  199. $this->add_review_options_group($mform, $quizconfig, 'open',
  200. mod_quiz_display_options::LATER_WHILE_OPEN);
  201. $this->add_review_options_group($mform, $quizconfig, 'closed',
  202. mod_quiz_display_options::AFTER_CLOSE);
  203. foreach ($behaviours as $behaviour => $notused) {
  204. $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
  205. foreach ($unusedoptions as $unusedoption) {
  206. $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
  207. 'eq', $behaviour);
  208. }
  209. }
  210. $mform->disabledIf('attemptduring', 'preferredbehaviour',
  211. 'neq', 'wontmatch');
  212. $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
  213. 'neq', 'wontmatch');
  214. // -------------------------------------------------------------------------------
  215. $mform->addElement('header', 'display', get_string('display', 'form'));
  216. // Show user picture.
  217. $mform->addElement('select', 'showuserpicture', get_string('showuserpicture', 'quiz'),
  218. quiz_get_user_image_options());
  219. $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
  220. $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
  221. $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
  222. // Overall decimal points.
  223. $options = array();
  224. for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
  225. $options[$i] = $i;
  226. }
  227. $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
  228. $options);
  229. $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
  230. $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
  231. $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
  232. // Question decimal points.
  233. $options = array(-1 => get_string('sameasoverall', 'quiz'));
  234. for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
  235. $options[$i] = $i;
  236. }
  237. $mform->addElement('select', 'questiondecimalpoints',
  238. get_string('decimalplacesquestion', 'quiz'), $options);
  239. $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
  240. $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
  241. $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
  242. // Show blocks during quiz attempt.
  243. $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
  244. $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
  245. $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
  246. $mform->setDefault('showblocks', $quizconfig->showblocks);
  247. // -------------------------------------------------------------------------------
  248. $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
  249. // Require password to begin quiz attempt.
  250. $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
  251. $mform->setType('quizpassword', PARAM_TEXT);
  252. $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
  253. $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
  254. $mform->setDefault('quizpassword', $quizconfig->password);
  255. // IP address.
  256. $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
  257. $mform->setType('subnet', PARAM_TEXT);
  258. $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
  259. $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
  260. $mform->setDefault('subnet', $quizconfig->subnet);
  261. // Enforced time delay between quiz attempts.
  262. $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
  263. array('optional' => true));
  264. $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
  265. $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
  266. $mform->setDefault('delay1', $quizconfig->delay1);
  267. $mform->disabledIf('delay1', 'attempts', 'eq', 1);
  268. $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
  269. array('optional' => true));
  270. $mform->addHelpButton('delay2', 'delaylater', 'quiz');
  271. $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
  272. $mform->setDefault('delay2', $quizconfig->delay2);
  273. $mform->disabledIf('delay2', 'attempts', 'eq', 1);
  274. $mform->disabledIf('delay2', 'attempts', 'eq', 2);
  275. // Browser security choices.
  276. $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
  277. quiz_access_manager::get_browser_security_choices());
  278. $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
  279. $mform->setAdvanced('browsersecurity', $quizconfig->browsersecurity_adv);
  280. $mform->setDefault('browsersecurity', $quizconfig->browsersecurity);
  281. // Any other rule plugins.
  282. quiz_access_manager::add_settings_form_fields($this, $mform);
  283. // -------------------------------------------------------------------------------
  284. $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
  285. $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
  286. if (isset($this->current->grade)) {
  287. $needwarning = $this->current->grade === 0;
  288. } else {
  289. $needwarning = $quizconfig->maximumgrade == 0;
  290. }
  291. if ($needwarning) {
  292. $mform->addElement('static', 'nogradewarning', '',
  293. get_string('nogradewarning', 'quiz'));
  294. }
  295. $mform->addElement('static', 'gradeboundarystatic1',
  296. get_string('gradeboundary', 'quiz'), '100%');
  297. $repeatarray = array();
  298. $repeatedoptions = array();
  299. $repeatarray[] = $mform->createElement('editor', 'feedbacktext',
  300. get_string('feedback', 'quiz'), array('rows' => 3), array('maxfiles' => EDITOR_UNLIMITED_FILES,
  301. 'noclean' => true, 'context' => $this->context));
  302. $repeatarray[] = $mform->createElement('text', 'feedbackboundaries',
  303. get_string('gradeboundary', 'quiz'), array('size' => 10));
  304. $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
  305. $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
  306. if (!empty($this->_instance)) {
  307. $this->_feedbacks = $DB->get_records('quiz_feedback',
  308. array('quizid' => $this->_instance), 'mingrade DESC');
  309. } else {
  310. $this->_feedbacks = array();
  311. }
  312. $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
  313. $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
  314. $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
  315. get_string('addmoreoverallfeedbacks', 'quiz'), true);
  316. // Put some extra elements in before the button.
  317. $mform->insertElementBefore($mform->createElement('editor',
  318. "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('rows' => 3),
  319. array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
  320. 'context' => $this->context)),
  321. 'boundary_add_fields');
  322. $mform->insertElementBefore($mform->createElement('static',
  323. 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
  324. 'boundary_add_fields');
  325. // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
  326. // repeat_elements because we don't want to dissable the first feedbacktext.
  327. for ($i = 0; $i < $nextel; $i++) {
  328. $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
  329. $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
  330. }
  331. // -------------------------------------------------------------------------------
  332. $this->standard_coursemodule_elements();
  333. // Check and act on whether setting outcomes is considered an advanced setting.
  334. $mform->setAdvanced('modoutcomes', !empty($quizconfig->outcomes_adv));
  335. // The standard_coursemodule_elements method sets this to 100, but the
  336. // quiz has its own setting, so use that.
  337. $mform->setDefault('grade', $quizconfig->maximumgrade);
  338. // -------------------------------------------------------------------------------
  339. $this->add_action_buttons();
  340. }
  341. protected function add_review_options_group($mform, $quizconfig, $whenname,
  342. $when, $withhelp = false) {
  343. global $OUTPUT;
  344. $group = array();
  345. foreach (self::$reviewfields as $field => $string) {
  346. list($identifier, $component) = $string;
  347. $label = get_string($identifier, $component);
  348. if ($withhelp) {
  349. $label .= ' ' . $OUTPUT->help_icon($identifier, $component);
  350. }
  351. $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
  352. }
  353. $mform->addGroup($group, $whenname . 'optionsgrp',
  354. get_string('review' . $whenname, 'quiz'), null, false);
  355. foreach (self::$reviewfields as $field => $notused) {
  356. $cfgfield = 'review' . $field;
  357. if ($quizconfig->$cfgfield & $when) {
  358. $mform->setDefault($field . $whenname, 1);
  359. } else {
  360. $mform->setDefault($field . $whenname, 0);
  361. }
  362. }
  363. if ($whenname != 'during') {
  364. $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
  365. $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
  366. $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
  367. $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
  368. }
  369. }
  370. protected function preprocessing_review_settings(&$toform, $whenname, $when) {
  371. foreach (self::$reviewfields as $field => $notused) {
  372. $fieldname = 'review' . $field;
  373. if (array_key_exists($fieldname, $toform)) {
  374. $toform[$field . $whenname] = $toform[$fieldname] & $when;
  375. }
  376. }
  377. }
  378. public function data_preprocessing(&$toform) {
  379. if (isset($toform['grade'])) {
  380. // Convert to a real number, so we don't get 0.0000.
  381. $toform['grade'] = $toform['grade'] + 0;
  382. }
  383. if (count($this->_feedbacks)) {
  384. $key = 0;
  385. foreach ($this->_feedbacks as $feedback) {
  386. $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
  387. $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
  388. $draftid, // Draftid.
  389. $this->context->id, // Context.
  390. 'mod_quiz', // Component.
  391. 'feedback', // Filarea.
  392. !empty($feedback->id) ? (int) $feedback->id : null, // Itemid.
  393. null,
  394. $feedback->feedbacktext // Text.
  395. );
  396. $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
  397. $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
  398. if ($toform['grade'] == 0) {
  399. // When a quiz is un-graded, there can only be one lot of
  400. // feedback. If the quiz previously had a maximum grade and
  401. // several lots of feedback, we must now avoid putting text
  402. // into input boxes that are disabled, but which the
  403. // validation will insist are blank.
  404. break;
  405. }
  406. if ($feedback->mingrade > 0) {
  407. $toform['feedbackboundaries['.$key.']'] =
  408. (100.0 * $feedback->mingrade / $toform['grade']) . '%';
  409. }
  410. $key++;
  411. }
  412. }
  413. if (isset($toform['timelimit'])) {
  414. $toform['timelimitenable'] = $toform['timelimit'] > 0;
  415. }
  416. $this->preprocessing_review_settings($toform, 'during',
  417. mod_quiz_display_options::DURING);
  418. $this->preprocessing_review_settings($toform, 'immediately',
  419. mod_quiz_display_options::IMMEDIATELY_AFTER);
  420. $this->preprocessing_review_settings($toform, 'open',
  421. mod_quiz_display_options::LATER_WHILE_OPEN);
  422. $this->preprocessing_review_settings($toform, 'closed',
  423. mod_quiz_display_options::AFTER_CLOSE);
  424. $toform['attemptduring'] = true;
  425. $toform['overallfeedbackduring'] = false;
  426. // Password field - different in form to stop browsers that remember
  427. // passwords from getting confused.
  428. if (isset($toform['password'])) {
  429. $toform['quizpassword'] = $toform['password'];
  430. unset($toform['password']);
  431. }
  432. // Load any settings belonging to the access rules.
  433. if (!empty($toform['instance'])) {
  434. $accesssettings = quiz_access_manager::load_settings($toform['instance']);
  435. foreach ($accesssettings as $name => $value) {
  436. $toform[$name] = $value;
  437. }
  438. }
  439. }
  440. public function validation($data, $files) {
  441. $errors = parent::validation($data, $files);
  442. // Check open and close times are consistent.
  443. if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
  444. $data['timeclose'] < $data['timeopen']) {
  445. $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
  446. }
  447. // Check that the grace period is not too short.
  448. if ($data['overduehandling'] == 'graceperiod') {
  449. $graceperiodmin = get_config('quiz', 'graceperiodmin');
  450. if ($data['graceperiod'] <= $graceperiodmin) {
  451. $errors['graceperiod'] = get_string('graceperiodtoosmall', 'quiz', format_time($graceperiodmin));
  452. }
  453. }
  454. // Check the boundary value is a number or a percentage, and in range.
  455. $i = 0;
  456. while (!empty($data['feedbackboundaries'][$i] )) {
  457. $boundary = trim($data['feedbackboundaries'][$i]);
  458. if (strlen($boundary) > 0) {
  459. if ($boundary[strlen($boundary) - 1] == '%') {
  460. $boundary = trim(substr($boundary, 0, -1));
  461. if (is_numeric($boundary)) {
  462. $boundary = $boundary * $data['grade'] / 100.0;
  463. } else {
  464. $errors["feedbackboundaries[$i]"] =
  465. get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
  466. }
  467. } else if (!is_numeric($boundary)) {
  468. $errors["feedbackboundaries[$i]"] =
  469. get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
  470. }
  471. }
  472. if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
  473. $errors["feedbackboundaries[$i]"] =
  474. get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
  475. }
  476. if (is_numeric($boundary) && $i > 0 &&
  477. $boundary >= $data['feedbackboundaries'][$i - 1]) {
  478. $errors["feedbackboundaries[$i]"] =
  479. get_string('feedbackerrororder', 'quiz', $i + 1);
  480. }
  481. $data['feedbackboundaries'][$i] = $boundary;
  482. $i += 1;
  483. }
  484. $numboundaries = $i;
  485. // Check there is nothing in the remaining unused fields.
  486. if (!empty($data['feedbackboundaries'])) {
  487. for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
  488. if (!empty($data['feedbackboundaries'][$i] ) &&
  489. trim($data['feedbackboundaries'][$i] ) != '') {
  490. $errors["feedbackboundaries[$i]"] =
  491. get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
  492. }
  493. }
  494. }
  495. for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
  496. if (!empty($data['feedbacktext'][$i]['text']) &&
  497. trim($data['feedbacktext'][$i]['text'] ) != '') {
  498. $errors["feedbacktext[$i]"] =
  499. get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
  500. }
  501. }
  502. // Any other rule plugins.
  503. $errors = quiz_access_manager::validate_settings_form_fields($errors, $data, $files, $this);
  504. return $errors;
  505. }
  506. }