MDL-9506 Fixed a bug with the fetch method. This method should not be called statically when setting an internal object. For example, when the grade_category is calling its load_parent_category, it should not call grade_category::fetch, but new grade_category. Otherwise, the method assigns the new variables to the calling object.

This commit is contained in:
nicolasconnault 2007-05-11 02:43:46 +00:00
parent b55997c112
commit 1d4b666828
2 changed files with 11 additions and 8 deletions

View File

@ -310,9 +310,9 @@ class grade_category extends grade_object {
$result = $result && $this->grade_item->update();
$paths = explode('/', $this->path);
$wheresql = '';
foreach ($paths as $categoryid) {
$wheresql .= "iteminstance = $categoryid OR";
}
@ -600,13 +600,18 @@ class grade_category extends grade_object {
/**
* Retrieves from DB, instantiates and saves the associated grade_item object.
* If no grade_item exists yet, create one.
* @return object Grade_item
*/
function load_grade_item() {
$grade_items = get_records_select('grade_items', "iteminstance = $this->id AND itemtype = 'category'", null, '*', 0, 1);
$params = current($grade_items);
$this->grade_item = new grade_item($params);
if ($grade_items){
$params = current($grade_items);
$this->grade_item = new grade_item($params);
} else {
$this->grade_item = new grade_item();
}
// If the associated grade_item isn't yet created, do it now. But first try loading it, in case it exists in DB.
if (empty($this->grade_item->id)) {
@ -626,7 +631,7 @@ class grade_category extends grade_object {
*/
function load_parent_category() {
if (empty($this->parent_category) && !empty($this->parent)) {
$this->parent_category = grade_category::fetch('id', $this->parent);
$this->parent_category = new grade_category(array('id' => $this->parent));
}
return $this->parent_category;
}

View File

@ -165,8 +165,6 @@ class grade_category_test extends gradelib_test {
function test_grade_category_generate_grades() {
$category = new grade_category($this->grade_categories[0]);
$this->assertTrue(method_exists($category, 'generate_grades'));
$raw_grades = $category->generate_grades();
$this->assertEqual(3, count($raw_grades));
}
function test_grade_category_aggregate_grades() {