1 |
efrain |
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\output\grades;
|
|
|
18 |
|
|
|
19 |
use html_writer;
|
|
|
20 |
use renderable;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Represents a grade out of a give total, that wants to be output in a particular way.
|
|
|
25 |
*
|
|
|
26 |
* @package mod_quiz
|
|
|
27 |
* @category output
|
|
|
28 |
* @copyright 2024 The Open University
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class grade_out_of implements renderable {
|
|
|
32 |
/** @var string Indicates we want a short rendering. Also the lang string used. */
|
|
|
33 |
const SHORT = 'outofshort';
|
|
|
34 |
|
|
|
35 |
/** @var string Indicates we want the default rendering. Also the lang string used. */
|
|
|
36 |
const NORMAL = 'outof';
|
|
|
37 |
|
|
|
38 |
/** @var string like normal, but with the percent equivalent in brackets. Also the lang string used */
|
|
|
39 |
const WITH_PERCENT = 'outofpercent';
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Constructor.
|
|
|
43 |
*
|
|
|
44 |
* @param stdClass $quiz Quiz settings.
|
|
|
45 |
* @param float $grade the mark to show.
|
|
|
46 |
* @param float $maxgrade the total to show it out of.
|
|
|
47 |
* @param string|null $name optional, a name for what this grade is.
|
|
|
48 |
* @param string $style which format to use, grade_out_of::NORMAL (default), ::SHORT or ::WITH_PERCENT.
|
|
|
49 |
*/
|
|
|
50 |
public function __construct(
|
|
|
51 |
|
|
|
52 |
/** @var stdClass Quiz settings (so we can access the settings like decimal places). */
|
|
|
53 |
public readonly stdClass $quiz,
|
|
|
54 |
|
|
|
55 |
/** @var float the grade to show. */
|
|
|
56 |
public float $grade,
|
|
|
57 |
|
|
|
58 |
/** @var float the total the grade is out of. */
|
|
|
59 |
public float $maxgrade,
|
|
|
60 |
|
|
|
61 |
/** @var string|null optional, a name for what this grade is. Must be output via format_string. */
|
|
|
62 |
public readonly ?string $name = null,
|
|
|
63 |
|
|
|
64 |
/** @var string The display style, one of the consts above. */
|
|
|
65 |
public readonly string $style = self::NORMAL,
|
|
|
66 |
|
|
|
67 |
) {
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Get the lang string to use to display the grade in the requested style.
|
|
|
72 |
*
|
|
|
73 |
* @return string lang string key from the mod_quiz lang pack.
|
|
|
74 |
*/
|
|
|
75 |
public function get_string_key(): string {
|
|
|
76 |
return $this->style;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Get the formatted values to be inserted into the {@see get_string_key()} string placeholders.
|
|
|
81 |
*
|
|
|
82 |
* Values are not styled. To apply the recommended styling, call {@see style_formatted_values()}
|
|
|
83 |
*
|
|
|
84 |
* @return stdClass to be passed as the third argument to get_string().
|
|
|
85 |
*/
|
|
|
86 |
public function get_formatted_values(): stdClass {
|
|
|
87 |
$a = new stdClass();
|
|
|
88 |
$a->grade = quiz_format_grade($this->quiz, $this->grade);
|
|
|
89 |
$a->maxgrade = quiz_format_grade($this->quiz, $this->maxgrade);
|
|
|
90 |
if ($this->style === self::WITH_PERCENT) {
|
|
|
91 |
$a->percent = format_float($this->grade * 100 / $this->maxgrade,
|
|
|
92 |
$this->quiz->decimalpoints, true, true);
|
|
|
93 |
}
|
|
|
94 |
return $a;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Apply the normal styling to the values returned by {@see get_formatted_values()}.
|
|
|
99 |
*
|
|
|
100 |
* @param stdClass $a formatted values, as returned by get_formatted_values.
|
|
|
101 |
* @return stdClass same structure, with some values wrapped in <b> tags.
|
|
|
102 |
*/
|
|
|
103 |
public function style_formatted_values(stdClass $a): stdClass {
|
|
|
104 |
if ($this->style !== self::SHORT) {
|
|
|
105 |
$a->grade = html_writer::tag('b', $a->grade);
|
|
|
106 |
}
|
|
|
107 |
if ($this->style === self::WITH_PERCENT) {
|
|
|
108 |
$a->percent = html_writer::tag('b', $a->percent);
|
|
|
109 |
}
|
|
|
110 |
return $a;
|
|
|
111 |
}
|
|
|
112 |
}
|