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 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  namespace mod_quiz\admin;
  18  
  19  /**
  20   * Admin settings class for the quiz review options.
  21   *
  22   * @package   mod_quiz
  23   * @category  admin
  24   * @copyright  2008 Tim Hunt
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class review_setting extends \admin_setting {
  28      /**
  29       * @var integer should match the constants defined in
  30       * {@see display_options}. Copied for performance reasons.
  31       */
  32      const DURING            = 0x10000;
  33  
  34      /**
  35       * @var integer should match the constants defined in
  36       * {@see display_options}. Copied for performance reasons.
  37       */
  38      const IMMEDIATELY_AFTER = 0x01000;
  39  
  40      /**
  41       * @var integer should match the constants defined in
  42       * {@see display_options}. Copied for performance reasons.
  43       */
  44      const LATER_WHILE_OPEN  = 0x00100;
  45  
  46      /**
  47       * @var integer should match the constants defined in
  48       * {@see display_options}. Copied for performance reasons.
  49       */
  50      const AFTER_CLOSE       = 0x00010;
  51  
  52      /**
  53       * @var boolean|null forced checked / disabled attributes for the during time.
  54       */
  55      protected $duringstate;
  56  
  57      /**
  58       * This should match {@link mod_quiz_mod_form::$reviewfields} but copied
  59       * here because generating the admin tree needs to be fast.
  60       * @return array
  61       */
  62      public static function fields() {
  63          return [
  64              'attempt'          => get_string('theattempt', 'quiz'),
  65              'correctness'      => get_string('whethercorrect', 'question'),
  66              'maxmarks'         => get_string('maxmarks', 'quiz'),
  67              'marks'            => get_string('marks', 'question'),
  68              'specificfeedback' => get_string('specificfeedback', 'question'),
  69              'generalfeedback'  => get_string('generalfeedback', 'question'),
  70              'rightanswer'      => get_string('rightanswer', 'question'),
  71              'overallfeedback'  => get_string('overallfeedback', 'quiz'),
  72          ];
  73      }
  74  
  75      /**
  76       * Constructor.
  77       *
  78       * @param string $name unique ascii name, either 'mysetting' for settings that in config,
  79       *                     or 'myplugin/mysetting' for ones in config_plugins.
  80       * @param string $visiblename localised name
  81       * @param string $description localised long description
  82       * @param mixed $defaultsetting string or array depending on implementation
  83       * @param bool|null $duringstate
  84       */
  85      public function __construct($name, $visiblename, $description,
  86              $defaultsetting, $duringstate = null) {
  87          $this->duringstate = $duringstate;
  88          parent::__construct($name, $visiblename, $description, $defaultsetting);
  89      }
  90  
  91      /**
  92       * Return the combination that means all times.
  93       * @return int all times.
  94       */
  95      public static function all_on() {
  96          return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
  97                  self::AFTER_CLOSE;
  98      }
  99  
 100      /**
 101       * Get an array of the names of all the possible times.
 102       * @return array an array of time constant => lang string.
 103       */
 104      protected static function times() {
 105          return [
 106              self::DURING            => get_string('reviewduring', 'quiz'),
 107              self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
 108              self::LATER_WHILE_OPEN  => get_string('reviewopen', 'quiz'),
 109              self::AFTER_CLOSE       => get_string('reviewclosed', 'quiz'),
 110          ];
 111      }
 112  
 113      protected function normalise_data($data) {
 114          $times = self::times();
 115          $value = 0;
 116          foreach ($times as $timemask => $name) {
 117              if ($timemask == self::DURING && !is_null($this->duringstate)) {
 118                  if ($this->duringstate) {
 119                      $value += $timemask;
 120                  }
 121              } else if (!empty($data[$timemask])) {
 122                  $value += $timemask;
 123              }
 124          }
 125          return $value;
 126      }
 127  
 128      public function get_setting() {
 129          return $this->config_read($this->name);
 130      }
 131  
 132      public function write_setting($data) {
 133          if (is_array($data) || empty($data)) {
 134              $data = $this->normalise_data($data);
 135          }
 136          $this->config_write($this->name, $data);
 137          return '';
 138      }
 139  
 140      public function output_html($data, $query = '') {
 141          if (is_array($data) || empty($data)) {
 142              $data = $this->normalise_data($data);
 143          }
 144  
 145          $return = '<div class="group"><input type="hidden" name="' .
 146                      $this->get_full_name() . '[' . self::DURING . ']" value="0" />';
 147          foreach (self::times() as $timemask => $namestring) {
 148              $id = $this->get_id(). '_' . $timemask;
 149              $state = '';
 150              if ($data & $timemask) {
 151                  $state = 'checked="checked" ';
 152              }
 153              if ($timemask == self::DURING && !is_null($this->duringstate)) {
 154                  $state = 'disabled="disabled" ';
 155                  if ($this->duringstate) {
 156                      $state .= 'checked="checked" ';
 157                  }
 158              }
 159              $return .= '<span><input type="checkbox" name="' .
 160                      $this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
 161                      '" ' . $state . '/> <label for="' . $id . '">' .
 162                      $namestring . "</label></span>\n";
 163          }
 164          $return .= "</div>\n";
 165  
 166          return format_admin_setting($this, $this->visiblename, $return,
 167                  $this->description, true, '', get_string('everythingon', 'quiz'), $query);
 168      }
 169  }