MDL-77564 Quiz display options: Hide or show the grade information
[moodle.git] / mod / quiz / classes / question / display_options.php
blobb9904ff27963fb27941173514e18a704d91ef85b
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.
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/>.
17 namespace mod_quiz\question;
19 defined('MOODLE_INTERNAL') || die();
20 require_once($CFG->dirroot . '/question/engine/lib.php');
22 /**
23 * An extension of question_display_options that includes the extra options used by the quiz.
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
30 class display_options extends \question_display_options {
31 /**
32 * The bitmask patterns use in the review option settings.
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.
38 * @var int bit used to indicate 'during the attempt'.
40 const DURING = 0x10000;
42 /** @var int as above, bit used to indicate 'immediately after the attempt'. */
43 const IMMEDIATELY_AFTER = 0x01000;
45 /** @var int as above, bit used to indicate 'later while the quiz is still open'. */
46 const LATER_WHILE_OPEN = 0x00100;
48 /** @var int as above, bit used to indicate 'after the quiz is closed'. */
49 const AFTER_CLOSE = 0x00010;
51 /**
52 * @var bool if this is false, then the student is not allowed to review
53 * anything about the attempt.
55 public $attempt = true;
57 /**
58 * @var int whether the attempt overall feedback is visible.
60 public $overallfeedback = self::VISIBLE;
62 /**
63 * Set up the various options from the quiz settings, and a time constant.
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.
70 public static function make_from_quiz(\stdClass $quiz, int $when): self {
71 $options = new self();
73 $options->attempt = self::extract($quiz->reviewattempt, $when, true, false);
74 $options->correctness = self::extract($quiz->reviewcorrectness, $when);
75 $options->marks = self::extract($quiz->reviewmaxmarks, $when,
76 self::extract($quiz->reviewmarks, $when, self::MARK_AND_MAX, self::MAX_ONLY), self::HIDDEN);
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);
82 $options->numpartscorrect = $options->feedback;
83 $options->manualcomment = $options->feedback;
85 if ($quiz->questiondecimalpoints != -1) {
86 $options->markdp = $quiz->questiondecimalpoints;
87 } else {
88 $options->markdp = $quiz->decimalpoints;
91 return $options;
94 /**
95 * Helper function to return one value or another depending on whether one bit is set.
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.
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;