From: nicolasconnault Date: Thu, 3 May 2007 08:08:38 +0000 (+0000) Subject: MDL-9506 See previous revision. This adds same functionality but in relation to the... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4d0263c6e21625ad4046ad855b39581c2d5057c2;p=moodle.git MDL-9506 See previous revision. This adds same functionality but in relation to the hidden field. --- diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 15b1c0c23c..14ecd78961 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -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 diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 2656e2129d..9e9ab4dfad 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -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() {