*/
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.
*
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.
*
$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.
*/