From: nicolasconnault <nicolasconnault>
Date: Fri, 11 May 2007 03:29:00 +0000 (+0000)
Subject: MDL-9506 Finished implementing the propagation of needsupdate flag up the hierarchy... 
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=77d2540eea3c57c61395a9dbea55f3bb200e5487;p=moodle.git

MDL-9506 Finished implementing the propagation of needsupdate flag up the hierarchy when a lower element justifies it. All unit tests pass. The next task is to implement the generation of raw grades and final grades based on this needsupdate setting.
---

diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php
index d48dd2e922..c4c7db977f 100644
--- a/lib/grade/grade_category.php
+++ b/lib/grade/grade_category.php
@@ -226,14 +226,14 @@ class grade_category extends grade_object {
      */
     function insert() {
         $result = parent::insert();
+        
+        $this->path = grade_category::build_path($this);
 
         // Build path and depth variables
         if (!empty($this->parent)) {
-            $this->path = grade_category::build_path($this);
             $this->depth = $this->get_depth_from_path();
         } else {
             $this->depth = 1;
-            $this->path = "/$this->id";
         }
         
         $this->update();
@@ -302,24 +302,25 @@ class grade_category extends grade_object {
 
         if (empty($this->grade_item)) {
             die("Associated grade_item object does not exist for this grade_category!" . print_object($this)); 
-            // TODO Send error message, this is a critical error: each category MUST have a matching grade_item object
+            // TODO Send error message, this is a critical error: each category MUST have a matching grade_item object and load_grade_item() is supposed to create one!
         }
-
-        $this->grade_item->needsupdate = true;
-        
-        $result = $result && $this->grade_item->update();
+        $this->path = grade_category::build_path($this);
 
         $paths = explode('/', $this->path);
         
-        $wheresql = '';
+        // Remove the first index, which is always empty
+        unset($paths[0]);
         
-        foreach ($paths as $categoryid) {
-            $wheresql .= "iteminstance = $categoryid OR";
+        if (!empty($paths)) {
+            $wheresql = '';
+            
+            foreach ($paths as $categoryid) {
+                $wheresql .= "iteminstance = $categoryid OR ";
+            }
+            $wheresql = substr($wheresql, 0, strrpos($wheresql, 'OR'));
+            $grade_items = set_field_select('grade_items', 'needsupdate', '1', $wheresql);
+            $this->grade_item->update_from_db();
         }
-
-        $wheresql = substr($wheresql, 0, strrpos($wheresql, 'OR'));
-        
-        // TODO use this sql fragment to set needsupdate to true for all grade_items whose iteminstance matches the categoryids
         return $result;
     }
 
diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php
index 823759d342..ab8e8df81e 100644
--- a/lib/grade/grade_item.php
+++ b/lib/grade/grade_item.php
@@ -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', 'grade_grades_raw', 'scale', 'category');
+    var $nonfields = array('table', 'nonfields', 'calculation', 'grade_grades_raw', 'grade_grades_final', 'scale', 'category', 'outcome');
   
     /**
      * The course this grade_item belongs to.
diff --git a/lib/simpletest/grade/simpletest/testgradeitem.php b/lib/simpletest/grade/simpletest/testgradeitem.php
index a64f5204bb..2d1bb26d18 100755
--- a/lib/simpletest/grade/simpletest/testgradeitem.php
+++ b/lib/simpletest/grade/simpletest/testgradeitem.php
@@ -73,6 +73,7 @@ class grade_item_test extends gradelib_test {
         $grade_item->insert();
 
         // Now check the needsupdate variable, it should have been set to true
+        $category->grade_item->update_from_db();
         $this->assertTrue($category->grade_item->needsupdate);
 
         $last_grade_item = end($this->grade_items);
@@ -93,6 +94,7 @@ class grade_item_test extends gradelib_test {
         $this->assertTrue($grade_item->delete());
 
         // Now check the needsupdate variable, it should have been set to true
+        $category->grade_item->update_from_db();
         $this->assertTrue($category->grade_item->needsupdate);
 
         $this->assertFalse(get_record('grade_items', 'id', $grade_item->id));
@@ -120,7 +122,13 @@ class grade_item_test extends gradelib_test {
         $this->assertTrue($grade_item->update(true));
         
         // Now check the needsupdate variable, it should have been set to true
+        $category->grade_item->update_from_db();
         $this->assertTrue($category->grade_item->needsupdate);
+
+        // Also check parent
+        $category->load_parent_category();
+        $category->parent_category->load_grade_item();
+        $this->assertTrue($category->parent_category->grade_item->needsupdate);
         
         $iteminfo = get_field('grade_items', 'iteminfo', 'id', $this->grade_items[0]->id);
         $this->assertEqual($grade_item->iteminfo, $iteminfo);