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 Dec 2, 2024
1 parent eca0de4 commit 9938ee6
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 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="{{deductionid}}" 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
10 changes: 10 additions & 0 deletions grade/report/grader/tests/behat/behat_gradereport_grader.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ public static function get_partial_named_selectors(): array {
),
];
}

/**
* Enable penalty for overridden grade.
*
* @Given I enable penalty for overridden grade
*/
public function i_enable_penalty_for_overridden_grade(): void {
set_config('gradepenalty_enabled', 1);
set_config('gradepenalty_overriddengrade', 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@gradereport @gradereport_grader @gradereport_grader_deduction
Feature: As a teacher, I want to override a grade with a deduction and check the gradebook.

Background:
Given the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And I enable penalty for overridden grade
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "grade items" exist:
| itemname | grademin | grademax | course |
| Manual grade 01 | 0 | 100 | C1 |
| Manual grade 02 | 0 | 100 | C1 |
And the following "grade grades" exist:
| gradeitem | user | grade | deductedmark |
| Manual grade 01 | student1 | 60 | 10 |
| Manual grade 02 | student1 | 80 | 20 |
When I log in as "teacher1"

@javascript
Scenario: Override a grade with a deduction and check the gradebook
And I am on "Course 1" course homepage
And I navigate to "View > Grader report" in the course gradebook
And the following should exist in the "user-grades" table:
| -1- | -2- | -3- | -4- | -5- |
| Student 1 | student1@example.com | 60 | 80 | 140 |
And I turn editing mode on
And I set the field "Student 1 Manual grade 01 grade" to "80"
And I click on "Deduct 10.00" "checkbox"
And I click on "Save changes" "button"
And I turn editing mode off
And the following should exist in the "user-grades" table:
| -1- | -2- | -3- | -4- | -5- |
| Student 1 | student1@example.com | 70 | 80 | 150 |
And I turn editing mode on
And I set the field "Student 1 Manual grade 02 grade" to "100"
And I click on "Save changes" "button"
And I turn editing mode off
And the following should exist in the "user-grades" table:
| -1- | -2- | -3- | -4- | -5- |
| Student 1 | student1@example.com | 70 | 100 | 170 |
2 changes: 2 additions & 0 deletions lang/en/grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@
$string['gradepenalty_enabled'] = 'Grade penalty';
$string['gradepenalty_enabled_help'] = 'If enabled, the penalty will be applied to the grades of supported modules.';
$string['gradepenalty_indicator_info'] = 'Late penalty applied -{$a} marks';
$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

0 comments on commit 9938ee6

Please sign in to comment.