]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9506 Added grade_grades_raw as a proper object used by grade_item. Also added...
authornicolasconnault <nicolasconnault>
Mon, 30 Apr 2007 04:49:25 +0000 (04:49 +0000)
committernicolasconnault <nicolasconnault>
Mon, 30 Apr 2007 04:49:25 +0000 (04:49 +0000)
lib/grade/grade_grades_raw.php
lib/grade/grade_item.php
lib/gradelib.php
lib/simpletest/testgradelib.php

index dc054f02d133b5d7a94d78c780bfdb44b8079082..d98b096a27d93fb50792478918f081c7b24d404f 100644 (file)
@@ -124,6 +124,41 @@ class grade_grades_raw extends grade_object {
             return false;
         }
     } 
+
+    /**
+     * In addition to the normal updating set up in grade_object, this object also records
+     * its pre-update value and its new value in the grade_history table.
+     *
+     * @param float $newgrade The new gradevalue of this object
+     * @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;
+
+        $result = parent::update();
+        
+        if ($result) {
+            $logentry = new stdClass();
+            $logentry->itemid = $this->itemid;
+            $logentry->userid = $this->userid;
+            $logentry->oldgrade = $oldgrade;
+            $logentry->newgrade = $this->gradevalue;
+            $logentry->note = $note;
+            $logentry->howmodified = $howmodified;
+            $logentry->timemodified = mktime();
+            $logentry->usermodified = $USER->id;
+
+            insert_record('grade_history', $logentry);
+            return true;
+        } else {
+            return false;
+        }
+
+    }
 }
 
 ?>
index 947f648181c5d51c76ca514c5e1d9a5ce6319db7..029ca1d8f445de95fba51da4b044cb23332ec267 100644 (file)
@@ -40,7 +40,7 @@ class grade_item extends grade_object {
      * Array of class variables that are not part of the DB table fields
      * @var array $nonfields
      */
-    var $nonfields = array('table', 'nonfields', 'calculation');
+    var $nonfields = array('table', 'nonfields', 'calculation', 'grade_grades_raw');
   
     /**
      * The course this grade_item belongs to.
@@ -167,6 +167,12 @@ class grade_item extends grade_object {
      * @var string $calculation
      */
     var $calculation;
+    
+    /**
+     * Array of grade_grades_raw objects linked to this grade_item. They are indexed by userid.
+     * @var array $grade_grades_raw
+     */
+    var $grade_grades_raw = array();
 
     /**
      * Finds and returns a grade_item object based on 1-3 field values.
@@ -203,13 +209,30 @@ class grade_item extends grade_object {
      * @return mixed An array of all raw_grades (stdClass objects) for this grade_item, or a single raw_grade.
      */
     function get_raw($userid=NULL) {
-        $grade_raw = null;
+        if (empty($this->grade_grades_raw)) {
+            $this->load_raw();
+        }
+
+        $grade_raw_array = null;
         if (!empty($userid)) {
-            $grade_raw = get_record('grade_grades_raw', 'itemid', $this->id, 'userid', $userid);
+            $r = get_record('grade_grades_raw', 'itemid', $this->id, 'userid', $userid);
+            $grade_raw_array[$r->userid] = new grade_grades_raw($r);
         } else {
-            $grade_raw = get_records('grade_grades_raw', 'itemid', $this->id);
+            $grade_raw_array = $this->grade_grades_raw;
+        }
+        return $grade_raw_array;
+    }
+
+    /**
+     * Loads all the grade_grades_raw objects for this grade_item from the DB into grade_item::$grade_grades_raw array.
+     * @return array grade_grades_raw objects
+     */      
+    function load_raw() {
+        $grade_raw_array = get_records('grade_grades_raw', 'itemid', $this->id);
+        foreach ($grade_raw_array as $r) {
+            $this->grade_grades_raw[$r->userid] = new grade_grades_raw($r);
         }
-        return $grade_raw;
+        return $this->grade_grades_raw;
     }
 
     /**
@@ -295,8 +318,7 @@ class grade_item extends grade_object {
     */
     function get_category() {
         if (!empty($this->categoryid)) {
-            $grade_category = new grade_category($this->category_id);
-            return $grade_category;
+            return grade_category::fetch('id', $this->categoryid);
         } else {
             return null;
         }
@@ -318,5 +340,37 @@ class grade_item extends grade_object {
             return $final->locked;
         }
     }
