Skip to content

Commit

Permalink
MDL-66733 grade: Add helper to get correct user date for grade
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 20, 2019
1 parent 1c3efe4 commit 4bb39ea
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 57 deletions.
19 changes: 19 additions & 0 deletions lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1619,3 +1619,22 @@ function grade_floats_different($f1, $f2) {
function grade_floats_equal($f1, $f2) {
return (grade_floatval($f1) === grade_floatval($f2));
}

/**
* Get the most appropriate grade date for a grade item given the user that the grade relates to.
*
* @param \stdClass $grade
* @param \stdClass $user
* @return int
*/
function grade_get_date_for_user_grade(\stdClass $grade, \stdClass $user): int {
// The `datesubmitted` is the time that the grade was created.
// The `dategraded` is the time that it was modified or overwritten.
// If the grade was last modified by the user themselves use the date graded.
// Otherwise use date submitted.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
return $grade->dategraded;
} else {
return $grade->datesubmitted;
}
}
66 changes: 66 additions & 0 deletions lib/tests/gradelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,70 @@ public function test_grade_regrade_final_grades() {
// Now because of the failure, two things need updating.
$this->assertEquals(2, $DB->count_records('grade_items', ['courseid' => $course->id, 'needsupdate' => 1]));
}

/**
* Tests for the grade_get_date_for_user_grade function.
*
* @dataProvider grade_get_date_for_user_grade_provider
* @param stdClass $grade
* @param stdClass $user
* @param int $expected
*/
public function test_grade_get_date_for_user_grade(stdClass $grade, stdClass $user, int $expected): void {
$this->assertEquals($expected, grade_get_date_for_user_grade($grade, $user));
}

/**
* Data provider for tests of the grade_get_date_for_user_grade function.
*
* @return array
*/
public function grade_get_date_for_user_grade_provider(): array {
$u1 = (object) [
'id' => 42,
];
$u2 = (object) [
'id' => 930,
];

$d1 = 1234567890;
$d2 = 9876543210;

$g1 = (object) [
'usermodified' => $u1->id,
'dategraded' => $d1,
'datesubmitted' => $d2,
];
$g2 = (object) [
'usermodified' => $u1->id,
'dategraded' => $d1,
'datesubmitted' => 0,
];

return [
'If the user is the last person to have modified the grade_item then show the date that it was graded' => [
$g1,
$u1,
$d1,
],
'If the user is not the last person to have modified the grade_item, ' .
'and there is no submission date, then show the date that it was submitted' => [
$g1,
$u2,
$d2,
],
'If the user is not the last person to have modified the grade_item, ' .
'but there is no submission date, then show the date that it was graded' => [
$g2,
$u2,
$d1,
],
'If the user is the last person to have modified the grade_item, ' .
'and there is no submission date, then still show the date that it was graded' => [
$g2,
$u1,
$d1,
],
];
}
}
13 changes: 3 additions & 10 deletions mod/data/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1201,22 +1201,15 @@ function data_user_outline($course, $user, $mod, $data) {
}
return $result;
} else if ($grade) {
$result = new stdClass();
$result = (object) [
'time' => grade_get_date_for_user_grade($grade, $user),
];
if (!$grade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
} else {
$result->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

//datesubmitted == time created. dategraded == time modified or time overridden
//if grade was last modified by the user themselves use date graded. Otherwise use date submitted
//TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}

return $result;
}
return NULL;
Expand Down
13 changes: 3 additions & 10 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,22 +463,15 @@ function forum_user_outline($course, $user, $mod, $forum) {
}
return $result;
} else if ($grade) {
$result = new stdClass();
$result = (object) [
'time' => grade_get_date_for_user_grade($grade, $user),
];
if (!$grade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
} else {
$result->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

//datesubmitted == time created. dategraded == time modified or time overridden
//if grade was last modified by the user themselves use date graded. Otherwise use date submitted
//TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}

return $result;
}
return NULL;
Expand Down
13 changes: 3 additions & 10 deletions mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,15 @@ function glossary_user_outline($course, $user, $mod, $glossary) {
}
return $result;
} else if ($grade) {
$result = new stdClass();
$result = (object) [
'time' => grade_get_date_for_user_grade($grade, $user),
];
if (!$grade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
} else {
$result->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

//datesubmitted == time created. dategraded == time modified or time overridden
//if grade was last modified by the user themselves use date graded. Otherwise use date submitted
//TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}

return $result;
}
return NULL;
Expand Down
9 changes: 1 addition & 8 deletions mod/lesson/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,7 @@ function lesson_user_outline($course, $user, $mod, $lesson) {
$return->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

// Datesubmitted == time created. dategraded == time modified or time overridden.
// If grade was last modified by the user themselves use date graded. Otherwise use date submitted.
// TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$return->time = $grade->dategraded;
} else {
$return->time = $grade->datesubmitted;
}
$return->time = grade_get_date_for_user_grade($grade, $user);
}
}
return $return;
Expand Down
10 changes: 1 addition & 9 deletions mod/quiz/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,7 @@ function quiz_user_outline($course, $user, $mod, $quiz) {
$result->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

// Datesubmitted == time created. dategraded == time modified or time overridden
// if grade was last modified by the user themselves use date graded. Otherwise use
// date submitted.
// TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}
$result->time = grade_get_date_for_user_grade($grade, $user);

return $result;
}
Expand Down
13 changes: 3 additions & 10 deletions mod/scorm/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,22 +354,15 @@ function scorm_user_outline($course, $user, $mod, $scorm) {
$grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
$result = new stdClass();
$result = (object) [
'time' => grade_get_date_for_user_grade($grade, $user),
];
if (!$grade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
$result->info = get_string('grade') . ': '. $grade->str_long_grade;
} else {
$result->info = get_string('grade') . ': ' . get_string('hidden', 'grades');
}

// Datesubmitted == time created. dategraded == time modified or time overridden
// if grade was last modified by the user themselves use date graded. Otherwise use date submitted.
// TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}

return $result;
}
return null;
Expand Down

0 comments on commit 4bb39ea

Please sign in to comment.