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 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              'marks'            => get_string('marks', 'question'),
  67              'specificfeedback' => get_string('specificfeedback', 'question'),
  68              'generalfeedback'  => get_string('generalfeedback', 'question'),
  69              'rightanswer'      => get_string('rightanswer', 'question'),
  70              'overallfeedback'  => get_string('overallfeedback', 'quiz'),
  71          ];
  72      }
  73  
  74      /**
  75       * Constructor.
  76       *
  77       * @param string $name unique ascii name, either 'mysetting' for settings that in config,
  78       *                     or 'myplugin/mysetting' for ones in config_plugins.
  79       * @param string $visiblename localised name
  80       * @param string $description localised long description
  81       * @param mixed $defaultsetting string or array depending on implementation
  82       * @param bool|null $duringstate
  83       */
  84      public function __construct($name, $visiblename, $description,
  85              $defaultsetting, $duringstate = null) {
  86          $this->duringstate = $duringstate;
  87          parent::__construct($name, $visiblename, $description, $defaultsetting);
  88      }
  89  
  90      /**
  91       * Return the combination that means all times.
  92       * @return int all times.
  93       */
  94      public static function all_on() {
  95          return self::DURING | self::IMMEDIATELY_AFTER | self::LATER_WHILE_OPEN |
  96                  self::AFTER_CLOSE;
  97      }
  98  
  99      /**
 100       * Get an array of the names of all the possible times.
 101       * @return array an array of time constant => lang string.
 102       */
 103      protected static function times() {
 104          return [
 105              self::DURING            => get_string('reviewduring', 'quiz'),
 106              self::IMMEDIATELY_AFTER => get_string('reviewimmediately', 'quiz'),
 107              self::LATER_WHILE_OPEN  => get_string('reviewopen', 'quiz'),
 108              self::AFTER_CLOSE       => get_string('reviewclosed', 'quiz'),
 109          ];
 110      }
 111  
 112      protected function normalise_data($data) {
 113          $times = self::times();
 114          $value = 0;
 115          foreach ($times as $timemask => $name) {
 116              if ($timemask == self::DURING && !is_null($this->duringstate)) {
 117                  if ($this->duringstate) {
 118                      $value += $timemask;
 119                  }
 120              } else if (!empty($data[$timemask])) {
 121                  $value += $timemask;
 122              }
 123          }
 124          return $value;
 125      }
 126  
 127      public function get_setting() {
 128          return $this->config_read($this->name);
 129      }
 130  
 131      public function write_setting($data) {
 132          if (is_array($data) || empty($data)) {
 133              $data = $this->normalise_data($data);
 134          }
 135          $this->config_write($this->name, $data);
 136          return '';
 137      }
 138  
 139      public function output_html($data, $query = '') {
 140          if (is_array($data) || empty($data)) {
 141              $data = $this->normalise_data($data);
 142          }
 143  
 144          $return = '<div class="group"><input type="hidden" name="' .
 145                      $this->get_full_name() . '[' . self::DURING . ']" value="0" />';
 146          foreach (self::times() as $timemask => $namestring) {
 147              $id = $this->get_id(). '_' . $timemask;
 148              $state = '';
 149              if ($data & $timemask) {
 150                  $state = 'checked="checked" ';
 151              }
 152              if ($timemask == self::DURING && !is_null($this->duringstate)) {
 153                  $state = 'disabled="disabled" ';
 154                  if ($this->duringstate) {
 155                      $state .= 'checked="checked" ';
 156                  }
 157              }
 158              $return .= '<span><input type="checkbox" name="' .
 159                      $this->get_full_name() . '[' . $timemask . ']" value="1" id="' . $id .
 160                      '" ' . $state . '/> <label for="' . $id . '">' .
 161                      $namestring . "</label></span>\n";
 162          }
 163          $return .= "</div>\n";
 164  
 165          return format_admin_setting($this, $this->visiblename, $return,
 166                  $this->description, true, '', get_string('everythingon', 'quiz'), $query);
 167      }
 168  }