MDL-40697 core_grades: trigger the user_graded event
[moodle.git] / lib / grade / grade_item.php
index 6fc05cf..1e94ea8 100644 (file)
@@ -719,7 +719,13 @@ class grade_item extends grade_object {
                 $grade->finalgrade = $this->adjust_raw_grade($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
 
                 if (grade_floats_different($grade_record->finalgrade, $grade->finalgrade)) {
-                    if (!$grade->update('system')) {
+                    $success = $grade->update('system');
+
+                    // If successful trigger a user_graded event.
+                    if ($success) {
+                        $grade->load_grade_item();
+                        \core\event\user_graded::create_from_grade($grade)->trigger();
+                    } else {
                         $result = "Internal error updating final grade";
                     }
                 }
@@ -1606,12 +1612,21 @@ class grade_item extends grade_object {
             $grade->timemodified = time(); // hack alert - date graded
             $result = (bool)$grade->insert($source);
 
+            // If the grade insert was successful and the final grade was not null then trigger a user_graded event.
+            if ($result && !is_null($grade->finalgrade)) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         } else if (grade_floats_different($grade->finalgrade, $oldgrade->finalgrade)
                 or $grade->feedback       !== $oldgrade->feedback
                 or $grade->feedbackformat != $oldgrade->feedbackformat
                 or ($oldgrade->overridden == 0 and $grade->overridden > 0)) {
             $grade->timemodified = time(); // hack alert - date graded
             $result = $grade->update($source);
+
+            // If the grade update was successful and the actual grade has changed then trigger a user_graded event.
+            if ($result && grade_floats_different($grade->finalgrade, $oldgrade->finalgrade)) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         } else {
             // no grade change
             return $result;
@@ -1757,6 +1772,10 @@ class grade_item extends grade_object {
         if (empty($grade->id)) {
             $result = (bool)$grade->insert($source);
 
+            // If the grade insert was successful and the final grade was not null then trigger a user_graded event.
+            if ($result && !is_null($grade->finalgrade)) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         } else if (grade_floats_different($grade->finalgrade,  $oldgrade->finalgrade)
                 or grade_floats_different($grade->rawgrade,    $oldgrade->rawgrade)
                 or grade_floats_different($grade->rawgrademin, $oldgrade->rawgrademin)
@@ -1768,6 +1787,11 @@ class grade_item extends grade_object {
                 or $grade->timemodified   != $oldgrade->timemodified // part of hack above
                 ) {
             $result = $grade->update($source);
+
+            // If the grade update was successful and the actual grade has changed then trigger a user_graded event.
+            if ($result && grade_floats_different($grade->finalgrade, $oldgrade->finalgrade)) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         } else {
             return $result;
         }
@@ -1971,7 +1995,12 @@ class grade_item extends grade_object {
         // update in db if changed
         if (grade_floats_different($grade->finalgrade, $oldfinalgrade)) {
             $grade->timemodified = time();
-            $grade->update('compute');
+            $success = $grade->update('compute');
+
+            // If successful trigger a user_graded event.
+            if ($success) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         }
 
         if ($result !== false) {