From 1c307f2186070b54dab29f2cb41293e3eae8a300 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 7 Jun 2007 12:45:24 +0000 Subject: [PATCH] grade_item idnumber now self generated if needed. deleted flag implemented in grade_item::update method added GRADE_TYPE_NONE to gradelib and updated grade_item object Implemented multiple items (generation of itemnumber when not explicitly given) --- lib/grade/grade_category.php | 36 ++++++++------- lib/grade/grade_item.php | 45 ++++++++++++++++--- lib/grade/grade_object.php | 4 +- lib/grade/grade_tree.php | 4 +- .../grade/simpletest/testgradeitem.php | 29 ++++++++++++ lib/simpletest/testgradelib.php | 27 +++-------- 6 files changed, 95 insertions(+), 50 deletions(-) diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 113fb3219f..21eba83a87 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -551,6 +551,19 @@ class grade_category extends grade_object { } } + /** + * Disassociates this category from its category parent(s). The object is then updated in DB. + * @return boolean Success or Failure + */ + function divorce_parent() { + $this->old_parent = $this->get_parent_category(); + $this->parent = null; + $this->parent_category = null; + $this->depth = 1; + $this->path = '/' . $this->id; + return $this->update(); + } + /** * Looks at a path string (e.g. /2/45/56) and returns the depth level represented by this path (in this example, 3). * If no string is given, it looks at the obect's path and assigns the resulting depth to its $depth variable. @@ -770,19 +783,12 @@ class grade_category extends grade_object { // We passed all the checks, time to set the category as a parent. foreach ($children as $child) { - if ($first_child_type == 'grade_item') { - $child->categoryid = $this->id; - if (!$child->update()) { - debugging("Could not set this category as a parent for one of its child grade_items, DB operation failed."); - return false; - } - } elseif ($first_child_type == 'grade_category') { - $child->parent = $this->id; - if (!$child->update()) { - debugging("Could not set this category as a parent for one of its child categories, DB operation failed."); - return false; - } - } + $child->divorce_parent(); + $child->set_parent_id($this->id); + if (!$child->update()) { + debugging("Could not set this category as a parent for one of its children, DB operation failed."); + return false; + } } // TODO Assign correct sortorders to the newly assigned children and parent. Simply add 1 to all of them! @@ -843,10 +849,6 @@ class grade_category extends grade_object { * @param id $parentid */ function set_parent_id($parentid) { - if ($this->parent != $parentid) { - $this->old_parent = $this->get_parent_category(); - } - $this->parent = $parentid; $this->path = grade_category::build_path($this); $this->depth = $this->get_depth_from_path(); diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 200741a42a..3c7f4b9920 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -229,6 +229,11 @@ class grade_item extends grade_object { * In addition to update() as defined in grade_object, handle the grade_outcome and grade_scale objects. */ function update() { + // If item is flagged as deleted, only update that flag in DB. The other changes are ignored. + if (!empty($this->deleted) && $this->deleted) { + return set_field('grade_items', 'deleted', 1, 'id', $this->id); + } + if (!empty($this->outcome->id)) { $this->outcomeid = $this->outcome->id; } @@ -397,6 +402,22 @@ class grade_item extends grade_object { $this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator } } + + // If a grade_item already exists with these itemtype, itemmodule and iteminstance + // but not itemnumber, generate an itemnumber. + if (empty($this->itemnumber) && !empty($this->itemtype) && !empty($this->itemmodule) && !empty($this->iteminstance)) { + $existing_item = get_record('grade_items', + 'iteminstance', $this->iteminstance, + 'itemmodule', $this->itemmodule, + 'itemtype', $this->itemtype); + + if (empty($existing_item->itemnumber)) { + $existing_item->itemnumber = 0; + } + + $this->itemnumber = $existing_item->itemnumber + 1; + } + $result = parent::insert(); @@ -708,7 +729,18 @@ class grade_item extends grade_object { return $result; } - + + /** + * Disassociates this item from its category parent(s). The object is then updated in DB. + * @return boolean Success or Failure + */ + function divorce_parent() { + $this->old_parent = $this->get_category(); + $this->category = null; + $this->categoryid = null; + return $this->update(); + } + /** * Instantiates a grade_scale object whose data is retrieved from the DB, * if this item's scaleid variable is set. @@ -808,8 +840,11 @@ class grade_item extends grade_object { $standardised_finals = array(); $final_grades = $this->load_final(true); - foreach ($final_grades as $userid => $final) { - $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1); + + if (!empty($final_grades)) { + foreach ($final_grades as $userid => $final) { + $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1); + } } return $standardised_finals; @@ -1002,10 +1037,6 @@ class grade_item extends grade_object { * @param int $parentid */ function set_parent_id($parentid) { - if ($this->categoryid != $parentid) { - $this->old_parent = $this->get_category(); - } - $this->categoryid = $parentid; } diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index dee86f744a..5cd82e1618 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.com // // // -// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com // +// Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -144,7 +144,7 @@ class grade_object { $class = get_class($this); $object = new $class(array('id' => $this->id)); foreach ($object as $var => $val) { - if ($this->$var != $val) { + if (!in_array($var, $this->nonfields) && $this->$var != $val) { $this->$var = $val; } } diff --git a/lib/grade/grade_tree.php b/lib/grade/grade_tree.php index 1671020c67..7c79d1803c 100644 --- a/lib/grade/grade_tree.php +++ b/lib/grade/grade_tree.php @@ -95,12 +95,12 @@ class grade_tree { * objects for the given courseid or the entire site if no courseid given. Full objects are instantiated * by default, but this can be switched off. The tree is indexed by sortorder, to facilitate CRUD operations * and renumbering. - * @param int $courseid + * @param int $courseid If null, a blank object is instantiated. If 0, all courses are retrieved in the entire site (can be very slow!) * @param boolean $include_grades * @param array $tree */ function grade_tree($courseid=NULL, $include_grades=false, $tree=NULL) { - if (empty($courseid)) { + if (is_null($courseid)) { // empty object, do nothing } else { if ($courseid == 0) { diff --git a/lib/simpletest/grade/simpletest/testgradeitem.php b/lib/simpletest/grade/simpletest/testgradeitem.php index aa8ae68bd4..270e2ac15e 100755 --- a/lib/simpletest/grade/simpletest/testgradeitem.php +++ b/lib/simpletest/grade/simpletest/testgradeitem.php @@ -82,6 +82,35 @@ class grade_item_test extends gradelib_test { $this->assertEqual(11, $grade_item->sortorder); } + function test_grade_item_generate_itemnumber() { + $grade_item = new grade_item($this->grade_items[0]); + $copy_grade_item = fullclone($grade_item); + $copy_grade_item->itemnumber = null; + $result_id = $copy_grade_item->insert(); + $this->assertEqual(1, $copy_grade_item->itemnumber); + + } + + function test_grade_item_generate_idnumber() { + + } + + function test_grade_item_update_when_flagged_as_deleted() { + + } + + function test_grade_item_update_guess_outcomeid() { + + } + + function test_grade_item_update_default_gradetype() { + + } + + function test_grade_item_update_guess_scaleid() { + + } + function test_grade_item_delete() { $grade_item = new grade_item($this->grade_items[0]); $this->assertTrue(method_exists($grade_item, 'delete')); diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 612762d84b..d5d4c44d9d 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -102,7 +102,6 @@ class gradelib_test extends UnitTestCase { $table = new XMLDBTable('grade_items'); if (!table_exists($table)) { - /// Adding fields to table grade_items $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); @@ -113,7 +112,7 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); $table->addFieldInfo('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); $table->addFieldInfo('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); @@ -124,11 +123,10 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->addFieldInfo('deleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - - /// Adding keys to table grade_items $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); $table->addKeyInfo('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id')); @@ -144,7 +142,6 @@ class gradelib_test extends UnitTestCase { if ($result && !table_exists($table)) { - /// Adding fields to table grade_categories $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); @@ -154,11 +151,8 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); - - /// Adding keys to table grade_categories $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); $table->addKeyInfo('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id')); @@ -272,7 +266,6 @@ class gradelib_test extends UnitTestCase { if ($result && !table_exists($table)) { - /// Adding fields to table grade_grades_final $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); @@ -283,8 +276,6 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - - /// Adding keys to table grade_grades_final $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id')); $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); @@ -299,7 +290,6 @@ class gradelib_test extends UnitTestCase { if ($result && !table_exists($table)) { - /// Adding fields to table grade_grades_raw $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); @@ -310,8 +300,6 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $table->addFieldInfo('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - - /// Adding keys to table grade_grades_raw $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); $table->addKeyInfo('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id')); $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); @@ -327,7 +315,6 @@ class gradelib_test extends UnitTestCase { if ($result && !table_exists($table)) { - /// Adding fields to table scale $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); @@ -335,11 +322,7 @@ class gradelib_test extends UnitTestCase { $table->addFieldInfo('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - - /// Adding keys to table scale $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - - /// Adding indexes to table scale $table->addIndexInfo('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); /// Launch create table for scale @@ -643,7 +626,7 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'unittestorphangradeitem1'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'quiz'; - $grade_item->iteminstance = 1; + $grade_item->iteminstance = 5; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 10; $grade_item->grademax = 120; @@ -665,7 +648,7 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'singleparentitem1'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'forum'; - $grade_item->iteminstance = 3; + $grade_item->iteminstance = 7; $grade_item->gradetype = GRADE_TYPE_SCALE; $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; @@ -687,7 +670,7 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'singleparentitem2'; $grade_item->itemtype = 'mod'; $grade_item->itemmodule = 'forum'; - $grade_item->iteminstance = 3; + $grade_item->iteminstance = 9; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->grademin = 0; $grade_item->grademax = 100; -- 2.39.5