Skip to content

Commit

Permalink
MDL-82125 core_grades: penalty indicator on grade reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Dec 2, 2024
1 parent ab83c9a commit b62618f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 17 deletions.
3 changes: 2 additions & 1 deletion grade/classes/local/penalty/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static function apply_penalty(int $userid, grade_item $gradeitem,
// Apply the penalty to the grade.
if (!$previewonly) {
// Update the final grade after the penalty is applied.
$gradeitem->update_raw_grade($userid, $beforepenaltyhook->get_grade_after_penalty());
$gradeitem->update_raw_grade($userid, $beforepenaltyhook->get_grade_after_penalty(), 'gradepenalty');
$gradeitem->update_deducted_mark($userid, $beforepenaltyhook->get_deducted_grade());

// Hook for plugins to process further after the penalty is applied to the grade.
$afterpenaltyhook = new after_penalty_applied($userid, $gradeitem, $submissiondate, $duedate,
Expand Down
2 changes: 1 addition & 1 deletion grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ public function get_right_rows(bool $displayaverages): array {

$context->extraclasses = 'gradevalue ' . $hidden . $gradepass;
$context->text = grade_format_gradevalue($gradeval, $item, true,
$gradedisplaytype, null);
$gradedisplaytype, null, $grade->deductedmark);
}
}

Expand Down
7 changes: 4 additions & 3 deletions grade/report/user/classes/report/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ private function fill_table_recursive(array &$element) {
if ($this->canviewhidden) {
$gradeitemdata['graderaw'] = $gradeval;
$data['grade']['content'] = grade_format_gradevalue($gradeval,
$gradegrade->grade_item,
true) . $gradestatus;
$gradegrade->grade_item, true, null, null, $gradegrade->deductedmark
) . $gradestatus;
}
} else {
$gradestatusclass = '';
Expand All @@ -728,7 +728,8 @@ private function fill_table_recursive(array &$element) {

$data['grade']['class'] = "{$class} {$gradestatusclass}";
$data['grade']['content'] = $gradepassicon . grade_format_gradevalue($gradeval,
$gradegrade->grade_item, true) . $gradestatus;
$gradegrade->grade_item, true, null, null, $gradegrade->deductedmark
) . $gradestatus;
$gradeitemdata['graderaw'] = $gradeval;
}
$data['grade']['headers'] = "$headercat $headerrow grade$userid";
Expand Down
1 change: 1 addition & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,7 @@
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the time this grade was last modified"/>
<FIELD NAME="aggregationstatus" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="unknown" SEQUENCE="false" COMMENT="One of several values describing how this grade_grade was used when calculating the aggregation. Possible values are &quot;unknown&quot;, &quot;dropped&quot;, &quot;novalue&quot;, &quot;used&quot;"/>
<FIELD NAME="aggregationweight" TYPE="number" LENGTH="10" NOTNULL="false" SEQUENCE="false" DECIMALS="5" COMMENT="If the aggregationstatus == 'included', then this is the percent this item contributed to the aggregation."/>
<FIELD NAME="deductedmark" TYPE="number" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" DECIMALS="5" COMMENT="The mark deducted from final grade"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
16 changes: 16 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1236,5 +1236,21 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2024111500.04);
}

if ($oldversion < 2024111100.00) {

// Define field penalty to be added to grade_grades.
$table = new xmldb_table('grade_grades');
$field = new xmldb_field('deductedmark', XMLDB_TYPE_NUMBER, '10, 5', null,
XMLDB_NOTNULL, null, '0', 'aggregationweight');

// Conditionally launch add field penalty.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2024111100.00);
}

return true;
}
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
15 changes: 15 additions & 0 deletions lib/grade/grade_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,21 @@ public function update_raw_grade($userid, $rawgrade = false, $source = null, $fe
return $result;
}

