Skip to content

Commit

Permalink
MDL-82131 core_grades: penalty indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Aug 15, 2024
1 parent a75365f commit 9d7c1d9
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
90 changes: 90 additions & 0 deletions grade/classes/output/penalty_indicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_grades\output;

use core\output\renderer_base;
use templatable;
use renderable;

/**
* The base class for the action bar in the gradebook pages.
*
* @package core_grades
* @copyright 2024 Catalyst IT Australia Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class penalty_indicator implements templatable, renderable {
/** @var int $decimals the decimal places */
protected int $decimals;

/** @var float $penalty the deducted grade */
protected float $penalty;

/** @var float|null $finalgrade the final grade */
protected ?float $finalgrade;

/** @var float|null $maxgrade the maximum grade */
protected ?float $maxgrade;

/** @var array|null $icon icon data */
protected ?array $icon;

/**
* The class constructor.
*
* @param int $decimals the decimal places
* @param float $penalty the deducted grade
* @param float|null $finalgrade the final grade
* @param float|null $maxgrade the maximum grade
* @param array|null $icon icon data
*/
public function __construct(int $decimals, float $penalty, ?float $finalgrade = null,
?float $maxgrade = null, ?array $icon = null) {
$this->icon = $icon;
$this->decimals = $decimals;
$this->penalty = $penalty;
$this->finalgrade = $finalgrade;
$this->maxgrade = $maxgrade;
}

/**
* Returns the template for the actions bar.
*
* @return string
*/
public function get_template(): string {
return 'core_grades/penalty_indicator';
}

/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the penalty indicator.
* @return array
*/
public function export_for_template(renderer_base $output) {
$context = [
'penalty' => format_float($this->penalty, $this->decimals),
'finalgrade' => $this->finalgrade ? format_float($this->finalgrade, $this->decimals) : null,
'maxgrade' => $this->maxgrade ? format_float($this->maxgrade, $this->decimals) : null,
'icon' => $this->icon ?: ['name' => 'i/risk_xss', 'component' => 'core'] ,
'info' => get_string('gradepenalty_indicator_info', 'core_grades', format_float($this->penalty, $this->decimals)),
];

return $context;
}
}
12 changes: 12 additions & 0 deletions grade/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use core\output\comboboxsearch;
use \core_grades\output\action_bar;
use core_grades\output\penalty_indicator;
use core_message\helper;
use core_message\api;

Expand Down Expand Up @@ -227,4 +228,15 @@ public function user_heading(stdClass $user, int $courseid, bool $showbuttons =

return $this->render_from_template('core_grades/user_heading', $headingdata);
}

/**
* Renders the penalty indicator.
*
* @param penalty_indicator $penaltyindicator
* @return string The HTML output
*/
public function render_penalty_indicator(penalty_indicator $penaltyindicator): string {
$data = $penaltyindicator->export_for_template($this);
return $this->render_from_template($penaltyindicator->get_template(), $data);
}
}
43 changes: 43 additions & 0 deletions grade/templates/penalty_indicator.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core_grades/penalty_indicator
This template is used to render the penalty indicator.
Example context (json):
{
"penalty": 20,
"finalgrade": 50,
"maxgrade": 100,
"info": "Late penalty applied -20 marks"
}
}}

{{#icon}}
<span class="penalty-indicator-icon" title="{{info}}">
{{#pix}}{{name}}, {{component}}{{/pix}}
</span>
{{/icon}}
{{#finalgrade}}
<span class="penalty-indicator-value">
{{#maxgrade}}
{{finalgrade}} / {{maxgrade}}
{{/maxgrade}}
{{^maxgrade}}
{{finalgrade}}
{{/maxgrade}}
</span>
{{/finalgrade}}
90 changes: 90 additions & 0 deletions grade/tests/output/penalty_indicator_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_grades\output;

use advanced_testcase;

/**
* Test class for penalty_indicator
*
* @package core_grades
* @copyright 2024 Catalyst IT Australia Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class penalty_indicator_test extends advanced_testcase {
/**
* Data provider for test_export_for_template
* @return array
*/
public static function export_for_template_provider(): array {
return [
// Default icon, with max grade.
[
'html' => <<<EOD
<span class="penalty-indicator-icon" title="Late penalty applied -10.00 marks">
<i class="icon fa fa-exclamation-triangle text-danger fa-fw " aria-hidden="true" ></i>
</span>
<span class="penalty-indicator-value">
90.00 / 100.00
</span>
EOD,
'icon' => [],
'penalty' => 10,
'finalgrade' => 90,
'maxgrade' => 100,
],
// Custom icon, without max grade.
[
'html' => <<<EOD
<span class="penalty-indicator-icon" title="Late penalty applied -10.00 marks">
<i class="icon fa fa-flag fa-fw " aria-hidden="true" ></i>
</span>
<span class="penalty-indicator-value">
90.00
</span>
EOD,
'icon' => ['name' => 'i/flagged', 'component' => 'core'],
'penalty' => 10,
'finalgrade' => 90,
'maxgrade' => null,
],
];
}

/**
* Test penalty_indicator
*
* @dataProvider export_for_template_provider
*
* @covers \core_grades\output\penalty_indicator
*
* @param string $expectedhtml The expected html
* @param array $icon icon to display before the penalty
* @param float $penalty The penalty
* @param float $finalgrade The final grade
* @param float|null $maxgrade The max grade
*/
public function test_export_for_template(string $expectedhtml, array $icon, float $penalty,
float $finalgrade, ?float $maxgrade): void {
global $PAGE;
$indicator = new \core_grades\output\penalty_indicator(2, $penalty, $finalgrade, $maxgrade, $icon);
$renderer = $PAGE->get_renderer('core_grades');
$html = $renderer->render_penalty_indicator($indicator);

$this->assertEquals($expectedhtml, $html);
}
}
1 change: 1 addition & 0 deletions lang/en/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
$string['gradepass'] = 'Grade to pass';
$string['gradepass_help'] = 'This setting determines the minimum grade required to pass. The value is used in activity and course completion, and in the gradebook, where pass grades are highlighted in green and fail grades in red.';
$string['gradepassgreaterthangrade'] = 'The grade to pass can not be greater than the maximum possible grade {$a}';
$string['gradepenalty_indicator_info'] = 'Late penalty applied -{$a} marks';
$string['gradepointdefault'] = 'Grade point default';
$string['gradepointdefault_help'] = 'This setting determines the default value for the grade point value available in a grade item.';
$string['gradepointdefault_validateerror'] = 'This setting must be an integer between 1 and the grade point maximum.';
Expand Down

0 comments on commit 9d7c1d9

Please sign in to comment.