Skip to content

Commit

Permalink
MDL-82125 mod_assign: show penalty indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Aug 26, 2024
1 parent 2497f1e commit 7c439ba
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
8 changes: 5 additions & 3 deletions mod/assign/gradingtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,10 @@ public function col_scale($row) {
* @param boolean $editable
* @param int $userid The user id of the user this grade belongs to
* @param int $modified Timestamp showing when the grade was last modified
* @param float $deductedmark The deducted mark if penalty is applied
* @return string The formatted grade
*/
public function display_grade($grade, $editable, $userid, $modified) {
public function display_grade($grade, $editable, $userid, $modified, $deductedmark = 0) {
if ($this->is_downloading()) {
if ($this->assignment->get_instance()->grade >= 0) {
if ($grade == -1 || $grade === null) {
Expand All @@ -782,7 +783,7 @@ public function display_grade($grade, $editable, $userid, $modified) {
return $scale;
}
}
return $this->assignment->display_grade($grade, $editable, $userid, $modified);
return $this->assignment->display_grade($grade, $editable, $userid, $modified, $deductedmark);
}

/**
Expand Down Expand Up @@ -1043,11 +1044,12 @@ public function col_grade(stdClass $row): string {
* @return string
*/
public function col_finalgrade(stdClass $row) {
global $PAGE;
$o = '';

$grade = $this->get_gradebook_data_for_user($row->userid);
if ($grade) {
$o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked);
$o = $this->display_grade($grade->grade, false, $row->userid, $row->timemarked, $grade->deductedmark);
}

return $o;
Expand Down
63 changes: 49 additions & 14 deletions mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2007,22 +2007,17 @@ public function should_provide_intro_attachments(int $userid): bool {
* @param boolean $editing Are we allowing changes to this grade?
* @param int $userid The user id the grade belongs to
* @param int $modified Timestamp from when the grade was last modified
* @param float $penalty The penalty to apply to the grade
* @param float $deductedmark The deducted mark if penalty is applied
* @return string User-friendly representation of grade
*/
public function display_grade($grade, $editing, $userid=0, $modified=0, $penalty = 0) {
global $DB;
public function display_grade($grade, $editing, $userid=0, $modified=0, $deductedmark = 0) {
global $DB, $PAGE;

static $scalegrades = array();

$o = '';

if ($this->get_instance()->grade >= 0) {
// Apply penalty percentage to the grade.
if (!is_null($grade) && $grade >= 0) {
$grade *= 1 - $penalty / 100;
}

// Normal number.
if ($editing && $this->get_instance()->grade > 0) {
if ($grade < 0) {
Expand All @@ -2041,7 +2036,6 @@ public function display_grade($grade, $editing, $userid=0, $modified=0, $penalty
maxlength="10"
class="quickgrade"/>';
$o .= '&nbsp;/&nbsp;' . format_float($this->get_instance()->grade, $this->get_grade_item()->get_decimals());
return $o;
} else {
if ($grade == -1 || $grade === null) {
$o .= '-';
Expand All @@ -2053,9 +2047,17 @@ class="quickgrade"/>';
$o .= '&nbsp;/&nbsp;' . format_float($this->get_instance()->grade, $item->get_decimals());
}
}
return $o;
}

// Add penalty indicator.
$penaltyindicator = '';
if ($deductedmark > 0) {
$indicator = new \core_grades\output\penalty_indicator(2, $deductedmark);
$renderer = $PAGE->get_renderer('core_grades');
$penaltyindicator = $renderer->render_penalty_indicator($indicator);
}

return $penaltyindicator . $o;
} else {
// Scale.
if (empty($this->cache['scale'])) {
Expand Down Expand Up @@ -5495,7 +5497,7 @@ public function get_assign_feedback_status_renderable($user) {
);
$gradefordisplay = $gradebookgrade->str_long_grade;
} else {
$gradefordisplay = $this->display_grade($gradebookgrade->grade, false);
$gradefordisplay = $this->display_grade($gradebookgrade->grade, false, 0, 0, $gradebookgrade->deductedmark);
}
$gradeddate = $gradebookgrade->dategraded;

Expand Down Expand Up @@ -5702,23 +5704,47 @@ protected function get_all_grades($userid) {
}
}

[$penalisedgrade, $deductedmark] = $this->calculate_penalised_grade($grade);

// Now get the gradefordisplay.
if ($controller) {
$controller->set_grade_range(make_grades_menu($this->get_instance()->grade), $this->get_instance()->grade > 0);
$grade->gradefordisplay = $controller->render_grade($PAGE,
$grade->id,
$gradingitem,
$grade->grade,
$penalisedgrade,
$cangrade);
} else {
$grade->gradefordisplay = $this->display_grade($grade->grade, false, 0, 0, $grade->penalty);
$grade->gradefordisplay = $this->display_grade($penalisedgrade, false, 0, 0, $deductedmark);
}

}

return $grades;
}

/**
* Calculate penalised grade and deducted mark.
*
* @param stdClass $grade The grade object
* @return array [$penalisedgrade, $deductedmark] the penalised grade and the deducted mark
*/
public function calculate_penalised_grade(stdClass $grade): array {
$penalisedgrade = $grade->grade;
$deductedmark = 0;

// No calculation needed if the grade is null or negative.
if (is_null($penalisedgrade) || $penalisedgrade < 0) {
return [$penalisedgrade, $deductedmark];
}

if ($grade->penalty > 0) {
$deductedmark = $grade->grade * $grade->penalty / 100;
$penalisedgrade = $grade->grade - $deductedmark;
}
return [$penalisedgrade, $deductedmark];
}

/**
* Get the submissions for all previous attempts.
*
Expand Down Expand Up @@ -7855,7 +7881,7 @@ protected function get_grading_instance($userid, $grade, $gradingdisabled) {
* @return void
*/
public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data, $params) {
global $USER, $CFG, $SESSION;
global $USER, $CFG, $SESSION, $PAGE;
$settings = $this->get_instance();

$rownum = isset($params['rownum']) ? $params['rownum'] : 0;
Expand Down Expand Up @@ -7977,6 +8003,15 @@ public function add_grade_form_elements(MoodleQuickForm $mform, stdClass $data,
$gradestring = $usergrade;
}

// Penalty indicator.
$usergrade = $gradinginfo->items[0]->grades[$userid];
if (isset($usergrade->grade) && $usergrade->deductedmark > 0) {
$indicator = new \core_grades\output\penalty_indicator(2, $usergrade->deductedmark);
$renderer = $PAGE->get_renderer('core_grades');
$penaltyindicator = $renderer->render_penalty_indicator($indicator);
$gradestring = $penaltyindicator . $gradestring;
}

if ($this->get_instance()->markingworkflow) {
$states = $this->get_marking_workflow_states_for_current_user();
$options = array('' => get_string('markingworkflowstatenotmarked', 'assign')) + $states;
Expand Down
16 changes: 16 additions & 0 deletions mod/assign/tests/behat/behat_mod_assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ public function i_should_see_marking_guide_information(TableNode $table) {
$criteriacheck++;
}
}

/**
* Enable grade penalty.
*
* @Given I enable grade penalty for assignment
*/
public function i_enable_grade_penalty_for_assignment(): void {
global $DB;

set_config('gradepenalty_enabled', 1);
set_config('gradepenalty_supportedplugins', 'assign');
\core\plugininfo\gradepenalty::enable_plugin('duedate', true);

$rule = ['contextid' => 1, 'overdueby' => DAYSECS, 'penalty' => 10, 'sortorder' => 0];
$DB->insert_record('gradepenalty_duedate_rule', (object) $rule);
}
}
45 changes: 44 additions & 1 deletion mod/assign/tests/behat/display_grade.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@mod @mod_assign
@mod @mod_assign @assign_grade
Feature: Check that the assignment grade can be updated correctly
In order to ensure that the grade is shown correctly in the grading table
As a teacher
Expand Down Expand Up @@ -72,3 +72,46 @@ Feature: Check that the assignment grade can be updated correctly
And I press "Save changes"
And I follow "View all submissions"
Then "Student 1" row "Grade" column of "generaltable" table should contain "50.00"

@javascript
Scenario: Update the grade for an assignment with penalty
Given the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student10@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "groups" exist:
| name | course | idnumber |
| Group 1 | C1 | G1 |
And I enable grade penalty for assignment
And the following "activity" exists:
| activity | assign |
| course | C1 |
| name | Test assignment name |
| intro | Test assignment description |
| grade | 100 |
| duedate | ##yesterday## |
| gradepenalty | 1 |
| assignsubmission_onlinetext_enabled | 1 |
| submissiondrafts | 0 |
# Add a submission.
And the following "mod_assign > submissions" exist:
| assign | user | onlinetext |
| Test assignment name | student1 | I'm the student first submission |
And I am on the "Test assignment name" Activity page logged in as admin
Then I follow "View all submissions"
And I change window size to "large"
And I click on "Grade" "link" in the "Student 1" "table_row"
And I set the field "Grade out of 100" to "100"
And I set the field "Notify student" to "0"
And I press "Save changes"
And I follow "View all submissions"
And "Student 1" row "Grade" column of "generaltable" table should contain "100.00"
And "Student 1" row "Final grade" column of "generaltable" table should contain "90.00"
And the "title" attribute of ".penalty-indicator-icon" "css_element" should contain "Late penalty applied -10.00 marks"

0 comments on commit 7c439ba

Please sign in to comment.