Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [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      protected $includeauto;
  39      protected $hidden = array();
  40      protected $counts;
  41      protected $shownames;
  42  
  43      /** @var bool $showcustomfields whether custom field values should be shown. */
  44      protected $showcustomfields;
  45  
  46      /** @var stdClass $context the quiz context. */
  47      protected $context;
  48  
  49      /**
  50       * quiz_grading_settings_form constructor.
  51       *
  52       * @param array $hidden Array of options form.
  53       * @param stdClass $counts object that stores the number of each type of attempt.
  54       * @param bool $shownames whether student names should be shown.
  55       * @param bool $showcustomfields whether custom field values should be shown.
  56       * @param stdClass $context context object.
  57       */
  58      public function __construct(array $hidden, stdClass $counts, bool $shownames, bool $showcustomfields, stdClass $context) {
  59          global $CFG;
  60          $this->includeauto = !empty($hidden['includeauto']);
  61          $this->hidden = $hidden;
  62          $this->counts = $counts;
  63          $this->shownames = $shownames;
  64          $this->showcustomfields = $showcustomfields;
  65          $this->context = $context;
  66          parent::__construct($CFG->wwwroot . '/mod/quiz/report.php');
  67      }
  68  
  69      protected function definition() {
  70          $mform = $this->_form;
  71  
  72          $mform->addElement('header', 'options', get_string('options', 'quiz_grading'));
  73  
  74          $gradeoptions = array();
  75          foreach (array('needsgrading', 'manuallygraded', 'autograded', 'all') as $type) {
  76              if (empty($this->counts->$type)) {
  77                  continue;
  78              }
  79              if ($type == 'autograded' && !$this->includeauto) {
  80                  continue;
  81              }
  82              $gradeoptions[$type] = get_string('gradeattempts' . $type, 'quiz_grading',
  83                      $this->counts->$type);
  84          }
  85          $mform->addElement('select', 'grade', get_string('attemptstograde', 'quiz_grading'),
  86                  $gradeoptions);
  87  
  88          $mform->addElement('text', 'pagesize', get_string('questionsperpage', 'quiz_grading'),
  89                  array('size' => 3));
  90          $mform->addRule('pagesize', null, 'positiveint', null, 'client');
  91          $mform->setType('pagesize', PARAM_INT);
  92  
  93          $orderoptions = [
  94              'random' => get_string('random', 'quiz_grading'),
  95              'date' => get_string('date')
  96          ];
  97          if ($this->shownames) {
  98              $orderoptions['studentfirstname'] = get_string('firstname');
  99              $orderoptions['studentlastname']  = get_string('lastname');
 100          }
 101          // If the current user can see custom user fields, add the custom user fields to the select menu.
 102          if ($this->showcustomfields) {
 103              $userfieldsapi = \core_user\fields::for_identity($this->context);
 104              foreach ($userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]) as $field) {
 105                  $orderoptions[s($field)] = \core_user\fields::get_display_name(s($field));
 106              }
 107          }
 108          $mform->addElement('select', 'order', get_string('orderattemptsby', 'quiz_grading'),
 109                  $orderoptions);
 110  
 111          foreach ($this->hidden as $name => $value) {
 112              $mform->addElement('hidden', $name, $value);
 113              if ($name == 'mode') {
 114                  $mform->setType($name, PARAM_ALPHA);
 115              } else {
 116                  $mform->setType($name, PARAM_INT);
 117              }
 118          }
 119  
 120          $mform->addElement('submit', 'submitbutton', get_string('changeoptions', 'quiz_grading'));
 121      }
 122  }