MDL-66958 core_grade: Add grade status abstract

Also implement the new abstract in Forum
This commit is contained in:
Mathew May 2019-11-06 09:13:37 +08:00
parent 8b04be120b
commit 63cd8d45db
3 changed files with 62 additions and 0 deletions

View File

@ -391,6 +391,15 @@ abstract class component_gradeitem {
*/ */
abstract public function get_grade_for_user(stdClass $gradeduser, stdClass $grader): ?stdClass; abstract public function get_grade_for_user(stdClass $gradeduser, stdClass $grader): ?stdClass;
/**
* Get the grade status for the specified user.
* If the user has a grade as defined by the implementor return true else return false.
*
* @param stdClass $gradeduser The user being graded
* @return bool The grade status
*/
abstract public function user_has_grade(stdClass $gradeduser): bool;
/** /**
* Get grades for all users for the specified gradeitem. * Get grades for all users for the specified gradeitem.
* *

View File

@ -184,6 +184,32 @@ class forum_gradeitem extends component_gradeitem {
return $grade ?: null; return $grade ?: null;
} }
/**
* Get the grade status for the specified user.
* Check if a grade obj exists & $grade->grade !== null.
* If the user has a grade return true.
*
* @param stdClass $gradeduser The user being graded
* @return bool The grade exists
* @throws \dml_exception
*/
public function user_has_grade(stdClass $gradeduser): bool {
global $DB;
$params = [
'forum' => $this->forum->get_id(),
'itemnumber' => $this->itemnumber,
'userid' => $gradeduser->id,
];
$grade = $DB->get_record($this->get_table_name(), $params);
if (empty($grade) || $grade->grade === null) {
return false;
}
return true;
}
/** /**
* Get grades for all users for the specified gradeitem. * Get grades for all users for the specified gradeitem.
* *

View File

@ -62,6 +62,33 @@ class forum_gradeitem_test extends \advanced_testcase {
$this->assertEquals($student->id, $grade->userid); $this->assertEquals($student->id, $grade->userid);
} }
/**
* Test fetching of a grade for a user when the grade has been created.
*/
public function test_user_has_grade(): void {
$forum = $this->get_forum_instance([
'grade_forum' => 100,
]);
$course = $forum->get_course_record();
[$student] = $this->helper_create_users($course, 1);
[$grader] = $this->helper_create_users($course, 1, 'editingteacher');
$gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
$hasgrade = $gradeitem->user_has_grade($student);
$this->assertEquals(false, $hasgrade);
// Create the grade record.
$gradeitem->create_empty_grade($student, $grader);
$hasgrade = $gradeitem->user_has_grade($student);
$this->assertEquals(false, $hasgrade);
// Store a new value.
$gradeitem->store_grade_from_formdata($student, $grader, (object) ['grade' => 97]);
$hasgrade = $gradeitem->user_has_grade($student);
$this->assertEquals(true, $hasgrade);
}
/** /**
* Ensure that it is possible to get, and update, a grade for a user when simple direct grading is in use. * Ensure that it is possible to get, and update, a grade for a user when simple direct grading is in use.
*/ */