Skip to content

Commit 74d3ba1

Browse files
committed
MDL-84981 core_grades: Show penalty exemption icon
1 parent 261b9f2 commit 74d3ba1

File tree

16 files changed

+182
-306
lines changed

16 files changed

+182
-306
lines changed

grade/classes/output/penalty_indicator.php

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 core_grades\output;
18+
19+
use core\output\renderer_base;
20+
use core\output\templatable;
21+
use core\output\renderable;
22+
use core_grades\penalty_exemption;
23+
use grade_grade;
24+
25+
/**
26+
* The base class for the action bar in the gradebook pages.
27+
*
28+
* @package core_grades
29+
* @copyright 2024 Catalyst IT Australia Pty Ltd
30+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*/
32+
class penalty_status implements renderable, templatable {
33+
/**
34+
* The class constructor.
35+
*
36+
* @param grade_grade $grade The user grade.
37+
* @param int $decimals The number of decimal places to show in the grade.
38+
* @param bool $showexemptions Whether to show exemptions in the status.
39+
*/
40+
public function __construct(
41+
/** @var grade_grade $grade The user grade. */
42+
protected grade_grade $grade,
43+
44+
/** @var int $decimals The number of decimal places to show in the grade. */
45+
protected int $decimals = 2,
46+
47+
/** @var bool $showexemptions Whether to show exemptions in the status. */
48+
protected bool $showexemptions = true,
49+
) {
50+
}
51+
52+
/**
53+
* Returns the template for the actions bar.
54+
*
55+
* @return string
56+
*/
57+
public function get_template(): string {
58+
return 'core_grades/penalty_status';
59+
}
60+
61+
#[\Override]
62+
public function export_for_template(renderer_base $output): array {
63+
if ($this->showexemptions && penalty_exemption::is_user_exempt($this->grade->userid, $this->grade->get_context()->id)) {
64+
return [
65+
'isexempt' => true,
66+
'penaltyapplied' => false,
67+
'exemptionicon' => ['name' => 'i/shield', 'component' => 'core'],
68+
'exemptioninfo' => get_string('gradepenalty_exemption_info', 'core_grades'),
69+
];
70+
}
71+
72+
if ($this->grade->is_penalty_applied_to_final_grade()) {
73+
$penalty = format_float($this->grade->deductedmark, $this->decimals);
74+
return [
75+
'isexempt' => false,
76+
'penaltyapplied' => true,
77+
'penaltyicon' => ['name' => 'i/risk_xss', 'component' => 'core'],
78+
'penaltyinfo' => get_string('gradepenalty_indicator_info', 'core_grades', $penalty),
79+
];
80+
}
81+
82+
return [
83+
'isexempt' => false,
84+
'penaltyapplied' => false,
85+
];
86+
}
87+
}

grade/classes/penalty_manager.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace core_grades;
1818

19+
use cm_info;
1920
use core\context;
2021
use core\plugininfo\gradepenalty;
2122
use core\url;
@@ -250,20 +251,15 @@ private static function apply_penalty(
250251
* Returns the penalty indicator HTML code if a penalty is applied to the grade.
251252
* Otherwise, returns an empty string.
252253
*
253-
* @param grade_grade $grade Grade object
254-
* @return string HTML code for penalty indicator
254+
* @param grade_grade $grade Grade object.
255+
* @param bool $showexemptions Whether to show exemptions in the status.
256+
* @return string HTML code for the penalty indicator or an empty string.
255257
*/
256-
public static function show_penalty_indicator(grade_grade $grade): string {
258+
public static function render_penalty_status(grade_grade $grade, bool $showexemptions = true): string {
257259
global $PAGE;
258-
259-
// Show penalty indicator if penalty is greater than 0.
260-
if ($grade->is_penalty_applied_to_final_grade()) {
261-
$indicator = new \core_grades\output\penalty_indicator(2, $grade);
262-
$renderer = $PAGE->get_renderer('core_grades');
263-
return $renderer->render_penalty_indicator($indicator);
264-
}
265-
266-
return '';
260+
$indicator = new \core_grades\output\penalty_status($grade, 2, $showexemptions);
261+
$renderer = $PAGE->get_renderer('core_grades');
262+
return $renderer->render_penalty_status($indicator);
267263
}
268264

269265
/**

grade/renderer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use core\output\comboboxsearch;
2020
use core_grades\output\action_bar;
21-
use core_grades\output\penalty_indicator;
21+
use core_grades\output\penalty_status;
2222
use core_message\helper;
2323
use core_message\api;
2424

@@ -241,11 +241,11 @@ public function user_heading(stdClass $user, int $courseid, bool $showbuttons =
241241
/**
242242
* Renders the penalty indicator.
243243
*
244-
* @param penalty_indicator $penaltyindicator
244+
* @param penalty_status $penaltystatus
245245
* @return string The HTML output
246246
*/
247-
public function render_penalty_indicator(penalty_indicator $penaltyindicator): string {
248-
$data = $penaltyindicator->export_for_template($this);
249-
return $this->render_from_template($penaltyindicator->get_template(), $data);
247+
public function render_penalty_status(penalty_status $penaltystatus): string {
248+
$data = $penaltystatus->export_for_template($this);
249+
return $this->render_from_template($penaltystatus->get_template(), $data);
250250
}
251251
}

grade/report/grader/lib.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,9 @@ public function get_right_rows(bool $displayaverages): array {
11831183
}
11841184

11851185
$context->extraclasses = 'gradevalue ' . $hidden . $gradepass;
1186-
$context->text = grade_format_gradevalue($gradeval, $item, true,
1186+
$context->text = penalty_manager::render_penalty_status($grade);
1187+
$context->text .= grade_format_gradevalue($gradeval, $item, true,
11871188
$gradedisplaytype, null);
1188-
$context->text .= penalty_manager::show_penalty_indicator($grade);
11891189
}
11901190
}
11911191

grade/report/user/classes/report/user.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ private function fill_table_recursive(array &$element) {
703703
$gradeitemdata['graderaw'] = $gradeval;
704704
$data['grade']['content'] = grade_format_gradevalue($gradeval,
705705
$gradegrade->grade_item,
706-
true) . penalty_manager::show_penalty_indicator($gradegrade) . $gradestatus;
706+
true) . penalty_manager::render_penalty_status($gradegrade) . $gradestatus;
707707
}
708708
} else {
709709
$gradestatusclass = '';
@@ -730,7 +730,7 @@ private function fill_table_recursive(array &$element) {
730730

731731
$data['grade']['class'] = "{$class} {$gradestatusclass}";
732732
$data['grade']['content'] = $gradepassicon . grade_format_gradevalue($gradeval,
733-
$gradegrade->grade_item, true) . penalty_manager::show_penalty_indicator($gradegrade) . $gradestatus;
733+
$gradegrade->grade_item, true) . penalty_manager::render_penalty_status($gradegrade) . $gradestatus;
734734
$gradeitemdata['graderaw'] = $gradeval;
735735
}
736736
$data['grade']['headers'] = "$headercat $headerrow grade$userid";

