MDL-66958 core_grade: Add grade status abstract
authorMathew May <mathewm@hotmail.co.nz>
Wed, 6 Nov 2019 01:13:37 +0000 (09:13 +0800)
committerMathew May <mathewm@hotmail.co.nz>
Mon, 11 Nov 2019 03:25:17 +0000 (11:25 +0800)
Also implement the new abstract in Forum

grade/classes/component_gradeitem.php
mod/forum/classes/grades/forum_gradeitem.php
mod/forum/tests/grades_forum_gradeitem_test.php

index a8f6b34..9e7ba92 100644 (file)
@@ -391,6 +391,15 @@ abstract class component_gradeitem {
      */
     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.
      *
index 7d2b50c..0918615 100644 (file)
@@ -184,6 +184,32 @@ class forum_gradeitem extends component_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.
      *
index 68c5c82..cffa7c4 100644 (file)
@@ -62,6 +62,33 @@ class forum_gradeitem_test extends \advanced_testcase {
         $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.
      */