From: nicolasconnault Date: Fri, 23 May 2008 14:52:50 +0000 (+0000) Subject: MDL-14967 Upgraded gradebook code and unit tests. 4 failing tests in grade_item to... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=da3801e84bae2ed97c76f5c609fccefa9bcad694;p=moodle.git MDL-14967 Upgraded gradebook code and unit tests. 4 failing tests in grade_item to fix. --- diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index f6b9d8ff1e..f93c662126 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -30,13 +30,13 @@ class grade_category extends grade_object { * The DB table. * @var string $table */ - var $table = 'grade_categories'; + public $table = 'grade_categories'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation', + public $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats', 'timecreated', 'timemodified'); @@ -44,104 +44,104 @@ class grade_category extends grade_object { * The course this category belongs to. * @var int $courseid */ - var $courseid; + public $courseid; /** * The category this category belongs to (optional). * @var int $parent */ - var $parent; + public $parent; /** * The grade_category object referenced by $this->parent (PK). * @var object $parent_category */ - var $parent_category; + public $parent_category; /** * The number of parents this category has. * @var int $depth */ - var $depth = 0; + public $depth = 0; /** * Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being * this category's autoincrement ID number. * @var string $path */ - var $path; + public $path; /** * The name of this category. * @var string $fullname */ - var $fullname; + public $fullname; /** * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) . * @var int $aggregation */ - var $aggregation = GRADE_AGGREGATE_MEAN; + public $aggregation = GRADE_AGGREGATE_MEAN; /** * Keep only the X highest items. * @var int $keephigh */ - var $keephigh = 0; + public $keephigh = 0; /** * Drop the X lowest items. * @var int $droplow */ - var $droplow = 0; + public $droplow = 0; /** * Aggregate only graded items * @var int $aggregateonlygraded */ - var $aggregateonlygraded = 0; + public $aggregateonlygraded = 0; /** * Aggregate outcomes together with normal items * @var int $aggregateoutcomes */ - var $aggregateoutcomes = 0; + public $aggregateoutcomes = 0; /** * Ignore subcategories when aggregating * @var int $aggregatesubcats */ - var $aggregatesubcats = 0; + public $aggregatesubcats = 0; /** * Array of grade_items or grade_categories nested exactly 1 level below this category * @var array $children */ - var $children; + public $children; /** * A hierarchical array of all children below this category. This is stored separately from * $children because it is more memory-intensive and may not be used as often. * @var array $all_children */ - var $all_children; + public $all_children; /** * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values * for this category. * @var object $grade_item */ - var $grade_item; + public $grade_item; /** * Temporary sortorder for speedup of children resorting */ - var $sortorder; + public $sortorder; /** * List of options which can be "forced" from site settings. */ - var $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats'); + public $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats'); /** * Builds this category's path string based on its parents (if any) and its own id number. @@ -153,11 +153,13 @@ class grade_category extends grade_object { * @param object $grade_category * @return int The depth of this category (2 means there is one parent) */ - function build_path($grade_category) { + public function build_path($grade_category) { + global $DB; + if (empty($grade_category->parent)) { return '/'.$grade_category->id.'/'; } else { - $parent = get_record('grade_categories', 'id', $grade_category->parent); + $parent = $DB->get_record('grade_categories', array('id' => $grade_category->parent)); return grade_category::build_path($parent).$grade_category->id.'/'; } } @@ -169,7 +171,7 @@ class grade_category extends grade_object { * @param array $params associative arrays varname=>value * @return object grade_category instance or false if none found. */ - function fetch($params) { + public static function fetch($params) { return grade_object::fetch_helper('grade_categories', 'grade_category', $params); } @@ -180,7 +182,7 @@ class grade_category extends grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_category insatnces or false if none found. */ - function fetch_all($params) { + public static function fetch_all($params) { return grade_object::fetch_all_helper('grade_categories', 'grade_category', $params); } @@ -189,7 +191,7 @@ class grade_category extends grade_object { * @param string $source from where was the object updated (mod/forum, manual, etc.) * @return boolean success */ - function update($source=null) { + public function update($source=null) { // load the grade item or create a new one $this->load_grade_item(); @@ -227,7 +229,7 @@ class grade_category extends grade_object { $child->path = null; $child->depth = 0; $child->update($source); - } + } } } @@ -239,7 +241,7 @@ class grade_category extends grade_object { * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return boolean success */ - function delete($source=null) { + public function delete($source=null) { $grade_item = $this->load_grade_item(); if ($this->is_course_category()) { @@ -295,7 +297,7 @@ class grade_category extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return int PK ID if successful, false otherwise */ - function insert($source=null) { + public function insert($source=null) { if (empty($this->courseid)) { print_error('cannotinsertgrade'); @@ -329,7 +331,7 @@ class grade_category extends grade_object { * @param int $courseid * @return bool success */ - function insert_course_category($courseid) { + public function insert_course_category($courseid) { $this->courseid = $courseid; $this->fullname = '?'; $this->path = null; @@ -358,7 +360,7 @@ class grade_category extends grade_object { * This assumes that this object has an id number and a matching record in DB. If not, it will return false. * @return boolean */ - function qualifies_for_regrading() { + public function qualifies_for_regrading() { if (empty($this->id)) { debugging("Can not regrade non existing category"); return false; @@ -380,7 +382,7 @@ class grade_category extends grade_object { * Marks the category and course item as needing update - categories are always regraded. * @return void */ - function force_regrading() { + public function force_regrading() { $grade_item = $this->load_grade_item(); $grade_item->force_regrading(); } @@ -400,8 +402,8 @@ class grade_category extends grade_object { * 3. Aggregate these grades * 4. Save them in final grades of associated category grade item */ - function generate_grades($userid=null) { - global $CFG; + public function generate_grades($userid=null) { + global $CFG, $DB; $this->load_grade_item(); @@ -419,7 +421,7 @@ class grade_category extends grade_object { $sql = "SELECT * FROM {$CFG->prefix}grade_items WHERE id IN ($gis)"; - $items = get_records_sql($sql); + $items = $DB->get_records_sql($sql); } if ($userid) { @@ -439,12 +441,12 @@ class grade_category extends grade_object { ORDER BY g.userid"; // group the results by userid and aggregate the grades for this user - if ($rs = get_recordset_sql($sql)) { + if ($rs = $DB->get_recordset_sql($sql)) { $prevuser = 0; $grade_values = array(); $excluded = array(); $oldgrade = null; - while ($used = rs_fetch_next_record($rs)) { + foreach ($rs as $used) { if ($used->userid != $prevuser) { $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded); $prevuser = $used->userid; @@ -460,8 +462,8 @@ class grade_category extends grade_object { $oldgrade = $used; } } + $rs->close(); $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one - rs_close($rs); } return true; @@ -477,7 +479,7 @@ class grade_category extends grade_object { * @param bool $excluded * @return boolean (just plain return;) */ - function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) { + private function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) { global $CFG; if (empty($userid)) { //ignore first call @@ -581,7 +583,7 @@ class grade_category extends grade_object { /** * Internal function - aggregation maths. */ - function aggregate_values($grade_values, $items) { + private function aggregate_values($grade_values, $items) { switch ($this->aggregation) { case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies $num = count($grade_values); @@ -685,7 +687,7 @@ class grade_category extends grade_object { * @param bool $excluded * @return boolean (just plain return;) */ - function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) { + private function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) { // ungraded and exluded items are not used in aggregation foreach ($grade_values as $itemid=>$v) { if (is_null($v)) { @@ -745,7 +747,7 @@ class grade_category extends grade_object { * @param array $grade_values * @return array Limited grades. */ - function apply_limit_rules(&$grade_values) { + public function apply_limit_rules(&$grade_values) { arsort($grade_values, SORT_NUMERIC); if (!empty($this->droplow)) { for ($i = 0; $i < $this->droplow; $i++) { @@ -763,7 +765,7 @@ class grade_category extends grade_object { * Returns true if category uses special aggregation coeficient * @return boolean true if coeficient used */ - function is_aggregationcoef_used() { + private function is_aggregationcoef_used() { return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN or $this->aggregation == GRADE_AGGREGATE_SUM); @@ -777,7 +779,7 @@ class grade_category extends grade_object { * @param boolean $include_category_items as category children * @return array */ - function fetch_course_tree($courseid, $include_category_items=false) { + public static function fetch_course_tree($courseid, $include_category_items=false) { $course_category = grade_category::fetch_course_category($courseid); $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1, 'children'=>$course_category->get_children($include_category_items)); @@ -787,7 +789,7 @@ class grade_category extends grade_object { return grade_category::_fetch_course_tree_recursion($category_array, $sortorder); } - function _fetch_course_tree_recursion($category_array, &$sortorder) { + private function _fetch_course_tree_recursion($category_array, &$sortorder) { // update the sortorder in db if needed if ($category_array['object']->sortorder != $sortorder) { $category_array['object']->set_sortorder($sortorder); @@ -827,14 +829,15 @@ class grade_category extends grade_object { * as well as all levels (0). The elements are indexed by sort order. * @return array Array of child objects (grade_category and grade_item). */ - function get_children($include_category_items=false) { + public function get_children($include_category_items=false) { + global $DB; // This function must be as fast as possible ;-) // fetch all course grade items and categories into memory - we do not expect hundreds of these in course // we have to limit the number of queries though, because it will be used often in grade reports - $cats = get_records('grade_categories', 'courseid', $this->courseid); - $items = get_records('grade_items', 'courseid', $this->courseid); + $cats = $DB->get_records('grade_categories', array('courseid' => $this->courseid)); + $items = $DB->get_records('grade_items', array('courseid' => $this->courseid)); // init children array first foreach ($cats as $catid=>$cat) { @@ -881,7 +884,7 @@ class grade_category extends grade_object { //fix paths and depts static $recursioncounter = 0; // prevents infinite recursion $recursioncounter++; - if ($recursioncounter < 5) { + if ($recursioncounter < 5) { // fix paths and depths! $grade_category = new grade_category($cat, false); $grade_category->depth = 0; @@ -889,7 +892,7 @@ class grade_category extends grade_object { $grade_category->update('system'); return $this->get_children($include_category_items); } - } + } // prevent problems with duplicate sortorders in db $sortorder = $cat->sortorder; while(array_key_exists($sortorder, $cats[$cat->parent]->children)) { @@ -916,7 +919,7 @@ class grade_category extends grade_object { } - function _get_children_recursion($category) { + private function _get_children_recursion($category) { $children_array = array(); foreach($category->children as $sortorder=>$child) { @@ -951,7 +954,7 @@ class grade_category extends grade_object { * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item. * @return object Grade_item */ - function load_grade_item() { + public function load_grade_item() { if (empty($this->grade_item)) { $this->grade_item = $this->get_grade_item(); } @@ -963,7 +966,7 @@ class grade_category extends grade_object { * If no grade_item exists yet, create one. * @return object Grade_item */ - function get_grade_item() { + public function get_grade_item() { if (empty($this->id)) { debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set."); return false; @@ -1000,7 +1003,7 @@ class grade_category extends grade_object { * referenced record in the DB. * @return object Parent_category */ - function load_parent_category() { + public function load_parent_category() { if (empty($this->parent_category) && !empty($this->parent)) { $this->parent_category = $this->get_parent_category(); } @@ -1011,7 +1014,7 @@ class grade_category extends grade_object { * Uses $this->parent to instantiate and return a grade_category object. * @return object Parent_category */ - function get_parent_category() { + public function get_parent_category() { if (!empty($this->parent)) { $parent_category = new grade_category(array('id' => $this->parent)); return $parent_category; @@ -1025,10 +1028,11 @@ class grade_category extends grade_object { * when we do not know the exact type of an object. * @return string name */ - function get_name() { + public function get_name() { + global $DB; // For a course category, we return the course name if the fullname is set to '?' in the DB (empty in the category edit form) if (empty($this->parent) && $this->fullname == '?') { - $course = get_record('course', 'id', $this->courseid); + $course = $DB->get_record('course', array('id'=> $this->courseid)); return format_string($course->fullname); } else { return $this->fullname; @@ -1040,7 +1044,7 @@ class grade_category extends grade_object { * @param int parentid * @return boolean success */ - function set_parent($parentid, $source=null) { + public function set_parent($parentid, $source=null) { if ($this->parent == $parentid) { return true; } @@ -1075,7 +1079,7 @@ class grade_category extends grade_object { * @param int $userid Optional: to retrieve a single final grade * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade. */ - function get_final($userid=NULL) { + public function get_final($userid=NULL) { $this->load_grade_item(); return $this->grade_item->get_final($userid); } @@ -1085,7 +1089,7 @@ class grade_category extends grade_object { * grade_item, for cases where the object type is not known. * @return int Sort order */ - function get_sortorder() { + public function get_sortorder() { $this->load_grade_item(); return $this->grade_item->get_sortorder(); } @@ -1095,7 +1099,7 @@ class grade_category extends grade_object { * grade_item, for cases where the object type is not known. * @return string idnumber */ - function get_idnumber() { + public function get_idnumber() { $this->load_grade_item(); return $this->grade_item->get_idnumber(); } @@ -1106,7 +1110,7 @@ class grade_category extends grade_object { * @param int $sortorder * @return void */ - function set_sortorder($sortorder) { + public function set_sortorder($sortorder) { $this->load_grade_item(); $this->grade_item->set_sortorder($sortorder); } @@ -1115,7 +1119,7 @@ class grade_category extends grade_object { * Move this category after the given sortorder - does not change the parent * @param int $sortorder to place after */ - function move_after_sortorder($sortorder) { + public function move_after_sortorder($sortorder) { $this->load_grade_item(); $this->grade_item->move_after_sortorder($sortorder); } @@ -1124,7 +1128,7 @@ class grade_category extends grade_object { * Return true if this is the top most category that represents the total course grade. * @return boolean */ - function is_course_category() { + public function is_course_category() { $this->load_grade_item(); return $this->grade_item->is_course_item(); } @@ -1134,7 +1138,7 @@ class grade_category extends grade_object { * @static * @return object grade_category instance for course grade */ - function fetch_course_category($courseid) { + public function fetch_course_category($courseid) { if (empty($courseid)) { debugging('Missing course id!'); return false; @@ -1156,7 +1160,7 @@ class grade_category extends grade_object { * Is grading object editable? * @return boolean */ - function is_editable() { + public function is_editable() { return true; } @@ -1165,7 +1169,7 @@ class grade_category extends grade_object { * grade_item, for cases where the object type is not known. * @return boolean */ - function is_locked() { + public function is_locked() { $this->load_grade_item(); return $this->grade_item->is_locked(); } @@ -1178,7 +1182,7 @@ class grade_category extends grade_object { * @param boolean $refresh refresh grades when unlocking * @return boolean success if category locked (not all children mayb be locked though) */ - function set_locked($lockedstate, $cascade=false, $refresh=true) { + public function set_locked($lockedstate, $cascade=false, $refresh=true) { $this->load_grade_item(); $result = $this->grade_item->set_locked($lockedstate, $cascade, true); @@ -1209,7 +1213,7 @@ class grade_category extends grade_object { * grade_item. * @return boolean */ - function is_hidden() { + public function is_hidden() { $this->load_grade_item(); return $this->grade_item->is_hidden(); } @@ -1218,7 +1222,7 @@ class grade_category extends grade_object { * Check grade hidden status. Uses data from both grade item and grade. * @return boolean true if hiddenuntil, false if not */ - function is_hiddenuntil() { + public function is_hiddenuntil() { $this->load_grade_item(); return $this->grade_item->is_hiddenuntil(); } @@ -1230,7 +1234,7 @@ class grade_category extends grade_object { * @param boolean $cascade apply to child objects too * @return void */ - function set_hidden($hidden, $cascade=false) { + public function set_hidden($hidden, $cascade=false) { $this->load_grade_item(); $this->grade_item->set_hidden($hidden); if ($cascade) { @@ -1251,7 +1255,7 @@ class grade_category extends grade_object { * Applies default settings on this category * @return bool true if anything changed */ - function apply_default_settings() { + public function apply_default_settings() { global $CFG; foreach ($this->forceable as $property) { @@ -1268,7 +1272,7 @@ class grade_category extends grade_object { * Applies forced settings on this category * @return bool true if anything changed */ - function apply_forced_settings() { + public function apply_forced_settings() { global $CFG; $updated = false; @@ -1289,7 +1293,7 @@ class grade_category extends grade_object { * Notification of change in forced category settings. * @static */ - function updated_forced_settings() { + public static function updated_forced_settings() { global $CFG; $sql = "UPDATE {$CFG->prefix}grade_items SET needsupdate=1 WHERE itemtype='course' or itemtype='category'"; execute_sql($sql, false); diff --git a/lib/grade/grade_grade.php b/lib/grade/grade_grade.php index f367a846b1..f299e4b40c 100644 --- a/lib/grade/grade_grade.php +++ b/lib/grade/grade_grade.php @@ -31,13 +31,13 @@ class grade_grade extends grade_object { * The DB table. * @var string $table */ - var $table = 'grade_grades'; + public $table = 'grade_grades'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin', + public $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin', 'rawscaleid', 'usermodified', 'finalgrade', 'hidden', 'locked', 'locktime', 'exported', 'overridden', 'excluded', 'timecreated', 'timemodified'); @@ -45,109 +45,109 @@ class grade_grade extends grade_object { * Array of optional fields with default values (these should match db defaults) * @var array $optional_fields */ - var $optional_fields = array('feedback'=>null, 'feedbackformat'=>0, 'information'=>null, 'informationformat'=>0); + public $optional_fields = array('feedback'=>null, 'feedbackformat'=>0, 'information'=>null, 'informationformat'=>0); /** * The id of the grade_item this grade belongs to. * @var int $itemid */ - var $itemid; + public $itemid; /** * The grade_item object referenced by $this->itemid. * @var object $grade_item */ - var $grade_item; + public $grade_item; /** * The id of the user this grade belongs to. * @var int $userid */ - var $userid; + public $userid; /** * The grade value of this raw grade, if such was provided by the module. * @var float $rawgrade */ - var $rawgrade; + public $rawgrade; /** * The maximum allowable grade when this grade was created. * @var float $rawgrademax */ - var $rawgrademax = 100; + public $rawgrademax = 100; /** * The minimum allowable grade when this grade was created. * @var float $rawgrademin */ - var $rawgrademin = 0; + public $rawgrademin = 0; /** * id of the scale, if this grade is based on a scale. * @var int $rawscaleid */ - var $rawscaleid; + public $rawscaleid; /** * The userid of the person who last modified this grade. * @var int $usermodified */ - var $usermodified; + public $usermodified; /** * The final value of this grade. * @var float $finalgrade */ - var $finalgrade; + public $finalgrade; /** * 0 if visible, 1 always hidden or date not visible until * @var float $hidden */ - var $hidden = 0; + public $hidden = 0; /** * 0 not locked, date when the item was locked * @var float locked */ - var $locked = 0; + public $locked = 0; /** * 0 no automatic locking, date when to lock the grade automatically * @var float $locktime */ - var $locktime = 0; + public $locktime = 0; /** * Exported flag * @var boolean $exported */ - var $exported = 0; + public $exported = 0; /** * Overridden flag * @var boolean $overridden */ - var $overridden = 0; + public $overridden = 0; /** * Grade excluded from aggregation functions * @var boolean $excluded */ - var $excluded = 0; + public $excluded = 0; /** * TODO: HACK: create a new field datesubmitted - the date of submission if any * @var boolean $timecreated */ - var $timecreated = null; + public $timecreated = null; /** * TODO: HACK: create a new field dategraded - the date of grading * @var boolean $timemodified */ - var $timemodified = null; + public $timemodified = null; /** @@ -157,7 +157,8 @@ class grade_grade extends grade_object { * @param bool $include_missing include grades that do not exist yet * @return array userid=>grade_grade array */ - function fetch_users_grades($grade_item, $userids, $include_missing=true) { + public function fetch_users_grades($grade_item, $userids, $include_missing=true) { + global $DB; // hmm, there might be a problem with length of sql query // if there are too many users requested - we might run out of memory anyway @@ -172,7 +173,7 @@ class grade_grade extends grade_object { $user_ids_cvs = implode(',', $userids); $result = array(); - if ($grade_records = get_records_select('grade_grades', "itemid={$grade_item->id} AND userid IN ($user_ids_cvs)")) { + if ($grade_records = $DB->get_records_select('grade_grades', "itemid={$grade_item->id} AND userid IN ($user_ids_cvs)")) { foreach ($grade_records as $record) { $result[$record->userid] = new grade_grade($record, false); } @@ -195,7 +196,7 @@ class grade_grade extends grade_object { * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access. * @return object grade_item. */ - function load_grade_item() { + public function load_grade_item() { if (empty($this->itemid)) { debugging('Missing itemid'); $this->grade_item = null; @@ -217,7 +218,7 @@ class grade_grade extends grade_object { * Is grading object editable? * @return boolean */ - function is_editable() { + public function is_editable() { if ($this->is_locked()) { return false; } @@ -238,7 +239,7 @@ class grade_grade extends grade_object { * * @return boolean true if locked, false if not */ - function is_locked() { + public function is_locked() { $this->load_grade_item(); if (empty($this->grade_item)) { return !empty($this->locked); @@ -251,7 +252,7 @@ class grade_grade extends grade_object { * Checks if grade overridden * @return boolean */ - function is_overridden() { + public function is_overridden() { return !empty($this->overridden); } @@ -260,7 +261,7 @@ class grade_grade extends grade_object { * might be null if not submitted. * @return int */ - function get_datesubmitted() { + public function get_datesubmitted() { //TODO: HACK - create new fields in 2.0 return $this->timecreated; } @@ -270,7 +271,7 @@ class grade_grade extends grade_object { * might be null if no grade present. * @return int */ - function get_dategraded() { + public function get_dategraded() { //TODO: HACK - create new fields in 2.0 if (is_null($this->finalgrade) and is_null($this->feedback)) { return null; // no grade == no date @@ -287,7 +288,7 @@ class grade_grade extends grade_object { * @param boolean $refresh refresh grades from external activities if needed * @return boolean true is db state changed */ - function set_overridden($state, $refresh = true) { + public function set_overridden($state, $refresh = true) { if (empty($this->overridden) and $state) { $this->overridden = time(); $this->update(); @@ -311,7 +312,7 @@ class grade_grade extends grade_object { * Checks if grade excluded from aggregation functions * @return boolean */ - function is_excluded() { + public function is_excluded() { return !empty($this->excluded); } @@ -320,7 +321,7 @@ class grade_grade extends grade_object { * @param boolean $state requested excluded state * @return boolean true is db state changed */ - function set_excluded($state) { + public function set_excluded($state) { if (empty($this->excluded) and $state) { $this->excluded = time(); $this->update(); @@ -342,7 +343,7 @@ class grade_grade extends grade_object { * @param boolean $refresh refresh grades when unlocking * @return boolean true if sucessful, false if can not set new lock state for grade */ - function set_locked($lockedstate, $cascade=false, $refresh=true) { + public function set_locked($lockedstate, $cascade=false, $refresh=true) { $this->load_grade_item(); if ($lockedstate) { @@ -380,20 +381,20 @@ class grade_grade extends grade_object { * @param array $items array of all grade item ids * @return void */ - function check_locktime_all($items) { - global $CFG; + public function check_locktime_all($items) { + global $CFG, $DB; $items_sql = implode(',', $items); $now = time(); // no rounding needed, this is not supposed to be called every 10 seconds - if ($rs = get_recordset_select('grade_grades', "itemid IN ($items_sql) AND locked = 0 AND locktime > 0 AND locktime < $now")) { - while ($grade = rs_fetch_next_record($rs)) { + if ($rs = $DB->get_recordset_select('grade_grades', "itemid IN ($items_sql) AND locked = 0 AND locktime > 0 AND locktime < $now")) { + foreach ($rs as $grade) { $grade_grade = new grade_grade($grade, false); $grade_grade->locked = time(); $grade_grade->update('locktime'); } - rs_close($rs); + $rs->close(); } } @@ -403,7 +404,7 @@ class grade_grade extends grade_object { * @param int $locktime timestamp for lock to activate * @return void */ - function set_locktime($locktime) { + public function set_locktime($locktime) { $this->locktime = $locktime; $this->update(); } @@ -413,7 +414,7 @@ class grade_grade extends grade_object { * * @return int $locktime timestamp for lock to activate */ - function get_locktime() { + public function get_locktime() { $this->load_grade_item(); $item_locktime = $this->grade_item->get_locktime(); @@ -430,20 +431,20 @@ class grade_grade extends grade_object { * Check grade hidden status. Uses data from both grade item and grade. * @return boolean true if hidden, false if not */ - function is_hidden() { + public function is_hidden() { $this->load_grade_item(); if (empty($this->grade_item)) { return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()); } else { return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()) or $this->grade_item->is_hidden(); - } + } } /** * Check grade hidden status. Uses data from both grade item and grade. * @return boolean true if hiddenuntil, false if not */ - function is_hiddenuntil() { + public function is_hiddenuntil() { $this->load_grade_item(); if ($this->hidden == 1 or $this->grade_item->hidden == 1) { @@ -461,7 +462,7 @@ class grade_grade extends grade_object { * Check grade hidden status. Uses data from both grade item and grade. * @return int 0 means visible, 1 hidden always, timestamp hidden until */ - function get_hidden() { + public function get_hidden() { $this->load_grade_item(); $item_hidden = $this->grade_item->get_hidden(); @@ -490,7 +491,7 @@ class grade_grade extends grade_object { * @param boolean $cascade ignored * @param int $hidden new hidden status */ - function set_hidden($hidden, $cascade=false) { + public function set_hidden($hidden, $cascade=false) { $this->hidden = $hidden; $this->update(); } @@ -502,7 +503,7 @@ class grade_grade extends grade_object { * @param array $params associative arrays varname=>value * @return object grade_grade instance or false if none found. */ - function fetch($params) { + public static function fetch($params) { return grade_object::fetch_helper('grade_grades', 'grade_grade', $params); } @@ -513,7 +514,7 @@ class grade_grade extends grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_grade insatnces or false if none found. */ - function fetch_all($params) { + public static function fetch_all($params) { return grade_object::fetch_all_helper('grade_grades', 'grade_grade', $params); } @@ -530,7 +531,7 @@ class grade_grade extends grade_object { * @param float $target_max * @return float Converted value */ - function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) { + public static function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) { if (is_null($rawgrade)) { return null; } @@ -556,7 +557,7 @@ class grade_grade extends grade_object { * @param array $items $grade_items array of grade items, & used for better internal caching * @return array */ - function get_hiding_affected(&$grade_grades, &$grade_items) { + public static function get_hiding_affected(&$grade_grades, &$grade_items) { global $CFG; if (count($grade_grades) !== count($grade_items)) { @@ -692,7 +693,7 @@ class grade_grade extends grade_object { * @param object $grade_item An optional grade_item of which gradepass value we can use, saves having to load the grade_grade's grade_item * @return boolean */ - function is_passed($grade_item = null) { + public function is_passed($grade_item = null) { if (empty($grade_item)) { if (!isset($this->grade_item)) { $this->load_grade_item(); @@ -715,9 +716,9 @@ class grade_grade extends grade_object { return $this->finalgrade >= $this->grade_item->gradepass; } - function insert($source=null) { - // TODO: dategraded hack - do not update times, they are used for submission and grading - //$this->timecreated = $this->timemodified = time(); + public function insert($source=null) { + // TODO: dategraded hack - do not update times, they are used for submission and grading + //$this->timecreated = $this->timemodified = time(); return parent::insert($source); } @@ -727,7 +728,7 @@ class grade_grade extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return boolean success */ - function update($source=null) { + public function update($source=null) { $this->rawgrade = grade_floatval($this->rawgrade); $this->finalgrade = grade_floatval($this->finalgrade); $this->rawgrademin = grade_floatval($this->rawgrademin); diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index a9c289a958..f43fc539fa 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -34,13 +34,13 @@ class grade_item extends grade_object { * DB Table (used by grade_object). * @var string $table */ - var $table = 'grade_items'; + public $table = 'grade_items'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'courseid', 'categoryid', 'itemname', 'itemtype', 'itemmodule', 'iteminstance', + public $required_fields = array('id', 'courseid', 'categoryid', 'itemname', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber', 'iteminfo', 'idnumber', 'calculation', 'gradetype', 'grademax', 'grademin', 'scaleid', 'outcomeid', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'sortorder', 'display', 'decimals', 'hidden', 'locked', 'locktime', 'needsupdate', 'timecreated', @@ -50,198 +50,198 @@ class grade_item extends grade_object { * The course this grade_item belongs to. * @var int $courseid */ - var $courseid; + public $courseid; /** * The category this grade_item belongs to (optional). * @var int $categoryid */ - var $categoryid; + public $categoryid; /** * The grade_category object referenced $this->iteminstance (itemtype must be == 'category' or == 'course' in that case). * @var object $item_category */ - var $item_category; + public $item_category; /** * The grade_category object referenced by $this->categoryid. * @var object $parent_category */ - var $parent_category; + public $parent_category; /** * The name of this grade_item (pushed by the module). * @var string $itemname */ - var $itemname; + public $itemname; /** * e.g. 'category', 'course' and 'mod', 'blocks', 'import', etc... * @var string $itemtype */ - var $itemtype; + public $itemtype; /** * The module pushing this grade (e.g. 'forum', 'quiz', 'assignment' etc). * @var string $itemmodule */ - var $itemmodule; + public $itemmodule; /** * ID of the item module * @var int $iteminstance */ - var $iteminstance; + public $iteminstance; /** * Number of the item in a series of multiple grades pushed by an activity. * @var int $itemnumber */ - var $itemnumber; + public $itemnumber; /** * Info and notes about this item. * @var string $iteminfo */ - var $iteminfo; + public $iteminfo; /** * Arbitrary idnumber provided by the module responsible. * @var string $idnumber */ - var $idnumber; + public $idnumber; /** * Calculation string used for this item. * @var string $calculation */ - var $calculation; + public $calculation; /** * Indicates if we already tried to normalize the grade calculation formula. * This flag helps to minimize db access when broken formulas used in calculation. * @var boolean */ - var $calculation_normalized; + public $calculation_normalized; /** * Math evaluation object */ - var $formula; + public $formula; /** * The type of grade (0 = none, 1 = value, 2 = scale, 3 = text) * @var int $gradetype */ - var $gradetype = GRADE_TYPE_VALUE; + public $gradetype = GRADE_TYPE_VALUE; /** * Maximum allowable grade. * @var float $grademax */ - var $grademax = 100; + public $grademax = 100; /** * Minimum allowable grade. * @var float $grademin */ - var $grademin = 0; + public $grademin = 0; /** * id of the scale, if this grade is based on a scale. * @var int $scaleid */ - var $scaleid; + public $scaleid; /** * A grade_scale object (referenced by $this->scaleid). * @var object $scale */ - var $scale; + public $scale; /** * The id of the optional grade_outcome associated with this grade_item. * @var int $outcomeid */ - var $outcomeid; + public $outcomeid; /** * The grade_outcome this grade is associated with, if applicable. * @var object $outcome */ - var $outcome; + public $outcome; /** * grade required to pass. (grademin <= gradepass <= grademax) * @var float $gradepass */ - var $gradepass = 0; + public $gradepass = 0; /** * Multiply all grades by this number. * @var float $multfactor */ - var $multfactor = 1.0; + public $multfactor = 1.0; /** * Add this to all grades. * @var float $plusfactor */ - var $plusfactor = 0; + public $plusfactor = 0; /** * Aggregation coeficient used for weighted averages * @var float $aggregationcoef */ - var $aggregationcoef = 0; + public $aggregationcoef = 0; /** * Sorting order of the columns. * @var int $sortorder */ - var $sortorder = 0; + public $sortorder = 0; /** * Display type of the grades (Real, Percentage, Letter, or default). * @var int $display */ - var $display = GRADE_DISPLAY_TYPE_DEFAULT; + public $display = GRADE_DISPLAY_TYPE_DEFAULT; /** * The number of digits after the decimal point symbol. Applies only to REAL and PERCENTAGE grade display types. * @var int $decimals */ - var $decimals = null; + public $decimals = null; /** * 0 if visible, 1 always hidden or date not visible until * @var int $hidden */ - var $hidden = 0; + public $hidden = 0; /** * Grade item lock flag. Empty if not locked, locked if any value present, usually date when item was locked. Locking prevents updating. * @var int $locked */ - var $locked = 0; + public $locked = 0; /** * Date after which the grade will be locked. Empty means no automatic locking. * @var int $locktime */ - var $locktime = 0; + public $locktime = 0; /** * If set, the whole column will be recalculated, then this flag will be switched off. * @var boolean $needsupdate */ - var $needsupdate = 1; + public $needsupdate = 1; /** * Cached dependson array */ - var $dependson_cache = null; + public $dependson_cache = null; /** * In addition to update() as defined in grade_object, handle the grade_outcome and grade_scale objects. @@ -250,7 +250,7 @@ class grade_item extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return boolean success */ - function update($source=null) { + public function update($source=null) { // reset caches $this->dependson_cache = null; @@ -283,7 +283,7 @@ class grade_item extends grade_object { * This assumes that this object has an id number and a matching record in DB. If not, it will return false. * @return boolean */ - function qualifies_for_regrading() { + public function qualifies_for_regrading() { if (empty($this->id)) { return false; } @@ -317,7 +317,7 @@ class grade_item extends grade_object { * @param array $params associative arrays varname=>value * @return object grade_item instance or false if none found. */ - function fetch($params) { + public static function fetch($params) { return grade_object::fetch_helper('grade_items', 'grade_item', $params); } @@ -328,7 +328,7 @@ class grade_item extends grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_item insatnces or false if none found. */ - function fetch_all($params) { + public static function fetch_all($params) { return grade_object::fetch_all_helper('grade_items', 'grade_item', $params); } @@ -337,7 +337,7 @@ class grade_item extends grade_object { * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return boolean success */ - function delete($source=null) { + public function delete($source=null) { $this->delete_all_grades($source); return parent::delete($source); } @@ -347,7 +347,7 @@ class grade_item extends grade_object { * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return boolean success */ - function delete_all_grades($source=null) { + public function delete_all_grades($source=null) { if (!$this->is_course_item()) { $this->force_regrading(); } @@ -366,8 +366,8 @@ class grade_item extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return int PK ID if successful, false otherwise */ - function insert($source=null) { - global $CFG; + public function insert($source=null) { + global $CFG, $DB; if (empty($this->courseid)) { print_error('cannotinsertgrade'); @@ -384,7 +384,7 @@ class grade_item extends grade_object { } // always place the new items at the end, move them after insert if needed - $last_sortorder = get_field_select('grade_items', 'MAX(sortorder)', "courseid = {$this->courseid}"); + $last_sortorder = $DB->get_field_select('grade_items', 'MAX(sortorder)', "courseid = {$this->courseid}"); if (!empty($last_sortorder)) { $this->sortorder = $last_sortorder + 1; } else { @@ -421,7 +421,8 @@ class grade_item extends grade_object { * @param string $idnumber (without magic quotes) * @return boolean success */ - function add_idnumber($idnumber) { + public function add_idnumber($idnumber) { + global $DB; if (!empty($this->idnumber)) { return false; } @@ -433,7 +434,7 @@ class grade_item extends grade_object { if (!empty($cm->idnumber)) { return false; } - if (set_field('course_modules', 'idnumber', addslashes($idnumber), 'id', $cm->id)) { + if ($DB->set_field('course_modules', 'idnumber', addslashes($idnumber), array('id' => $cm->id))) { $this->idnumber = $idnumber; return $this->update(); } @@ -453,7 +454,7 @@ class grade_item extends grade_object { * @param int $userid * @return boolean Locked state */ - function is_locked($userid=NULL) { + public function is_locked($userid=NULL) { if (!empty($this->locked)) { return true; } @@ -475,7 +476,7 @@ class grade_item extends grade_object { * @param boolean $refresh refresh grades when unlocking * @return boolean true if grade_item all grades updated, false if at least one update fails */ - function set_locked($lockedstate, $cascade=false, $refresh=true) { + public function set_locked($lockedstate, $cascade=false, $refresh=true) { if ($lockedstate) { /// setting lock if ($this->needsupdate) { @@ -527,7 +528,7 @@ class grade_item extends grade_object { /** * Lock the grade if needed - make sure this is called only when final grades are valid */ - function check_locktime() { + public function check_locktime() { if (!empty($this->locked)) { return; // already locked } @@ -544,7 +545,7 @@ class grade_item extends grade_object { * @param int $locktime timestamp for lock to activate * @return void */ - function set_locktime($locktime) { + public function set_locktime($locktime) { $this->locktime = $locktime; $this->update(); } @@ -554,7 +555,7 @@ class grade_item extends grade_object { * * @return int $locktime timestamp for lock to activate */ - function get_locktime() { + public function get_locktime() { return $this->locktime; } @@ -562,7 +563,7 @@ class grade_item extends grade_object { * Returns the hidden state of this grade_item * @return boolean hidden state */ - function is_hidden() { + public function is_hidden() { return ($this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time())); } @@ -570,7 +571,7 @@ class grade_item extends grade_object { * Check grade hidden status. Uses data from both grade item and grade. * @return boolean true if hiddenuntil, false if not */ - function is_hiddenuntil() { + public function is_hiddenuntil() { return $this->hidden > 1; } @@ -578,7 +579,7 @@ class grade_item extends grade_object { * Check grade item hidden status. * @return int 0 means visible, 1 hidden always, timestamp hidden until */ - function get_hidden() { + public function get_hidden() { return $this->hidden; } @@ -588,7 +589,7 @@ class grade_item extends grade_object { * @param boolean $cascade apply to child objects too * @return void */ - function set_hidden($hidden, $cascade=false) { + public function set_hidden($hidden, $cascade=false) { $this->hidden = $hidden; $this->update(); @@ -606,19 +607,20 @@ class grade_item extends grade_object { * Returns the number of grades that are hidden. * @param return int Number of hidden grades */ - function has_hidden_grades($groupsql="", $groupwheresql="") { - global $CFG; - return get_field_sql("SELECT COUNT(*) FROM {$CFG->prefix}grade_grades g LEFT JOIN " + public function has_hidden_grades($groupsql="", $groupwheresql="") { + global $CFG, $DB; + return $DB->get_field_sql("SELECT COUNT(*) FROM {$CFG->prefix}grade_grades g LEFT JOIN " ."{$CFG->prefix}user u ON g.userid = u.id $groupsql WHERE itemid = $this->id AND hidden = 1 $groupwheresql"); } /** * Mark regrading as finished successfully. */ - function regrading_finished() { + public function regrading_finished() { + global $DB; $this->needsupdate = 0; //do not use $this->update() because we do not want this logged in grade_item_history - set_field('grade_items', 'needsupdate', 0, 'id', $this->id); + $DB->set_field('grade_items', 'needsupdate', 0, array('id' => $this->id)); } /** @@ -630,8 +632,8 @@ class grade_item extends grade_object { * * @return boolean true if ok, error string otherwise */ - function regrade_final_grades($userid=null) { - global $CFG; + public function regrade_final_grades($userid=null) { + global $CFG, $DB; // locked grade items already have correct final grades if ($this->is_locked()) { @@ -675,12 +677,12 @@ class grade_item extends grade_object { $grade_inst = new grade_grade(); $fields = implode(',', $grade_inst->required_fields); if ($userid) { - $rs = get_recordset_select('grade_grades', "itemid={$this->id} AND userid=$userid", '', $fields); + $rs = $DB->get_recordset_select('grade_grades', "itemid={$this->id} AND userid=$userid", null, '', $fields); } else { - $rs = get_recordset('grade_grades', 'itemid', $this->id, '', $fields); + $rs = $DB->get_recordset('grade_grades', array('itemid' => $this->id), '', $fields); } if ($rs) { - while ($grade_record = rs_fetch_next_record($rs)) { + foreach ($rs as $grade_record) { $grade = new grade_grade($grade_record, false); if (!empty($grade_record->locked) or !empty($grade_record->overridden)) { @@ -696,7 +698,7 @@ class grade_item extends grade_object { } } } - rs_close($rs); + $rs->close(); } return $result; @@ -710,7 +712,7 @@ class grade_item extends grade_object { * @param float $rawmax original rawmax * @return mixed */ - function adjust_raw_grade($rawgrade, $rawmin, $rawmax) { + public function adjust_raw_grade($rawgrade, $rawmin, $rawmax) { if (is_null($rawgrade)) { return null; } @@ -773,7 +775,7 @@ class grade_item extends grade_object { * Sets this grade_item's needsupdate to true. Also marks the course item as needing update. * @return void */ - function force_regrading() { + public function force_regrading() { global $DB; $this->needsupdate = 1; //mark this item and course item only - categories and calculated items are always regraded @@ -787,7 +789,7 @@ class grade_item extends grade_object { * if this item's scaleid variable is set. * @return object grade_scale or null if no scale used */ - function load_scale() { + public function load_scale() { if ($this->gradetype != GRADE_TYPE_SCALE) { $this->scaleid = null; } @@ -821,7 +823,7 @@ class grade_item extends grade_object { * if this item's outcomeid variable is set. * @return object grade_outcome */ - function load_outcome() { + public function load_outcome() { if (!empty($this->outcomeid)) { $this->outcome = grade_outcome::fetch(array('id'=>$this->outcomeid)); } @@ -834,7 +836,7 @@ class grade_item extends grade_object { * * @return mixed grade_category object if applicable, false if course item */ - function get_parent_category() { + public function get_parent_category() { if ($this->is_category_item() or $this->is_course_item()) { return $this->get_item_category(); @@ -848,7 +850,7 @@ class grade_item extends grade_object { * from the DB and assigns it to $this->parent_category. It also returns the object. * @return object Grade_category */ - function load_parent_category() { + public function load_parent_category() { if (empty($this->parent_category->id)) { $this->parent_category = $this->get_parent_category(); } @@ -860,7 +862,7 @@ class grade_item extends grade_object { * * @return mixed grade_category object if applicable, false otherwise */ - function get_item_category() { + public function get_item_category() { if (!$this->is_course_item() and !$this->is_category_item()) { return false; } @@ -872,7 +874,7 @@ class grade_item extends grade_object { * from the DB and assigns it to $this->item_category. It also returns the object. * @return object Grade_category */ - function load_item_category() { + public function load_item_category() { if (empty($this->category->id)) { $this->item_category = $this->get_item_category(); } @@ -883,7 +885,7 @@ class grade_item extends grade_object { * Is the grade item associated with category? * @return boolean */ - function is_category_item() { + public function is_category_item() { return ($this->itemtype == 'category'); } @@ -891,7 +893,7 @@ class grade_item extends grade_object { * Is the grade item associated with course? * @return boolean */ - function is_course_item() { + public function is_course_item() { return ($this->itemtype == 'course'); } @@ -899,7 +901,7 @@ class grade_item extends grade_object { * Is this a manualy graded item? * @return boolean */ - function is_manual_item() { + public function is_manual_item() { return ($this->itemtype == 'manual'); } @@ -907,7 +909,7 @@ class grade_item extends grade_object { * Is this an outcome item? * @return boolean */ - function is_outcome_item() { + public function is_outcome_item() { return !empty($this->outcomeid); } @@ -915,7 +917,7 @@ class grade_item extends grade_object { * Is the grade item external - associated with module, plugin or something else? * @return boolean */ - function is_external_item() { + public function is_external_item() { return ($this->itemtype == 'mod'); } @@ -923,7 +925,7 @@ class grade_item extends grade_object { * Is the grade item overridable * @return boolean */ - function is_overridable_item() { + public function is_overridable_item() { return !$this->is_outcome_item() and ($this->is_external_item() or $this->is_calculated() or $this->is_course_item() or $this->is_category_item()); } @@ -931,7 +933,7 @@ class grade_item extends grade_object { * Is the grade item feedback overridable * @return boolean */ - function is_overridable_item_feedback() { + public function is_overridable_item_feedback() { return !$this->is_outcome_item() and $this->is_external_item(); } @@ -939,7 +941,7 @@ class grade_item extends grade_object { * Returns true if grade items uses raw grades * @return boolean */ - function is_raw_used() { + public function is_raw_used() { return ($this->is_external_item() and !$this->is_calculated() and !$this->is_outcome_item()); } @@ -948,7 +950,7 @@ class grade_item extends grade_object { * @param int $courseid * @return course item object */ - function fetch_course_item($courseid) { + public function fetch_course_item($courseid) { if ($course_item = grade_item::fetch(array('courseid'=>$courseid, 'itemtype'=>'course'))) { return $course_item; } @@ -962,7 +964,7 @@ class grade_item extends grade_object { * Is grading object editable? * @return boolean */ - function is_editable() { + public function is_editable() { return true; } @@ -970,7 +972,7 @@ class grade_item extends grade_object { * Checks if grade calculated. Returns this object's calculation. * @return boolean true if grade item calculated. */ - function is_calculated() { + public function is_calculated() { if (empty($this->calculation)) { return false; } @@ -993,7 +995,7 @@ class grade_item extends grade_object { * Returns calculation string if grade calculated. * @return mixed string if calculation used, null if not */ - function get_calculation() { + public function get_calculation() { if ($this->is_calculated()) { return grade_item::denormalize_formula($this->calculation, $this->courseid); @@ -1009,7 +1011,7 @@ class grade_item extends grade_object { * @param string $formula string representation of formula used for calculation * @return boolean success */ - function set_calculation($formula) { + public function set_calculation($formula) { $this->calculation = grade_item::normalize_formula($formula, $this->courseid); $this->calculation_normalized = true; return $this->update(); @@ -1021,7 +1023,7 @@ class grade_item extends grade_object { * @param string $formula * @return string denormalized string */ - function denormalize_formula($formula, $courseid) { + public static function denormalize_formula($formula, $courseid) { if (empty($formula)) { return ''; } @@ -1047,7 +1049,7 @@ class grade_item extends grade_object { * @param string $formula * @return string normalized string */ - function normalize_formula($formula, $courseid) { + public static function normalize_formula($formula, $courseid) { $formula = trim($formula); if (empty($formula)) { @@ -1070,14 +1072,15 @@ class grade_item extends grade_object { * @param int $userid Optional: to retrieve a single final grade * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade. */ - function get_final($userid=NULL) { + public function get_final($userid=NULL) { + global $DB; if ($userid) { - if ($user = get_record('grade_grades', 'itemid', $this->id, 'userid', $userid)) { + if ($user = $DB->get_record('grade_grades', array('itemid' => $this->id, 'userid' => $userid))) { return $user; } } else { - if ($grades = get_records('grade_grades', 'itemid', $this->id)) { + if ($grades = $DB->get_records('grade_grades', array('itemid' => $this->id))) { //TODO: speed up with better SQL $result = array(); foreach ($grades as $grade) { @@ -1095,7 +1098,7 @@ class grade_item extends grade_object { * @param int $userid * @return object grade_grade object instance */ - function get_grade($userid, $create=true) { + public function get_grade($userid, $create=true) { if (empty($this->id)) { debugging('Can not use before insert'); return false; @@ -1114,7 +1117,7 @@ class grade_item extends grade_object { * grade_category, for cases where the object type is not know. * @return int Sort order */ - function get_sortorder() { + public function get_sortorder() { return $this->sortorder; } @@ -1123,7 +1126,7 @@ class grade_item extends grade_object { * grade_category, for cases where the object type is not know. * @return string idnumber */ - function get_idnumber() { + public function get_idnumber() { return $this->idnumber; } @@ -1132,7 +1135,7 @@ class grade_item extends grade_object { * grade_category, for cases where the object type is not know. * @return string idnumber */ - function get_grade_item() { + public function get_grade_item() { return $this; } @@ -1142,7 +1145,7 @@ class grade_item extends grade_object { * @param int $sortorder * @return void */ - function set_sortorder($sortorder) { + public function set_sortorder($sortorder) { if ($this->sortorder == $sortorder) { return; } @@ -1150,7 +1153,7 @@ class grade_item extends grade_object { $this->update(); } - function move_after_sortorder($sortorder) { + public function move_after_sortorder($sortorder) { global $CFG; //make some room first @@ -1167,7 +1170,7 @@ class grade_item extends grade_object { * when we do not know the exact type of an object. * @return string name */ - function get_name() { + public function get_name() { if (!empty($this->itemname)) { // MDL-10557 return format_string($this->itemname); @@ -1188,7 +1191,7 @@ class grade_item extends grade_object { * @param int $parentid * @return boolean success; */ - function set_parent($parentid) { + public function set_parent($parentid) { if ($this->is_course_item() or $this->is_category_item()) { print_error('cannotsetparentforcatoritem'); } @@ -1216,8 +1219,8 @@ class grade_item extends grade_object { * @param bool $reset_cache * @return array of grade_item ids this one depends on */ - function depends_on($reset_cache=false) { - global $CFG; + public function depends_on($reset_cache=false) { + global $CFG, $DB; if ($reset_cache) { $this->dependson_cache = null; @@ -1289,7 +1292,7 @@ class grade_item extends grade_object { $outcomes_sql"; } - if ($children = get_records_sql($sql)) { + if ($children = $DB->get_records_sql($sql)) { $this->dependson_cache = array_keys($children); return $this->dependson_cache; } else { @@ -1307,14 +1310,15 @@ class grade_item extends grade_object { * Refetch grades from modules, plugins. * @param int $userid optional, one user only */ - function refresh_grades($userid=0) { + public function refresh_grades($userid=0) { + global $DB; if ($this->itemtype == 'mod') { if ($this->is_outcome_item()) { //nothing to do return; } - if (!$activity = get_record($this->itemmodule, 'id', $this->iteminstance)) { + if (!$activity = $DB->get_record($this->itemmodule, array('id' => $this->iteminstance))) { debugging("Can not find $this->itemmodule activity with id $this->iteminstance"); return; } @@ -1345,7 +1349,7 @@ class grade_item extends grade_object { * @param int $feedbackformat * @return boolean success */ - function update_final_grade($userid, $finalgrade=false, $source=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE, $usermodified=null) { + public function update_final_grade($userid, $finalgrade=false, $source=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE, $usermodified=null) { global $USER, $CFG; $result = true; @@ -1465,7 +1469,7 @@ class grade_item extends grade_object { * @param object $grade object - usefull for bulk upgrades * @return boolean success */ - function update_raw_grade($userid, $rawgrade=false, $source=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE, $usermodified=null, $dategraded=null, $datesubmitted=null, $grade=null) { + public function update_raw_grade($userid, $rawgrade=false, $source=NULL, $feedback=false, $feedbackformat=FORMAT_MOODLE, $usermodified=null, $dategraded=null, $datesubmitted=null, $grade=null) { global $USER; $result = true; @@ -1601,8 +1605,8 @@ class grade_item extends grade_object { * The parameters are taken from final grades of grade items in current course only. * @return boolean false if error */ - function compute($userid=null) { - global $CFG; + public function compute($userid=null) { + global $CFG, $DB; if (!$this->is_calculated()) { return false; @@ -1622,7 +1626,7 @@ class grade_item extends grade_object { LEFT OUTER JOIN {$CFG->prefix}grade_grades g ON (g.userid = go.userid AND g.itemid = $this->id) WHERE gi.id <> $this->id AND g.id IS NULL"; - if ($missing = get_records_sql($sql)) { + if ($missing = $DB->get_records_sql($sql)) { foreach ($missing as $m) { $grade = new grade_grade(array('itemid'=>$this->id, 'userid'=>$m->userid), false); $grade->grade_item =& $this; @@ -1662,11 +1666,11 @@ class grade_item extends grade_object { $return = true; // group the grades by userid and use formula on the group - if ($rs = get_recordset_sql($sql)) { + if ($rs = $DB->get_recordset_sql($sql)) { $prevuser = 0; $grade_records = array(); $oldgrade = null; - while ($used = rs_fetch_next_record($rs)) { + foreach ($rs as $used) { if ($used->userid != $prevuser) { if (!$this->use_formula($prevuser, $grade_records, $useditems, $oldgrade)) { $return = false; @@ -1680,11 +1684,11 @@ class grade_item extends grade_object { } $grade_records['gi'.$used->itemid] = $used->finalgrade; } + $rs->close(); if (!$this->use_formula($prevuser, $grade_records, $useditems, $oldgrade)) { $return = false; } } - rs_close($rs); return $return; } @@ -1692,7 +1696,7 @@ class grade_item extends grade_object { /** * internal function - does the final grade calculation */ - function use_formula($userid, $params, $useditems, $oldgrade) { + public function use_formula($userid, $params, $useditems, $oldgrade) { if (empty($userid)) { return true; } @@ -1766,8 +1770,8 @@ class grade_item extends grade_object { * @param string $formula * @return boolean true if calculation possible, false otherwise */ - function validate_formula($formulastr) { - global $CFG; + public function validate_formula($formulastr) { + global $CFG, $DB; require_once($CFG->libdir.'/mathslib.php'); $formulastr = grade_item::normalize_formula($formulastr, $this->courseid); @@ -1810,7 +1814,7 @@ class grade_item extends grade_object { FROM {$CFG->prefix}grade_items gi WHERE gi.id IN ($gis) and gi.courseid={$this->courseid}"; // from the same course only! - if (!$grade_items = get_records_sql($sql)) { + if (!$grade_items = $DB->get_records_sql($sql)) { $grade_items = array(); } } @@ -1843,7 +1847,7 @@ class grade_item extends grade_object { * Returns the value of the display type. It can be set at 3 levels: grade_item, course setting and site. The lowest level overrides the higher ones. * @return int Display type */ - function get_displaytype() { + public function get_displaytype() { global $CFG; if ($this->display == GRADE_DISPLAY_TYPE_DEFAULT) { @@ -1858,7 +1862,7 @@ class grade_item extends grade_object { * Returns the value of the decimals field. It can be set at 3 levels: grade_item, course setting and site. The lowest level overrides the higher ones. * @return int Decimals (0 - 5) */ - function get_decimals() { + public function get_decimals() { global $CFG; if (is_null($this->decimals)) { diff --git a/lib/grade/grade_object.php b/lib/grade/grade_object.php index 0525c79caf..fdedb135f1 100644 --- a/lib/grade/grade_object.php +++ b/lib/grade/grade_object.php @@ -27,37 +27,39 @@ * An abstract object that holds methods and attributes common to all grade_* objects defined here. * @abstract */ -class grade_object { +abstract class grade_object { + public $table; + /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'timecreated', 'timemodified'); + public $required_fields = array('id', 'timecreated', 'timemodified'); /** * Array of optional fields with default values - usually long text information that is not always needed. * If you want to create an instance without optional fields use: new grade_object($only_required_fields, false); * @var array $optional_fields */ - var $optional_fields = array(); + public $optional_fields = array(); /** * The PK. * @var int $id */ - var $id; + public $id; /** * The first time this grade_object was created. * @var int $timecreated */ - var $timecreated; + public $timecreated; /** * The last time this grade_object was modified. * @var int $timemodified */ - var $timemodified; + public $timemodified; /** * Constructor. Optionally (and by default) attempts to fetch corresponding row from DB. @@ -65,7 +67,7 @@ class grade_object { * @param boolean $fetch Whether to fetch corresponding row from DB or not, * optional fields might not be defined if false used */ - function grade_object($params=NULL, $fetch=true) { + public function __construct($params=NULL, $fetch=true) { if (!empty($params) and (is_array($params) or is_object($params))) { if ($fetch) { if ($data = $this->fetch($params)) { @@ -89,7 +91,8 @@ class grade_object { * If id present (==instance exists in db) fetches data from db. * Defaults are used for new instances. */ - function load_optional_fields() { + public function load_optional_fields() { + global $DB; foreach ($this->optional_fields as $field=>$default) { if (array_key_exists($field, $this)) { continue; @@ -97,7 +100,7 @@ class grade_object { if (empty($this->id)) { $this->$field = $default; } else { - $this->$field = get_field($this->table, $field, 'id', $this->id); + $this->$field = $DB->get_field($this->table, $field, array('id', $this->id)); } } } @@ -109,9 +112,7 @@ class grade_object { * @param array $params associative arrays varname=>value * @return object grade_object instance or false if none found. */ - function fetch($params) { - print_error('mustbeoveride', 'debug', '', 'fetch()'); - } + public static abstract function fetch($params); /** * Finds and returns all grade_object instances based on params. @@ -120,16 +121,14 @@ class grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_object insatnces or false if none found. */ - function fetch_all($params) { - print_error('mustbeoveride', 'debug', '', 'fetch_all()'); - } + public static abstract function fetch_all($params); /** * Factory method - uses the parameters to retrieve matching instance from the DB. * @static final protected * @return mixed object instance or false if not found */ - function fetch_helper($table, $classname, $params) { + protected static function fetch_helper($table, $classname, $params) { if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { if (count($instances) > 1) { // we should not tolerate any errors here - problems might appear later @@ -146,7 +145,7 @@ class grade_object { * @static final protected * @return mixed array of object instances or false if not found */ - function fetch_all_helper($table, $classname, $params) { + protected static function fetch_all_helper($table, $classname, $params) { $instance = new $classname(); $classvars = (array)$instance; @@ -173,7 +172,8 @@ class grade_object { $wheresql = implode("AND", $wheresql); } - if ($datas = get_records_select($table, $wheresql, 'id')) { + global $DB; + if ($datas = $DB->get_records_select($table, $wheresql, array('id'))) { $result = array(); foreach($datas as $data) { $instance = new $classname(); @@ -192,8 +192,8 @@ class grade_object { * @param string $source from where was the object updated (mod/forum, manual, etc.) * @return boolean success */ - function update($source=null) { - global $USER, $CFG; + public function update($source=null) { + global $USER, $CFG, $DB; if (empty($this->id)) { debugging('Can not update grade object, no id!'); @@ -202,7 +202,7 @@ class grade_object { $data = $this->get_record_data(); - if (!update_record($this->table, addslashes_recursive($data))) { + if (!$DB->update_record($this->table, addslashes_recursive($data))) { return false; } @@ -213,7 +213,7 @@ class grade_object { $data->source = $source; $data->timemodified = time(); $data->userlogged = $USER->id; - insert_record($this->table.'_history', addslashes_recursive($data)); + $DB->insert_record($this->table.'_history', addslashes_recursive($data)); } return true; @@ -224,8 +224,8 @@ class grade_object { * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return boolean success */ - function delete($source=null) { - global $USER, $CFG; + public function delete($source=null) { + global $USER, $CFG, $DB; if (empty($this->id)) { debugging('Can not delete grade object, no id!'); @@ -243,7 +243,7 @@ class grade_object { $data->source = $source; $data->timemodified = time(); $data->userlogged = $USER->id; - insert_record($this->table.'_history', addslashes_recursive($data)); + $DB->insert_record($this->table.'_history', addslashes_recursive($data)); } return true; @@ -255,7 +255,7 @@ class grade_object { /** * Returns object with fields and values that are defined in database */ - function get_record_data() { + public function get_record_data() { $data = new object(); // we need to do this to prevent infinite loops in addslashes_recursive - grade_item -> category ->grade_item foreach ($this as $var=>$value) { @@ -277,8 +277,8 @@ class grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return int PK ID if successful, false otherwise */ - function insert($source=null) { - global $USER, $CFG; + public function insert($source=null) { + global $USER, $CFG, $DB; if (!empty($this->id)) { debugging("Grade object already exists!"); @@ -287,7 +287,7 @@ class grade_object { $data = $this->get_record_data(); - if (!$this->id = insert_record($this->table, addslashes_recursive($data))) { + if (!$this->id = $DB->insert_record($this->table, addslashes_recursive($data))) { debugging("Could not insert object into db"); return false; } @@ -304,7 +304,7 @@ class grade_object { $data->source = $source; $data->timemodified = time(); $data->userlogged = $USER->id; - insert_record($this->table.'_history', addslashes_recursive($data)); + $DB->insert_record($this->table.'_history', addslashes_recursive($data)); } return $this->id; @@ -316,13 +316,13 @@ class grade_object { * the object. This is different from the update() function, which acts on the DB record * based on the object. */ - function update_from_db() { + public function update_from_db() { if (empty($this->id)) { debugging("The object could not be used in its state to retrieve a matching record from the DB, because its id field is not set."); return false; } - - if (!$params = get_record($this->table, 'id', $this->id)) { + global $DB; + if (!$params = $DB->get_record($this->table, array('id' => $this->id))) { debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!"); return false; } @@ -337,7 +337,7 @@ class grade_object { * and assigns the value to the corresponding variable in this object. * @static final */ - function set_properties(&$instance, $params) { + public static function set_properties(&$instance, $params) { $params = (array) $params; foreach ($params as $var => $value) { if (in_array($var, $instance->required_fields) or array_key_exists($var, $instance->optional_fields)) { diff --git a/lib/grade/grade_outcome.php b/lib/grade/grade_outcome.php index d08d3c7005..0e39fc2e8e 100644 --- a/lib/grade/grade_outcome.php +++ b/lib/grade/grade_outcome.php @@ -34,63 +34,63 @@ class grade_outcome extends grade_object { * DB Table (used by grade_object). * @var string $table */ - var $table = 'grade_outcomes'; + public $table = 'grade_outcomes'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'courseid', 'shortname', 'fullname', 'scaleid', + public $required_fields = array('id', 'courseid', 'shortname', 'fullname', 'scaleid', 'description', 'timecreated', 'timemodified', 'usermodified'); /** * The course this outcome belongs to. * @var int $courseid */ - var $courseid; + public $courseid; /** * The shortname of the outcome. * @var string $shortname */ - var $shortname; + public $shortname; /** * The fullname of the outcome. * @var string $fullname */ - var $fullname; + public $fullname; /** * A full grade_scale object referenced by $this->scaleid. * @var object $scale */ - var $scale; + public $scale; /** * The id of the scale referenced by this outcome. * @var int $scaleid */ - var $scaleid; + public $scaleid; /** * The description of this outcome - FORMAT_MOODLE. * @var string $description */ - var $description; + public $description; /** * The userid of the person who last modified this outcome. * @var int $usermodified */ - var $usermodified; + public $usermodified; /** * Deletes this outcome from the database. * @param string $source from where was the object deleted (mod/forum, manual, etc.) * @return boolean success */ - function delete($source=null) { + public function delete($source=null) { if (!empty($this->courseid)) { delete_records('grade_outcomes_courses', 'outcomeid', $this->id, 'courseid', $this->courseid); } @@ -104,7 +104,8 @@ class grade_outcome extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return int PK ID if successful, false otherwise */ - function insert($source=null) { + public function insert($source=null) { + global $DB; $this->timecreated = $this->timemodified = time(); @@ -113,7 +114,7 @@ class grade_outcome extends grade_object { $goc = new object(); $goc->courseid = $this->courseid; $goc->outcomeid = $this->id; - insert_record('grade_outcomes_courses', $goc); + $DB->insert_record('grade_outcomes_courses', $goc); } } return $result; @@ -124,7 +125,7 @@ class grade_outcome extends grade_object { * @param string $source from where was the object inserted * @return boolean success */ - function update($source=null) { + public function update($source=null) { $this->timemodified = time(); if ($result = parent::update($source)) { @@ -140,7 +141,8 @@ class grade_outcome extends grade_object { * @param int $courseid * @return succes - false if incorrect courseid requested */ - function use_in($courseid) { + public function use_in($courseid) { + global $DB; if (!empty($this->courseid) and $courseid != $this->courseid) { return false; } @@ -149,7 +151,7 @@ class grade_outcome extends grade_object { $goc = new object(); $goc->courseid = $courseid; $goc->outcomeid = $this->id; - return (bool)insert_record('grade_outcomes_courses', $goc); + return (bool)$DB->insert_record('grade_outcomes_courses', $goc); } return true; } @@ -161,7 +163,7 @@ class grade_outcome extends grade_object { * @param array $params associative arrays varname=>value * @return object grade_outcome instance or false if none found. */ - function fetch($params) { + public static function fetch($params) { return grade_object::fetch_helper('grade_outcomes', 'grade_outcome', $params); } @@ -172,7 +174,7 @@ class grade_outcome extends grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_outcome insatnces or false if none found. */ - function fetch_all($params) { + public static function fetch_all($params) { return grade_object::fetch_all_helper('grade_outcomes', 'grade_outcome', $params); } @@ -180,7 +182,7 @@ class grade_outcome extends grade_object { * Instantiates a grade_scale object whose data is retrieved from the * @return object grade_scale */ - function load_scale() { + public function load_scale() { if (empty($this->scale->id) or $this->scale->id != $this->scaleid) { $this->scale = grade_scale::fetch(array('id'=>$this->scaleid)); $this->scale->load_items(); @@ -193,7 +195,7 @@ class grade_outcome extends grade_object { * @static * @return object */ - function fetch_all_global() { + public static function fetch_all_global() { if (!$outcomes = grade_outcome::fetch_all(array('courseid'=>null))) { $outcomes = array(); } @@ -206,7 +208,7 @@ class grade_outcome extends grade_object { * @param int $courseid * @return object */ - function fetch_all_local($courseid) { + public static function fetch_all_local($courseid) { if (!$outcomes =grade_outcome::fetch_all(array('courseid'=>$courseid))) { $outcomes = array(); } @@ -219,8 +221,8 @@ class grade_outcome extends grade_object { * @param int $courseid * @return array */ - function fetch_all_available($courseid) { - global $CFG; + public static function fetch_all_available($courseid) { + global $CFG, $DB; $result = array(); $sql = "SELECT go.* @@ -228,7 +230,7 @@ class grade_outcome extends grade_object { WHERE go.id = goc.outcomeid AND goc.courseid = {$courseid} ORDER BY go.id ASC"; - if ($datas = get_records_sql($sql)) { + if ($datas = $DB->get_records_sql($sql)) { foreach($datas as $data) { $instance = new grade_outcome(); grade_object::set_properties($instance, $data); @@ -244,7 +246,7 @@ class grade_outcome extends grade_object { * when we do not know the exact type of an object. * @return string name */ - function get_name() { + public function get_name() { return format_string($this->fullname); } @@ -252,7 +254,7 @@ class grade_outcome extends grade_object { * Returns unique outcome short name. * @return string name */ - function get_shortname() { + public function get_shortname() { return $this->shortname; } @@ -260,7 +262,7 @@ class grade_outcome extends grade_object { * Checks if outcome can be deleted. * @return boolean */ - function can_delete() { + public function can_delete() { if ($this->get_item_uses_count()) { return false; } @@ -276,7 +278,7 @@ class grade_outcome extends grade_object { * Returns the number of places where outcome is used. * @return int */ - function get_course_uses_count() { + public function get_course_uses_count() { global $CFG; if (!empty($this->courseid)) { @@ -290,7 +292,7 @@ class grade_outcome extends grade_object { * Returns the number of places where outcome is used. * @return int */ - function get_item_uses_count() { + public function get_item_uses_count() { return count_records('grade_items', 'outcomeid', $this->id); } @@ -306,8 +308,8 @@ class grade_outcome extends grade_object { * @param bool $items Whether or not to return the list of items using this outcome * @return float */ - function get_grade_info($courseid=null, $average=true, $items=false) { - global $CFG; + public function get_grade_info($courseid=null, $average=true, $items=false) { + global $CFG, $DB; if (!isset($this->id)) { debugging("You must setup the outcome's id before calling its get_grade_info() method!"); @@ -336,7 +338,7 @@ class grade_outcome extends grade_object { AND {$CFG->prefix}grade_outcomes.id = $this->id $wheresql"; - $grades = get_records_sql($sql); + $grades = $DB->get_records_sql($sql); $retval = array(); if ($average !== false && count($grades) > 0) { diff --git a/lib/grade/grade_scale.php b/lib/grade/grade_scale.php index b551601073..c506ccf9ef 100644 --- a/lib/grade/grade_scale.php +++ b/lib/grade/grade_scale.php @@ -34,45 +34,45 @@ class grade_scale extends grade_object { * DB Table (used by grade_object). * @var string $table */ - var $table = 'scale'; + public $table = 'scale'; /** * Array of required table fields, must start with 'id'. * @var array $required_fields */ - var $required_fields = array('id', 'courseid', 'userid', 'name', 'scale', 'description', 'timemodified'); + public $required_fields = array('id', 'courseid', 'userid', 'name', 'scale', 'description', 'timemodified'); /** * The course this scale belongs to. * @var int $courseid */ - var $courseid; + public $courseid; - var $userid; + public $userid; /** * The name of the scale. * @var string $name */ - var $name; + public $name; /** * The items in this scale. * @var array $scale_items */ - var $scale_items = array(); + public $scale_items = array(); /** * A string representatin of the scale items (a comma-separated list). * @var string $scale */ - var $scale; + public $scale; /** * A description for this scale. * @var string $description */ - var $description; + public $description; /** * Finds and returns a grade_scale instance based on params. @@ -81,7 +81,7 @@ class grade_scale extends grade_object { * @param array $params associative arrays varname=>value * @return object grade_scale instance or false if none found. */ - function fetch($params) { + public static function fetch($params) { return grade_object::fetch_helper('scale', 'grade_scale', $params); } @@ -92,7 +92,7 @@ class grade_scale extends grade_object { * @param array $params associative arrays varname=>value * @return array array of grade_scale insatnces or false if none found. */ - function fetch_all($params) { + public static function fetch_all($params) { return grade_object::fetch_all_helper('scale', 'grade_scale', $params); } @@ -103,7 +103,7 @@ class grade_scale extends grade_object { * @param string $source from where was the object inserted (mod/forum, manual, etc.) * @return int PK ID if successful, false otherwise */ - function insert($source=null) { + public function insert($source=null) { $this->timecreated = time(); $this->timemodified = time(); return parent::insert($source); @@ -114,7 +114,7 @@ class grade_scale extends grade_object { * @param string $source from where was the object inserted * @return boolean success */ - function update($source=null) { + public function update($source=null) { $this->timemodified = time(); return parent::update($source); } @@ -124,7 +124,7 @@ class grade_scale extends grade_object { * when we do not know the exact type of an object. * @return string name */ - function get_name() { + public function get_name() { return format_string($this->name); } @@ -138,7 +138,7 @@ class grade_scale extends grade_object { * @param mixed $items Could be null, a string or an array. The method behaves differently for each case. * @return array The resulting array of scale items or null if the method failed to produce one. */ - function load_items($items=NULL) { + public function load_items($items=NULL) { if (empty($items)) { $this->scale_items = explode(',', $this->scale); } elseif (is_array($items)) { @@ -168,7 +168,7 @@ class grade_scale extends grade_object { * @param mixed $items Could be null, a string or an array. The method behaves differently for each case. * @return array The resulting string of scale items or null if the method failed to produce one. */ - function compact_items($items=NULL) { + public function compact_items($items=NULL) { if (empty($items)) { $this->scale = implode(',', $this->scale_items); } elseif (is_array($items)) { @@ -188,9 +188,10 @@ class grade_scale extends grade_object { * @param float $grade * @return string */ - function get_nearest_item($grade) { + public function get_nearest_item($grade) { + global $DB; // Obtain nearest scale item from average - $scales_array = get_records_list('scale', 'id', $this->id); + $scales_array = $DB->get_records_list('scale', array('id' => $this->id)); $scale = $scales_array[$this->id]; $scales = explode(",", $scale->scale); @@ -206,7 +207,7 @@ class grade_scale extends grade_object { * Static function returning all global scales * @return object */ - function fetch_all_global() { + public function fetch_all_global() { return grade_scale::fetch_all(array('courseid'=>0)); } @@ -214,7 +215,7 @@ class grade_scale extends grade_object { * Static function returning all local course scales * @return object */ - function fetch_all_local($courseid) { + public static function fetch_all_local($courseid) { return grade_scale::fetch_all(array('courseid'=>$courseid)); } @@ -222,7 +223,7 @@ class grade_scale extends grade_object { * Checks if scale can be deleted. * @return boolean */ - function can_delete() { + public function can_delete() { return !$this->is_used(); } @@ -230,7 +231,8 @@ class grade_scale extends grade_object { * Returns if scale used anywhere - activities, grade items, outcomes, etc. * @return bool */ - function is_used() { + public function is_used() { + global $DB; global $CFG; // count grade items excluding the @@ -246,7 +248,7 @@ class grade_scale extends grade_object { } $legacy_mods = false; - if ($mods = get_records('modules', 'visible', 1)) { + if ($mods = $DB->get_records('modules', array('visible' => 1))) { foreach ($mods as $mod) { //Check cm->name/lib.php exists if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) { diff --git a/lib/grade/simpletest/testgradecategory.php b/lib/grade/simpletest/testgradecategory.php index 13c9f39941..b87d1995bf 100755 --- a/lib/grade/simpletest/testgradecategory.php +++ b/lib/grade/simpletest/testgradecategory.php @@ -100,6 +100,7 @@ class grade_category_test extends grade_test { } function test_grade_category_update() { + global $DB; $grade_category = new grade_category($this->grade_categories[0]); $this->assertTrue(method_exists($grade_category, 'update')); @@ -113,13 +114,13 @@ class grade_category_test extends grade_test { $this->assertTrue($grade_category->update()); - $fullname = get_field('grade_categories', 'fullname', 'id', $this->grade_categories[0]->id); + $fullname = $DB->get_field('grade_categories', 'fullname', array('id' => $this->grade_categories[0]->id)); $this->assertEqual($grade_category->fullname, $fullname); - $path = get_field('grade_categories', 'path', 'id', $this->grade_categories[0]->id); + $path = $DB->get_field('grade_categories', 'path', array('id' => $this->grade_categories[0]->id)); $this->assertEqual($grade_category->path, $path); - $depth = get_field('grade_categories', 'depth', 'id', $this->grade_categories[0]->id); + $depth = $DB->get_field('grade_categories', 'depth', array('id' => $this->grade_categories[0]->id)); $this->assertEqual($grade_category->depth, $depth); $grade_item = $grade_category->get_grade_item(); @@ -127,11 +128,12 @@ class grade_category_test extends grade_test { } function test_grade_category_delete() { + global $DB; $grade_category = new grade_category($this->grade_categories[0]); $this->assertTrue(method_exists($grade_category, 'delete')); $this->assertTrue($grade_category->delete()); - $this->assertFalse(get_record('grade_categories', 'id', $grade_category->id)); + $this->assertFalse($DB->get_record('grade_categories', array('id' => $grade_category->id))); } function test_grade_category_insert() { diff --git a/lib/grade/simpletest/testgradeitem.php b/lib/grade/simpletest/testgradeitem.php index 771334d108..07fc0e12f2 100755 --- a/lib/grade/simpletest/testgradeitem.php +++ b/lib/grade/simpletest/testgradeitem.php @@ -42,7 +42,7 @@ Mock::generatePartial('grade_item', 'mock_grade_item_for_test_is_calculated', ar @set_time_limit(0); class grade_item_test extends grade_test { - + function test_grade_item_construct() { $params = new stdClass(); @@ -80,15 +80,17 @@ class grade_item_test extends grade_test { } function test_grade_item_delete() { + global $DB; $grade_item = new grade_item($this->grade_items[0]); $this->assertTrue(method_exists($grade_item, 'delete')); $this->assertTrue($grade_item->delete()); - $this->assertFalse(get_record('grade_items', 'id', $grade_item->id)); + $this->assertFalse($DB->get_record('grade_items', array('id' => $grade_item->id))); } function test_grade_item_update() { + global $DB; $grade_item = new grade_item($this->grade_items[0]); $this->assertTrue(method_exists($grade_item, 'update')); @@ -100,7 +102,7 @@ class grade_item_test extends grade_test { $this->assertTrue($grade_item->qualifies_for_regrading()); $this->assertTrue($grade_item->update()); - $iteminfo = get_field('grade_items', 'iteminfo', 'id', $this->grade_items[0]->id); + $iteminfo = $DB->get_field('grade_items', 'iteminfo', array('id' => $this->grade_items[0]->id)); $this->assertEqual($grade_item->iteminfo, $iteminfo); } @@ -208,7 +210,7 @@ class grade_item_test extends grade_test { $this->assertEqual($grade_item->sortorder, 6); $after = grade_item::fetch(array('id'=>$this->grade_items[6]->id)); - $this->assertEqual($after->sortorder, 8); + $this->assertEqual($after->sortorder, 7); } function test_grade_item_get_name() { @@ -447,14 +449,14 @@ class grade_item_test extends grade_test { $res = array($this->grade_items[4]->id, $this->grade_items[5]->id); $this->assertEqual($res, $deps); } - + function test_grade_item_is_calculated() { $grade_item = new mock_grade_item_for_test_is_calculated($this); $grade_item->set_properties($grade_item, $this->grade_items[1]); $this->assertTrue(method_exists($grade_item, 'is_calculated')); $grade_itemsource = new grade_item($this->grade_items[0]); $normalizedformula = str_replace("[[$grade_itemsource->idnumber]]", "##gi$grade_itemsource->id##", $this->grade_items[1]->calculation); - + $grade_item->expectOnce('set_calculation', array($grade_item->calculation)); $grade_item->setReturnValue('set_calculation', $normalizedformula); $this->assertTrue($grade_item->is_calculated()); @@ -495,13 +497,13 @@ class grade_item_test extends grade_test { $grade_grade->delete(); $grade_item->compute(); - $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid)); $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade); $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid)); $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade); $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid)); $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade); + } } diff --git a/lib/grade/simpletest/testgradeoutcome.php b/lib/grade/simpletest/testgradeoutcome.php index d0d28df278..366c2081e3 100644 --- a/lib/grade/simpletest/testgradeoutcome.php +++ b/lib/grade/simpletest/testgradeoutcome.php @@ -68,20 +68,22 @@ class grade_outcome_test extends grade_test { } function test_grade_outcome_update() { + global $DB; $grade_outcome = new grade_outcome($this->grade_outcomes[0]); $this->assertTrue(method_exists($grade_outcome, 'update')); $grade_outcome->shortname = 'Team work'; $this->assertTrue($grade_outcome->update()); - $shortname = get_field('grade_outcomes', 'shortname', 'id', $this->grade_outcomes[0]->id); + $shortname = $DB->get_field('grade_outcomes', 'shortname', array('id' => $this->grade_outcomes[0]->id)); $this->assertEqual($grade_outcome->shortname, $shortname); } function test_grade_outcome_delete() { + global $DB; $grade_outcome = new grade_outcome($this->grade_outcomes[0]); $this->assertTrue(method_exists($grade_outcome, 'delete')); $this->assertTrue($grade_outcome->delete()); - $this->assertFalse(get_record('grade_outcomes', 'id', $grade_outcome->id)); + $this->assertFalse($DB->get_record('grade_outcomes', array('id' => $grade_outcome->id))); } function test_grade_outcome_fetch() { diff --git a/lib/grade/simpletest/testgradescale.php b/lib/grade/simpletest/testgradescale.php index f804cae3af..1ba6c9362d 100755 --- a/lib/grade/simpletest/testgradescale.php +++ b/lib/grade/simpletest/testgradescale.php @@ -77,21 +77,23 @@ class grade_scale_test extends grade_test { } function test_grade_scale_update() { + global $DB; $grade_scale = new grade_scale($this->scale[0]); $this->assertTrue(method_exists($grade_scale, 'update')); - + $grade_scale->name = 'Updated info for this unittest grade_scale'; $this->assertTrue($grade_scale->update()); - $name = get_field('scale', 'name', 'id', $this->scale[0]->id); + $name = $DB->get_field('scale', 'name', array('id' => $this->scale[0]->id)); $this->assertEqual($grade_scale->name, $name); } function test_grade_scale_delete() { + global $DB; $grade_scale = new grade_scale($this->scale[0]); $this->assertTrue(method_exists($grade_scale, 'delete')); $this->assertTrue($grade_scale->delete()); - $this->assertFalse(get_record('scale', 'id', $grade_scale->id)); + $this->assertFalse($DB->get_record('scale', array('id' => $grade_scale->id))); } function test_grade_scale_fetch() { diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index 0fc187af39..93104ad698 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -43,16 +43,7 @@ Mock::generate('grade_scale', 'mock_grade_scale'); Mock::generate('grade_category', 'mock_grade_category'); Mock::generate('grade_grade', 'mock_grade_grade'); Mock::generate('grade_outcome', 'mock_grade_outcome'); -Mock::generate('grade_lib_wrapper', 'mock_lib_wrapper'); -Mock::generate('ADODB_' . $CFG->dbtype, 'mock_db'); -Mock::generate('ADORecordSet_' . $CFG->dbtype, 'mock_rs'); -// Prepare partial mocks for the static grade object instances -Mock::generatePartial('grade_category', 'mock_grade_category_partial', array('fetch', 'fetch_all')); -Mock::generatePartial('grade_item', 'mock_grade_item_partial', array('fetch', 'fetch_all')); -Mock::generatePartial('grade_grade', 'mock_grade_grade_partial', array('fetch', 'fetch_all')); -Mock::generatePartial('grade_outcome', 'mock_grade_outcome_partial', array('fetch', 'fetch_all')); -Mock::generatePartial('grade_scale', 'mock_grade_scale_partial', array('fetch', 'fetch_all')); /** * Here is a brief explanation of the test data set up in these unit tests. @@ -67,30 +58,43 @@ class grade_test extends UnitTestCase { * every test has access to these test data. The order of the following array is * crucial, because of the interrelationships between objects. */ - var $tables = array('grade_categories', + public $tables = array('grade_categories', 'scale', 'grade_items', 'grade_grades', 'grade_outcomes'); - var $grade_items = array(); - var $grade_categories = array(); - var $grade_grades = array(); - var $grade_outcomes = array(); - var $scale = array(); + public $grade_items = array(); + public $grade_categories = array(); + public $grade_grades = array(); + public $grade_outcomes = array(); + public $scale = array(); - var $activities = array(); - var $courseid = 1; - var $userid = 1; + public $activities = array(); + public $courseid = 1; + public $userid = 1; + + public static $db = null; + public $realdb; + public $dbmanager; /** * Create temporary test tables and entries in the database for these tests. * These tests have to work on a brand new site. - * Override $CFG->prefix while these tests run. */ function setUp() { // Set global category settings to -1 (not force) - global $CFG; + global $CFG, $DB; + + if (is_null(grade_test::$db)) { + $this->realdb = $DB; + grade_test::$db = new mysqli_adodb_moodle_database(); + grade_test::$db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, "mdl_unittest_"); + } + + $DB = grade_test::$db; + $this->dbmanager = $DB->get_manager(); + $CFG->grade_droplow = -1; $CFG->grade_keephigh = -1; $CFG->grade_aggregation = -1; @@ -98,8 +102,6 @@ class grade_test extends UnitTestCase { $CFG->grade_aggregateoutcomes = -1; $CFG->grade_aggregatesubcats = -1; - $CFG->old_prefix = $CFG->prefix; - $CFG->prefix .= 'unittest_'; if (!$this->prepare_test_tables()) { die("Could not create all the test tables!"); } @@ -115,286 +117,287 @@ class grade_test extends UnitTestCase { } function prepare_test_tables() { + global $CFG, $DB; $result = true; /// Define table course_modules to be created $table = new xmldb_table('course_modules'); - if (!table_exists($table)) { + if (!$this->dbmanager->table_exists($table)) { /// Adding fields to table course_modules - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('module', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('section', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null); - $table->addFieldInfo('added', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('score', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('indent', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); - $table->addFieldInfo('visibleold', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); - $table->addFieldInfo('groupmode', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('groupingid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('groupmembersonly', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('module', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('section', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null); + $table->add_field('added', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('score', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('indent', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('visibleold', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('groupmode', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('groupingid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('groupmembersonly', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); /// Adding keys to table course_modules - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('groupingid', XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('groupingid', XMLDB_KEY_FOREIGN, array('groupingid'), 'groupings', array('id')); /// Adding indexes to table course_modules - $table->addIndexInfo('visible', XMLDB_INDEX_NOTUNIQUE, array('visible')); - $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table->addIndexInfo('module', XMLDB_INDEX_NOTUNIQUE, array('module')); - $table->addIndexInfo('instance', XMLDB_INDEX_NOTUNIQUE, array('instance')); - $table->addIndexInfo('idnumber-course', XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'course')); + $table->add_index('visible', XMLDB_INDEX_NOTUNIQUE, array('visible')); + $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); + $table->add_index('module', XMLDB_INDEX_NOTUNIQUE, array('module')); + $table->add_index('instance', XMLDB_INDEX_NOTUNIQUE, array('instance')); + $table->add_index('idnumber-course', XMLDB_INDEX_NOTUNIQUE, array('idnumber', 'course')); /// Launch create table for course_modules - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table modules to be created $table = new xmldb_table('modules'); - if (!table_exists($table)) { + if (!$this->dbmanager->table_exists($table)) { /// Adding fields to table modules - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('cron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('lastcron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('search', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('cron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('lastcron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('search', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '1'); /// Adding keys to table modules - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); /// Adding indexes to table modules - $table->addIndexInfo('name', XMLDB_INDEX_NOTUNIQUE, array('name')); + $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name')); /// Launch create table for modules - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_items to be created $table = new xmldb_table('grade_items'); - if (!table_exists($table)) { - $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, 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, 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, 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, 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'); - $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); - $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null); - $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0'); - $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null); - $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', 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')); - $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); - $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id')); + if (!$this->dbmanager->table_exists($table)) { + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null); + $table->add_field('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); + $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('outcomeid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null); + $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0'); + $table->add_field('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('deleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id')); + $table->add_key('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); + $table->add_key('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id')); /// Launch create table for grade_items - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_categories to be created $table = new xmldb_table('grade_categories'); - if ($result && !table_exists($table)) { - - $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, 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, 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('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, 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')); + if ($result && !$this->dbmanager->table_exists($table)) { + + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id')); /// Launch create table for grade_categories - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_grades to be created $table = new xmldb_table('grade_grades'); - if ($result && !table_exists($table)) { - - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, 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); - $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); - $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); - $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('rawscaleid', 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('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); - $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); - $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); - $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, 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('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id')); - $table->addKeyInfo('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); - $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id')); - $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); + if ($result && !$this->dbmanager->table_exists($table)) { + + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); + $table->add_field('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); + $table->add_field('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); + $table->add_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id')); + $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); + $table->add_key('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id')); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); /// Launch create table for grade_grades - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_outcomes to be created $table = new xmldb_table('grade_outcomes'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table grade_outcomes - $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, null, null, null, null, null); - $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $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); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); /// Adding keys to table grade_outcomes - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); - $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); - $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); /// Launch create table for grade_outcomes - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table scale to be created $table = new xmldb_table('scale'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { - $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'); - $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $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'); - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addIndexInfo('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); /// Launch create table for scale - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table quiz to be created $table = new xmldb_table('quiz'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table quiz - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('optionflags', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('penaltyscheme', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('attempts', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('attemptonlast', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('grademethod', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); - $table->addFieldInfo('decimalpoints', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '2'); - $table->addFieldInfo('review', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('questionsperpage', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('shufflequestions', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('shuffleanswers', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('questions', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('sumgrades', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('grade', 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, '0'); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('timelimit', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('password', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('subnet', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('popup', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('delay1', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('delay2', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('optionflags', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('penaltyscheme', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('attempts', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('attemptonlast', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('grademethod', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('decimalpoints', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '2'); + $table->add_field('review', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('questionsperpage', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('shufflequestions', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('shuffleanswers', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('questions', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('sumgrades', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('timelimit', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('subnet', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('popup', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('delay1', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('delay2', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); /// Adding keys to table quiz - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); /// Adding indexes to table quiz - $table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course')); + $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); /// Launch create table for quiz - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } return $result; @@ -402,223 +405,224 @@ class grade_test extends UnitTestCase { function prepare_test_history_tables() { + global $DB; $result = true; /// Define table grade_items to be created $table = new xmldb_table('grade_items_history'); - if (!table_exists($table)) { + if (!$this->dbmanager->table_exists($table)) { /// Adding fields to table grade_items_history - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 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, 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, 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, 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'); - $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); - $table->addFieldInfo('outcomeid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null); - $table->addFieldInfo('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0'); - $table->addFieldInfo('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null); - $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', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('categoryid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('itemname', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('itemtype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('itemmodule', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null); + $table->add_field('iteminstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('itemnumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('iteminfo', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('calculation', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('gradetype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1'); + $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); + $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('outcomeid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, null, null); + $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('multfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '1.0'); + $table->add_field('plusfactor', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('hidden', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('needsupdate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); /// Adding keys to table grade_items_history - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_items', 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')); - $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); - $table->addKeyInfo('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_items', array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'grade_categories', array('id')); + $table->add_key('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); + $table->add_key('outcomeid', XMLDB_KEY_FOREIGN, array('outcomeid'), 'grade_outcomes', array('id')); /// Adding indexes to table grade_items_history - $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action')); + $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); /// Launch create table for grade_items_history - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_categories to be created $table = new xmldb_table('grade_categories_history'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table grade_categories_history - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 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); - $table->addFieldInfo('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $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('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('parent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('depth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('aggregation', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('keephigh', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('droplow', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregateonlygraded', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregateoutcomes', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('aggregatesubcats', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); /// Adding keys to table grade_categories_history - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_categories', 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')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_categories', array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('parent', XMLDB_KEY_FOREIGN, array('parent'), 'grade_categories', array('id')); /// Adding indexes to table grade_categories_history - $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action')); + $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); /// Launch create table for grade_categories_history - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_grades to be created $table = new xmldb_table('grade_grades_history'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table grade_grades_history - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 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); - $table->addFieldInfo('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); - $table->addFieldInfo('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); - $table->addFieldInfo('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('rawscaleid', 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('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); - $table->addFieldInfo('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); - $table->addFieldInfo('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); - $table->addFieldInfo('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); + $table->add_field('rawgrademax', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '100'); + $table->add_field('rawgrademin', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('rawscaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('finalgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null); + $table->add_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('locktime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('exported', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('overridden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('excluded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('feedback', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('feedbackformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('information', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null); + $table->add_field('informationformat', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); /// Adding keys to table grade_grades_history - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_grades', 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')); - $table->addKeyInfo('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id')); - $table->addKeyInfo('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); - $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_grades', array('id')); + $table->add_key('itemid', XMLDB_KEY_FOREIGN, array('itemid'), 'grade_items', array('id')); + $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); + $table->add_key('rawscaleid', XMLDB_KEY_FOREIGN, array('rawscaleid'), 'scale', array('id')); + $table->add_key('usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); + $table->add_key('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id')); /// Adding indexes to table grade_grades_history - $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action')); + $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); /// Launch create table for grade_grades_history - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table grade_outcomes to be created $table = new xmldb_table('grade_outcomes_history'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table grade_outcomes_history - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); /// Adding keys to table grade_outcomes_history - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_outcomes', array('id')); - $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); - $table->addKeyInfo('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); - $table->addKeyInfo('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'grade_outcomes', array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('scaleid', XMLDB_KEY_FOREIGN, array('scaleid'), 'scale', array('id')); + $table->add_key('loggeduser', XMLDB_KEY_FOREIGN, array('loggeduser'), 'user', array('id')); /// Adding indexes to table grade_outcomes_history - $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action')); + $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); /// Launch create table for grade_outcomes_history - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } /// Define table scale to be created $table = new xmldb_table('scale_history'); - if ($result && !table_exists($table)) { + if ($result && !$this->dbmanager->table_exists($table)) { /// Adding fields to table scale_history - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - $table->addFieldInfo('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); - $table->addFieldInfo('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); - $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); - $table->addFieldInfo('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 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'); - $table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); - $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->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); + $table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('loggeduser', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0'); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('scale', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); + $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null); /// Adding keys to table scale_history - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->addKeyInfo('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'scales', array('id')); - $table->addKeyInfo('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->add_key('oldid', XMLDB_KEY_FOREIGN, array('oldid'), 'scales', array('id')); + $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); /// Adding indexes to table scale_history - $table->addIndexInfo('action', XMLDB_INDEX_NOTUNIQUE, array('action')); + $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); /// Launch create table for scale_history - $result = $result && create_table($table, true, false); + $result = $result && $this->dbmanager->create_table($table, true, false); } else { - delete_records($table->name); + $DB->delete_records($table->name, array()); } return $result; @@ -626,21 +630,21 @@ class grade_test extends UnitTestCase { /** * Drop test tables from DB. - * Restore original $CFG->prefix. */ function tearDown() { - global $CFG; // delete the contents of tables before the test run - the unit test might fail on fatal error and the data would not be deleted! foreach ($this->tables as $table) { unset($this->$table); } - $CFG->prefix = $CFG->old_prefix; + global $DB; + $DB = $this->realdb; } /** * Load scale data into the database, and adds the corresponding objects to this class' variable. */ function load_scale() { + global $DB; $scale = new stdClass(); $scale->name = 'unittestscale1'; @@ -650,7 +654,7 @@ class grade_test extends UnitTestCase { $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)) { + if ($scale->id = $DB->insert_record('scale', $scale)) { $this->scale[0] = $scale; $temp = explode(',', $scale->scale); $this->scalemax[0] = count($temp) -1; @@ -665,7 +669,7 @@ class grade_test extends UnitTestCase { $scale->description = 'This scale is used to mark standard assignments.'; $scale->timemodified = mktime(); - if ($scale->id = insert_record('scale', $scale)) { + if ($scale->id = $DB->insert_record('scale', $scale)) { $this->scale[1] = $scale; $temp = explode(',', $scale->scale); $this->scalemax[1] = count($temp) -1; @@ -682,7 +686,7 @@ class grade_test extends UnitTestCase { $temp = explode(',', $scale->scale); $scale->max = count($temp) -1; - if ($scale->id = insert_record('scale', $scale)) { + if ($scale->id = $DB->insert_record('scale', $scale)) { $this->scale[2] = $scale; $temp = explode(',', $scale->scale); $this->scalemax[2] = count($temp) -1; @@ -697,7 +701,7 @@ class grade_test extends UnitTestCase { $temp = explode(',', $scale->scale); $scale->max = count($temp) -1; - if ($scale->id = insert_record('scale', $scale)) { + if ($scale->id = $DB->insert_record('scale', $scale)) { $this->scale[3] = $scale; $temp = explode(',', $scale->scale); $this->scalemax[3] = count($temp) -1; @@ -712,7 +716,7 @@ class grade_test extends UnitTestCase { $temp = explode(',', $scale->scale); $scale->max = count($temp) -1; - if ($scale->id = insert_record('scale', $scale)) { + if ($scale->id = $DB->insert_record('scale', $scale)) { $this->scale[4] = $scale; $temp = explode(',', $scale->scale); $this->scalemax[4] = count($temp) -1; @@ -723,6 +727,7 @@ class grade_test extends UnitTestCase { * Load grade_category data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_categories() { + global $DB; $course_category = grade_category::fetch_course_category($this->courseid); @@ -739,7 +744,7 @@ class grade_test extends UnitTestCase { $grade_category->timemodified = mktime(); $grade_category->depth = 2; - if ($grade_category->id = insert_record('grade_categories', $grade_category)) { + if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/'; update_record('grade_categories', $grade_category); $this->grade_categories[0] = $grade_category; @@ -758,7 +763,7 @@ class grade_test extends UnitTestCase { $grade_category->timemodified = mktime(); $grade_category->depth = 3; - if ($grade_category->id = insert_record('grade_categories', $grade_category)) { + if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/'; update_record('grade_categories', $grade_category); $this->grade_categories[1] = $grade_category; @@ -777,7 +782,7 @@ class grade_test extends UnitTestCase { $grade_category->timemodified = mktime(); $grade_category->depth = 3; - if ($grade_category->id = insert_record('grade_categories', $grade_category)) { + if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/'; update_record('grade_categories', $grade_category); $this->grade_categories[2] = $grade_category; @@ -798,7 +803,7 @@ class grade_test extends UnitTestCase { $grade_category->timemodified = mktime(); $grade_category->depth = 2; - if ($grade_category->id = insert_record('grade_categories', $grade_category)) { + if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/'; update_record('grade_categories', $grade_category); $this->grade_categories[3] = $grade_category; @@ -809,21 +814,22 @@ class grade_test extends UnitTestCase { * Load module entries in modules table\ */ function load_modules() { + global $DB; $module = new stdClass(); $module->name = 'assignment'; - if ($module->id = insert_record('modules', $module)) { + if ($module->id = $DB->insert_record('modules', $module)) { $this->modules[0] = $module; } $module = new stdClass(); $module->name = 'quiz'; - if ($module->id = insert_record('modules', $module)) { + if ($module->id = $DB->insert_record('modules', $module)) { $this->modules[1] = $module; } $module = new stdClass(); $module->name = 'forum'; - if ($module->id = insert_record('modules', $module)) { + if ($module->id = $DB->insert_record('modules', $module)) { $this->modules[2] = $module; } } @@ -832,11 +838,12 @@ class grade_test extends UnitTestCase { * Load module instance entries in course_modules table */ function load_course_modules() { + global $DB; $course_module = new stdClass(); $course_module->course = $this->courseid; $quiz->module = 1; $quiz->instance = 2; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } @@ -844,7 +851,7 @@ class grade_test extends UnitTestCase { $course_module->course = $this->courseid; $quiz->module = 2; $quiz->instance = 1; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } @@ -852,7 +859,7 @@ class grade_test extends UnitTestCase { $course_module->course = $this->courseid; $quiz->module = 2; $quiz->instance = 5; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } @@ -860,7 +867,7 @@ class grade_test extends UnitTestCase { $course_module->course = $this->courseid; $quiz->module = 3; $quiz->instance = 3; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } @@ -868,7 +875,7 @@ class grade_test extends UnitTestCase { $course_module->course = $this->courseid; $quiz->module = 3; $quiz->instance = 7; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } @@ -876,7 +883,7 @@ class grade_test extends UnitTestCase { $course_module->course = $this->courseid; $quiz->module = 3; $quiz->instance = 9; - if ($course_module->id = insert_record('course_modules', $course_module)) { + if ($course_module->id = $DB->insert_record('course_modules', $course_module)) { $this->course_module[0] = $course_module; } } @@ -885,12 +892,13 @@ class grade_test extends UnitTestCase { * Load test quiz data into the database */ function load_quiz_activities() { + global $DB; $quiz = new stdClass(); $quiz->course = $this->courseid; $quiz->name = 'test quiz'; $quiz->intro = 'let us quiz you!'; $quiz->questions = '1,2'; - if ($quiz->id = insert_record('quiz', $quiz)) { + if ($quiz->id = $DB->insert_record('quiz', $quiz)) { $this->activities[0] = $quiz; } @@ -899,7 +907,7 @@ class grade_test extends UnitTestCase { $quiz->name = 'test quiz 2'; $quiz->intro = 'let us quiz you again!'; $quiz->questions = '1,3'; - if ($quiz->id = insert_record('quiz', $quiz)) { + if ($quiz->id = $DB->insert_record('quiz', $quiz)) { $this->activities[1] = $quiz; } @@ -909,6 +917,7 @@ class grade_test extends UnitTestCase { * Load grade_item data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_items() { + global $DB; $course_category = grade_category::fetch_course_category($this->courseid); @@ -931,7 +940,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 3; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[0] = $grade_item; } @@ -954,7 +963,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 4; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[1] = $grade_item; } @@ -976,7 +985,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 6; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[2] = $grade_item; } @@ -997,7 +1006,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 1; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[3] = $grade_item; } @@ -1017,7 +1026,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 2; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[4] = $grade_item; } @@ -1037,7 +1046,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 5; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[5] = $grade_item; } @@ -1061,7 +1070,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 7; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[6] = $grade_item; } @@ -1084,7 +1093,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 9; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[7] = $grade_item; } @@ -1105,7 +1114,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 10; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[8] = $grade_item; } @@ -1127,7 +1136,7 @@ class grade_test extends UnitTestCase { $grade_item->timemodified = mktime(); $grade_item->sortorder = 8; - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[9] = $grade_item; } @@ -1148,7 +1157,7 @@ class grade_test extends UnitTestCase { $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); - if ($grade_item->id = insert_record('grade_items', $grade_item)) { + if ($grade_item->id = $DB->insert_record('grade_items', $grade_item)) { $this->grade_items[10] = $grade_item; } @@ -1158,6 +1167,7 @@ class grade_test extends UnitTestCase { * Load grade_grades data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_grades() { + global $DB; // Grades for grade_item 1 $grade = new stdClass(); $grade->itemid = $this->grade_items[0]->id; @@ -1171,7 +1181,7 @@ class grade_test extends UnitTestCase { $grade->feedback = 'Good, but not good enough..'; $grade->feedbackformat = FORMAT_PLAIN; - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[0] = $grade; } @@ -1183,7 +1193,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[1] = $grade; } @@ -1195,7 +1205,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[2] = $grade; } @@ -1209,7 +1219,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[3] = $grade; } @@ -1220,7 +1230,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[4] = $grade; } @@ -1231,7 +1241,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[5] = $grade; } @@ -1247,7 +1257,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[6] = $grade; } @@ -1260,7 +1270,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1273,7 +1283,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1287,7 +1297,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1299,7 +1309,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1311,7 +1321,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1325,7 +1335,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1337,7 +1347,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1351,7 +1361,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1363,7 +1373,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } @@ -1375,7 +1385,7 @@ class grade_test extends UnitTestCase { $grade->timecreated = mktime(); $grade->timemodified = mktime(); - if ($grade->id = insert_record('grade_grades', $grade)) { + if ($grade->id = $DB->insert_record('grade_grades', $grade)) { $this->grade_grades[] = $grade; } } @@ -1384,6 +1394,7 @@ class grade_test extends UnitTestCase { * Load grade_outcome data into the database, and adds the corresponding objects to this class' variable. */ function load_grade_outcomes() { + global $DB; // Calculation for grade_item 1 $grade_outcome = new stdClass(); $grade_outcome->fullname = 'Team work'; @@ -1393,7 +1404,7 @@ class grade_test extends UnitTestCase { $grade_outcome->timemodified = mktime(); $grade_outcome->scaleid = $this->scale[2]->id; - if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + if ($grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome)) { $this->grade_outcomes[] = $grade_outcome; } @@ -1406,7 +1417,7 @@ class grade_test extends UnitTestCase { $grade_outcome->timemodified = mktime(); $grade_outcome->scaleid = $this->scale[3]->id; - if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + if ($grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome)) { $this->grade_outcomes[] = $grade_outcome; } @@ -1419,7 +1430,7 @@ class grade_test extends UnitTestCase { $grade_outcome->timemodified = mktime(); $grade_outcome->scaleid = $this->scale[4]->id; - if ($grade_outcome->id = insert_record('grade_outcomes', $grade_outcome)) { + if ($grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome)) { $this->grade_outcomes[] = $grade_outcome; } } diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 7d731bd4d6..da33d8d261 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -7,7 +7,7 @@ // Moodle - Modular Object-Oriented Dynamic Learning Environment // // http://moodle.org // // // -// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // +// Copyright (C) 1999 onwards 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 //