grade/templates/penalty_indicator.mustache renamed to grade/templates/penalty_status.mustache

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,37 @@
1212
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1313
}}
1414
{{!
15-
@template core_grades/penalty_indicator
16-
17-
This template is used to render the penalty indicator.
15+
@template core_grades/penalty_status
1816
17+
This template is used to render the penalty status.
1918
2019
Example context (json):
2120
{
22-
"penalty": 20,
23-
"finalgrade": 50,
24-
"maxgrade": 100,
25-
"info": "Late penalty applied -20 marks"
21+
"isexempt": true,
22+
"penaltyapplied": false,
23+
"exemptionicon": {"name": "i/shield", "component": "core"},
24+
"exemptioninfo": "Grade penalty exemption applied"
2625
}
2726
}}
2827

29-
{{#icon}}
30-
<span data-bs-toggle="tooltip" class="penalty-indicator-icon" title="{{info}}">
31-
{{#pix}}{{name}}, {{component}}{{/pix}}
32-
</span>
33-
{{/icon}}
34-
{{#finalgrade}}
35-
<span class="penalty-indicator-value">
36-
{{#grademax}}
37-
{{finalgrade}} / {{grademax}}
38-
{{/grademax}}
39-
{{^grademax}}
40-
{{finalgrade}}
41-
{{/grademax}}
42-
</span>
43-
{{/finalgrade}}
28+
{{#isexempt}}
29+
<span class="gradepenaltystatus" data-bs-toggle="tooltip" title="{{exemptioninfo}}"><!-- Prevent whitespace
30+
-->{{#pix}}{{exemptionicon.name}}, {{exemptionicon.component}}, {{exemptioninfo}}{{/pix}}</span>
31+
{{/isexempt}}
32+
{{^isexempt}}
33+
{{#penaltyapplied}}
34+
<span class="gradepenaltystatus" data-bs-toggle="tooltip" title="{{penaltyinfo}}"><!-- Prevent whitespace
35+
-->{{#pix}}{{penaltyicon.name}}, {{penaltyicon.component}}, {{penaltyinfo}}{{/pix}}</span>
36+
{{/penaltyapplied}}
37+
{{/isexempt}}
4438

4539
{{#js}}
4640
require(['theme_boost/bootstrap/tooltip'], function(Tooltip) {
4741
// Re-init the tooltip to ensure those dynamically added through AJAX are initialized
48-
document.querySelectorAll('.penalty-indicator-icon[data-bs-toggle="tooltip"]').forEach(tooltipElement => {
42+
document.querySelectorAll('.gradepenaltystatus[data-bs-toggle="tooltip"]').forEach(tooltipElement => {
4943
Tooltip.getInstance(tooltipElement)?.dispose();
5044
new Tooltip(tooltipElement);
45+
tooltipElement.querySelector('i[title]').removeAttribute('title');
5146
});
5247
});
5348
{{/js}}

0 commit comments

Comments
 (0)