Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace mod_quiz\local\reports;
  18  
  19  use html_writer;
  20  use MoodleQuickForm;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  require_once($CFG->libdir . '/formslib.php');
  25  
  26  /**
  27   * Base class for the settings form for {@see attempts_report}s.
  28   *
  29   * @package   mod_quiz
  30   * @copyright 2012 The Open University
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  abstract class attempts_report_options_form extends \moodleform {
  34  
  35      protected function definition() {
  36          $mform = $this->_form;
  37  
  38          $mform->addElement('header', 'preferencespage',
  39                  get_string('reportwhattoinclude', 'quiz'));
  40  
  41          $this->standard_attempt_fields($mform);
  42          $this->other_attempt_fields($mform);
  43  
  44          $mform->addElement('header', 'preferencesuser',
  45                  get_string('reportdisplayoptions', 'quiz'));
  46  
  47          $this->standard_preference_fields($mform);
  48          $this->other_preference_fields($mform);
  49  
  50          $mform->addElement('submit', 'submitbutton',
  51                  get_string('showreport', 'quiz'));
  52      }
  53  
  54      /**
  55       * Add the standard form fields for selecting which attempts to include in the report.
  56       *
  57       * @param MoodleQuickForm $mform the form we are building.
  58       */
  59      protected function standard_attempt_fields(MoodleQuickForm $mform) {
  60  
  61          $mform->addElement('select', 'attempts', get_string('reportattemptsfrom', 'quiz'), [
  62                      attempts_report::ENROLLED_WITH    => get_string('reportuserswith', 'quiz'),
  63                      attempts_report::ENROLLED_WITHOUT => get_string('reportuserswithout', 'quiz'),
  64                      attempts_report::ENROLLED_ALL     => get_string('reportuserswithorwithout', 'quiz'),
  65                      attempts_report::ALL_WITH         => get_string('reportusersall', 'quiz'),
  66          ]);
  67  
  68          $stategroup = [
  69              $mform->createElement('advcheckbox', 'stateinprogress', '',
  70                      get_string('stateinprogress', 'quiz')),
  71              $mform->createElement('advcheckbox', 'stateoverdue', '',
  72                      get_string('stateoverdue', 'quiz')),
  73              $mform->createElement('advcheckbox', 'statefinished', '',
  74                      get_string('statefinished', 'quiz')),
  75              $mform->createElement('advcheckbox', 'stateabandoned', '',
  76                      get_string('stateabandoned', 'quiz')),
  77          ];
  78          $mform->addGroup($stategroup, 'stateoptions',
  79                  get_string('reportattemptsthatare', 'quiz'), [' '], false);
  80          $mform->setDefault('stateinprogress', 1);
  81          $mform->setDefault('stateoverdue',    1);
  82          $mform->setDefault('statefinished',   1);
  83          $mform->setDefault('stateabandoned',  1);
  84          $mform->disabledIf('stateinprogress', 'attempts', 'eq', attempts_report::ENROLLED_WITHOUT);
  85          $mform->disabledIf('stateoverdue',    'attempts', 'eq', attempts_report::ENROLLED_WITHOUT);
  86          $mform->disabledIf('statefinished',   'attempts', 'eq', attempts_report::ENROLLED_WITHOUT);
  87          $mform->disabledIf('stateabandoned',  'attempts', 'eq', attempts_report::ENROLLED_WITHOUT);
  88  
  89          if (quiz_report_can_filter_only_graded($this->_customdata['quiz'])) {
  90              $gm = html_writer::tag('span',
  91                      quiz_get_grading_option_name($this->_customdata['quiz']->grademethod),
  92                      ['class' => 'highlight']);
  93              $mform->addElement('advcheckbox', 'onlygraded', '',
  94                      get_string('reportshowonlyfinished', 'quiz', $gm));
  95              $mform->disabledIf('onlygraded', 'attempts', 'eq', attempts_report::ENROLLED_WITHOUT);
  96              $mform->disabledIf('onlygraded', 'statefinished', 'notchecked');
  97          }
  98      }
  99  
 100      /**
 101       * Extension point to allow subclasses to add their own fields in the attempts section.
 102       *
 103       * @param MoodleQuickForm $mform the form we are building.
 104       */
 105      protected function other_attempt_fields(MoodleQuickForm $mform) {
 106      }
 107  
 108      /**
 109       * Add the standard options fields to the form.
 110       *
 111       * @param MoodleQuickForm $mform the form we are building.
 112       */
 113      protected function standard_preference_fields(MoodleQuickForm $mform) {
 114          $mform->addElement('text', 'pagesize', get_string('pagesize', 'quiz'));
 115          $mform->setType('pagesize', PARAM_INT);
 116      }
 117  
 118      /**
 119       * Extension point to allow subclasses to add their own fields in the options section.
 120       *
 121       * @param MoodleQuickForm $mform the form we are building.
 122       */
 123      protected function other_preference_fields(MoodleQuickForm $mform) {
 124      }
 125  
 126      public function validation($data, $files) {
 127          $errors = parent::validation($data, $files);
 128  
 129          if ($data['attempts'] != attempts_report::ENROLLED_WITHOUT && !(
 130                  $data['stateinprogress'] || $data['stateoverdue'] || $data['statefinished'] || $data['stateabandoned'])) {
 131              $errors['stateoptions'] = get_string('reportmustselectstate', 'quiz');
 132          }
 133  
 134          return $errors;
 135      }
 136  }