MDL-40697 core_grades: trigger the user_graded event
authorMark Nelson <markn@moodle.com>
Mon, 15 Sep 2014 03:46:06 +0000 (20:46 -0700)
committerMark Nelson <markn@moodle.com>
Wed, 8 Oct 2014 02:08:35 +0000 (19:08 -0700)
lib/grade/grade_category.php
lib/grade/grade_item.php

index f26f821..01015ce 100644 (file)
@@ -628,7 +628,12 @@ class grade_category extends grade_object {
             $grade->finalgrade = null;
 
             if (!is_null($oldfinalgrade)) {
-                $grade->update('aggregation');
+                $success = $grade->update('aggregation');
+
+                // If successful trigger a user_graded event.
+                if ($success) {
+                    \core\event\user_graded::create_from_grade($grade)->trigger();
+                }
             }
             $dropped = $grade_values;
             $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
@@ -707,7 +712,12 @@ class grade_category extends grade_object {
             $grade->finalgrade = null;
 
             if (!is_null($oldfinalgrade)) {
-                $grade->update('aggregation');
+                $success = $grade->update('aggregation');
+
+                // If successful trigger a user_graded event.
+                if ($success) {
+                    \core\event\user_graded::create_from_grade($grade)->trigger();
+                }
             }
             $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
             return;
@@ -744,7 +754,12 @@ class grade_category extends grade_object {
         if (grade_floats_different($grade->finalgrade, $oldfinalgrade) ||
                 grade_floats_different($grade->rawgrademax, $oldrawgrademax) ||
                 grade_floats_different($grade->rawgrademin, $oldrawgrademin)) {
-            $grade->update('aggregation');
+            $success = $grade->update('aggregation');
+
+            // If successful trigger a user_graded event.
+            if ($success) {
+                \core\event\user_graded::create_from_grade($grade)->trigger();
+            }
         }
 
         $this->set_usedinaggregation($userid, $usedweights, $novalue, $dropped, $extracredit);
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) {