]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-9506 See previous revision. This adds same functionality but in relation to the...
authornicolasconnault <nicolasconnault>
Thu, 3 May 2007 08:08:38 +0000 (08:08 +0000)
committernicolasconnault <nicolasconnault>
Thu, 3 May 2007 08:08:38 +0000 (08:08 +0000)
lib/grade/grade_item.php
lib/simpletest/testgradelib.php

index 15b1c0c23ca3fa268f6cfc5534e29a0d67b43987..14ecd78961deec03ce024fcaaa630252d8efde8f 100644 (file)
@@ -415,6 +415,41 @@ class grade_item extends grade_object {
         return $count;
     }
 
+    /**
+     * Locks or unlocks this grade_item and (optionally) all its associated final grades. 
+     * @param boolean $update_final Whether to update final grades too
+     * @param boolean $new_state Optional new state. Will use inverse of current state otherwise.
+     * @return int Number of final grades changed, or false if error occurred during update.
+     */
+    function toggle_hiding($update_final=false, $new_state=NULL) {
+        $this->hidden = !$this->hidden;
+        
+        if (!empty($new_state)) {
+            $this->hidden = $new_state;
+        }
+
+        if (!$this->update()) {
+            return false;
+        }
+        
+        $count = 0;
+        
+        if ($update_final) {
+            $this->load_final();
+            foreach ($this->grade_grades_final as $id => $final) {
+                $final->hidden = $this->hidden;
+                if (!$final->update()) {
+                    return false;
+                }
+                $count++;
+            }
+            $this->load_final();
+        }
+
+        return $count;
+    }
+    
+    
     /**
      * Performs the necessary calculations on the grades_final referenced by this grade_item,
      * and stores the results in grade_grades_final. Performs this for only one userid if 
index 2656e2129dbc0fb4f2e69fd52f2ee19cf640fc64..9e9ab4dfad7f40e703204a05163cd98b88a7153b 100644 (file)
@@ -897,6 +897,24 @@ class gradelib_test extends UnitTestCase {
         $this->assertTrue($grade_item->grade_grades_final[3]->locked);
     }
 
+    function test_grade_item_toggle_hiding() {
+        $grade_item = new grade_item($this->grade_items[0]);
+        $this->assertTrue(method_exists($grade_item, 'toggle_hiding'));
+
+        $this->assertFalse($grade_item->hidden);
+        $this->assertEqual(0, $grade_item->toggle_hiding());
+        $this->assertTrue($grade_item->hidden);
+        $grade_item->load_final();
+        $this->assertFalse($grade_item->grade_grades_final[1]->hidden);
+        
+        $grade_item->hidden = false;
+        $this->assertEqual(3, $grade_item->toggle_hiding(true));
+        $this->assertTrue($grade_item->hidden);
+        $this->assertTrue($grade_item->grade_grades_final[1]->hidden);
+        $this->assertTrue($grade_item->grade_grades_final[2]->hidden);
+        $this->assertTrue($grade_item->grade_grades_final[3]->hidden);
+    }
+
 // GRADE_CATEGORY OBJECT
 
     function test_grade_category_construct() {