forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-82131 core_grades: penalty indicator
- Loading branch information
Nathan Nguyen
committed
Aug 15, 2024
1 parent
a75365f
commit 9d7c1d9
Showing
5 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters