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.
   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 qbank_previewquestion;
  18  
  19  use question_display_options;
  20  
  21  /**
  22   * Displays question preview options as default and set the options.
  23   *
  24   * Setting default, getting and setting user preferences in question preview options.
  25   *
  26   * @package    qbank_previewquestion
  27   * @copyright  2010 The Open University
  28   * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class question_preview_options extends question_display_options {
  32  
  33      /** @var string the behaviour to use for this preview. */
  34      public $behaviour;
  35  
  36      /** @var number the maximum mark to use for this preview. */
  37      public $maxmark;
  38  
  39      /** @var int the variant of the question to preview. */
  40      public $variant;
  41  
  42      /** @var string prefix to append to field names to get user_preference names. */
  43      const OPTIONPREFIX = 'question_preview_options_';
  44  
  45      /** @var int Special value for question version ID to indicate that we should always use the latest version */
  46      const ALWAYS_LATEST = 0;
  47  
  48      /**
  49       * Constructor.
  50       * @param \stdClass $question
  51       */
  52      public function __construct($question) {
  53          $this->behaviour = 'deferredfeedback';
  54          $this->maxmark = $question->defaultmark;
  55          $this->variant = null;
  56          $this->correctness = self::VISIBLE;
  57          $this->marks = self::MARK_AND_MAX;
  58          $this->markdp = get_config('quiz', 'decimalpoints');
  59          $this->feedback = self::VISIBLE;
  60          $this->numpartscorrect = $this->feedback;
  61          $this->generalfeedback = self::VISIBLE;
  62          $this->rightanswer = self::VISIBLE;
  63          $this->history = self::HIDDEN;
  64          $this->flags = self::HIDDEN;
  65          $this->manualcomment = self::HIDDEN;
  66      }
  67  
  68      /**
  69       * Names of the options we store in the user preferences table.
  70       * @return array
  71       */
  72      protected function get_user_pref_fields(): array {
  73          return ['behaviour', 'correctness', 'marks', 'markdp', 'feedback', 'generalfeedback', 'rightanswer', 'history'];
  74      }
  75  
  76      /**
  77       * Names and param types of the options we read from the request.
  78       * @return array
  79       */
  80      protected function get_field_types(): array {
  81          return [
  82                  'behaviour' => PARAM_ALPHA,
  83                  'maxmark' => PARAM_LOCALISEDFLOAT,
  84                  'variant' => PARAM_INT,
  85                  'correctness' => PARAM_BOOL,
  86                  'marks' => PARAM_INT,
  87                  'markdp' => PARAM_INT,
  88                  'feedback' => PARAM_BOOL,
  89                  'generalfeedback' => PARAM_BOOL,
  90                  'rightanswer' => PARAM_BOOL,
  91                  'history' => PARAM_BOOL,
  92          ];
  93      }
  94  
  95      /**
  96       * Load the value of the options from the user_preferences table.
  97       */
  98      public function load_user_defaults(): void {
  99          $defaults = get_config('question_preview');
 100          foreach ($this->get_user_pref_fields() as $field) {
 101              $this->$field = get_user_preferences(
 102                      self::OPTIONPREFIX . $field, $defaults->$field);
 103          }
 104          $this->numpartscorrect = $this->feedback;
 105      }
 106  
 107      /**
 108       * Save a change to the user's preview options to the database.
 109       * @param object $newoptions
 110       */
 111      public function save_user_preview_options($newoptions): void {
 112          foreach ($this->get_user_pref_fields() as $field) {
 113              if (isset($newoptions->$field)) {
 114                  set_user_preference(self::OPTIONPREFIX . $field, $newoptions->$field);
 115              }
 116          }
 117      }
 118  
 119      /**
 120       * Set the value of any fields included in the request.
 121       */
 122      public function set_from_request(): void {
 123          foreach ($this->get_field_types() as $field => $type) {
 124              $this->$field = optional_param($field, $this->$field, $type);
 125          }
 126          $this->numpartscorrect = $this->feedback;
 127      }
 128  
 129      /**
 130       * Parameters needed in the URL when continuing this preview.
 131       *
 132       * @return array URL fragment.
 133       */
 134      public function get_url_params(): array {
 135          $params = [];
 136          foreach ($this->get_field_types() as $field => $notused) {
 137              if ($field === 'behaviour' || $field === 'maxmark' || is_null($this->$field)) {
 138                  continue;
 139              }
 140              $params[$field] = $this->$field;
 141          }
 142          return $params;
 143      }
 144  }