From f92dcad81a5f90d902adf99a54f6cbeb071c4b9d Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 24 Jun 2007 22:26:33 +0000 Subject: [PATCH] MDL-10233 improved static factory methods of grade obejcts fetch() and fetch_all(); implemented default values of grade objects, fixed null values for some properties that are not required - unit tests included, tested with PHP4 and PHP5 --- grade/export/grade_export_form.php | 2 +- grade/export/lib.php | 2 +- grade/export/txt/grade_export_txt_form.php | 2 +- grade/export/txt/index.php | 2 +- grade/import/grade_import_form.php | 2 +- lib/db/install.xml | 6 +- lib/db/upgrade.php | 41 ++++- lib/grade/grade_category.php | 67 +++---- lib/grade/grade_grades.php | 56 +++--- lib/grade/grade_grades_text.php | 38 ++-- lib/grade/grade_history.php | 41 +++-- lib/grade/grade_item.php | 91 +++++----- lib/grade/grade_object.php | 168 +++++++++++------- lib/grade/grade_outcome.php | 49 ++--- lib/grade/grade_scale.php | 33 ++-- lib/grade/grade_tree.php | 15 +- lib/gradelib.php | 37 +--- lib/simpletest/fixtures/gradetest.php | 38 ++-- .../grade/simpletest/testgradecategory.php | 14 +- .../grade/simpletest/testgradegrades.php | 14 +- ...tgradetext.php => testgradegradestext.php} | 10 +- .../grade/simpletest/testgradehistory.php | 11 +- .../grade/simpletest/testgradeitem.php | 28 ++- .../grade/simpletest/testgradeoutcome.php | 15 +- .../grade/simpletest/testgradescale.php | 2 +- .../grade/simpletest/testgradetree.php | 4 +- lib/simpletest/testgradelib.php | 33 ---- tags | 20 +-- version.php | 2 +- 29 files changed, 446 insertions(+), 397 deletions(-) rename lib/simpletest/grade/simpletest/{testgradetext.php => testgradegradestext.php} (92%) diff --git a/grade/export/grade_export_form.php b/grade/export/grade_export_form.php index cde373fc1a..dfda9fd6a9 100755 --- a/grade/export/grade_export_form.php +++ b/grade/export/grade_export_form.php @@ -9,7 +9,7 @@ class grade_export_form extends moodleform { $mform->addElement('header', 'general', get_string('gradeitemsinc', 'grades')); // TODO: localize $id = $this->_customdata['id']; // course id $mform->addElement('hidden', 'id', $id); - if ($grade_items = grade_get_items($id)) { + if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) { foreach ($grade_items as $grade_item) { $element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id)); $element->setChecked(1); diff --git a/grade/export/lib.php b/grade/export/lib.php index 611df8745f..9890271c33 100755 --- a/grade/export/lib.php +++ b/grade/export/lib.php @@ -127,7 +127,7 @@ class grade_export { } } else { // else we get all items for this course - $gradeitems = grade_get_items($this->id); + $gradeitems = grade_grades::fetch_all(array('courseid'=>$this->id)); } if ($gradeitems) { diff --git a/grade/export/txt/grade_export_txt_form.php b/grade/export/txt/grade_export_txt_form.php index ec0156555e..d27770108b 100755 --- a/grade/export/txt/grade_export_txt_form.php +++ b/grade/export/txt/grade_export_txt_form.php @@ -9,7 +9,7 @@ class grade_export_txt_form extends moodleform { $mform->addElement('header', 'general', 'Gradeitems to be included'); // TODO: localize $id = $this->_customdata['id']; // course id $mform->addElement('hidden', 'id', $id); - if ($grade_items = grade_get_items($id)) { + if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) { foreach ($grade_items as $grade_item) { $element = new HTML_QuickForm_advcheckbox('itemids[]', null, $grade_item->itemname, array('selected'=>'selected'), array(0, $grade_item->id)); $element->setChecked(1); diff --git a/grade/export/txt/index.php b/grade/export/txt/index.php index 2ab595bb25..6f93d3abb5 100755 --- a/grade/export/txt/index.php +++ b/grade/export/txt/index.php @@ -61,7 +61,7 @@ $mform->display(); // print_gradeitem_selections($id); // print all items for selections // make this a standard function in lib maybe -if ($grade_items = grade_get_items($id)) { +if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) { echo '
'; echo '
'; foreach ($grade_items as $grade_item) { diff --git a/grade/import/grade_import_form.php b/grade/import/grade_import_form.php index 4b33d81a75..9c223e8f66 100755 --- a/grade/import/grade_import_form.php +++ b/grade/import/grade_import_form.php @@ -66,7 +66,7 @@ class grade_import_mapping_form extends moodleform { include_once($CFG->libdir.'/gradelib.php'); if ($id) { - if ($grade_items = grade_get_items($id)) { + if ($grade_items = grade_grades::fetch_all(array('courseid'=>$id))) { foreach ($grade_items as $grade_item) { $gradeitems[$grade_item->idnumber] = $grade_item->itemname; } diff --git a/lib/db/install.xml b/lib/db/install.xml index 9d2717c703..9764f4b9e0 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1241,12 +1241,12 @@ - + - + - + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 694bb843a9..96143cfa8a 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -812,12 +812,12 @@ function xmldb_main_upgrade($oldversion=0) { $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); - $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null); $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $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('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); @@ -1025,7 +1025,7 @@ function xmldb_main_upgrade($oldversion=0) { /// Define field path to be added to grade_categories $table = new XMLDBTable('grade_categories'); $field = new XMLDBField('path'); - $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'depth'); + $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'depth'); /// Launch add field path $result = $result && add_field($table, $field); @@ -1333,6 +1333,39 @@ function xmldb_main_upgrade($oldversion=0) { /// Launch add field calculation $result = $result && add_field($table, $field); } + } + + if ($result && $oldversion < 2007062401) { + + /// Changing nullability of field itemname on table grade_items to null + $table = new XMLDBTable('grade_items'); + $field = new XMLDBField('itemname'); + $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'categoryid'); + + /// Launch change of nullability for field itemname + $result = $result && change_field_notnull($table, $field); + + $field = new XMLDBField('itemmodule'); + $field->setAttributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null, 'itemtype'); + + /// Launch change of nullability for field itemname + $result = $result && change_field_notnull($table, $field); + + $field = new XMLDBField('iteminfo'); + $field->setAttributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, 'itemnumber'); + + /// Launch change of nullability for field itemname + $result = $result && change_field_notnull($table, $field); + + + /// Changing nullability of field path on table grade_categories to null + $table = new XMLDBTable('grade_categories'); + $field = new XMLDBField('path'); + $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'depth'); + + /// Launch change of nullability for field path + $result = $result && change_field_notnull($table, $field); + /// Remove the obsoleted unitttests tables - they will be recreated automatically $tables = array('grade_categories', diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 07d0718f82..25495e1fed 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -85,19 +85,19 @@ class grade_category extends grade_object { * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) . * @var int $aggregation */ - var $aggregation; + var $aggregation = GRADE_AGGREGATE_MEAN_ALL; /** * Keep only the X highest items. * @var int $keephigh */ - var $keephigh; + var $keephigh = 0; /** * Drop the X lowest items. * @var int $droplow */ - var $droplow; + var $droplow = 0; /** * Array of grade_items or grade_categories nested exactly 1 level below this category @@ -119,18 +119,6 @@ class grade_category extends grade_object { */ var $grade_item; - /** - * Constructor. Extends the basic functionality defined in grade_object. - * @param array $params Can also be a standard object. - * @param boolean $fetch Whether or not to fetch the corresponding row from the DB. - * @param object $grade_item The associated grade_item object can be passed during construction. - */ - function grade_category($params=NULL, $fetch=true) { - $this->grade_object($params, $fetch); - $this->path = grade_category::build_path($this); - } - - /** * Builds this category's path string based on its parents (if any) and its own id number. * This is typically done just before inserting this object in the DB for the first time, @@ -152,22 +140,35 @@ class grade_category extends grade_object { /** - * Finds and returns a grade_category object based on 1-3 field values. + * Finds and returns a grade_category instance based on params. * @static * - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_category object or false if none found. - */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_category = get_record('grade_categories', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_category = new grade_category($grade_category); - return $grade_category; + * @param array $params associative arrays varname=>value + * @return object grade_category instance or false if none found. + */ + function fetch($params) { + if ($category = grade_object::fetch_helper('grade_categories', 'grade_category', $params)) { + $category->path = grade_category::build_path($category); + return $category; + + } else { + return false; + } + } + + /** + * Finds and returns all grade_category instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_category insatnces or false if none found. + */ + function fetch_all($params) { + if ($categories = grade_object::fetch_all_helper('grade_categories', 'grade_category', $params)) { + foreach ($categories as $key=>$value) { + $categories[$key]->path = grade_category::build_path($categories[$key]); + } + return $categories; } else { return false; @@ -360,7 +361,7 @@ class grade_category extends grade_object { $items = array(); foreach($depends_on as $dep) { - $items[$dep] = grade_item::fetch('id', $dep); + $items[$dep] = grade_item::fetch(array('id'=>$dep)); } // where to look for final grades - include or grade item too @@ -712,9 +713,11 @@ class grade_category extends grade_object { return false; } - $grade_item = new grade_item(array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id), false); - if (!$grade_items = $grade_item->fetch_all_using_this()) { + $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id); + + if (!$grade_items = grade_item::fetch_all($params)) { // create a new one + $grade_item = new grade_item($params, false); $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->insert(); diff --git a/lib/grade/grade_grades.php b/lib/grade/grade_grades.php index 7d05b5d5a4..edec57607d 100644 --- a/lib/grade/grade_grades.php +++ b/lib/grade/grade_grades.php @@ -67,13 +67,13 @@ class grade_grades extends grade_object { * The maximum allowable grade when this grade was created. * @var float $rawgrademax */ - var $rawgrademax; + var $rawgrademax = 100; /** * The minimum allowable grade when this grade was created. * @var float $rawgrademin */ - var $rawgrademin; + var $rawgrademin = 0; /** * id of the scale, if this grade is based on a scale. @@ -105,34 +105,25 @@ class grade_grades extends grade_object { * 0 if visible, 1 always hidden or date not visible until * @var float $hidden */ - var $hidden; + var $hidden = 0; /** * 0 not locked, date when the item was locked * @var float locked */ - var $locked; + var $locked = 0; /** * 0 no automatic locking, date when to lock the grade automatically * @var float $locktime */ - var $locktime; + var $locktime = 0; /** * Exported flag * @var boolean $exported */ - var $exported; - - /** - * Constructor. Extends the basic functionality defined in grade_object. - * @param array $params Can also be a standard object. - * @param boolean $fetch Wether or not to fetch the corresponding row from the DB. - */ - function grade_grades($params=NULL, $fetch=true) { - $this->grade_object($params, $fetch); - } + var $exported = 0; /** * Loads the grade_grades_text object linked to this grade (through the intersection of itemid and userid), and @@ -141,7 +132,7 @@ class grade_grades extends grade_object { */ function load_text() { if (empty($this->grade_grades_text->id)) { - $this->grade_grades_text = grade_grades_text::fetch('itemid', $this->itemid, 'userid', $this->userid); + $this->grade_grades_text = grade_grades_text::fetch(array('itemid'=>$this->itemid, 'userid'=>$this->userid)); } return $this->grade_grades_text; @@ -153,7 +144,7 @@ class grade_grades extends grade_object { */ function load_grade_item() { if (empty($this->grade_item) && !empty($this->itemid)) { - $this->grade_item = grade_item::fetch('id', $this->itemid); + $this->grade_item = grade_item::fetch(array('id'=>$this->itemid)); } return $this->grade_item; } @@ -214,26 +205,25 @@ class grade_grades extends grade_object { } /** - * Finds and returns a grade_grades object based on 1-3 field values. + * Finds and returns a grade_grades instance based on params. * @static * - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_category object or false if none found. + * @param array $params associative arrays varname=>value + * @return object grade_grades instance or false if none found. */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($object = get_record('grade_grades', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $object = new grade_grades($object); - return $object; + function fetch($params) { + return grade_object::fetch_helper('grade_grades', 'grade_grades', $params); + } - } else { - return false; - } + /** + * Finds and returns all grade_grades instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_grades insatnces or false if none found. + */ + function fetch_all($params) { + return grade_object::fetch_all_helper('grade_grades', 'grade_grades', $params); } /** diff --git a/lib/grade/grade_grades_text.php b/lib/grade/grade_grades_text.php index 3700232ffe..3d41b7b6e3 100644 --- a/lib/grade/grade_grades_text.php +++ b/lib/grade/grade_grades_text.php @@ -70,7 +70,7 @@ class grade_grades_text extends grade_object { * Text format for information (FORMAT_PLAIN, FORMAT_HTML etc...). * @var int $informationformat */ - var $informationformat; + var $informationformat = FORMAT_MOODLE; /** * Manual feedback from the teacher. This could be a code like 'mi'. @@ -82,7 +82,7 @@ class grade_grades_text extends grade_object { * Text format for feedback (FORMAT_PLAIN, FORMAT_HTML etc...). * @var int $feedbackformat */ - var $feedbackformat; + var $feedbackformat = FORMAT_MOODLE; /** * The userid of the person who last modified this text. @@ -91,27 +91,25 @@ class grade_grades_text extends grade_object { var $usermodified; /** - * Finds and returns a grade_text object based on 1-3 field values. + * Finds and returns a grade_grades_text instance based on params. * @static * - * @param boolean $static Unless set to true, this method will also set $this object with the returned values. - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_text object or false if none found. + * @param array $params associative arrays varname=>value + * @return object grade_grades_text instance or false if none found. */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_text = get_record('grade_grades_text', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_text = new grade_grades_text($grade_text); - return $grade_text; + function fetch($params) { + return grade_object::fetch_helper('grade_grades_text', 'grade_grades_text', $params); + } - } else { - return false; - } + /** + * Finds and returns all grade_grades_text instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_grades_text insatnces or false if none found. + */ + function fetch_all($params) { + return grade_object::fetch_all_helper('grade_grades_text', 'grade_grades_text', $params); } /** @@ -120,7 +118,7 @@ class grade_grades_text extends grade_object { */ function load_grade_item() { if (empty($this->grade_item) && !empty($this->itemid)) { - $this->grade_item = grade_item::fetch('id', $this->itemid); + $this->grade_item = grade_item::fetch(array('id'=>$this->itemid)); } return $this->grade_item; } diff --git a/lib/grade/grade_history.php b/lib/grade/grade_history.php index 9c8da938af..47f77de52e 100644 --- a/lib/grade/grade_history.php +++ b/lib/grade/grade_history.php @@ -72,33 +72,38 @@ class grade_history extends grade_object { */ var $note; + /** + * Which user account did the modification. + * @var string $usermodified + */ + var $usermodified; + /** * How the grade was modified ('manual', 'module', 'import' etc...). * @var string $howmodified */ - var $howmodified; + var $howmodified = 'manual'; /** - * Finds and returns a grade_history object based on 1-3 field values. + * Finds and returns a grade_history instance based on params. * @static * - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_history object or false if none found. + * @param array $params associative arrays varname=>value + * @return object grade_history instance or false if none found. */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_history = get_record('grade_history', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_history = new grade_history($grade_history); - return $grade_history; + function fetch($params) { + return grade_object::fetch_helper('grade_history', 'grade_history', $params); + } - } else { - return false; - } + /** + * Finds and returns all grade_history instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_history insatnces or false if none found. + */ + function fetch_all($params) { + return grade_object::fetch_all_helper('grade_history', 'grade_history', $params); } /** @@ -120,8 +125,6 @@ class grade_history extends grade_object { $history->newgrade = $newgrade; $history->note = $note; $history->howmodified = $howmodified; - $history->timemodified = time(); - $history->usermodified = $USER->id; return $history->insert(); } diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 7b26cd560a..af7a982015 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -73,7 +73,7 @@ class grade_item extends grade_object { var $itemname; /** - * e.g. 'mod', 'blocks', 'import', 'calculate' etc... + * e.g. 'category', 'total' and 'mod', 'blocks', 'import', etc... * @var string $itemtype */ var $itemtype; @@ -129,19 +129,19 @@ class grade_item extends grade_object { * The type of grade (0 = none, 1 = value, 2 = scale, 3 = text) * @var int $gradetype */ - var $gradetype; + var $gradetype = GRADE_TYPE_VALUE; /** * Maximum allowable grade. * @var float $grademax */ - var $grademax; + var $grademax = 100; /** * Minimum allowable grade. * @var float $grademin */ - var $grademin; + var $grademin = 0; /** * id of the scale, if this grade is based on a scale. @@ -168,61 +168,58 @@ class grade_item extends grade_object { var $outcome; /** - * grade required to pass. (grademin < gradepass <= grademax) + * grade required to pass. (grademin <= gradepass <= grademax) * @var float $gradepass */ - var $gradepass; + var $gradepass = 0; /** * Multiply all grades by this number. * @var float $multfactor */ - var $multfactor; + var $multfactor = 1.0; /** * Add this to all grades. * @var float $plusfactor */ - var $plusfactor; + var $plusfactor = 0; /** * Sorting order of the columns. * @var int $sortorder */ - var $sortorder; + var $sortorder = 0; /** * Date until which to hide this grade_item. If null, 0 or false, grade_item is not hidden. Hiding prevents viewing. * @var int $hidden */ - var $hidden; + var $hidden = 0; /** - * Date until which to lock this grade_item. If null, 0 or false, grade_item is not locked. Locking prevents updating. + * Grade item lock flag. Enmpty if not locked, lcoked if any value presetn ,usually date when was locked. Locking prevents updating. * @var int $locked */ - var $locked; + var $locked = 0; + + /** + * Date when to lock the grade. Empty means no automatic locking. + * @var int $locktime + */ + var $locktime = 0; /** * Whether or not the module instance referred to by this grade_item has been deleted. * @var int $deleted */ - var $deleted; + var $deleted = 0; /** * If set, the whole column will be recalculated, then this flag will be switched off. * @var boolean $needsupdate */ - var $needsupdate; - - /** - * Constructor. Extends the basic functionality defined in grade_object. - * @param array $params Can also be a standard object. - * @param boolean $fetch Wether or not to fetch the corresponding row from the DB. - */ - function grade_item($params=NULL, $fetch=true) { - $this->grade_object($params, $fetch); - } + var $needsupdate = 0; /** * In addition to update() as defined in grade_object, handle the grade_outcome and grade_scale objects. @@ -293,26 +290,25 @@ class grade_item extends grade_object { } /** - * Finds and returns a grade_item object based on 1-3 field values. + * Finds and returns a grade_item instance based on params. * @static * - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_item object or false if none found. - */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_item = get_record('grade_items', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_item = new grade_item($grade_item); - return $grade_item; + * @param array $params associative arrays varname=>value + * @return object grade_item instance or false if none found. + */ + function fetch($params) { + return grade_object::fetch_helper('grade_items', 'grade_item', $params); + } - } else { - return false; - } + /** + * Finds and returns all grade_item instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_item insatnces or false if none found. + */ + function fetch_all($params) { + return grade_object::fetch_all_helper('grade_items', 'grade_item', $params); } /** @@ -728,7 +724,7 @@ class grade_item extends grade_object { } if (!empty($this->scaleid)) { - $this->scale = grade_scale::fetch('id', $this->scaleid); + $this->scale = grade_scale::fetch(array('id'=>$this->scaleid)); $this->scale->load_items(); $this->grademax = count($this->scale->scale_items) - 1; $this->grademin = 0; @@ -746,7 +742,7 @@ class grade_item extends grade_object { */ function load_outcome() { if (!empty($this->outcomeid)) { - $this->outcome = grade_outcome::fetch('id', $this->outcomeid); + $this->outcome = grade_outcome::fetch(array('id'=>$this->outcomeid)); } return $this->outcome; } @@ -762,9 +758,9 @@ class grade_item extends grade_object { $category = null; if (!empty($this->categoryid)) { - $category = grade_category::fetch('id', $this->categoryid); + $category = grade_category::fetch(array('id'=>$this->categoryid)); } elseif (!empty($this->iteminstance) && $this->itemtype == 'category') { - $category = grade_category::fetch('id', $this->iteminstance); + $category = grade_category::fetch(array('id'=>$this->iteminstance)); } return $category; @@ -798,7 +794,7 @@ class grade_item extends grade_object { // first detect if we need to update calculation formula from [idnumber] to [#giXXX#] (after backup, etc.) if (!$this->calculation_normalized and preg_match_all('/\[(?!#gi)(.*?)\]/', $this->calculation, $matches)) { foreach ($matches[1] as $idnumber) { - if ($grade_item = grade_item::fetch('courseid', $this->courseid, 'idnumber', $idnumber)) { + if ($grade_item = grade_item::fetch(array('courseid'=>$this->courseid, 'idnumber'=>$idnumber))) { $this->calculation = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $this->calculation); } } @@ -820,7 +816,7 @@ class grade_item extends grade_object { // denormalize formula - convert [#giXX#] to [idnumber] if (preg_match_all('/\[#gi([0-9]+)#\]/', $formula, $matches)) { foreach ($matches[1] as $id) { - if ($grade_item = grade_item::fetch('id', $id)) { + if ($grade_item = grade_item::fetch(array('id'=>$id))) { if (!empty($grade_item->idnumber)) { $formula = str_replace('[#gi'.$grade_item->id.'#]', '['.$grade_item->idnumber.']', $formula); } @@ -854,8 +850,7 @@ class grade_item extends grade_object { } // normalize formula - we want grade item ids [#giXXX#] instead of [idnumber] - $grade_item = new grade_item(array('courseid'=>$this->courseid), false); - if ($grade_items = $grade_item->fetch_all_using_this()) { + if ($grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid))) { foreach ($grade_items as $grade_item) { $formula = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $formula); } diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index 43c40cb3e6..8dd5066b76 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -60,19 +60,107 @@ class grade_object { /** * Constructor. Optionally (and by default) attempts to fetch corresponding row from DB. - * @param object $params an object with named parameters for this grade item. + * @param array $params an array with required parameters for this grade object. * @param boolean $fetch Whether to fetch corresponding row from DB or not. */ function grade_object($params=NULL, $fetch = true) { - if (!empty($params) && (is_array($params) || is_object($params))) { - $this->assign_to_this($params); - - if ($fetch) { - $records = $this->fetch_all_using_this(); - if ($records && count($records) > 0) { - $this->assign_to_this(current($records)); - } + if (!empty($params) and (is_array($params) or is_object($params))) { + if ($fetch and $data = $this->fetch($params)) { + grade_object::set_properties($this, $data); + + } else { + grade_object::set_properties($this, $params); + } + } + } + + /** + * Finds and returns a grade_object instance based on params. + * @static abstract + * + * @param array $params associative arrays varname=>value + * @return object grade_object instance or false if none found. + */ + function fetch($params) { + error('Abstract method fetch() not overrided in '.get_class($this)); + } + + /** + * Finds and returns all grade_object instances based on params. + * @static abstract + * + * @param array $params associative arrays varname=>value + * @return array array of grade_object insatnces or false if none found. + */ + function fetch_all($params) { + error('Abstract method fetch_all() not overrided in '.get_class($this)); + } + + /** + * Factory method - uses the parameters to retrieve matching instance from the DB. + * @static final protected + * @return mixed object insatnce or false if not found + */ + function fetch_helper($table, $classname, $params) { + // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-( + // in PHP5 we could do it much better + if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { + if (count($instances) > 1) { + // we should not tolerate any errors here - proplems might appear later + error('Found more than one record in fetch() !'); } + return reset($instances); + } else { + return false; + } + } + + /** + * Factory method - uses the parameters to retrieve all matching instances from the DB. + * @static final protected + * @return mixed array of object instances or false if not found + */ + function fetch_all_helper($table, $classname, $params) { + // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-( + // in PHP5 we could do it much better + $instance = new $classname(); + + $classvars = (array)$instance; + $params = (array)$params; + + $wheresql = array(); + + // remove incorrect params - warn developer if needed + foreach ($params as $var=>$value) { + if (!array_key_exists($var, $classvars) or in_array($var, $instance->nonfields)) { + debugging("Incorrect property name $var for class $classname"); + continue; + } + if (is_null($value)) { + $wheresql[] = " $var IS NULL "; + } else { + $value = addslashes($value); + $wheresql[] = " $var = '$value' "; + } + } + + if (empty($wheresql)) { + $wheresql = ''; + } else { + $wheresql = implode("AND", $wheresql); + } + + if ($datas = get_records_select($table, $wheresql, 'id')) { + $result = array(); + foreach($datas as $data) { + $instance = new $classname(); + grade_object::set_properties($instance, $data); + $result[$instance->id] = $instance; + } + return $result; + + } else { + return false; } } @@ -86,7 +174,7 @@ class grade_object { $this->timemodified = time(); - if (empty($this->usermodified)) { + if (array_key_exists('usermodified', $this)) { $this->usermodified = $USER->id; } @@ -116,20 +204,11 @@ class grade_object { $this->timecreated = $this->timemodified = time(); - if (empty($this->usermodified)) { + if (array_key_exists('usermodified', $this)) { $this->usermodified = $USER->id; } - $clonethis = fullclone($this); - - // Unset non-set and null fields - foreach ($clonethis as $var => $val) { - if (!isset($val)) { - unset($clonethis->$var); - } - } - - if (!$this->id = insert_record($this->table, addslashes_recursive($clonethis), true)) { + if (!$this->id = insert_record($this->table, addslashes_recursive($this))) { debugging("Could not insert object into db"); return false; } @@ -157,54 +236,21 @@ class grade_object { return false; } - $this->assign_to_this($params); + grade_object::set_properties($this, $params); return true; } - /** - * Uses the variables of this object to retrieve all matching objects from the DB. - * @return array $objects - */ - function fetch_all_using_this() { - $variables = get_object_vars($this); - $wheresql = ''; - - foreach ($variables as $var => $value) { - if (!empty($value) && !in_array($var, $this->nonfields)) { - $value = addslashes($value); - $wheresql .= " $var = '$value' AND "; - } - } - - // Trim trailing AND - $wheresql = substr($wheresql, 0, strrpos($wheresql, 'AND')); - - $objects = get_records_select($this->table, $wheresql, 'id'); - - if (!empty($objects)) { - $full_objects = array(); - - // Convert the stdClass objects returned by the get_records_select method into proper objects - $classname = get_class($this); - foreach ($objects as $id => $stdobject) { - $full_objects[$id] = new $classname($stdobject, false); - } - return $full_objects; - } else { - return $objects; - } - } - - /** * Given an associated array or object, cycles through each key/variable * and assigns the value to the corresponding variable in this object. + * @static final */ - function assign_to_this($params) { - foreach ($params as $param => $value) { - if (in_object_vars($param, $this)) { - $this->$param = $value; + function set_properties(&$instance, $params) { + $classvars = (array)$instance; + foreach ($params as $var => $value) { + if (array_key_exists($var, $classvars)) { + $instance->$var = $value; } } } diff --git a/lib/grade/grade_outcome.php b/lib/grade/grade_outcome.php index bbda5954a4..1127f101f7 100644 --- a/lib/grade/grade_outcome.php +++ b/lib/grade/grade_outcome.php @@ -79,36 +79,41 @@ class grade_outcome extends grade_object { var $usermodified; /** - * Constructor. Extends the basic functionality defined in grade_object. - * @param array $params Can also be a standard object. - * @param boolean $fetch Wether or not to fetch the corresponding row from the DB. + * Finds and returns a grade_outcome instance based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return object grade_outcome instance or false if none found. */ - function grade_outcome($params=NULL, $fetch=true) { - $this->grade_object($params, $fetch); - if (!empty($this->scaleid)) { - $this->scale = new grade_scale(array('id' => $this->scaleid)); - $this->scale->load_items(); + function fetch($params) { + if ($outcome = grade_object::fetch_helper('grade_outcomes', 'grade_outcome', $params)) { + if (!empty($outcome->scaleid)) { + $outcome->scale = new grade_scale(array('id'=>$outcome->scaleid)); + $outcome->scale->load_items(); + } + return $outcome; + + } else { + return false; } } /** - * Finds and returns a grade_outcome object based on 1-3 field values. + * Finds and returns all grade_outcome instances based on params. * @static * - * @param boolean $static Unless set to true, this method will also set $this object with the returned values. - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_outcome object or false if none found. + * @param array $params associative arrays varname=>value + * @return array array of grade_outcome insatnces or false if none found. */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_outcome = get_record('grade_outcomes', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_outcome = new grade_outcome($grade_outcome); - return $grade_outcome; + function fetch_all($params) { + if ($outcomes = grade_object::fetch_all_helper('grade_outcomes', 'grade_outcome', $params)) { + foreach ($outcomes as $key=>$value) { + if (!empty($outcomes[$key]->scaleid)) { + $outcomes[$key]->scale = new grade_scale(array('id'=>$outcomes[$key]->scaleid)); + $outcomes[$key]->scale->load_items(); + } + } + return $outcomes; } else { return false; diff --git a/lib/grade/grade_scale.php b/lib/grade/grade_scale.php index 59d6f7999d..d49986626d 100644 --- a/lib/grade/grade_scale.php +++ b/lib/grade/grade_scale.php @@ -48,6 +48,8 @@ class grade_scale extends grade_object { */ var $courseid; + var $userid; + /** * The name of the scale. * @var string $name @@ -73,26 +75,25 @@ class grade_scale extends grade_object { var $description; /** - * Finds and returns a grade_scale object based on 1-3 field values. + * Finds and returns a grade_scale instance based on params. * @static * - * @param string $field1 - * @param string $value1 - * @param string $field2 - * @param string $value2 - * @param string $field3 - * @param string $value3 - * @param string $fields - * @return object grade_scale object or false if none found. + * @param array $params associative arrays varname=>value + * @return object grade_scale instance or false if none found. */ - function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { - if ($grade_scale = get_record('scale', $field1, $value1, $field2, $value2, $field3, $value3, $fields)) { - $grade_scale = new grade_scale($grade_scale); - return $grade_scale; + function fetch($params) { + return grade_object::fetch_helper('scale', 'grade_scale', $params); + } - } else { - return false; - } + /** + * Finds and returns all grade_scale instances based on params. + * @static + * + * @param array $params associative arrays varname=>value + * @return array array of grade_scale insatnces or false if none found. + */ + function fetch_all($params) { + return grade_object::fetch_all_helper('scale', 'grade_scale', $params); } /** diff --git a/lib/grade/grade_tree.php b/lib/grade/grade_tree.php index c8dafb4e11..e07df04dc6 100644 --- a/lib/grade/grade_tree.php +++ b/lib/grade/grade_tree.php @@ -576,12 +576,9 @@ class grade_tree { $itemtree = array(); foreach ($children as $element) { - $finals = array(); - if ($this->include_grades) { - $final = new grade_grades(); - $final->itemid = $element['object']->id; - $finals = $final->fetch_all_using_this(); + if (!$this->include_grades or !$finals = grade_grades::fetch_all(array('itemid'=>$element['object']->id))) { + $finals = array(); } $itemtree[$element['object']->sortorder] = array('object' => $element['object'], 'finalgrades' => $finals); @@ -740,12 +737,8 @@ class grade_tree { } foreach ($items as $itemid => $item) { - $finals = array(); - - if ($this->include_grades) { - $final = new grade_grades(); - $final->itemid = $itemid; - $finals = $final->fetch_all_using_this(); + if (!$this->include_grades or !$finals = grade_grades::fetch_all(array('itemid'=>$itemid))) { + $finals = array(); } $sortorder = $item->sortorder; diff --git a/lib/gradelib.php b/lib/gradelib.php index 0804fa639a..7b30ed7cb8 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -95,8 +95,7 @@ function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, return GRADE_UPDATE_FAILED; } - $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false); - if (!$grade_items = $grade_item->fetch_all_using_this()) { + if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) { // create a new one $grade_item = false; @@ -277,8 +276,7 @@ function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, */ function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) { - $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false); - if (!$grade_items = $grade_item->fetch_all_using_this()) { + if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) { return false; } else if (count($grade_items) == 1){ @@ -299,31 +297,6 @@ function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $item /***** END OF PUBLIC API *****/ -/** -* Extracts from the gradebook all the grade items attached to the calling object. -* For example, an assignment may want to retrieve all the grade_items for itself, -* and get three outcome scales in return. This will affect the grading interface. -* -* Note: Each parameter refines the search. So if you only give the courseid, -* all the grade_items for this course will be returned. If you add the -* itemtype 'mod', all grade_items for this courseif AND for the 'mod' -* type will be returned, etc... -* -* @param int $courseid The id of the course to which the grade items belong -* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc -* @param string $itemmodule 'forum, 'quiz', 'csv' etc -* @param int $iteminstance id of the item module -* @param int $itemnumber Can be used to distinguish multiple grades for an activity -* @param string $itemname The name of the grade item -* @param int $idnumber grade item Primary Key -* @return array An array of grade items -*/ -function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $itemname=NULL, $idnumber=NULL) { - $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemname', 'itemnumber', 'idnumber'), false); - $grade_items = $grade_item->fetch_all_using_this(); - return $grade_items; -} - /** * Updates all final grades in course. * @@ -338,9 +311,7 @@ function grade_update_final_grades($courseid, $regradeall=false) { set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid); } - $grade_item = new grade_item(array('courseid'=>$courseid), false); - - if (!$grade_items = $grade_item->fetch_all_using_this()) { + if (!$grade_items = $grade_item->fetch_all(array('courseid'=>$courseid))) { return true; } @@ -617,7 +588,7 @@ function grade_update_mod_grades($modinstance) { function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) { // does it already exist? - if ($grade_items = grade_get_items($modinstance->course, 'mod', $modinstance->modname, $modinstance->id, 0)) { + if ($grade_items = grade_grades::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) { if (count($grade_items) > 1) { debugging('Multiple legacy grade_items found.'); return false; diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index 5fa8ed6af2..323fd82d75 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -99,12 +99,12 @@ class grade_test extends UnitTestCase { $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); - $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); $table->addFieldInfo('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null); $table->addFieldInfo('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); $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('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); $table->addFieldInfo('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); $table->addFieldInfo('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); @@ -118,11 +118,12 @@ class grade_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('locktime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, 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); + $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')); @@ -142,13 +143,14 @@ class grade_test extends UnitTestCase { $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); $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->addFieldInfo('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); $table->addFieldInfo('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); $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('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); + $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')); @@ -256,7 +258,6 @@ class grade_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); - /// Adding keys to table grade_grades $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')); @@ -316,11 +317,11 @@ class grade_test extends UnitTestCase { $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(); - $temp = explode(',', $scale->scale); - $scale->max = count($temp) -1; if ($scale->id = insert_record('scale', $scale)) { $this->scale[0] = $scale; + $temp = explode(',', $scale->scale); + $this->scalemax[0] = count($temp) -1; } $scale = new stdClass(); @@ -331,11 +332,11 @@ class grade_test extends UnitTestCase { $scale->scale = 'Distinction, Very Good, Good, Pass, Fail'; $scale->description = 'This scale is used to mark standard assignments.'; $scale->timemodified = mktime(); - $temp = explode(',', $scale->scale); - $scale->max = count($temp) -1; if ($scale->id = insert_record('scale', $scale)) { $this->scale[1] = $scale; + $temp = explode(',', $scale->scale); + $this->scalemax[1] = count($temp) -1; } $scale = new stdClass(); @@ -351,6 +352,8 @@ class grade_test extends UnitTestCase { if ($scale->id = insert_record('scale', $scale)) { $this->scale[2] = $scale; + $temp = explode(',', $scale->scale); + $this->scalemax[2] = count($temp) -1; } $scale->name = 'unittestscale4'; @@ -364,6 +367,8 @@ class grade_test extends UnitTestCase { if ($scale->id = insert_record('scale', $scale)) { $this->scale[3] = $scale; + $temp = explode(',', $scale->scale); + $this->scalemax[3] = count($temp) -1; } $scale->name = 'unittestscale5'; @@ -377,6 +382,8 @@ class grade_test extends UnitTestCase { if ($scale->id = insert_record('scale', $scale)) { $this->scale[4] = $scale; + $temp = explode(',', $scale->scale); + $this->scalemax[4] = count($temp) -1; } } @@ -391,7 +398,6 @@ class grade_test extends UnitTestCase { $grade_category->aggregation = GRADE_AGGREGATE_MEAN_GRADED; $grade_category->keephigh = 100; $grade_category->droplow = 0; - $grade_category->hidden = 0; $grade_category->timecreated = mktime(); $grade_category->timemodified = mktime(); $grade_category->depth = 1; @@ -407,7 +413,6 @@ class grade_test extends UnitTestCase { $grade_category->aggregation = GRADE_AGGREGATE_MEAN_GRADED; $grade_category->keephigh = 100; $grade_category->droplow = 0; - $grade_category->hidden = 0; $grade_category->parent = $this->grade_categories[0]->id; $grade_category->timecreated = mktime(); $grade_category->timemodified = mktime(); @@ -424,7 +429,6 @@ class grade_test extends UnitTestCase { $grade_category->aggregation = GRADE_AGGREGATE_MEAN_GRADED; $grade_category->keephigh = 100; $grade_category->droplow = 0; - $grade_category->hidden = 0; $grade_category->parent = $this->grade_categories[0]->id; $grade_category->timecreated = mktime(); $grade_category->timemodified = mktime(); @@ -443,7 +447,6 @@ class grade_test extends UnitTestCase { $grade_category->aggregation = GRADE_AGGREGATE_MEAN_GRADED; $grade_category->keephigh = 100; $grade_category->droplow = 0; - $grade_category->hidden = 0; $grade_category->timecreated = mktime(); $grade_category->timemodified = mktime(); $grade_category->depth = 1; @@ -515,7 +518,7 @@ class grade_test extends UnitTestCase { $grade_item->gradetype = GRADE_TYPE_SCALE; $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; - $grade_item->grademax = $this->scale[0]->max; + $grade_item->grademax = $this->scalemax[0]; $grade_item->iteminfo = 'Grade item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); @@ -622,7 +625,7 @@ class grade_test extends UnitTestCase { $grade_item->gradetype = GRADE_TYPE_SCALE; $grade_item->scaleid = $this->scale[0]->id; $grade_item->grademin = 0; - $grade_item->grademax = $this->scale[0]->max; + $grade_item->grademax = $this->scalemax[0]; $grade_item->iteminfo = 'Grade item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); @@ -923,7 +926,6 @@ class grade_test extends UnitTestCase { function load_grade_outcomes() { // Calculation for grade_item 1 $grade_outcome = new stdClass(); - $grade_outcome->itemid = $this->grade_items[0]->id; $grade_outcome->shortname = 'Team work'; $grade_outcome->timecreated = mktime(); $grade_outcome->timemodified = mktime(); @@ -935,7 +937,6 @@ class grade_test extends UnitTestCase { // Calculation for grade_item 2 $grade_outcome = new stdClass(); - $grade_outcome->itemid = $this->grade_items[1]->id; $grade_outcome->shortname = 'Complete circuit board'; $grade_outcome->timecreated = mktime(); $grade_outcome->timemodified = mktime(); @@ -947,7 +948,6 @@ class grade_test extends UnitTestCase { // Calculation for grade_item 3 $grade_outcome = new stdClass(); - $grade_outcome->itemid = $this->grade_items[2]->id; $grade_outcome->shortname = 'Debug Java program'; $grade_outcome->timecreated = mktime(); $grade_outcome->timemodified = mktime(); diff --git a/lib/simpletest/grade/simpletest/testgradecategory.php b/lib/simpletest/grade/simpletest/testgradecategory.php index d1e1a8d200..35e6a46739 100755 --- a/lib/simpletest/grade/simpletest/testgradecategory.php +++ b/lib/simpletest/grade/simpletest/testgradecategory.php @@ -87,6 +87,8 @@ class grade_category_test extends grade_test { $grade_category->insert(); + $this->assertEqual('/'.$this->grade_categories[0]->id.'/'.$grade_category->id, $grade_category->path); + $last_grade_category = end($this->grade_categories); $this->assertFalse(empty($grade_category->grade_item)); @@ -121,11 +123,19 @@ class grade_category_test extends grade_test { $grade_category = new grade_category(); $this->assertTrue(method_exists($grade_category, 'fetch')); - $grade_category = grade_category::fetch('id', $this->grade_categories[0]->id); + $grade_category = grade_category::fetch(array('id'=>$this->grade_categories[0]->id)); $this->assertEqual($this->grade_categories[0]->id, $grade_category->id); $this->assertEqual($this->grade_categories[0]->fullname, $grade_category->fullname); } + function test_grade_category_fetch_all() { + $grade_category = new grade_category(); + $this->assertTrue(method_exists($grade_category, 'fetch_all')); + + $grade_categories = grade_category::fetch_all(array('courseid'=>$this->courseid)); + $this->assertEqual(count($this->grade_categories), count($grade_categories)); + } + function test_grade_category_get_children() { $category = new grade_category($this->grade_categories[0]); $this->assertTrue(method_exists($category, 'get_children')); @@ -244,6 +254,8 @@ class grade_category_test extends grade_test { $child2 = new grade_item(); $child1->itemname = 'new grade_item'; $child2->itemname = 'new grade_item'; + $child1->itemtype = 'something'; + $child2->itemtype = 'something'; $child1->sortorder = 1; $child2->sortorder = 2; $child1->courseid = $grade_category->courseid; diff --git a/lib/simpletest/grade/simpletest/testgradegrades.php b/lib/simpletest/grade/simpletest/testgradegrades.php index 0caaa17d53..9fc632a890 100755 --- a/lib/simpletest/grade/simpletest/testgradegrades.php +++ b/lib/simpletest/grade/simpletest/testgradegrades.php @@ -85,9 +85,17 @@ class grade_grades_test extends grade_test { $grade_grades = new grade_grades(); $this->assertTrue(method_exists($grade_grades, 'fetch')); - $grade_grades = grade_grades::fetch('id', $this->grade_grades[0]->id); - $this->assertEqual($this->grade_grades[0]->id, $grade_grades->id); - $this->assertEqual($this->grade_grades[0]->rawgrade, $grade_grades->rawgrade); + $grades = grade_grades::fetch(array('id'=>$this->grade_grades[0]->id)); + $this->assertEqual($this->grade_grades[0]->id, $grades->id); + $this->assertEqual($this->grade_grades[0]->rawgrade, $grades->rawgrade); + } + + function test_grade_grades_fetch_all() { + $grade_grades = new grade_grades(); + $this->assertTrue(method_exists($grade_grades, 'fetch_all')); + + $grades = grade_grades::fetch_all(array()); + $this->assertEqual(count($this->grade_grades), count($grades)); } function test_grade_raw_update_feedback() { diff --git a/lib/simpletest/grade/simpletest/testgradetext.php b/lib/simpletest/grade/simpletest/testgradegradestext.php similarity index 92% rename from lib/simpletest/grade/simpletest/testgradetext.php rename to lib/simpletest/grade/simpletest/testgradegradestext.php index e49bf3d5a8..48062db2c5 100755 --- a/lib/simpletest/grade/simpletest/testgradetext.php +++ b/lib/simpletest/grade/simpletest/testgradegradestext.php @@ -102,11 +102,19 @@ class grade_text_test extends grade_test { $grade_grades_text = new grade_grades_text(); $this->assertTrue(method_exists($grade_grades_text, 'fetch')); - $grade_grades_text = grade_grades_text::fetch('id', $this->grade_grades_text[0]->id); + $grade_grades_text = grade_grades_text::fetch(array('id'=>$this->grade_grades_text[0]->id)); $this->assertEqual($this->grade_grades_text[0]->id, $grade_grades_text->id); $this->assertEqual($this->grade_grades_text[0]->information, $grade_grades_text->information); } + function test_grade_grades_text_fetch_all() { + $grade_grades_text = new grade_grades_text(); + $this->assertTrue(method_exists($grade_grades_text, 'fetch_all')); + + $grade_grades_texts = grade_grades_text::fetch_all(array()); + $this->assertEqual(count($this->grade_grades_text[0]), count($grade_grades_texts)); + } + function test_grade_grades_text_load_grade_item() { $grade_grades_text = new grade_grades_text($this->grade_grades_text[0]); $this->assertTrue(method_exists($grade_grades_text, 'load_grade_item')); diff --git a/lib/simpletest/grade/simpletest/testgradehistory.php b/lib/simpletest/grade/simpletest/testgradehistory.php index 2b69044d6e..d9cb433108 100644 --- a/lib/simpletest/grade/simpletest/testgradehistory.php +++ b/lib/simpletest/grade/simpletest/testgradehistory.php @@ -95,9 +95,18 @@ class grade_history_test extends grade_test { $grade_history = new grade_history(); $this->assertTrue(method_exists($grade_history, 'fetch')); - $grade_history = grade_history::fetch('id', $this->grade_history[0]->id); + $grade_history = grade_history::fetch(array('id'=>$this->grade_history[0]->id)); $this->assertEqual($this->grade_history[0]->id, $grade_history->id); $this->assertEqual($this->grade_history[0]->note, $grade_history->note); } + + function test_grade_history_fetch_all() { + $grade_history = new grade_history(); + $this->assertTrue(method_exists($grade_history, 'fetch_all')); + + $histories = grade_history::fetch(array()); + $this->assertEqual(count($this->grade_history), count($histories)); + } + } ?> diff --git a/lib/simpletest/grade/simpletest/testgradeitem.php b/lib/simpletest/grade/simpletest/testgradeitem.php index f61f68ad1c..2d1e323fb0 100755 --- a/lib/simpletest/grade/simpletest/testgradeitem.php +++ b/lib/simpletest/grade/simpletest/testgradeitem.php @@ -189,27 +189,23 @@ class grade_item_test extends grade_test { $grade_item = new grade_item(); $this->assertTrue(method_exists($grade_item, 'fetch')); - $grade_item = grade_item::fetch('id', $this->grade_items[0]->id); + $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id)); $this->assertEqual($this->grade_items[0]->id, $grade_item->id); $this->assertEqual($this->grade_items[0]->iteminfo, $grade_item->iteminfo); - $grade_item = grade_item::fetch('itemtype', $this->grade_items[1]->itemtype, 'itemmodule', $this->grade_items[1]->itemmodule); + $grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule)); $this->assertEqual($this->grade_items[1]->id, $grade_item->id); $this->assertEqual($this->grade_items[1]->iteminfo, $grade_item->iteminfo); } - function test_grade_item_fetch_all_using_this() { + function test_grade_item_fetch_all() { $grade_item = new grade_item(); - $grade_item->itemtype = 'mod'; - $this->assertTrue(method_exists($grade_item, 'fetch_all_using_this')); + $this->assertTrue(method_exists($grade_item, 'fetch_all')); - $grade_items = $grade_item->fetch_all_using_this(); - $this->assertEqual(5, count($grade_items)); - $first_grade_item = reset($grade_items); - $this->assertEqual($this->grade_items[0]->id, $first_grade_item->id); + $grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid)); + $this->assertEqual(count($this->grade_items), count($grade_items)); } - /** * Retrieve all final scores for a given grade_item. */ @@ -428,20 +424,20 @@ class grade_item_test extends grade_test { $grade_item = new grade_item($this->grade_items[1]); $this->assertTrue(method_exists($grade_item, 'compute')); - $grade_grades = grade_grades::fetch('id', $this->grade_grades[3]->id); + $grade_grades = grade_grades::fetch(array('id'=>$this->grade_grades[3]->id)); $grade_grades->delete(); - $grade_grades = grade_grades::fetch('id', $this->grade_grades[4]->id); + $grade_grades = grade_grades::fetch(array('id'=>$this->grade_grades[4]->id)); $grade_grades->delete(); - $grade_grades = grade_grades::fetch('id', $this->grade_grades[5]->id); + $grade_grades = grade_grades::fetch(array('id'=>$this->grade_grades[5]->id)); $grade_grades->delete(); $grade_item->compute(); - $grade_grades = grade_grades::fetch('userid', $this->grade_grades[3]->userid, 'itemid', $this->grade_grades[3]->itemid); + $grade_grades = grade_grades::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid)); $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grades->finalgrade); - $grade_grades = grade_grades::fetch('userid', $this->grade_grades[4]->userid, 'itemid', $this->grade_grades[4]->itemid); + $grade_grades = grade_grades::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid)); $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grades->finalgrade); - $grade_grades = grade_grades::fetch('userid', $this->grade_grades[5]->userid, 'itemid', $this->grade_grades[5]->itemid); + $grade_grades = grade_grades::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid)); $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grades->finalgrade); } diff --git a/lib/simpletest/grade/simpletest/testgradeoutcome.php b/lib/simpletest/grade/simpletest/testgradeoutcome.php index e4164707cb..8f7c35a6c0 100644 --- a/lib/simpletest/grade/simpletest/testgradeoutcome.php +++ b/lib/simpletest/grade/simpletest/testgradeoutcome.php @@ -55,7 +55,8 @@ class grade_outcome_test extends grade_test { $this->assertTrue(method_exists($grade_outcome, 'insert')); $grade_outcome->courseid = $this->courseid; - $grade_outcome->shortname = 'Team work'; + $grade_outcome->shortname = 'tw'; + $grade_outcome->fullname = 'Team work'; $grade_outcome->insert(); @@ -87,9 +88,19 @@ class grade_outcome_test extends grade_test { $grade_outcome = new grade_outcome(); $this->assertTrue(method_exists($grade_outcome, 'fetch')); - $grade_outcome = grade_outcome::fetch('id', $this->grade_outcomes[0]->id); + $grade_outcome = grade_outcome::fetch(array('id'=>$this->grade_outcomes[0]->id)); $this->assertEqual($this->grade_outcomes[0]->id, $grade_outcome->id); $this->assertEqual($this->grade_outcomes[0]->shortname, $grade_outcome->shortname); + + $this->assertEqual($this->scale[2]->id, $grade_outcome->scale->id); + } + + function test_grade_outcome_fetch_all() { + $grade_outcome = new grade_outcome(); + $this->assertTrue(method_exists($grade_outcome, 'fetch_all')); + + $grade_outcomes = grade_outcome::fetch_all(array()); + $this->assertEqual(count($this->grade_outcomes), count($grade_outcomes)); } } ?> diff --git a/lib/simpletest/grade/simpletest/testgradescale.php b/lib/simpletest/grade/simpletest/testgradescale.php index c6bfc9d367..c284c4971e 100755 --- a/lib/simpletest/grade/simpletest/testgradescale.php +++ b/lib/simpletest/grade/simpletest/testgradescale.php @@ -98,7 +98,7 @@ class grade_scale_test extends grade_test { $grade_scale = new grade_scale(); $this->assertTrue(method_exists($grade_scale, 'fetch')); - $grade_scale = grade_scale::fetch('id', $this->scale[0]->id); + $grade_scale = grade_scale::fetch(array('id'=>$this->scale[0]->id)); $this->assertEqual($this->scale[0]->id, $grade_scale->id); $this->assertEqual($this->scale[0]->name, $grade_scale->name); } diff --git a/lib/simpletest/grade/simpletest/testgradetree.php b/lib/simpletest/grade/simpletest/testgradetree.php index 162864f730..51201acda3 100644 --- a/lib/simpletest/grade/simpletest/testgradetree.php +++ b/lib/simpletest/grade/simpletest/testgradetree.php @@ -356,13 +356,13 @@ class grade_tree_test extends grade_test { $tree->remove_element(7); $tree->renumber(); $tree->update_db(); - $item = grade_item::fetch('id', $this->grade_items[6]->id); + $item = grade_item::fetch(array('id'=>$this->grade_items[6]->id)); $this->assertTrue(empty($item->id)); $tree->move_element(4, 9); $tree->renumber(); $tree->update_db(); - $item = grade_item::fetch('id', $this->grade_items[1]->id); + $item = grade_item::fetch(array('id'=>$this->grade_items[1]->id)); $this->assertFalse(empty($item->id)); $this->assertEqual(8, $item->sortorder); diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 64c4753bfa..8f5dc94111 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -43,39 +43,6 @@ require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php'); * 3 users for 3 grade_items */ class gradelib_test extends grade_test { - - function test_grade_get_items() { - if (get_class($this) == 'gradelib_test') { - $grade_items = grade_get_items($this->courseid); - - $this->assertTrue(is_array($grade_items)); - $this->assertEqual(count($grade_items), 10); - } - } - -/* -// obsolted function, should be replaced by grade_update() or removed completely - function test_grade_create_category() { - if (get_class($this) == 'gradelib_test') { - $grade_category = new stdClass(); - $grade_category->timecreated = mktime(); - $grade_category->timemodified = mktime(); - - $items = array(new grade_item(), new grade_item()); - - $grade_category->id = grade_create_category($this->courseid, 'unittestcategory4', $items, GRADE_AGGREGATE_MEAN); - - $last_grade_category = end($this->grade_categories); - $this->assertEqual($grade_category->id, $last_grade_category->id + 1); - - $db_grade_category = get_record('grade_categories', 'id', $grade_category->id); - $db_grade_category = new grade_category($db_grade_category); - $db_grade_category->load_grade_item(); - $this->grade_categories[] = $db_grade_category; - $this->grade_items[] = $db_grade_category->grade_item; - } - } -*/ function test_grade_is_locked() { if (get_class($this) == 'gradelib_test') { $grade_item = $this->grade_items[0]; diff --git a/tags b/tags index 5de5db0b18..caeaaff811 100644 --- a/tags +++ b/tags @@ -5404,15 +5404,15 @@ fail lib/simpletestlib/test_case.php /^ function fail($message = "Fail") fatal mod/chat/chatd.php /^ function fatal($message) {$/;" f fault lib/soap/nusoap.php /^ function fault($faultcode,$faultstring,$faultactor='',$faultdetail=''){$/;" f fcFPP auth/fc/fcFPP.php /^ function fcFPP($host="localhost", $port="3333")$/;" f -fetch lib/grade/grade_calculation.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_category.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_grades_final.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_grades_raw.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_grades_text.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_history.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_item.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_outcome.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f -fetch lib/grade/grade_scale.php /^ function fetch($field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields="*") { $/;" f +fetch lib/grade/grade_calculation.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_category.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_grades_final.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_grades_raw.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_grades_text.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_history.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_item.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_outcome.php /^ function fetch($params) { $/;" f +fetch lib/grade/grade_scale.php /^ function fetch($params) { $/;" f fetch lib/simpletestlib/http.php /^ function &fetch($timeout) {$/;" f fetch lib/smarty/Smarty.class.php /^ function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)$/;" f fetchResponse lib/simpletestlib/user_agent.php /^ function &fetchResponse($url, $encoding) {$/;" f @@ -12106,7 +12106,7 @@ write lib/adodb/session/adodb-encrypt-secret.php /^ function write($data, $key) write lib/adodb/session/adodb-encrypt-sha1.php /^ function write($data, $key) $/;" f write lib/adodb/session/adodb-session.php /^ function write($key, $val) $/;" f write lib/adodb/session/adodb-session2.php /^ function write($key, $val) $/;" f -write lib/excel/OLEwriter.php /^ function write($data) \/\/por ahora sólo a STDOUT$/;" f +write lib/excel/OLEwriter.php /^ function write($data) \/\/por ahora s�lo a STDOUT$/;" f write lib/excel/Worksheet.php /^ function write($row, $col, $token, $format = 0)$/;" f write lib/excellib.class.php /^ function write($row, $col, $token, $format=0) {$/;" f write lib/odslib.class.php /^ function write($row, $col, $token, $format=0) {$/;" f diff --git a/version.php b/version.php index 5c17d34428..82db24949c 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2007062301; // YYYYMMDD = date + $version = 2007062401; // YYYYMMDD = date // XY = increments within a single day $release = '1.9 dev'; // Human-friendly version name -- 2.39.5