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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * This file defines the setting form for the quiz grading report.
  19   *
  20   * @package   quiz_grading
  21   * @copyright 2010 Tim Hunt
  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->libdir . '/formslib.php');
  29  
  30  
  31  /**
  32   * Quiz grading report settings form.
  33   *
  34   * @copyright 2010 Tim Hunt
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class quiz_grading_settings_form extends moodleform {
  38      /** @var bool whether the user has asked to include automatically graded attempts. */
  39      protected $includeauto;
  40  
  41      /** @var array extra hidden fields to add to the form. Things like cm id. */
  42      protected $hidden = [];
  43  
  44      /** @var stdClass data about how many attempts of each type their are. */
  45      protected $counts;
  46  
  47      /** @var bool Whether the user has permission to see user names. */
  48      protected $shownames;
  49  
  50      /** @var bool $showcustomfields whether custom field values should be shown. */
  51      protected $showcustomfields;
  52  
  53      /** @var stdClass $context the quiz context. */
  54      protected $context;
  55  
  56      /**
  57       * quiz_grading_settings_form constructor.
  58       *
  59       * @param array $hidden Array of options form.
  60       * @param stdClass $counts object that stores the number of each type of attempt.
  61       * @param bool $shownames whether student names should be shown.
  62       * @param bool $showcustomfields whether custom field values should be shown.
  63       * @param stdClass $context context object.
  64       */
  65      public function __construct(array $hidden, stdClass $counts, bool $shownames, bool $showcustomfields, stdClass $context) {
  66          global $CFG;
  67          $this->includeauto = !empty($hidden['includeauto']);
  68          $this->hidden = $hidden;
  69          $this->counts = $counts;
  70          $this->shownames = $shownames;
  71          $this->showcustomfields = $showcustomfields;
  72          $this->context = $context;
  73          parent::__construct($CFG->wwwroot . '/mod/quiz/report.php');
  74      }
  75  
  76      protected function definition() {
  77          $mform = $this->_form;
  78  
  79          $mform->addElement('header', 'options', get_string('options', 'quiz_grading'));
  80  
  81          $gradeoptions = [];
  82          foreach (['needsgrading', 'manuallygraded', 'autograded', 'all'] as $type) {
  83              if (empty($this->counts->$type)) {
  84                  continue;
  85              }
  86              if ($type == 'autograded' && !$this->includeauto) {
  87                  continue;
  88              }
  89              $gradeoptions[$type] = get_string('gradeattempts' . $type, 'quiz_grading',
  90                      $this->counts->$type);
  91          }
  92          $mform->addElement('select', 'grade', get_string('attemptstograde', 'quiz_grading'),
  93                  $gradeoptions);
  94  
  95          $mform->addElement('text', 'pagesize', get_string('questionsperpage', 'quiz_grading'),
  96                  ['size' => 3]);
  97          $mform->addRule('pagesize', null, 'positiveint', null, 'client');
  98          $mform->setType('pagesize', PARAM_INT);
  99  
 100          $orderoptions = [
 101              'random' => get_string('random', 'quiz_grading'),
 102              'date' => get_string('date')
 103          ];
 104          if ($this->shownames) {
 105              $orderoptions['studentfirstname'] = get_string('firstname');
 106              $orderoptions['studentlastname']  = get_string('lastname');
 107          }
 108          // If the current user can see custom user fields, add the custom user fields to the select menu.
 109          if ($this->showcustomfields) {
 110              $userfieldsapi = \core_user\fields::for_identity($this->context);
 111              foreach ($userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $field) {
 112                  $orderoptions[s($field)] = \core_user\fields::get_display_name(s($field));
 113              }
 114          }
 115          $mform->addElement('select', 'order', get_string('orderattemptsby', 'quiz_grading'),
 116                  $orderoptions);
 117  
 118          foreach ($this->hidden as $name => $value) {
 119              $mform->addElement('hidden', $name, $value);
 120              if ($name == 'mode') {
 121                  $mform->setType($name, PARAM_ALPHA);
 122              } else {
 123                  $mform->setType($name, PARAM_INT);
 124              }
 125          }
 126  
 127          $mform->addElement('submit', 'submitbutton', get_string('changeoptions', 'quiz_grading'));
 128      }
 129  }