MDL-67132 mod_lti: call grade api to trigger course total computation
authorClaude Vervoort <claude.vervoort@cengage.com>
Tue, 5 Nov 2019 16:09:51 +0000 (11:09 -0500)
committerClaude Vervoort <claude.vervoort@cengage.com>
Thu, 13 Feb 2020 20:20:04 +0000 (15:20 -0500)
lib/grade/grade_item.php
mod/lti/service/gradebookservices/classes/local/service/gradebookservices.php

index 9cb8385..3bb5812 100644 (file)
@@ -1734,7 +1734,10 @@ class grade_item extends grade_object {
      * @param int $usermodified The ID of the user making the modification
      * @return bool success
      */
-    public function update_final_grade($userid, $finalgrade=false, $source=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE, $usermodified=null) {
+    public function update_final_grade($userid, $finalgrade = false,
+                                       $source = null, $feedback = false,
+                                       $feedbackformat = FORMAT_MOODLE,
+                                       $usermodified = null, $timemodified = null) {
         global $USER, $CFG;
 
         $result = true;
@@ -1800,8 +1803,8 @@ class grade_item extends grade_object {
 
         $gradechanged = false;
         if (empty($grade->id)) {
-            $grade->timecreated  = null;   // hack alert - date submitted - no submission yet
-            $grade->timemodified = time(); // hack alert - date graded
+            $grade->timecreated = null;   // Hack alert - date submitted - no submission yet.
+            $grade->timemodified = $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.
@@ -1825,7 +1828,7 @@ class grade_item extends grade_object {
                 return $result;
             }
 
-            $grade->timemodified = time(); // hack alert - date graded
+            $grade->timemodified = $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.
index 2a7c001..c7e852b 100644 (file)
@@ -381,23 +381,18 @@ class gradebookservices extends service_base {
             $feedbackformat = FORMAT_PLAIN;
         }
 
-        if (!$grade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $userid))) {
-            $grade = new \grade_grade();
-            $grade->userid = $userid;
-            $grade->itemid = $gradeitem->id;
-        }
-        $grade->rawgrademax = $score->scoreMaximum;
-        $grade->timemodified = $timemodified;
-        $grade->feedbackformat = $feedbackformat;
-        $grade->feedback = $feedback;
         if ($gradeitem->is_manual_item()) {
-            $grade->finalgrade = $finalgrade;
-            if (empty($grade->id)) {
-                $result = (bool)$grade->insert($source);
-            } else {
-                $result = $grade->update($source);
-            }
+            $result = $gradeitem->update_final_grade($userid, $finalgrade, null, $feedback, FORMAT_PLAIN, null, $timemodified);
         } else {
+            if (!$grade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $userid))) {
+                $grade = new \grade_grade();
+                $grade->userid = $userid;
+                $grade->itemid = $gradeitem->id;
+            }
+            $grade->rawgrademax = $score->scoreMaximum;
+            $grade->timemodified = $timemodified;
+            $grade->feedbackformat = $feedbackformat;
+            $grade->feedback = $feedback;
             $grade->rawgrade = $finalgrade;
             $status = \grade_update($source, $gradeitem->courseid,
                          $gradeitem->itemtype, $gradeitem->itemmodule,