]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9137 Applied parts of Petr's patch on the gradebook objects. Unit tests unaffected.
authornicolasconnault <nicolasconnault>
Mon, 28 May 2007 02:05:21 +0000 (02:05 +0000)
committernicolasconnault <nicolasconnault>
Mon, 28 May 2007 02:05:21 +0000 (02:05 +0000)
lib/grade/grade_grades_raw.php
lib/grade/grade_object.php
lib/gradelib.php
lib/simpletest/grade/simpletest/testgraderaw.php

index ecdb038ec35fbaf1e9990f25d5733eea9d229f1c..41ff73eb2570bdbc866203fd1f7fe1df64c0501d 100644 (file)
@@ -37,7 +37,7 @@ class grade_grades_raw extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'scale');
+    var $nonfields = array('table', 'nonfields', 'scale', 'grade_grades_text', 'grade_item', 'feedback', 'feedbackformat');
     
     /**
      * The id of the grade_item this raw grade belongs to.
@@ -168,8 +168,6 @@ class grade_grades_raw extends grade_object {
                 return $this;
             } else {
                 $object = new grade_grades_raw($object);
-
-                $object->load_text();
                 return $object;
             }
         } else {
@@ -180,32 +178,42 @@ class grade_grades_raw extends grade_object {
     /**
      * Updates this grade with the given textual information. This will create a new grade_grades_text entry
      * if none was previously in DB for this raw grade, or will update the existing one.
-     * @param string $information Further info like forum rating distribution 4/5/7/0/1
+     * @param string $information Further info like forum rating distribution 4/5/7/0/1; null means keep as is
      * @param int $informationformat Text format for information
-     * @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'.
+     * @param string $feedback Manual feedback from the teacher. Could be a code like 'mi'; null means keep as is
      * @param int $feedbackformat Text format for the feedback
      * @return boolean Success or Failure
      */