+
+    /**
+     * Performs the necessary calculations on the grades_raw referenced by this grade_item,
+     * and stores the results in grade_grades_final. Performs this for only one userid if 
+     * requested. Also resets the needs_update flag once successfully performed.
+     *
+     * @param int $userid
+     * @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_final_grade($userid=NULL, $howmodified='manual', $note=NULL) {
+        if (empty($this->grade_grades_raw)) {
+            $this->load_raw();
+        }
+
+        $grade_raw_array = array();
+
+        if (!empty($userid)) {
+            $grade_raw_array[$userid] = $this->grade_grades_raw[$userid];
+        } else {
+            $grade_raw_array = $this->grade_grades_raw;
+        }
+        
+        // TODO implement parsing of formula and calculation MDL-9643
+        foreach ($grade_raw_array as $r) {
+            $newgradevalue = 0; // TODO replace '0' with calculated value
+            $r->update($newgradevalue, $howmodified, $note);
+        }
+
+        return true;
+    }
 }
 ?>
index f5aa91bced70961b7c613964c68332c975b74df1..b47462b4548d79ff50c4334338390e851b463c17 100644 (file)
@@ -40,6 +40,7 @@ define('GRADE_AGGREGATE_MODE', 3);
 require_once($CFG->libdir . '/grade/grade_category.php');
 require_once($CFG->libdir . '/grade/grade_item.php');
 require_once($CFG->libdir . '/grade/grade_calculation.php');
+require_once($CFG->libdir . '/grade/grade_grades_raw.php');
 
 /**
 * Extracts from the gradebook all the grade items attached to the calling object. 
@@ -118,4 +119,32 @@ function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL
     return $grade_item->is_locked($userid);
 } 
 
+/**
+ * Updates all grade_grades_final for each grade_item matching the given attributes.
+ * The search is further restricted, so that only grade_items that have needs_update == TRUE
+ * or that use calculation are retrieved.
+ *
+ * @param int $courseid
+ * @param int $gradeitemid
+ * @return int Number of grade_items updated
+ */
+function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) {
+    $grade_item = new grade_item();
+    $grade_item->courseid = $courseid;
+    $grade_item->id = $gradeitemid;
+    $grade_items = $grade_item->fetch_all_using_this();
+    
+    $count = 0;
+
+    foreach ($grade_items as $gi) {
+        $calculation = $gi->get_calculation();
+        if (!empty($calculation) || $gi->needsupdate) {
+            if ($gi->update_final_grade()) {
+                $count++;
+            }
+        }
+    }
+
+    return $count;
+}
 ?>
index 9cdc55c2a02bdac8fd7a86993ddda7aa61151656..3821d04f96b1665804bad3c2664fc28174a1f343 100644 (file)
@@ -634,8 +634,9 @@ class gradelib_test extends UnitTestCase {
         $grade_item = new grade_item($this->grade_items[0]);
         $this->assertTrue(method_exists($grade_item, 'get_raw'));
         
-        $raw_grade = $grade_item->get_raw($this->userid);
-        $this->assertEqual(1, count($raw_grade));
+        $raw_grades = $grade_item->get_raw($this->userid);
+        $raw_grade = current($raw_grades);
+        $this->assertEqual(1, count($raw_grades));        
         $this->assertEqual($this->grade_grades_raw[0]->gradevalue, $raw_grade->gradevalue);
     }
 
@@ -686,7 +687,11 @@ class gradelib_test extends UnitTestCase {
     }
 
     function test_grade_item_get_category() {
-
+        $grade_item = new grade_item($this->grade_items[0]);
+        $this->assertTrue(method_exists($grade_item, 'get_category'));
+        
+        $category = $grade_item->get_category();
+        $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
     }
 
 // GRADE_CATEGORY OBJECT