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\question;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  require_once($CFG->dirroot . '/question/engine/lib.php');
  21  
  22  /**
  23   * An extension of question_display_options that includes the extra options used by the quiz.
  24   *
  25   * @package   mod_quiz
  26   * @category  question
  27   * @copyright 2022 The Open University
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class display_options extends \question_display_options {
  31      /**
  32       * The bitmask patterns use in the review option settings.
  33       *
  34       * In the quiz settings, the review... (e.g. reviewmarks) values are
  35       * bit patterns that allow what is visible to be change at different times.
  36       * These constants define which bit is for which time.
  37       *
  38       * @var int bit used to indicate 'during the attempt'.
  39       */
  40      const DURING = 0x10000;
  41  
  42      /** @var int as above, bit used to indicate 'immediately after the attempt'. */
  43      const IMMEDIATELY_AFTER = 0x01000;
  44  
  45      /** @var int as above, bit used to indicate 'later while the quiz is still open'. */
  46      const LATER_WHILE_OPEN = 0x00100;
  47  
  48      /** @var int as above, bit used to indicate 'after the quiz is closed'. */
  49      const AFTER_CLOSE = 0x00010;
  50  
  51      /**
  52       * @var bool if this is false, then the student is not allowed to review
  53       * anything about the attempt.
  54       */
  55      public $attempt = true;
  56  
  57      /**
  58       * @var int whether the attempt overall feedback is visible.
  59       */
  60      public $overallfeedback = self::VISIBLE;
  61  
  62      /**
  63       * Set up the various options from the quiz settings, and a time constant.
  64       *
  65       * @param \stdClass $quiz the quiz settings from the database.
  66       * @param int $when of the constants {@see DURING}, {@see IMMEDIATELY_AFTER},
  67       *      {@see LATER_WHILE_OPEN} or {@see AFTER_CLOSE}.
  68       * @return display_options instance of this class set up appropriately.
  69       */
  70      public static function make_from_quiz(\stdClass $quiz, int $when): self {
  71          $options = new self();
  72  
  73          $options->attempt = self::extract($quiz->reviewattempt, $when, true, false);
  74          $options->correctness = self::extract($quiz->reviewcorrectness, $when);
  75          $options->marks = self::extract($quiz->reviewmarks, $when,
  76                  self::MARK_AND_MAX, self::MAX_ONLY);
  77          $options->feedback = self::extract($quiz->reviewspecificfeedback, $when);
  78          $options->generalfeedback = self::extract($quiz->reviewgeneralfeedback, $when);
  79          $options->rightanswer = self::extract($quiz->reviewrightanswer, $when);
  80          $options->overallfeedback = self::extract($quiz->reviewoverallfeedback, $when);
  81  
  82          $options->numpartscorrect = $options->feedback;
  83          $options->manualcomment = $options->feedback;
  84  
  85          if ($quiz->questiondecimalpoints != -1) {
  86              $options->markdp = $quiz->questiondecimalpoints;
  87          } else {
  88              $options->markdp = $quiz->decimalpoints;
  89          }
  90  
  91          return $options;
  92      }
  93  
  94      /**
  95       * Helper function to return one value or another depending on whether one bit is set.
  96       *
  97       * @param int $setting the setting to unpack (e.g. $quiz->reviewmarks).
  98       * @param int $when of the constants {@see DURING}, {@see IMMEDIATELY_AFTER},
  99       *      {@see LATER_WHILE_OPEN} or {@see AFTER_CLOSE}.
 100       * @param bool|int $whenset value to return when the bit is set.
 101       * @param bool|int $whennotset value to return when the bit is set.
 102       * @return bool|int $whenset or $whennotset, depending.
 103       */
 104      protected static function extract(int $setting, int $when,
 105              $whenset = self::VISIBLE, $whennotset = self::HIDDEN) {
 106          if ($setting & $when) {
 107              return $whenset;
 108          } else {
 109              return $whennotset;
 110          }
 111      }
 112  }