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 23, 2024
1 parent 97b4313 commit d4400df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 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

0 comments on commit d4400df

Please sign in to comment.