From 526e1a8a250c6715087e3cf1bb991b0dae9fffb3 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Mon, 28 May 2007 01:26:58 +0000 Subject: [PATCH] MDL-9506 Corrected a number of small errors in unit tests and classes. --- lib/grade/grade_category.php | 5 +- lib/grade/grade_grades_final.php | 3 + lib/grade/grade_item.php | 12 +- lib/grade/grade_object.php | 4 +- .../grade/simpletest/testgradecategory.php | 4 +- .../grade/simpletest/testgradefinal.php | 6 +- lib/simpletest/testgradelib.php | 138 +++++++++--------- 7 files changed, 94 insertions(+), 78 deletions(-) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 99f7f0dd09..da422fd139 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -738,7 +738,10 @@ class grade_category extends grade_object { debugging("Violated constraint: Attempted to set a category as a parent over children of 2 different types."); return false; } - if (grade_tree::get_element_type($child) == 'topcat') { + + $grade_tree = new grade_tree(); + + if ($grade_tree->get_element_type($child) == 'topcat') { debugging("Violated constraint: Attempted to set a category over children which are already top categories."); return false; } diff --git a/lib/grade/grade_grades_final.php b/lib/grade/grade_grades_final.php index 713113ffd0..7547b1c335 100644 --- a/lib/grade/grade_grades_final.php +++ b/lib/grade/grade_grades_final.php @@ -159,6 +159,9 @@ class grade_grades_final extends grade_object { function insert() { $exists = count_records('grade_grades_final', 'userid', $this->userid, 'itemid', $this->itemid); if ($exists) { + // Retrieve id number from DB + $record = get_record('grade_grades_final', 'userid', $this->userid, 'itemid', $this->itemid); + $this->id = $record->id; return $this->update(); } else { return parent::insert(); diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 55f06296c3..9358d56a2d 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -221,7 +221,11 @@ class grade_item extends grade_object { function load_scale() { if (!empty($this->scaleid)) { $this->scale = grade_scale::fetch('id', $this->scaleid); - $this->scale->load_items(); + if (method_exists($this->scale, 'load_items')) { + $this->scale->load_items(); + } else { + $this->scale = null; + } } return $this->scale; } @@ -355,6 +359,12 @@ class grade_item extends grade_object { // Retrieve scale and infer grademax from it if (!empty($this->scaleid)) { $this->load_scale(); + + if (!method_exists($this->scale, 'load_items')) { + debugging("The scale referenced by this grade_item ($this->scaleid) does not exist in the database. Grademax cannot be infered from the missing scale."); + return false; + } + $this->scale->load_items(); $this->grademax = count ($this->scale->scale_items); $this->grademin = 0; diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index 5b87aff595..aa8aea5b86 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -112,12 +112,12 @@ class grade_object { } $this->timecreated = $this->timemodified = time(); - + if (empty($this->usermodified)) { $this->usermodified = $USER->id; } - $clonethis = clone($this); + $clonethis = fullclone($this); // Unset non-set fields foreach ($clonethis as $var => $val) { diff --git a/lib/simpletest/grade/simpletest/testgradecategory.php b/lib/simpletest/grade/simpletest/testgradecategory.php index 7e30f213cc..7fb900a87f 100755 --- a/lib/simpletest/grade/simpletest/testgradecategory.php +++ b/lib/simpletest/grade/simpletest/testgradecategory.php @@ -83,7 +83,7 @@ class grade_category_test extends gradelib_test { $grade_category->parent = $this->grade_categories[0]->id; $grade_category->insert(); - + $last_grade_category = end($this->grade_categories); $this->assertFalse(empty($grade_category->grade_item)); @@ -194,7 +194,7 @@ class grade_category_test extends gradelib_test { for ($i = 0; $i < 3; $i++) { for ($j = 0; $j < 200; $j++) { - $grade_sets[$i][] = $this->generate_random_raw_grade($this->grade_items[$i], $j); + $grade_sets[$i][] = $this->generate_random_raw_grade(new grade_item($this->grade_items[$i]), $j); } } diff --git a/lib/simpletest/grade/simpletest/testgradefinal.php b/lib/simpletest/grade/simpletest/testgradefinal.php index f4a76faa20..1cc32963d5 100644 --- a/lib/simpletest/grade/simpletest/testgradefinal.php +++ b/lib/simpletest/grade/simpletest/testgradefinal.php @@ -53,14 +53,14 @@ class grade_final_test extends gradelib_test { $this->assertTrue(method_exists($grade_grades_final, 'insert')); $grade_grades_final->itemid = $this->grade_items[0]->id; - $grade_grades_final->userid = 1; + $grade_grades_final->userid = 4; $grade_grades_final->gradevalue = 88; - $grade_grades_final->insert(); + $this->assertTrue($grade_grades_final->insert()); $last_grade_grades_final = end($this->grade_grades_final); - $this->assertEqual($grade_grades_final->id, $last_grade_grades_final->id + 1); + $this->assertEqual($last_grade_grades_final->id + 1, $grade_grades_final->id); $this->assertFalse(empty($grade_grades_final->timecreated)); $this->assertFalse(empty($grade_grades_final->timemodified)); } diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 0ad1eb3442..9512fee609 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -54,9 +54,9 @@ class gradelib_test extends UnitTestCase { * crucial, because of the interrelationships between objects. */ var $tables = array('grade_categories', + 'scale', 'grade_items', 'grade_calculations', - 'scale', 'grade_grades_raw', 'grade_grades_final', 'grade_grades_text', @@ -375,6 +375,72 @@ class gradelib_test extends UnitTestCase { } } + /** + * Load scale data into the database, and adds the corresponding objects to this class' variable. + */ + function load_scale() { + $scale = new stdClass(); + + $scale->name = 'unittestscale1'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Way off topic, Not very helpful, Fairly neutral, Fairly helpful, Supportive, Some good information, Perfect answer!'; + $scale->description = 'This scale defines some of qualities that make posts helpful within the Moodle help forums.\n Your feedback will help others see how their posts are being received.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale = new stdClass(); + + $scale->name = 'unittestscale2'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; + $scale->description = 'This scale is used to mark standard assignments.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale = new stdClass(); + + $scale->name = 'unittestscale3'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Loner, Contentious, Disinterested, Participative, Follower, Leader'; + $scale->description = 'Describes the level of teamwork of a student.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale->name = 'unittestscale4'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Does not understand theory, Understands theory but fails practice, Manages through, Excels'; + $scale->description = 'Level of expertise at a technical task, with a theoretical framework.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + + $scale->name = 'unittestscale5'; + $scale->courseid = $this->courseid; + $scale->userid = $this->userid; + $scale->scale = 'Insufficient, Acceptable, Excellent.'; + $scale->description = 'Description of skills.'; + $scale->timemodified = mktime(); + + if ($scale->id = insert_record('scale', $scale)) { + $this->scale[] = $scale; + } + } + /** * Load grade_category data into the database, and adds the corresponding objects to this class' variable. */ @@ -507,7 +573,7 @@ class gradelib_test extends UnitTestCase { $grade_item->itemmodule = 'forum'; $grade_item->iteminstance = 3; $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = 1; + $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; $grade_item->grademax = 7; $grade_item->iteminfo = 'Grade item used for unit testing'; @@ -609,7 +675,7 @@ class gradelib_test extends UnitTestCase { $grade_item->itemmodule = 'forum'; $grade_item->iteminstance = 3; $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = 1; + $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; $grade_item->grademax = 7; $grade_item->iteminfo = 'Grade item used for unit testing'; @@ -707,72 +773,6 @@ class gradelib_test extends UnitTestCase { } } - /** - * Load scale data into the database, and adds the corresponding objects to this class' variable. - */ - function load_scale() { - $scale = new stdClass(); - - $scale->name = 'unittestscale1'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Way off topic, Not very helpful, Fairly neutral, Fairly helpful, Supportive, Some good information, Perfect answer!'; - $scale->description = 'This scale defines some of qualities that make posts helpful within the Moodle help forums.\n Your feedback will help others see how their posts are being received.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - - $scale = new stdClass(); - - $scale->name = 'unittestscale2'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; - $scale->description = 'This scale is used to mark standard assignments.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - - $scale = new stdClass(); - - $scale->name = 'unittestscale3'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Loner, Contentious, Disinterested, Participative, Follower, Leader'; - $scale->description = 'Describes the level of teamwork of a student.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - - $scale->name = 'unittestscale4'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Does not understand theory, Understands theory but fails practice, Manages through, Excels'; - $scale->description = 'Level of expertise at a technical task, with a theoretical framework.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - - $scale->name = 'unittestscale5'; - $scale->courseid = $this->courseid; - $scale->userid = $this->userid; - $scale->scale = 'Insufficient, Acceptable, Excellent.'; - $scale->description = 'Description of skills.'; - $scale->timemodified = mktime(); - - if ($scale->id = insert_record('scale', $scale)) { - $this->scale[] = $scale; - } - } - /** * Load grade_grades_raw data into the database, and adds the corresponding objects to this class' variable. */ -- 2.39.5