-    function annotate($information, $informationformat=FORMAT_PLAIN, $feedback=NULL, $feedbackformat=FORMAT_PLAIN) {
-        $grade_text = new grade_grades_text();
+    function annotate($information=NULL, $informationformat=FORMAT_PLAIN, $feedback=NULL, $feedbackformat=FORMAT_PLAIN) {
+        if (is_null($information) && is_null($feedback)) {
+            // Nothing to do
+            return true;
+        }
 
-        $grade_text->itemid            = $this->itemid;
-        $grade_text->userid            = $this->userid;
-        $grade_text->information       = $information;
-        $grade_text->informationformat = $informationformat;
-        $grade_text->feedback          = $feedback;
-        $grade_text->feedbackformat    = $feedbackformat;
+        if (!$grade_text = $this->load_text()) {
+            $grade_text = new grade_grades_text();
 
-        $result = true;
+            $grade_text->itemid            = $this->itemid;
+            $grade_text->userid            = $this->userid;
+            $grade_text->information       = $information;
+            $grade_text->informationformat = $informationformat;
+            $grade_text->feedback          = $feedback;
+            $grade_text->feedbackformat    = $feedbackformat;
 
-        if (empty($this->text)) {
             $result = $grade_text->insert();
+
         } else {
+            if (!is_null($informationformat)) {
+                $grade_text->information       = $information;
+                $grade_text->informationformat = $feedbackformat;
+            }
+            if (!is_null($feedback)) {
+                $grade_text->feedback       = $feedback;
+                $grade_text->feedbackformat = $feedbackformat;
+            }
             $result = $grade_text->update();
         }
 
-        $this->text = $grade_text;
-
         return $result;
     }
 
@@ -214,15 +222,13 @@ class grade_grades_raw extends grade_object {
      * its pre-update value and its new value in the grade_history table. The grade_item
      * object is also flagged for update.
      *
-     * @param float $newgrade The new gradevalue of this object
+     * @param float $newgrade The new gradevalue of this object, false if no change
      * @param string $howmodified What caused the modification? manual/module/import/cron...
      * @param string $note A note attached to this modification.
      * @return boolean Success or Failure.
      */
     function update($newgrade, $howmodified='manual', $note=NULL) {
         global $USER;
-        $oldgrade = $this->gradevalue;
-        $this->gradevalue = $newgrade;
         
         if (!empty($this->scale->id)) {
             $this->scaleid = $this->scale->id;
@@ -230,26 +236,33 @@ class grade_grades_raw extends grade_object {
             $this->scale->load_items();
             $this->grademax = count($this->scale->scale_items);
         }
+        
+        $trackhistory = false;
+
+        if ($newgrade != $this->gradevalue) {
+            $trackhistory = true;
+            $oldgrade = $this->gradevalue;
+            $this->gradevalue = $newgrade;
+        }
 
         $result = parent::update();
        
         // Update grade_grades_text if changed
-        if (!empty($this->text)) {
-            $grade_text = grade_grades_text::fetch('userid', $this->userid, 'itemid', $this->itemid);
-            if ($this->text != $grade_text && $this->text->id == $grade_text->id) {
-                $result = $result & $this->text->update();
-            }
+        if ($result && !empty($this->feedback)) {
+            $result = $this->annotate(NULL, NULL, $this->feedback, $this->feedbackformat);
         }
 
-        if ($result) {
+        if ($result && $trackhistory) {
             // TODO Handle history recording error, such as displaying a notice, but still return true
             grade_history::insert_change($this, $oldgrade, $howmodified, $note);
 
             // Notify parent grade_item of need to update
             $this->load_grade_item();
-            $result = $result && $this->grade_item->flag_for_update();
-
-            return $result;
+            $result = $this->grade_item->flag_for_update();
+        }
+        
+        if ($result) {
+            return true;
         } else {
             debugging("Could not update a raw grade in the database.");
             return false;
@@ -275,8 +288,13 @@ class grade_grades_raw extends grade_object {
         // Notify parent grade_item of need to update
         $this->load_grade_item();
         $result = $result && $this->grade_item->flag_for_update();
+        
+        // Update grade_grades_text if specified
+        if ($result && !empty($this->feedback)) {
+            $result = $this->annotate(NULL, NULL, $this->feedback, $this->feedbackformat);
+        }
 
-        return $result;
+        return $result && $this->grade_item->flag_for_update();
     }
 
     /**
index aa8aea5b86f9a5d08d2f4ef16ff97d4bccbf23b5..42a3cf99fc6d0d8fe6fa440265a920afcaa9bf13 100644 (file)
@@ -90,7 +90,7 @@ class grade_object {
             $this->usermodified = $USER->id;
         }
 
-        return update_record($this->table, $this);
+        return update_record($this->table, addslashes_recursive($this));
     }
 
     /**
@@ -108,7 +108,8 @@ class grade_object {
         global $USER;
 
         if (!empty($this->id)) {   // Object already exists, so let's do an update instead
-            $this->update();
+            debugging("Grade object already exists!");
+            return false;
         }
 
         $this->timecreated = $this->timemodified = time();
@@ -126,7 +127,7 @@ class grade_object {
             }
         }
         
-        return $this->id = insert_record($this->table, $clonethis, true);
+        return $this->id = insert_record($this->table, addslashes_recursive($clonethis), true);
     }
 
     /**
index 4814de935435528b561c957ee4138bdd77499abb..70e672829faf08fbf4217b77bf6db185e250b436 100644 (file)
@@ -92,7 +92,12 @@ function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=
 */
 function grade_create_item($params) {
     $grade_item = new grade_item($params);
-    return $grade_item->insert();
+    
+    if (empty($grade_item->id)) {
+        return $grade_item->insert();
+    } else {
+        return $grade_item->id;
+    }
 }
 
 /**
@@ -107,7 +112,12 @@ function grade_create_item($params) {
 */
 function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) {
     $grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation'));
-    return $grade_category->insert();
+    
+    if (empty($grade_category->id)) {
+        return $grade_category->insert();
+    } else {
+        return $grade_category->id;
+    }
 }
 
 
index 0b40bc9da3745e26f6cc78098888765b25d1eb19..09c2d624e4067b41650390448192c4eebd9c269a 100755 (executable)
@@ -96,7 +96,6 @@ class grade_raw_test extends gradelib_test {
         // Check the grade_item's needsupdate variable first
         $grade_grades_raw->load_grade_item();
         $this->assertFalse($grade_grades_raw->grade_item->needsupdate);
-
         $this->assertTrue($grade_grades_raw->update(89));
         $gradevalue = get_field('grade_grades_raw', 'gradevalue', 'id', $this->grade_grades_raw[0]->id);
         $this->assertEqual($grade_grades_raw->gradevalue, $gradevalue);