Skip to content

Commit

Permalink
MDL-82126 gradereport_grader: apply penalty to overridden grade
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Nov 7, 2024
1 parent e0df66a commit c3b04b5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
5 changes: 5 additions & 0 deletions admin/settings/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
new lang_string('gradepenalty_supportedplugins', 'grades'),
new lang_string('gradepenalty_supportedplugins_help', 'grades'), [], $options));

// Option to apply penalty to overridden grades.
$temp->add(new admin_setting_configcheckbox('gradepenalty_overriddengrade',
new lang_string('gradepenalty_overriddengrade', 'grades'),
new lang_string('gradepenalty_overriddengrade_help', 'grades'), 0));

$ADMIN->add('gradepenalty', $temp);
}

Expand Down
2 changes: 2 additions & 0 deletions grade/report/grader/lang/en/gradereport_grader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['applypenaltytext'] = 'Deduct {$a}';
$string['applypenaltytooltip'] = 'Apply a deduction to the grade. This value is retrieved from the current overridden grade.';
$string['aria:dropdowncolumns'] = 'Collapsed columns found';
$string['clearsearch'] = 'Clear searched users';
$string['collapsedcolumns'] = 'Collapsed columns <span class="badge rounded-pill bg-primary text-white ms-1" data-collapse="count">{$a}</span>';
Expand Down
38 changes: 28 additions & 10 deletions grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,21 @@ public function process_data($data) {
continue;
}

// If the grade item uses a custom scale
if (!empty($oldvalue->grade_item->scaleid)) {
// Detect if there is any mark deduction.
if (!isset($data->deduction[$userid][$itemid])) {
// If the grade item uses a custom scale
if (!empty($oldvalue->grade_item->scaleid)) {

if ((int)$oldvalue->finalgrade === (int)$postedvalue) {
continue;
}
} else {
// The grade item uses a numeric scale
if ((int)$oldvalue->finalgrade === (int)$postedvalue) {
continue;
}
} else {
// The grade item uses a numeric scale

// Format the finalgrade from the DB so that it matches the grade from the client
if ($postedvalue === format_float($oldvalue->finalgrade, $oldvalue->grade_item->get_decimals())) {
continue;
// Format the finalgrade from the DB so that it matches the grade from the client
if ($postedvalue === format_float($oldvalue->finalgrade, $oldvalue->grade_item->get_decimals())) {
continue;
}
}
}

Expand Down Expand Up @@ -328,6 +331,13 @@ public function process_data($data) {

$gradeitem->update_final_grade($userid, $finalgrade, 'gradebook', false,
FORMAT_MOODLE, null, null, true);

// Apply penalty.
if (isset($data->deduction[$userid][$itemid])) {
$deductedmark = $data->deduction[$userid][$itemid];
$gradeitem->update_final_grade($userid, $finalgrade - $deductedmark, 'gradepenalty', false,
FORMAT_MOODLE, null, null, true);
}
}
}
}
Expand Down Expand Up @@ -1146,6 +1156,14 @@ public function get_right_rows(bool $displayaverages): array {
if ($context->statusicons) {
$context->extraclasses .= ' statusicons';
}

// If option to reapply deduction is enabled, add the option to the context.
if (get_config('core', 'gradepenalty_overriddengrade')) {
$context->deductedmark = format_float($grade->deductedmark, $decimalpoints);
$context->reapplydeduction = $grade->deductedmark > 0;
$context->deductionid = 'deduction_' . $userid . '_' . $item->id;
$context->deductionname = 'deduction[' . $userid . '][' . $item->id .']';
}
} else {
$context->extraclasses = 'gradevalue' . $hidden . $gradepass;
$context->text = format_float($gradeval, $decimalpoints);
Expand Down
14 changes: 13 additions & 1 deletion grade/report/grader/templates/cell.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@
{{>core_grades/grades/grader/scale}}
{{/scale}}
{{^scale}}
{{>core_grades/grades/grader/input}}
<div class="container">
<div class="row">
{{>core_grades/grades/grader/input}}
</div>
{{#reapplydeduction}}
<div class="row deductedmark">
<input type="checkbox" name="{{deductionname}}" value="{{deductedmark}}" id="{{deductionid}}" class="ms-0">
<label for="{{deductionname}}" class="form-check-label ml-1" title="{{#str}}applypenaltytooltip, gradereport_grader{{/str}}">
{{#str}}applypenaltytext, gradereport_grader, {{deductedmark}}{{/str}}
</label>
</div>
{{/reapplydeduction}}
</div>
{{/scale}}
{{/iseditable}}
{{^iseditable}}
Expand Down
2 changes: 2 additions & 0 deletions lang/en/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@
$string['gradepenalty'] = 'Grade penalties';
$string['gradepenalty_enabled'] = 'Grade penalty';
$string['gradepenalty_enabled_help'] = 'If enabled, the penalty will be applied to the grades of supported modules.';
$string['gradepenalty_overriddengrade'] = 'Apply penalty to overridden grades';
$string['gradepenalty_overriddengrade_help'] = 'If enabled, the penalty will be applied to overridden grades.';
$string['gradepenalty_supportedplugins'] = 'Supported modules';
$string['gradepenalty_supportedplugins_help'] = 'Enable the grade penalty for the selected modules.';
$string['gradepointdefault'] = 'Grade point default';
Expand Down
5 changes: 4 additions & 1 deletion lib/grade/grade_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class grade_grade extends grade_object {
public $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin',
'rawscaleid', 'usermodified', 'finalgrade', 'hidden', 'locked',
'locktime', 'exported', 'overridden', 'excluded', 'timecreated',
'timemodified', 'aggregationstatus', 'aggregationweight');
'timemodified', 'aggregationstatus', 'aggregationweight', 'deductedmark');

/**
* Array of optional fields with default values (these should match db defaults)
Expand Down Expand Up @@ -218,6 +218,9 @@ class grade_grade extends grade_object {
*/
public $label;

/** @var float $deductedmark mark deducted from final grade */
public $deductedmark = 0;

/**
* Returns array of grades for given grade_item+users
*
Expand Down

0 comments on commit c3b04b5

Please sign in to comment.