/**
* Update penalty value for given user
*
* @param int $userid The graded user
* @param float $deductedmark The mark deducted from final grade
*/
public function update_deducted_mark(int $userid, float $deductedmark): void {
$grade = new grade_grade([
'itemid' => $this->id,
'userid' => $userid,
]);
$grade->deductedmark = $deductedmark;
$grade->update();
}

/**
* Calculates final grade values using the formula in the calculation property.
* The parameters are taken from final grades of grade items in current course only.
Expand Down
45 changes: 34 additions & 11 deletions lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $use
$grade->usermodified = $grade_grades[$userid]->usermodified;
$grade->datesubmitted = $grade_grades[$userid]->get_datesubmitted();
$grade->dategraded = $grade_grades[$userid]->get_dategraded();
$grade->deductedmark = $grade_grades[$userid]->deductedmark;

// create text representation of grade
if ($grade_item->gradetype == GRADE_TYPE_TEXT or $grade_item->gradetype == GRADE_TYPE_NONE) {
Expand Down Expand Up @@ -785,9 +786,12 @@ function grade_set_setting($courseid, $name, $value) {
* @param bool $localized use localised decimal separator
* @param int $displaytype type of display. For example GRADE_DISPLAY_TYPE_REAL, GRADE_DISPLAY_TYPE_PERCENTAGE, GRADE_DISPLAY_TYPE_LETTER
* @param int $decimals The number of decimal places when displaying float values
* @param float $penalty The penalty value
* @return string
*/
function grade_format_gradevalue(?float $value, &$grade_item, $localized=true, $displaytype=null, $decimals=null) {
function grade_format_gradevalue(?float $value, &$grade_item, $localized=true, $displaytype=null, $decimals=null, $penalty = 0.0) {
global $PAGE;

if ($grade_item->gradetype == GRADE_TYPE_NONE or $grade_item->gradetype == GRADE_TYPE_TEXT) {
return '';
}
Expand All @@ -812,40 +816,59 @@ function grade_format_gradevalue(?float $value, &$grade_item, $localized=true, $

switch ($displaytype) {
case GRADE_DISPLAY_TYPE_REAL:
return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized);
$gradetext = grade_format_gradevalue_real($value, $grade_item, $decimals, $localized);
break;

case GRADE_DISPLAY_TYPE_PERCENTAGE:
return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized);
$gradetext = grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized);
break;

case GRADE_DISPLAY_TYPE_LETTER:
return grade_format_gradevalue_letter($value, $grade_item);
$gradetext = grade_format_gradevalue_letter($value, $grade_item);
break;

case GRADE_DISPLAY_TYPE_REAL_PERCENTAGE:
return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
$gradetext = grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ')';
break;

case GRADE_DISPLAY_TYPE_REAL_LETTER:
return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
$gradetext = grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
grade_format_gradevalue_letter($value, $grade_item) . ')';
break;

case GRADE_DISPLAY_TYPE_PERCENTAGE_REAL:
return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
$gradetext = grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ')';
break;

case GRADE_DISPLAY_TYPE_LETTER_REAL:
return grade_format_gradevalue_letter($value, $grade_item) . ' (' .
$gradetext = grade_format_gradevalue_letter($value, $grade_item) . ' (' .
grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ')';
break;

case GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE:
return grade_format_gradevalue_letter($value, $grade_item) . ' (' .
$gradetext = grade_format_gradevalue_letter($value, $grade_item) . ' (' .
grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ')';
break;

case GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER:
return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
$gradetext = grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
grade_format_gradevalue_letter($value, $grade_item) . ')';
break;

default:
return '';
$gradetext = '';
}

// Show penalty indicator if penalty is greater than 0.
if ($penalty > 0.0) {
$indicator = new \core_grades\output\penalty_indicator(2, $penalty);
$renderer = $PAGE->get_renderer('core_grades');
$gradetext .= $renderer->render_penalty_indicator($indicator);
}

return $gradetext;
}

/**
Expand Down

0 comments on commit b62618f

Please sign in to comment.