MDL-27520 core_grades: add methods to grade_object for feedback files
authorMark Nelson <markn@moodle.com>
Tue, 16 Oct 2018 06:25:59 +0000 (14:25 +0800)
committerMark Nelson <markn@moodle.com>
Wed, 17 Oct 2018 07:57:27 +0000 (15:57 +0800)
lib/grade/grade_grade.php
lib/grade/grade_object.php

index 9413c57..aef9f51 100644 (file)
@@ -1020,42 +1020,41 @@ class grade_grade extends grade_object {
      * @return int The new grade_grade ID if successful, false otherwise
      */
     public function insert($source=null) {
-        global $USER, $CFG, $DB;
         // TODO: dategraded hack - do not update times, they are used for submission and grading (MDL-31379)
         //$this->timecreated = $this->timemodified = time();
+        return parent::insert($source);
+    }
 
-        if (!empty($this->id)) {
-            debugging("Grade object already exists!");
-            return false;
-        }
-
-        $data = $this->get_record_data();
-
-        $this->id = $DB->insert_record($this->table, $data);
-
-        // Set all object properties from real db data.
-        $this->update_from_db();
-
-        $data = $this->get_record_data();
+    /**
+     * In addition to update() as defined in grade_object rounds the float numbers using php function,
+     * the reason is we need to compare the db value with computed number to skip updates if possible.
+     *
+     * @param string $source from where was the object inserted (mod/forum, manual, etc.)
+     * @return bool success
+     */
+    public function update($source=null) {
+        $this->rawgrade = grade_floatval($this->rawgrade);
+        $this->finalgrade = grade_floatval($this->finalgrade);
+        $this->rawgrademin = grade_floatval($this->rawgrademin);
+        $this->rawgrademax = grade_floatval($this->rawgrademax);
+        return parent::update($source);
+    }
 
-        if (empty($CFG->disablegradehistory)) {
-            unset($data->timecreated);
-            $data->action       = GRADE_HISTORY_INSERT;
-            $data->oldid        = $this->id;
-            $data->source       = $source;
-            $data->timemodified = time();
-            $data->loggeduser   = $USER->id;
-            $historyid = $DB->insert_record($this->table.'_history', $data);
-        }
 
-        $this->notify_changed(false);
+    /**
+     * Handles adding feedback files in the gradebook.
+     *
+     * @param int|null $historyid
+     */
+    protected function add_feedback_files(int $historyid = null) {
+        global $CFG;
 
         // We only support feedback files for modules atm.
         if ($this->grade_item && $this->grade_item->is_external_item()) {
             $context = $this->get_context();
             $this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
 
-            if (empty($CFG->disablegradehistory)) {
+            if (empty($CFG->disablegradehistory) && $historyid) {
                 $this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
             }
         }
@@ -1064,40 +1063,12 @@ class grade_grade extends grade_object {
     }
 
     /**
-     * In addition to update() as defined in grade_object rounds the float numbers using php function,
-     * the reason is we need to compare the db value with computed number to skip updates if possible.
+     * Handles updating feedback files in the gradebook.
      *
-     * @param string $source from where was the object inserted (mod/forum, manual, etc.)
-     * @return bool success
+     * @param int|null $historyid
      */
-    public function update($source=null) {
-        global $USER, $CFG, $DB;
-
-        $this->rawgrade    = grade_floatval($this->rawgrade);
-        $this->finalgrade  = grade_floatval($this->finalgrade);
-        $this->rawgrademin = grade_floatval($this->rawgrademin);
-        $this->rawgrademax = grade_floatval($this->rawgrademax);
-
-        if (empty($this->id)) {
-            debugging('Can not update grade object, no id!');
-            return false;
-        }
-
-        $data = $this->get_record_data();
-
-        $DB->update_record($this->table, $data);
-
-        if (empty($CFG->disablegradehistory)) {
-            unset($data->timecreated);
-            $data->action       = GRADE_HISTORY_UPDATE;
-            $data->oldid        = $this->id;
-            $data->source       = $source;
-            $data->timemodified = time();
-            $data->loggeduser   = $USER->id;
-            $historyid = $DB->insert_record($this->table.'_history', $data);
-        }
-
-        $this->notify_changed(false);
+    protected function update_feedback_files(int $historyid = null){
+        global $CFG;
 
         // We only support feedback files for modules atm.
         if ($this->grade_item && $this->grade_item->is_external_item()) {
@@ -1108,7 +1079,7 @@ class grade_grade extends grade_object {
 
             $this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
 
-            if (empty($CFG->disablegradehistory)) {
+            if (empty($CFG->disablegradehistory) && $historyid) {
                 $this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
             }
         }
index 654228e..33e907e 100644 (file)
@@ -252,6 +252,7 @@ abstract class grade_object {
 
         $DB->update_record($this->table, $data);
 
+        $historyid = null;
         if (empty($CFG->disablegradehistory)) {
             unset($data->timecreated);
             $data->action       = GRADE_HISTORY_UPDATE;
@@ -259,10 +260,13 @@ abstract class grade_object {
             $data->source       = $source;
             $data->timemodified = time();
             $data->loggeduser   = $USER->id;
-            $DB->insert_record($this->table.'_history', $data);
+            $historyid = $DB->insert_record($this->table.'_history', $data);
         }
 
         $this->notify_changed(false);
+
+        $this->update_feedback_files($historyid);
+
         return true;
     }
 
@@ -346,6 +350,7 @@ abstract class grade_object {
 
         $data = $this->get_record_data();
 
+        $historyid = null;
         if (empty($CFG->disablegradehistory)) {
             unset($data->timecreated);
             $data->action       = GRADE_HISTORY_INSERT;
@@ -353,10 +358,13 @@ abstract class grade_object {
             $data->source       = $source;
             $data->timemodified = time();
             $data->loggeduser   = $USER->id;
-            $DB->insert_record($this->table.'_history', $data);
+            $historyid = $DB->insert_record($this->table.'_history', $data);
         }
 
         $this->notify_changed(false);
+
+        $this->add_feedback_files($historyid);
+
         return $this->id;
     }
 
@@ -411,6 +419,22 @@ abstract class grade_object {
     protected function notify_changed($deleted) {
     }
 
+    /**
+     * Handles adding feedback files in the gradebook.
+     *
+     * @param int|null $historyid
+     */
+    protected function add_feedback_files(int $historyid = null) {
+    }
+
+    /**
+     * Handles updating feedback files in the gradebook.
+     *
+     * @param int|null $historyid
+     */
+    protected function update_feedback_files(int $historyid = null) {
+    }
+
     /**
      * Returns the current hidden state of this grade_item
      *