MDL-9506 Added grade_item::toggle_locking and unit tests. Affects all final grades if reqested.

This commit is contained in:
nicolasconnault
2007-05-03 08:02:51 +00:00
parent 6f93e96c19
commit fae51e127b
2 changed files with 62 additions and 0 deletions
+34
View File
@@ -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,
+28
View File
@@ -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