Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   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  /**
  18   * Defines the quiz module ettings form.
  19   *
  20   * @package    mod_quiz
  21   * @copyright  2006 Jamie Pratt
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot . '/course/moodleform_mod.php');
  29  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  30  
  31  use mod_quiz\access_manager;
  32  use mod_quiz\question\display_options;
  33  
  34  /**
  35   * Settings form for the quiz module.
  36   *
  37   * @copyright  2006 Jamie Pratt
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class mod_quiz_mod_form extends moodleform_mod {
  41      /** @var array options to be used with date_time_selector fields in the quiz. */
  42      public static $datefieldoptions = ['optional' => true];
  43  
  44      /** @var array caches the quiz overall feedback, for convenience. */
  45      protected $_feedbacks;
  46  
  47      /** @var array for convenience stores the list of types of review option. Initialised in the constructor. */
  48      protected static $reviewfields = [];
  49  
  50      /** @var int the max number of attempts allowed in any user or group override on this quiz. */
  51      protected $maxattemptsanyoverride = null;
  52  
  53      public function __construct($current, $section, $cm, $course) {
  54          self::$reviewfields = [
  55              'attempt'          => ['theattempt', 'quiz'],
  56              'correctness'      => ['whethercorrect', 'question'],
  57              'marks'            => ['marks', 'quiz'],
  58              'specificfeedback' => ['specificfeedback', 'question'],
  59              'generalfeedback'  => ['generalfeedback', 'question'],
  60              'rightanswer'      => ['rightanswer', 'question'],
  61              'overallfeedback'  => ['reviewoverallfeedback', 'quiz'],
  62          ];
  63          parent::__construct($current, $section, $cm, $course);
  64      }
  65  
  66      protected function definition() {
  67          global $CFG, $DB, $PAGE;
  68          $quizconfig = get_config('quiz');
  69          $mform = $this->_form;
  70  
  71          // -------------------------------------------------------------------------------
  72          $mform->addElement('header', 'general', get_string('general', 'form'));
  73  
  74          // Name.
  75          $mform->addElement('text', 'name', get_string('name'), ['size' => '64']);
  76          if (!empty($CFG->formatstringstriptags)) {
  77              $mform->setType('name', PARAM_TEXT);
  78          } else {
  79              $mform->setType('name', PARAM_CLEANHTML);
  80          }
  81          $mform->addRule('name', null, 'required', null, 'client');
  82          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  83  
  84          // Introduction.
  85          $this->standard_intro_elements(get_string('introduction', 'quiz'));
  86  
  87          // -------------------------------------------------------------------------------
  88          $mform->addElement('header', 'timing', get_string('timing', 'quiz'));
  89  
  90          // Open and close dates.
  91          $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
  92                  self::$datefieldoptions);
  93          $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
  94  
  95          $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
  96                  self::$datefieldoptions);
  97  
  98          // Time limit.
  99          $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'),
 100                  ['optional' => true]);
 101          $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
 102  
 103          // What to do with overdue attempts.
 104          $mform->addElement('select', 'overduehandling', get_string('overduehandling', 'quiz'),
 105                  quiz_get_overdue_handling_options());
 106          $mform->addHelpButton('overduehandling', 'overduehandling', 'quiz');
 107          // TODO Formslib does OR logic on disableif, and we need AND logic here.
 108          // $mform->disabledIf('overduehandling', 'timelimit', 'eq', 0);
 109          // $mform->disabledIf('overduehandling', 'timeclose', 'eq', 0);
 110  
 111          // Grace period time.
 112          $mform->addElement('duration', 'graceperiod', get_string('graceperiod', 'quiz'),
 113                  ['optional' => true]);
 114          $mform->addHelpButton('graceperiod', 'graceperiod', 'quiz');
 115          $mform->hideIf('graceperiod', 'overduehandling', 'neq', 'graceperiod');
 116  
 117          // -------------------------------------------------------------------------------
 118          // Grade settings.
 119          $this->standard_grading_coursemodule_elements();
 120  
 121          $mform->removeElement('grade');
 122          if (property_exists($this->current, 'grade')) {
 123              $currentgrade = $this->current->grade;
 124          } else {
 125              $currentgrade = $quizconfig->maximumgrade;
 126          }
 127          $mform->addElement('hidden', 'grade', $currentgrade);
 128          $mform->setType('grade', PARAM_FLOAT);
 129  
 130          // Number of attempts.
 131          $attemptoptions = ['0' => get_string('unlimited')];
 132          for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
 133              $attemptoptions[$i] = $i;
 134          }
 135          $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'),
 136                  $attemptoptions);
 137  
 138          // Grading method.
 139          $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'),
 140                  quiz_get_grading_options());
 141          $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
 142          if ($this->get_max_attempts_for_any_override() < 2) {
 143              $mform->hideIf('grademethod', 'attempts', 'eq', 1);
 144          }
 145  
 146          // -------------------------------------------------------------------------------
 147          $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
 148  
 149          $pagegroup = [];
 150          $pagegroup[] = $mform->createElement('select', 'questionsperpage',
 151                  get_string('newpage', 'quiz'), quiz_questions_per_page_options(), ['id' => 'id_questionsperpage']);
 152          $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
 153  
 154          if (!empty($this->_cm) && !quiz_has_attempts($this->_cm->instance)) {
 155              $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
 156                      get_string('repaginatenow', 'quiz'), ['id' => 'id_repaginatenow']);
 157          }
 158  
 159          $mform->addGroup($pagegroup, 'questionsperpagegrp',
 160                  get_string('newpage', 'quiz'), null, false);
 161          $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
 162          $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
 163  
 164          // Navigation method.
 165          $mform->addElement('select', 'navmethod', get_string('navmethod', 'quiz'),
 166                  quiz_get_navigation_options());
 167          $mform->addHelpButton('navmethod', 'navmethod', 'quiz');
 168  
 169          // -------------------------------------------------------------------------------
 170          $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
 171  
 172          // Shuffle within questions.
 173          $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
 174          $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
 175  
 176          // How questions behave (question behaviour).
 177          if (!empty($this->current->preferredbehaviour)) {
 178              $currentbehaviour = $this->current->preferredbehaviour;
 179          } else {
 180              $currentbehaviour = '';
 181          }
 182          $behaviours = question_engine::get_behaviour_options($currentbehaviour);
 183          $mform->addElement('select', 'preferredbehaviour',
 184                  get_string('howquestionsbehave', 'question'), $behaviours);
 185          $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
 186  
 187          // Can redo completed questions.
 188          $redochoices = [0 => get_string('no'), 1 => get_string('canredoquestionsyes', 'quiz')];
 189          $mform->addElement('select', 'canredoquestions', get_string('canredoquestions', 'quiz'), $redochoices);
 190          $mform->addHelpButton('canredoquestions', 'canredoquestions', 'quiz');
 191          foreach ($behaviours as $behaviour => $notused) {
 192              if (!question_engine::can_questions_finish_during_the_attempt($behaviour)) {
 193                  $mform->hideIf('canredoquestions', 'preferredbehaviour', 'eq', $behaviour);
 194              }
 195          }
 196  
 197          // Each attempt builds on last.
 198          $mform->addElement('selectyesno', 'attemptonlast',
 199                  get_string('eachattemptbuildsonthelast', 'quiz'));
 200          $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
 201          if ($this->get_max_attempts_for_any_override() < 2) {
 202              $mform->hideIf('attemptonlast', 'attempts', 'eq', 1);
 203          }
 204  
 205          // -------------------------------------------------------------------------------
 206          $mform->addElement('header', 'reviewoptionshdr',
 207                  get_string('reviewoptionsheading', 'quiz'));
 208          $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
 209  
 210          // Review options.
 211          $this->add_review_options_group($mform, $quizconfig, 'during',
 212                  display_options::DURING, true);
 213          $this->add_review_options_group($mform, $quizconfig, 'immediately',
 214                  display_options::IMMEDIATELY_AFTER);
 215          $this->add_review_options_group($mform, $quizconfig, 'open',
 216                  display_options::LATER_WHILE_OPEN);
 217          $this->add_review_options_group($mform, $quizconfig, 'closed',
 218                  display_options::AFTER_CLOSE);
 219  
 220          foreach ($behaviours as $behaviour => $notused) {
 221              $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
 222              foreach ($unusedoptions as $unusedoption) {
 223                  $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
 224                          'eq', $behaviour);
 225              }
 226          }
 227          $mform->disabledIf('attemptduring', 'preferredbehaviour',
 228                  'neq', 'wontmatch');
 229          $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
 230                  'neq', 'wontmatch');
 231          foreach (self::$reviewfields as $field => $notused) {
 232              $mform->disabledIf($field . 'closed', 'timeclose[enabled]');
 233          }
 234  
 235          // -------------------------------------------------------------------------------
 236          $mform->addElement('header', 'display', get_string('appearance'));
 237  
 238          // Show user picture.
 239          $mform->addElement('select', 'showuserpicture', get_string('showuserpicture', 'quiz'),
 240                  quiz_get_user_image_options());
 241          $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
 242  
 243          // Overall decimal points.
 244          $options = [];
 245          for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
 246              $options[$i] = $i;
 247          }
 248          $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
 249                  $options);
 250          $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
 251  
 252          // Question decimal points.
 253          $options = [-1 => get_string('sameasoverall', 'quiz')];
 254          for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
 255              $options[$i] = $i;
 256          }
 257          $mform->addElement('select', 'questiondecimalpoints',
 258                  get_string('decimalplacesquestion', 'quiz'), $options);
 259          $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
 260  
 261          // Show blocks during quiz attempt.
 262          $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
 263          $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
 264  
 265          // -------------------------------------------------------------------------------
 266          $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
 267  
 268          // Require password to begin quiz attempt.
 269          $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
 270          $mform->setType('quizpassword', PARAM_TEXT);
 271          $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
 272  
 273          // IP address.
 274          $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
 275          $mform->setType('subnet', PARAM_TEXT);
 276          $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
 277  
 278          // Enforced time delay between quiz attempts.
 279          $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
 280                  ['optional' => true]);
 281          $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
 282          if ($this->get_max_attempts_for_any_override() < 2) {
 283              $mform->hideIf('delay1', 'attempts', 'eq', 1);
 284          }
 285  
 286          $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
 287                  ['optional' => true]);
 288          $mform->addHelpButton('delay2', 'delaylater', 'quiz');
 289          if ($this->get_max_attempts_for_any_override() < 3) {
 290              $mform->hideIf('delay2', 'attempts', 'eq', 1);
 291              $mform->hideIf('delay2', 'attempts', 'eq', 2);
 292          }
 293  
 294          // Browser security choices.
 295          $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
 296                  access_manager::get_browser_security_choices());
 297          $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
 298  
 299          // Any other rule plugins.
 300          access_manager::add_settings_form_fields($this, $mform);
 301  
 302          // -------------------------------------------------------------------------------
 303          $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
 304          $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
 305  
 306          if (isset($this->current->grade)) {
 307              $needwarning = $this->current->grade === 0;
 308          } else {
 309              $needwarning = $quizconfig->maximumgrade == 0;
 310          }
 311          if ($needwarning) {
 312              $mform->addElement('static', 'nogradewarning', '',
 313                      get_string('nogradewarning', 'quiz'));
 314          }
 315  
 316          $mform->addElement('static', 'gradeboundarystatic1',
 317                  get_string('gradeboundary', 'quiz'), '100%');
 318  
 319          $repeatarray = [];
 320          $repeatedoptions = [];
 321          $repeatarray[] = $mform->createElement('editor', 'feedbacktext',
 322                  get_string('feedback', 'quiz'), ['rows' => 3], ['maxfiles' => EDITOR_UNLIMITED_FILES,
 323                          'noclean' => true, 'context' => $this->context]);
 324          $repeatarray[] = $mform->createElement('text', 'feedbackboundaries',
 325                  get_string('gradeboundary', 'quiz'), ['size' => 10]);
 326          $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
 327          $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
 328  
 329          if (!empty($this->_instance)) {
 330              $this->_feedbacks = $DB->get_records('quiz_feedback',
 331                      ['quizid' => $this->_instance], 'mingrade DESC');
 332              $numfeedbacks = count($this->_feedbacks);
 333          } else {
 334              $this->_feedbacks = [];
 335              $numfeedbacks = $quizconfig->initialnumfeedbacks;
 336          }
 337          $numfeedbacks = max($numfeedbacks, 1);
 338  
 339          $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
 340                  $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
 341                  get_string('addmoreoverallfeedbacks', 'quiz'), true);
 342  
 343          // Put some extra elements in before the button.
 344          $mform->insertElementBefore($mform->createElement('editor',
 345                  "feedbacktext[$nextel]", get_string('feedback', 'quiz'), ['rows' => 3],
 346                  ['maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
 347                        'context' => $this->context]),
 348                  'boundary_add_fields');
 349          $mform->insertElementBefore($mform->createElement('static',
 350                  'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
 351                  'boundary_add_fields');
 352  
 353          // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
 354          // repeat_elements because we don't want to dissable the first feedbacktext.
 355          for ($i = 0; $i < $nextel; $i++) {
 356              $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
 357              $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
 358          }
 359  
 360          // -------------------------------------------------------------------------------
 361          $this->standard_coursemodule_elements();
 362  
 363          // Check and act on whether setting outcomes is considered an advanced setting.
 364          $mform->setAdvanced('modoutcomes', !empty($quizconfig->outcomes_adv));
 365  
 366          // The standard_coursemodule_elements method sets this to 100, but the
 367          // quiz has its own setting, so use that.
 368          $mform->setDefault('grade', $quizconfig->maximumgrade);
 369  
 370          // -------------------------------------------------------------------------------
 371          $this->apply_admin_defaults();
 372          $this->add_action_buttons();
 373  
 374          $PAGE->requires->yui_module('moodle-mod_quiz-modform', 'M.mod_quiz.modform.init');
 375      }
 376  
 377      protected function add_review_options_group($mform, $quizconfig, $whenname,
 378              $when, $withhelp = false) {
 379          global $OUTPUT;
 380  
 381          $group = [];
 382          foreach (self::$reviewfields as $field => $string) {
 383              list($identifier, $component) = $string;
 384  
 385              $label = get_string($identifier, $component);
 386              $group[] = $mform->createElement('html', html_writer::start_div('review_option_item'));
 387              $el = $mform->createElement('checkbox', $field . $whenname, '', $label);
 388              if ($withhelp) {
 389                  $el->_helpbutton = $OUTPUT->render(new help_icon($identifier, $component));
 390              }
 391              $group[] = $el;
 392              $group[] = $mform->createElement('html', html_writer::end_div());
 393          }
 394          $mform->addGroup($group, $whenname . 'optionsgrp',
 395                  get_string('review' . $whenname, 'quiz'), null, false);
 396  
 397          foreach (self::$reviewfields as $field => $notused) {
 398              $cfgfield = 'review' . $field;
 399              if ($quizconfig->$cfgfield & $when) {
 400                  $mform->setDefault($field . $whenname, 1);
 401              } else {
 402                  $mform->setDefault($field . $whenname, 0);
 403              }
 404          }
 405  
 406          if ($whenname != 'during') {
 407              $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
 408              $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
 409              $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
 410              $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
 411          }
 412      }
 413  
 414      protected function preprocessing_review_settings(&$toform, $whenname, $when) {
 415          foreach (self::$reviewfields as $field => $notused) {
 416              $fieldname = 'review' . $field;
 417              if (array_key_exists($fieldname, $toform)) {
 418                  $toform[$field . $whenname] = $toform[$fieldname] & $when;
 419              }
 420          }
 421      }
 422  
 423      public function data_preprocessing(&$toform) {
 424          if (isset($toform['grade'])) {
 425              // Convert to a real number, so we don't get 0.0000.
 426              $toform['grade'] = $toform['grade'] + 0;
 427          }
 428  
 429          if (count($this->_feedbacks)) {
 430              $key = 0;
 431              foreach ($this->_feedbacks as $feedback) {
 432                  $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
 433                  $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
 434                      $draftid,               // Draftid.
 435                      $this->context->id,     // Context.
 436                      'mod_quiz',             // Component.
 437                      'feedback',             // Filarea.
 438                      !empty($feedback->id) ? (int) $feedback->id : null, // Itemid.
 439                      null,
 440                      $feedback->feedbacktext // Text.
 441                  );
 442                  $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
 443                  $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
 444  
 445                  if ($toform['grade'] == 0) {
 446                      // When a quiz is un-graded, there can only be one lot of
 447                      // feedback. If the quiz previously had a maximum grade and
 448                      // several lots of feedback, we must now avoid putting text
 449                      // into input boxes that are disabled, but which the
 450                      // validation will insist are blank.
 451                      break;
 452                  }
 453  
 454                  if ($feedback->mingrade > 0) {
 455                      $toform['feedbackboundaries['.$key.']'] =
 456                              round(100.0 * $feedback->mingrade / $toform['grade'], 6) . '%';
 457                  }
 458                  $key++;
 459              }
 460          }
 461  
 462          if (isset($toform['timelimit'])) {
 463              $toform['timelimitenable'] = $toform['timelimit'] > 0;
 464          }
 465  
 466          $this->preprocessing_review_settings($toform, 'during',
 467                  display_options::DURING);
 468          $this->preprocessing_review_settings($toform, 'immediately',
 469                  display_options::IMMEDIATELY_AFTER);
 470          $this->preprocessing_review_settings($toform, 'open',
 471                  display_options::LATER_WHILE_OPEN);
 472          $this->preprocessing_review_settings($toform, 'closed',
 473                  display_options::AFTER_CLOSE);
 474          $toform['attemptduring'] = true;
 475          $toform['overallfeedbackduring'] = false;
 476  
 477          // Password field - different in form to stop browsers that remember
 478          // passwords from getting confused.
 479          if (isset($toform['password'])) {
 480              $toform['quizpassword'] = $toform['password'];
 481              unset($toform['password']);
 482          }
 483  
 484          // Load any settings belonging to the access rules.
 485          if (!empty($toform['instance'])) {
 486              $accesssettings = access_manager::load_settings($toform['instance']);
 487              foreach ($accesssettings as $name => $value) {
 488                  $toform[$name] = $value;
 489              }
 490          }
 491  
 492          if (empty($toform['completionminattempts'])) {
 493              $toform['completionminattempts'] = 1;
 494          } else {
 495              $toform['completionminattemptsenabled'] = $toform['completionminattempts'] > 0;
 496          }
 497      }
 498  
 499      /**
 500       * Allows module to modify the data returned by form get_data().
 501       * This method is also called in the bulk activity completion form.
 502       *
 503       * Only available on moodleform_mod.
 504       *
 505       * @param stdClass $data the form data to be modified.
 506       */
 507      public function data_postprocessing($data) {
 508          parent::data_postprocessing($data);
 509          if (!empty($data->completionunlocked)) {
 510              // Turn off completion settings if the checkboxes aren't ticked.
 511              $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
 512              if (empty($data->completionminattemptsenabled) || !$autocompletion) {
 513                  $data->completionminattempts = 0;
 514              }
 515          }
 516      }
 517  
 518      public function validation($data, $files) {
 519          $errors = parent::validation($data, $files);
 520  
 521          // Check open and close times are consistent.
 522          if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
 523                  $data['timeclose'] < $data['timeopen']) {
 524              $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
 525          }
 526  
 527          // Check that the grace period is not too short.
 528          if ($data['overduehandling'] == 'graceperiod') {
 529              $graceperiodmin = get_config('quiz', 'graceperiodmin');
 530              if ($data['graceperiod'] <= $graceperiodmin) {
 531                  $errors['graceperiod'] = get_string('graceperiodtoosmall', 'quiz', format_time($graceperiodmin));
 532              }
 533          }
 534  
 535          if (!empty($data['completionminattempts'])) {
 536              if ($data['attempts'] > 0 && $data['completionminattempts'] > $data['attempts']) {
 537                  $errors['completionminattemptsgroup'] = get_string('completionminattemptserror', 'quiz');
 538              }
 539          }
 540  
 541          // Check the boundary value is a number or a percentage, and in range.
 542          $i = 0;
 543          while (!empty($data['feedbackboundaries'][$i] )) {
 544              $boundary = trim($data['feedbackboundaries'][$i]);
 545              if (strlen($boundary) > 0) {
 546                  if ($boundary[strlen($boundary) - 1] == '%') {
 547                      $boundary = trim(substr($boundary, 0, -1));
 548                      if (is_numeric($boundary)) {
 549                          $boundary = $boundary * $data['grade'] / 100.0;
 550                      } else {
 551                          $errors["feedbackboundaries[$i]"] =
 552                                  get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
 553                      }
 554                  } else if (!is_numeric($boundary)) {
 555                      $errors["feedbackboundaries[$i]"] =
 556                              get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
 557                  }
 558              }
 559              if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
 560                  $errors["feedbackboundaries[$i]"] =
 561                          get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
 562              }
 563              if (is_numeric($boundary) && $i > 0 &&
 564                      $boundary >= $data['feedbackboundaries'][$i - 1]) {
 565                  $errors["feedbackboundaries[$i]"] =
 566                          get_string('feedbackerrororder', 'quiz', $i + 1);
 567              }
 568              $data['feedbackboundaries'][$i] = $boundary;
 569              $i += 1;
 570          }
 571          $numboundaries = $i;
 572  
 573          // Check there is nothing in the remaining unused fields.
 574          if (!empty($data['feedbackboundaries'])) {
 575              for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
 576                  if (!empty($data['feedbackboundaries'][$i] ) &&
 577                          trim($data['feedbackboundaries'][$i] ) != '') {
 578                      $errors["feedbackboundaries[$i]"] =
 579                              get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
 580                  }
 581              }
 582          }
 583          for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
 584              if (!empty($data['feedbacktext'][$i]['text']) &&
 585                      trim($data['feedbacktext'][$i]['text'] ) != '') {
 586                  $errors["feedbacktext[$i]"] =
 587                          get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
 588              }
 589          }
 590  
 591          // If CBM is involved, don't show the warning for grade to pass being larger than the maximum grade.
 592          if (($data['preferredbehaviour'] == 'deferredcbm') OR ($data['preferredbehaviour'] == 'immediatecbm')) {
 593              unset($errors['gradepass']);
 594          }
 595          // Any other rule plugins.
 596          $errors = access_manager::validate_settings_form_fields($errors, $data, $files, $this);
 597  
 598          return $errors;
 599      }
 600  
 601      /**
 602       * Display module-specific activity completion rules.
 603       * Part of the API defined by moodleform_mod
 604       * @return array Array of string IDs of added items, empty array if none
 605       */
 606      public function add_completion_rules() {
 607          $mform = $this->_form;
 608          $items = [];
 609  
 610          $mform->addElement('advcheckbox', 'completionattemptsexhausted', null,
 611              get_string('completionattemptsexhausted', 'quiz'),
 612              ['group' => 'cattempts']);
 613          $mform->disabledIf('completionattemptsexhausted', 'completionpassgrade', 'notchecked');
 614          $items[] = 'completionattemptsexhausted';
 615  
 616          $group = [];
 617          $group[] = $mform->createElement('checkbox', 'completionminattemptsenabled', '',
 618              get_string('completionminattempts', 'quiz'));
 619          $group[] = $mform->createElement('text', 'completionminattempts', '', ['size' => 3]);
 620          $mform->setType('completionminattempts', PARAM_INT);
 621          $mform->addGroup($group, 'completionminattemptsgroup', get_string('completionminattemptsgroup', 'quiz'), [' '], false);
 622          $mform->disabledIf('completionminattempts', 'completionminattemptsenabled', 'notchecked');
 623  
 624          $items[] = 'completionminattemptsgroup';
 625  
 626          return $items;
 627      }
 628  
 629      /**
 630       * Called during validation. Indicates whether a module-specific completion rule is selected.
 631       *
 632       * @param array $data Input data (not yet validated)
 633       * @return bool True if one or more rules is enabled, false if none are.
 634       */
 635      public function completion_rule_enabled($data) {
 636          return  !empty($data['completionattemptsexhausted']) ||
 637                  !empty($data['completionminattemptsenabled']);
 638      }
 639  
 640      /**
 641       * Get the maximum number of attempts that anyone might have due to a user
 642       * or group override. Used to decide whether disabledIf rules should be applied.
 643       * @return int the number of attempts allowed. For the purpose of this method,
 644       * unlimited is returned as 1000, not 0.
 645       */
 646      public function get_max_attempts_for_any_override() {
 647          global $DB;
 648  
 649          if (empty($this->_instance)) {
 650              // Quiz not created yet, so no overrides.
 651              return 1;
 652          }
 653  
 654          if ($this->maxattemptsanyoverride === null) {
 655              $this->maxattemptsanyoverride = $DB->get_field_sql("
 656                      SELECT MAX(CASE WHEN attempts = 0 THEN 1000 ELSE attempts END)
 657                        FROM {quiz_overrides}
 658                       WHERE quiz = ?",
 659                      [$this->_instance]);
 660              if ($this->maxattemptsanyoverride < 1) {
 661                  // This happens when no override alters the number of attempts.
 662                  $this->maxattemptsanyoverride = 1;
 663              }
 664          }
 665  
 666          return $this->maxattemptsanyoverride;
 667      }
 668  }