From: nicolasconnault Date: Thu, 3 May 2007 08:02:51 +0000 (+0000) Subject: MDL-9506 Added grade_item::toggle_locking and unit tests. Affects all final grades... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=fae51e127b534dc2a737bb3e7dee174b5a3de69c;p=moodle.git MDL-9506 Added grade_item::toggle_locking and unit tests. Affects all final grades if reqested. --- diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 9e08e4d85e..15b1c0c23c 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -380,6 +380,40 @@ class grade_item extends grade_object { return $final->locked; } } + + /** + * 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_locking($update_final=false, $new_state=NULL) { + $this->locked = !$this->locked; + + if (!empty($new_state)) { + $this->locked = $new_state; + } + + if (!$this->update()) { + return false; + } + + $count = 0; + + if ($update_final) { + $this->load_final(); + foreach ($this->grade_grades_final as $id => $final) { + $final->locked = $this->locked; + if (!$final->update()) { + return false; + } + $count++; + } + $this->load_final(); + } + + return $count; + } /** * Performs the necessary calculations on the grades_final referenced by this grade_item, diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 650a5ed95e..2656e2129d 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -879,6 +879,24 @@ class gradelib_test extends UnitTestCase { $this->assertEqual(4, $grade_item->adjust_grade($grade_raw)); } + function test_grade_item_toggle_locking() { + $grade_item = new grade_item($this->grade_items[0]); + $this->assertTrue(method_exists($grade_item, 'toggle_locking')); + + $this->assertFalse($grade_item->locked); + $this->assertEqual(0, $grade_item->toggle_locking()); + $this->assertTrue($grade_item->locked); + $grade_item->load_final(); + $this->assertFalse($grade_item->grade_grades_final[1]->locked); + + $grade_item->locked = false; + $this->assertEqual(3, $grade_item->toggle_locking(true)); + $this->assertTrue($grade_item->locked); + $this->assertTrue($grade_item->grade_grades_final[1]->locked); + $this->assertTrue($grade_item->grade_grades_final[2]->locked); + $this->assertTrue($grade_item->grade_grades_final[3]->locked); + } + // GRADE_CATEGORY OBJECT function test_grade_category_construct() { @@ -969,6 +987,8 @@ class gradelib_test extends UnitTestCase { function test_grade_category_get_children() { $category = new grade_category($this->grade_categories[0]); + $this->assertTrue(method_exists($category, 'get_children')); + $children_array = $category->get_children(0); $this->assertTrue(is_array($children_array)); $this->assertTrue(!empty($children_array[0])); @@ -995,6 +1015,14 @@ class gradelib_test extends UnitTestCase { $this->assertTrue(isset($children_array[0]['object'])); $this->assertEqual($this->grade_items[0]->id, $children_array[0]['object']->id); } + + function test_grade_category_has_children() { + $category = new grade_category($this->grade_categories[0]); + $this->assertTrue(method_exists($category, 'has_children')); + $this->assertTrue($category->has_children()); + $category = new grade_category(); + $this->assertFalse($category->has_children()); + } // GRADE_CALCULATION OBJECT