From 612607bd47bb1729e035ae525a184ebf6f7c47e4 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 5 Jun 2007 22:58:37 +0000 Subject: [PATCH] MDL-9516 new central grading function update_grade(); to be improved/tested more tomorrow --- admin/cron.php | 6 - lib/db/events.php | 28 +--- lib/db/install.xml | 2 +- lib/eventslib.php | 2 +- lib/grade/grade_item.php | 34 +++-- lib/gradelib.php | 255 ++++++++++++++++++++++++++++------ mod/assignment/db/events.php | 2 +- mod/assignment/db/upgrade.php | 2 +- mod/assignment/lib.php | 237 +++++-------------------------- mod/assignment/version.php | 2 +- mod/data/db/upgrade.php | 2 +- mod/data/lib.php | 128 +++++------------ mod/data/rate.php | 9 +- mod/data/version.php | 2 +- mod/forum/db/upgrade.php | 2 +- mod/forum/lib.php | 126 ++++------------- mod/forum/rate.php | 8 +- mod/forum/version.php | 2 +- mod/glossary/db/upgrade.php | 2 +- mod/glossary/lib.php | 127 ++++------------- mod/glossary/rate.php | 8 +- mod/glossary/version.php | 2 +- 22 files changed, 382 insertions(+), 606 deletions(-) diff --git a/admin/cron.php b/admin/cron.php index d0593aa9b0..8d1a074132 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -261,12 +261,6 @@ } } - // attemps to grab grades from third party/non-stard mods that still have xxx_grades() in lib.php - // which was obsoleted in 1.9. - mtrace("Grabbing grades from older modules if required..."); - include_once($CFG->dirroot.'/lib/gradelib.php'); - grade_grab_legacy_grades(); - } // End of occasional clean-up tasks diff --git a/lib/db/events.php b/lib/db/events.php index c52214d1ba..5def23b9d7 100644 --- a/lib/db/events.php +++ b/lib/db/events.php @@ -27,47 +27,23 @@ $handlers = array ( - -/* - * Grades added by activities - * - * required parameters (object or array): - * itemid - if from grade_items table, grade item must already exist - * userid - each grade must be associated to existing user - * - * optional params: - * gradevalue - raw grade value - * feedback - graders feedback - * feedbackformat - text format of the feedback - */ - 'grade_updated' => array ( - 'handlerfile' => '/lib/gradelib.php', - 'handlerfunction' => 'grade_handler', - 'schedule' => 'instant' - ), - /* * Grades created/modified outside of activities (import, gradebook overrides, etc.) * * required parameters (object or array): - * itemid - id from grade_items table, grade item must already exist + * idnumber - idnumber from grade_items table * userid - each grade must be associated with existing user * * optional params: * gradevalue - raw grade value * feedback - graders feedback * feedbackformat - text format of the feedback - * - * optional params (improves performance): - * itemtype - mod, block - * itemmodule - assignment, etc. */ - 'grade_updated_external' => array ( + 'grade_update_request' => array ( 'handlerfile' => '/lib/gradelib.php', 'handlerfunction' => 'grade_handler', 'schedule' => 'instant' ) ); - ?> diff --git a/lib/db/install.xml b/lib/db/install.xml index 36c1d4e317..9096428744 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1248,7 +1248,7 @@ - + diff --git a/lib/eventslib.php b/lib/eventslib.php index a3874722e4..178b9f61f4 100755 --- a/lib/eventslib.php +++ b/lib/eventslib.php @@ -448,7 +448,7 @@ function events_trigger($eventname, $eventdata) { events_queue_handler($handler, $event, $errormessage); } } else { - debugging("No handler found for event: $eventname"); + //debugging("No handler found for event: $eventname"); } return $failedcount; diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 42a79c4047..949e42648d 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -232,9 +232,17 @@ class grade_item extends grade_object { if (!empty($this->outcome->id)) { $this->outcomeid = $this->outcome->id; } - + + if (!isset($this->gradetype)) { + $this->gradetype = GRADE_TYPE_VALUE; + } + + if (empty($this->scaleid) and !empty($this->scale->id)) { + $this->scaleid = $this->scale->id; + } + // Retrieve scale and infer grademax from it - if (!empty($this->scaleid)) { + if ($this->gradetype == GRADE_TYPE_SCALE and !empty($this->scaleid)) { $this->load_scale(); if (!method_exists($this->scale, 'load_items')) { @@ -245,11 +253,9 @@ class grade_item extends grade_object { $this->scale->load_items(); $this->grademax = count ($this->scale->scale_items); $this->grademin = 0; - $this->gradetype = GRADE_TYPE_SCALE; - } - - if (!empty($this->scale->id)) { - $this->scaleid = $this->scale->id; + } else { + $this->scaleid = NULL; + unset($this->scale); } $qualifies = $this->qualifies_for_update(); @@ -348,13 +354,23 @@ class grade_item extends grade_object { function insert() { global $CFG; + if (!isset($this->gradetype)) { + $this->gradetype = GRADE_TYPE_VALUE; + } + + if (empty($this->scaleid) and !empty($this->scale->id)) { + $this->scaleid = $this->scale->id; + } + // Retrieve scale and infer grademax from it - if (!empty($this->scaleid)) { + if ($this->gradetype == GRADE_TYPE_SCALE and !empty($this->scaleid)) { $this->load_scale(); $this->scale->load_items(); $this->grademax = count ($this->scale->scale_items); $this->grademin = 0; - $this->gradetype = GRADE_TYPE_SCALE; + } else { + $this->scaleid = NULL; + unset($this->scale); } // If not set, infer courseid from referenced category diff --git a/lib/gradelib.php b/lib/gradelib.php index babdf95cd3..3f8e37f12b 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -36,15 +36,26 @@ define('GRADE_AGGREGATE_MEAN', 0); define('GRADE_AGGREGATE_MEDIAN', 1); define('GRADE_AGGREGATE_SUM', 2); define('GRADE_AGGREGATE_MODE', 3); + define('GRADE_CHILDTYPE_ITEM', 0); define('GRADE_CHILDTYPE_CAT', 1); + define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values + define('GRADE_TYPE_NONE', 0); define('GRADE_TYPE_VALUE', 1); define('GRADE_TYPE_SCALE', 2); define('GRADE_TYPE_TEXT', 3); +define('GRADE_UPDATE_OK', 0); +define('GRADE_UPDATE_FAILED', 1); +define('GRADE_UPDATE_MULTIPLE', 2); +define('GRADE_UPDATE_ITEM_DELETED', 3); +define('GRADE_UPDATE_INVALID_GRADE', 4); +define('GRADE_UPDATE_LOCKED', 5); + + require_once($CFG->libdir . '/grade/grade_category.php'); require_once($CFG->libdir . '/grade/grade_item.php'); require_once($CFG->libdir . '/grade/grade_calculation.php'); @@ -56,16 +67,166 @@ require_once($CFG->libdir . '/grade/grade_history.php'); require_once($CFG->libdir . '/grade/grade_grades_text.php'); require_once($CFG->libdir . '/grade/grade_tree.php'); +/***** PUBLIC GRADE API *****/ + +function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grade=NULL, $itemdetails=NULL) { + + // only following grade_item properties can be changed/used in this function + $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'deleted'); + + if (is_null($courseid) or is_null($itemtype)) { + debugging('Missing courseid or itemtype'); + return GRADE_UPDATE_FAILED; + } + + $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false); + if (!$grade_items = $grade_item->fetch_all_using_this()) { + // create a new one + $grade_item = false; + + } else if (count($grade_items) == 1){ + $grade_item = reset($grade_items); + unset($grade_items); //release memory + + } else { + + debugging('Found more than one grading item'); + return GRADE_UPDATE_MULTIPLE; + } + +/// Create or update the grade_item if needed + if (!$grade_item) { + $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'); + if ($itemdetails) { + $itemdetails = (array)$itemdetails; + foreach ($itemdetails as $k=>$v) { + if (!in_array($k, $allowed)) { + // ignore it + continue; + } + if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) { + // no grade item needed! + return GRADE_UPDATE_OK; + } + $params[$k] = $v; + } + } + $itemid = grade_create_item($params); + $grade_item = grade_item::fetch('id', $itemid); + + } else { + if ($grade_item->locked) { + debugging('Grading item is locked!'); + return GRADE_UPDATE_LOCKED; + } + + if ($itemdetails) { + $itemdetails = (array)$itemdetails; + $update = false; + foreach ($itemdetails as $k=>$v) { + if (!in_array($k, $allowed)) { + // ignore it + continue; + } + if ($grade_item->{$k} != $v) { + $grade_item->{$k} = $v; + $update = true; + } + } + if ($update) { + $grade_item->update(); + } + } + } + +/// Some extra checks + // do we use grading? + if ($grade_item->gradetype == GRADE_TYPE_NONE) { + return GRADE_UPDATE_OK; + } + + // no grade submitted + if (empty($grade)) { + return GRADE_UPDATE_OK; + } + + // no grading in deleted items + if ($grade_item->deleted) { + debugging('Grade item was already deleted!'); + return GRADE_UPDATE_ITEM_DELETED; + } + +/// Finally start processing of grades + if (is_object($grade)) { + $grades = array($grade); + } else { + if (array_key_exists('userid', $grade)) { + $grades = array($grade); + } else { + $grades = $grade; + } + } + + unset($grade); + + foreach ($grades as $grade) { + $grade = (array)$grade; + if (empty($grade['userid'])) { + debugging('Invalid grade submitted'); + return GRADE_UPDATE_INVALID_GRADE; + } + + // get the raw grade if it exist + $rawgrade = new grade_grades_raw(array('itemid'=>$grade_item->id, 'userid'=>$grade['userid'])); + $rawgrade->grade_item = &$grade_item; // we already have it, so let's use it + + // store these to keep track of original grade item settings + $rawgrade->grademax = $grade_item->grademax; + $rawgrade->grademin = $grade_item->grademin; + $rawgrade->scaleid = $grade_item->scaleid; + + if (isset($grade['feedback'])) { + $rawgrade->feedback = $grade['feedback']; + if (isset($grade['feedbackformat'])) { + $rawgrade->feedbackformat = $grade['feedbackformat']; + } else { + $rawgrade->feedbackformat = FORMAT_PLAIN; + } + } + + if (!isset($grade['gradevalue'])) { + $grade['gradevalue'] = null; // means no grade yet + } + + if ($rawgrade->id) { + $rawgrade->update($grade['gradevalue'], 'event'); + } else { + $rawgrade->gradevalue = $grade['gradevalue']; + $rawgrade->insert(); + } + + //trigger grade_updated event notification + $eventdata = new object(); + $eventdata->itemid = $grade_item->id; + $eventdata->grade = $grade; + events_trigger('grade_updated', $eventdata); + } + + return GRADE_UPDATE_OK; +} + +/***** END OF PUBLIC API *****/ + /** -* Extracts from the gradebook all the grade items attached to the calling object. -* For example, an assignment may want to retrieve all the grade_items for itself, +* Extracts from the gradebook all the grade items attached to the calling object. +* For example, an assignment may want to retrieve all the grade_items for itself, * and get three outcome scales in return. This will affect the grading interface. * * Note: Each parameter refines the search. So if you only give the courseid, * all the grade_items for this course will be returned. If you add the * itemtype 'mod', all grade_items for this courseif AND for the 'mod' * type will be returned, etc... -* +* * @param int $courseid The id of the course to which the grade items belong * @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc * @param string $itemmodule 'forum, 'quiz', 'csv' etc @@ -91,7 +252,7 @@ function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminsta */ function grade_create_item($params) { $grade_item = new grade_item($params); - + if (empty($grade_item->id)) { return $grade_item->insert(); } else { @@ -112,7 +273,7 @@ function grade_create_item($params) { */ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) { $grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation')); - + if (empty($grade_category->id)) { return $grade_category->insert(); } else { @@ -127,7 +288,7 @@ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_ * If it's locked to the current use then the module can print a nice message or prevent editing in the module. * If no $userid is given, the method will always return the grade_item's locked state. * If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked, -* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will +* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will * return the locked state of the specific grade. * * @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc @@ -140,7 +301,7 @@ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_ function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL, $userid=NULL) { $grade_item = new grade_item(compact('itemtype', 'itemmodule', 'iteminstance', 'itemnumber')); return $grade_item->is_locked($userid); -} +} /** * Updates all grade_grades_final for each grade_item matching the given attributes. @@ -156,7 +317,7 @@ function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) { $grade_item->courseid = $courseid; $grade_item->id = $gradeitemid; $grade_items = $grade_item->fetch_all_using_this(); - + $count = 0; foreach ($grade_items as $gi) { @@ -175,11 +336,11 @@ function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) { * For backward compatibility with old third-party modules, this function is called * via to admin/cron.php to search all mod/xxx/lib.php files for functions named xxx_grades(), * if the current modules does not have grade events registered with the grade book. - * Once the data is extracted, the events_trigger() function can be called to initiate - * an event as usual and copy/ *upgrade the data in the gradebook tables. + * Once the data is extracted, the events_trigger() function can be called to initiate + * an event as usual and copy/ *upgrade the data in the gradebook tables. */ function grade_grab_legacy_grades() { - + global $CFG, $db; if (!$mods = get_list_of_plugins('mod') ) { @@ -213,7 +374,7 @@ function grade_grab_legacy_grades() { $grademax = $grades->maxgrade; $scaleid = 0; - if (!is_numeric($grademax)) { + if (!is_numeric($grademax)) { // scale name is provided as a string, try to find it if (!$scale = get_record('scale', 'name', $grademax)) { debugging('Incorrect scale name! name:'.$grademax); @@ -227,7 +388,7 @@ function grade_grab_legacy_grades() { continue; } - foreach ($grades->grades as $userid=>$usergrade) { + foreach ($grades->grades as $userid=>$usergrade) { // make the grade_added eventdata $eventdata = new object(); $eventdata->itemid = $grade_item->id; @@ -303,7 +464,7 @@ function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) { return $grade_item; } - + // create new one $params = array('courseid' =>$modinstance->courseid, 'itemtype' =>'mod', @@ -313,11 +474,11 @@ function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) { 'idnumber' =>$modinstance->cmidnumber); if ($scaleid) { - $params['gradetype'] = GRADE_TYPE_SCALE; + $params['gradetype'] = GRADE_TYPE_SCALE; $params['scaleid'] = $scaleid; } else { - $params['gradetype'] = GRADE_TYPE_VALUE; + $params['gradetype'] = GRADE_TYPE_VALUE; $params['grademax'] = $grademax; $params['grademin'] = 0; } @@ -353,12 +514,12 @@ function standardise_score($gradevalue, $source_min, $source_max, $target_min, $ 'target_max' => $target_max, 'result' => $standardised_value)); } - return $standardised_value; + return $standardised_value; } /** - * Handles all grade_updated and grade_updated_external events, + * Handles some specific grade_update_request events, * see lib/db/events.php for description of $eventdata format. * * @param object $eventdata contains all the data for the event @@ -374,46 +535,50 @@ function grade_handler($eventdata) { return true; } - // grade item must be specified or else it could be accidentally duplicated, - if (empty($eventdata['itemid'])) { - debugging('Missing grade item id in event!'); + // grade item idnumber must be specified or else it could be accidentally duplicated, + if (empty($eventdata['idnumber'])) { + debugging('Missing grade item idnumber in event!'); return true; } // get the grade item from db - if (!$gradeitem = grade_item::fetch('id', $eventdata['itemid'])) { - debugging('Incorrect grade item id in event! id:'.$eventdata['itemid']); + $grade_item = new grade_item(array('idnumber'=>$eventdata['idnumber']), false); + if (!$grade_items = $grade_item->fetch_all_using_this()) { + // TODO: create a new one - tricky, watch out for duplicates !! + // use only special type 'import'(y) or 'manual'(?) + debugging('Can not create new grade items yet :-('); return true; - } - // get the raw grade if it exist - $rawgrade = new grade_grades_raw(array('itemid'=>$gradeitem->id, 'userid'=>$eventdata['userid'])); - $rawgrade->grade_item = &$gradeitem; // we already have it, so let's use it + } else if (count($grade_items) == 1) { + $grade_item = reset($grade_items); + unset($grade_items); //release memory - // store these to keep track of original grade item settings - $rawgrade->grademax = $gradeitem->grademax; - $rawgrade->grademin = $gradeitem->grademin; - $rawgrade->scaleid = $gradeitem->scaleid; + } else { + debugging('More than one grade item matched, grade update request failed'); + return true; + } - if (isset($eventdata['feedback'])) { - $rawgrade->feedback = $eventdata['feedback']; - if (isset($eventdata['feedbackformat'])) { - $rawgrade->feedbackformat = $eventdata['feedbackformat']; - } else { - $rawgrade->feedbackformat = FORMAT_PLAIN; - } + // !! TODO: whitelist only some types such as 'import'(?) 'manual'(?) and ignore the rest!! + if ($grade_item->itemtype == 'mnod') { + // modules must have own handlers for grade update requests + return true; + } + $grade = new object(); + $grade->userid = $eventdata['userid']; + if (isset($eventdata['gradevalue'])) { + $grade->feedback = $eventdata['gradevalue']; } - if (!isset($eventdata['gradevalue'])) { - $eventdata['gradevalue'] = null; // means no grade yet + if (isset($eventdata['feedback'])) { + $grade->feedback = $eventdata['feedback']; } - if ($rawgrade->id) { - $rawgrade->update($eventdata['gradevalue'], 'event'); - } else { - $rawgrade->gradevalue = $eventdata['gradevalue']; - $rawgrade->insert(); + if (isset($eventdata['feedbackformat'])) { + $grade->feedbackformat = $eventdata['feedbackformat']; } + grade_update($grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule, + $grade_item->iteminstance, $grade_item->itemnumber, $grade, $eventdata); + // everything ok :-) return true; diff --git a/mod/assignment/db/events.php b/mod/assignment/db/events.php index f78fff3b03..bdc5610740 100644 --- a/mod/assignment/db/events.php +++ b/mod/assignment/db/events.php @@ -34,7 +34,7 @@ $handlers = array ( */ 'grade_updated_external' => array ( 'handlerfile' => '/mod/assignment/lib.php', - 'handlerfunction' => 'assignment_external_grade_handler', + 'handlerfunction' => 'assignment_grade_update_handler', 'schedule' => 'instant' ) ); diff --git a/mod/assignment/db/upgrade.php b/mod/assignment/db/upgrade.php index 13b5ce2c86..8f946dfe72 100644 --- a/mod/assignment/db/upgrade.php +++ b/mod/assignment/db/upgrade.php @@ -23,7 +23,7 @@ function xmldb_assignment_upgrade($oldversion=0) { $result = true; - if ($result && $oldversion < 2007052700) { + if ($result && $oldversion < 2007060600) { require_once $CFG->dirroot.'/mod/assignment/lib.php'; // too much debug output $db->debug = false; diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 05c0a9ca17..95b40f9c1d 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -366,7 +366,7 @@ class assignment_base { } $assignment = stripslashes_recursive($assignment); - assignment_grade_item_create($assignment); + assignment_grade_item_update($assignment); } @@ -400,16 +400,6 @@ class assignment_base { $result = false; } - // Get the cm id to properly clean up the grade_items for this assignment - // bug 4976 - if (! $cm = get_record('modules', 'name', 'assignment')) { - $result = false; - } else { - if (! delete_records('grade_item', 'modid', $cm->id, 'cminstance', $assignment->id)) { - $result = false; - } - } - // delete file area with all attachments - ignore errors require_once($CFG->libdir.'/filelib.php'); fulldelete($CFG->dataroot.'/'.$assignment->course.'/'.$CFG->moddata.'/assignment/'.$assignment->id); @@ -484,8 +474,7 @@ class assignment_base { * Update grade item for this submission. */ function update_grade($submission) { - $grade_item = assignment_grade_item_get($this->assignment); - assignment_update_grades($grade_item, $submission->userid); + assignment_update_grades($this->assignment, $submission->userid); } /** @@ -1822,14 +1811,14 @@ function assignment_cron () { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ -function assignment_get_user_grades($assignmentid, $userid=0) { +function assignment_get_user_grades($assignment, $userid=0) { global $CFG; $user = $userid ? "AND u.id = $userid" : ""; - $sql = "SELECT u.id, s.grade AS gradevalue, s.submissioncomment AS feedback, s.format AS feedbackformat + $sql = "SELECT u.id, u.id AS userid, s.grade AS gradevalue, s.submissioncomment AS feedback, s.format AS feedbackformat FROM {$CFG->prefix}user u, {$CFG->prefix}assignment_submissions s - WHERE u.id = s.userid AND s.assignment = $assignmentid + WHERE u.id = s.userid AND s.assignment = $assignment->id $user"; return get_records_sql($sql); @@ -1838,27 +1827,23 @@ function assignment_get_user_grades($assignmentid, $userid=0) { /** * Update grades by firing grade_updated event * - * @param object $grade_item null means all assignments + * @param object $assignment null means all assignments * @param int $userid specific user only, 0 mean all */ -function assignment_update_grades($grade_item=null, $userid=0, $nullifnone=true) { +function assignment_update_grades($assignment=null, $userid=0, $nullifnone=true) { global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); + } - if ($grade_item != null) { - if ($grades = assignment_get_user_grades($grade_item->iteminstance, $userid)) { - foreach ($grades as $grade) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $grade->id; - if ($grade->gradevalue < 0) { - $eventdata->gradevalue = null; - } else { - $eventdata->gradevalue = $grade->gradevalue; + if ($assignment != null) { + if ($grades = assignment_get_user_grades($assignment, $userid)) { + foreach($grades as $k=>$v) { + if ($v->gradevalue == -1) { + $grades[$k]->gradevalue = null; } - $eventdata->feedback = $grade->feedback; - $eventdata->feedbackformat = $grade->feedbackformat; - events_trigger('grade_updated', $eventdata); } + grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades); } } else { @@ -1868,8 +1853,10 @@ function assignment_update_grades($grade_item=null, $userid=0, $nullifnone=true) if ($rs = get_recordset_sql($sql)) { if ($rs->RecordCount() > 0) { while ($assignment = rs_fetch_next_record($rs)) { - $grade_item = assignment_grade_item_get($assignment); - assignment_update_grades($grade_item); + assignment_grade_item_update($assignment); + if ($assignment->grade != 0) { + assignment_update_grades($assignment); + } } } rs_close($rs); @@ -1878,88 +1865,22 @@ function assignment_update_grades($grade_item=null, $userid=0, $nullifnone=true) } /** - * Return (create if needed) grade item for given assignment - * - * @param object $assignment object with optional cmidnumber - * @return object grade_item - */ -function assignment_grade_item_get($assignment) { - if (!isset($assignment->courseid)) { - $assignment->courseid = $assignment->course; - } - - if ($items = grade_get_items($assignment->courseid, 'mod', 'assignment', $assignment->id)) { - if (count($items) > 1) { - debugging('Multiple grade items present!'); - } - $grade_item = reset($items); - - } else { - if (!isset($forum->cmidnumber)) { - if (!$cm = get_coursemodule_from_instance('assignment', $assignment->id)) { - error("Course Module ID was incorrect"); - } - $assignment->cmidnumber = $cm->idnumber; - } - if (!$itemid = assignment_grade_item_create($assignment)) { - error('Can not create grade item!'); - } - $grade_item = grade_item::fetch('id', $itemid); - } - - return $grade_item; -} - -/** - * Update grade item for given assignment + * Create grade item for given assignment * * @param object $assignment object with extra cmidnumber - * @return object grade_item + * @return int 0 if ok, error code otherwise */ function assignment_grade_item_update($assignment) { - $grade_item = assignment_grade_item_get($assignment); - - $grade_item->itemname = $assignment->name; - $grade_item->idnumber = $assignment->cmidnumber; - - if ($assignment->grade > 0) { - $grade_item->gradetype = GRADE_TYPE_VALUE; - $grade_item->grademax = $assignment->grade; - $grade_item->grademin = 0; - $grade_item->scaleid = 0; - - } else if ($assignment->grade < 0) { - $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = -$assignment->grade; - - } else { - //how to indicate no grading? - $grade_item->gradetype = GRADE_TYPE_TEXT; - $grade_item->grademax = 0; - $grade_item->grademin = 0; - $grade_item->scaleid = 0; + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); } - $grade_item->update(); -} - -/** - * Create grade item for given assignment - * - * @param object $assignment object with extra cmidnumber - * @return object grade_item - */ -function assignment_grade_item_create($assignment) { if (!isset($assignment->courseid)) { $assignment->courseid = $assignment->course; } - $params = array('courseid' =>$assignment->courseid, - 'itemtype' =>'mod', - 'itemmodule' =>'assignment', - 'iteminstance'=>$assignment->id, - 'itemname' =>$assignment->name, - 'idnumber' =>$assignment->cmidnumber); + $params = array('itemname'=>$assignment->name, 'idnumber'=>$assignment->cmidnumber); if ($assignment->grade > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; @@ -1971,33 +1892,27 @@ function assignment_grade_item_create($assignment) { $params['scaleid'] = -$assignment->grade; } else { - //how to indicate no grading? - $params['gradetype'] = GRADE_TYPE_TEXT; - $params['grademax'] = $assignment->grade; - $params['grademax'] = 0; - $params['grademin'] = 0; + $params['gradetype'] = GRADE_TYPE_NONE; } - $itemid = grade_create_item($params); - return $itemid; + return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, $params); } /** * Delete grade item for given assignment * * @param object $assignment object - * @return object grade_item + * @return object assignment */ function assignment_grade_item_delete($assignment) { + global $CFG; + require_once($CFG->libdir.'/gradelib.php'); + if (!isset($assignment->courseid)) { $assignment->courseid = $assignment->course; } - if ($grade_items = grade_get_items($assignment->courseid, 'mod', 'assignment', $assignment->id)) { - foreach($grade_items as $grade_item) { - $grade_item->delete(); - } - } + return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted'=>1)); } /** @@ -2006,92 +1921,10 @@ function assignment_grade_item_delete($assignment) { * * see eventdata description in lib/db/events.php */ -function assignment_external_grade_handler($eventdata) { +function assignment_grade_update_handler($eventdata) { global $CFG, $USER; - $eventdata = (array)$eventdata; - - // each grade must belong to some user - if (empty($eventdata['userid'])) { - debugging('Missing user id in event data!'); - return true; - } - - // grade item must be specified or else it could be accidentally duplicated, - if (empty($eventdata['itemid'])) { - debugging('Missing grade item id in event!'); - return true; - } - - // shortcut - try first without fetching the grade_item - if (!empty($eventdata['itemtype']) and !empty($eventdata['itemmodule'])) { - if ($eventdata['itemtype'] != 'mod' or $eventdata['itemmodule'] != 'assignment') { - // not our event - return true; - } - } - - // get the grade item from db - if (!$grade_item = grade_item::fetch('id', $eventdata['itemid'])) { - debugging('Incorrect grade item id in event! id:'.$eventdata['itemid']); - return true; - } - - //verify it is our event - if ($grade_item->itemtype != 'mod' or $grade_item->itemmodule != 'assignment') { - // not our event - return true; - } - - if (!$assignment = get_record('assignment', 'id', $grade_item->iteminstance)) { - return true; - } - if (! $course = get_record('course', 'id', $assignment->course)) { - return true; - } - if (! $cm = get_coursemodule_from_instance('assignment', $assignment->id, $course->id)) { - return true; - } - - // Load up the required assignment class - require_once($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); - $assignmentclass = 'assignment_'.$assignment->assignmenttype; - $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm, $course); - - $sub = $assignmentinstance->get_submission((int)$eventdata['userid'], true); // Get or make one - $submission = new object(); - $submission->id = $sub->id; - $submission->userid = $sub->userid; - - if (isset($eventdata['gradevalue'])) { - $submission->grade = (int)$eventdata['gradevalue']; - if ($sub->grade != $submission->grade) { - $submission->mailed = 0; // Make sure mail goes out (again, even) - } - } else { - $submission->grade = -1; - } - - if (isset($eventdata['feedback'])) { - $submission->submissioncomment = addslashes($eventdata['feedback']); - if (isset($eventdata['feedbackformat'])) { - $submission->format = (int)$eventdata['feedbackformat']; - } else { - $submission->format = FORMAT_PLAIN; - } - if ($sub->submissioncomment != $submission->submissioncomment) { - $submission->mailed = 0; // Make sure mail goes out (again, even) - } - } - - $submission->teacher = $USER->id; - $submission->timemarked = time(); - - update_record('assignment_submissions', $submission); - - // TODO: add proper logging - add_to_log($course->id, 'assignment', 'update grades', - 'submissions.php?id='.$assignment->id.'&user='.$submission->userid, $submission->userid, $cm->id); + //TODO: ... return true; } diff --git a/mod/assignment/version.php b/mod/assignment/version.php index dfca1c5ac9..01272114fa 100644 --- a/mod/assignment/version.php +++ b/mod/assignment/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2007060300; +$module->version = 2007060600; $module->requires = 2007052800; // Requires this Moodle version $module->cron = 60; diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index d4593a802e..c86fa7ee45 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -55,7 +55,7 @@ function xmldb_data_upgrade($oldversion=0) { } - if ($result && $oldversion < 2007060300) { + if ($result && $oldversion < 2007060600) { require_once($CFG->dirroot.'/mod/data/lib.php'); // too much debug output $db->debug = false; diff --git a/mod/data/lib.php b/mod/data/lib.php index d9733b6be5..ef10f9afb6 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -627,7 +627,7 @@ function data_add_instance($data) { } $data = stripslashes_recursive($data); - data_grade_item_create($data); + data_grade_item_update($data); return $data->id; } @@ -732,7 +732,6 @@ function data_user_complete($course, $user, $mod, $data) { } } - /** * Return grade for given user or all users. * @@ -740,16 +739,16 @@ function data_user_complete($course, $user, $mod, $data) { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ -function data_get_user_grades($dataid, $userid=0) { +function data_get_user_grades($data, $userid=0) { global $CFG; $user = $userid ? "AND u.id = $userid" : ""; - $sql = "SELECT u.id, avg(drt.rating) AS gradevalue + $sql = "SELECT u.id, u.id AS userid, avg(drt.rating) AS gradevalue FROM {$CFG->prefix}user u, {$CFG->prefix}data_records dr, {$CFG->prefix}data_ratings drt WHERE u.id = dr.userid AND dr.id = drt.recordid - AND drt.userid != u.id AND dr.dataid = $dataid + AND drt.userid != u.id AND dr.dataid = $data->id $user GROUP BY u.id"; @@ -759,28 +758,25 @@ function data_get_user_grades($dataid, $userid=0) { /** * Update grades by firing grade_updated event * - * @param object $grade_item null means all databases + * @param object $data null means all databases * @param int $userid specific user only, 0 mean all */ -function data_update_grades($grade_item=null, $userid=0, $nullifnone=true) { +function data_update_grades($data=null, $userid=0, $nullifnone=true) { global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); + } - if ($grade_item != null) { - if ($grades = data_get_user_grades($grade_item->iteminstance, $userid)) { - foreach ($grades as $grade) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $grade->id; - $eventdata->gradevalue = $grade->gradevalue; - events_trigger('grade_updated', $eventdata); - } + if ($data != null) { + if ($grades = data_get_user_grades($data, $userid)) { + grade_update($data->course, 'mod', 'data', $data->id, 0, $grades); } else if ($userid and $nullifnone) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $userid; - $eventdata->gradevalue = NULL; - events_trigger('grade_updated', $eventdata); + $grade = new object(); + $grade->itemid = $data->id; + $grade->userid = $userid; + $grade->gradevalue = NULL; + grade_update($data->course, 'mod', 'data', $data->id, 0, $grade); } } else { @@ -790,11 +786,10 @@ function data_update_grades($grade_item=null, $userid=0, $nullifnone=true) { if ($rs = get_recordset_sql($sql)) { if ($rs->RecordCount() > 0) { while ($data = rs_fetch_next_record($rs)) { - if (!$data->assessed) { - continue; // no grading + data_grade_item_update($data); + if ($data->assessed) { + data_update_grades($data, 0, false); } - $grade_item = data_grade_item_get($data); - data_update_grades($grade_item, 0, false); } } rs_close($rs); @@ -803,80 +798,21 @@ function data_update_grades($grade_item=null, $userid=0, $nullifnone=true) { } /** - * Return (create if needed) grade item for given data - * - * @param object $data object with optional cmidnumber - * @return object grade_item - */ -function data_grade_item_get($data) { - if ($items = grade_get_items($data->course, 'mod', 'data', $data->id)) { - if (count($items) > 1) { - debugging('Multiple grade items present!'); - } - $grade_item = reset($items); - - } else { - if (!isset($data->cmidnumber)) { - if (!$cm = get_coursemodule_from_instance('data', $data->id)) { - error("Course Module ID was incorrect"); - } - $data->cmidnumber = $cm->idnumber; - } - if (!$itemid = data_grade_item_create($data)) { - error('Can not create grade item!'); - } - $grade_item = grade_item::fetch('id', $itemid); - } - - return $grade_item; -} - -/** - * Update grade item for given data + * Update/create grade item for given data * * @param object $data object with extra cmidnumber * @return object grade_item */ function data_grade_item_update($data) { - $grade_item = data_grade_item_get($data); - - $grade_item->name = $data->name; - $grade_item->idnumber = $data->cmidnumber; - - if (!$data->assessed or $data->scale == 0) { - //how to indicate no grading? - $grade_item->gradetype = GRADE_TYPE_TEXT; - - } else if ($data->scale > 0) { - $grade_item->gradetype = GRADE_TYPE_VALUE; - $grade_item->grademax = $data->scale; - $grade_item->grademin = 0; - - } else if ($data->scale < 0) { - $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = -$data->scale; + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); } - $grade_item->update(); -} - -/** - * Create grade item for given data - * - * @param object $data object with extra cmidnumber - * @return object grade_item - */ -function data_grade_item_create($data) { - $params = array('courseid' =>$data->course, - 'itemtype' =>'mod', - 'itemmodule' =>'data', - 'iteminstance'=>$data->id, - 'itemname' =>$data->name, - 'idnumber' =>$data->cmidnumber); + $params = array('itemname' =>$data->name, 'idnumber' =>$data->cmidnumber); if (!$data->assessed or $data->scale == 0) { - //how to indicate no grading? - $params['gradetype'] = GRADE_TYPE_TEXT; + $params['gradetype'] = GRADE_TYPE_NONE; } else if ($data->scale > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; @@ -888,8 +824,7 @@ function data_grade_item_create($data) { $params['scaleid'] = -$data->scale; } - $itemid = grade_create_item($params); - return $itemid; + return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, $params); } /** @@ -899,11 +834,10 @@ function data_grade_item_create($data) { * @return object grade_item */ function data_grade_item_delete($data) { - if ($grade_items = grade_get_items($data->course, 'mod', 'data', $data->id)) { - foreach($grade_items as $grade_item) { - $grade_item->delete(); - } - } + global $CFG; + require_once($CFG->libdir.'/gradelib.php'); + + return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1)); } /************************************************************************ diff --git a/mod/data/rate.php b/mod/data/rate.php index 58d0546add..e756de4878 100755 --- a/mod/data/rate.php +++ b/mod/data/rate.php @@ -29,8 +29,6 @@ error("Rating of items not allowed!"); } - $grade_item = data_grade_item_get($data); - if (!$frmdata = data_submitted() or !confirm_sesskey()) { error("This page was not accessed correctly"); } @@ -61,16 +59,17 @@ if ($oldrating = get_record('data_ratings', 'userid', $USER->id, 'recordid', $record->id)) { if ($rating == -999) { delete_records('data_ratings', 'userid', $oldrating->userid, 'recordid', $oldrating->recordid); - data_update_grades($grade_item, $record->userid); + data_update_grades($data, $record->userid); } else if ($rating != $oldrating->rating) { $oldrating->rating = $rating; if (! update_record('data_ratings', $oldrating)) { error("Could not update an old rating ($record->id = $rating)"); } - data_update_grades($grade_item, $record->userid); + data_update_grades($data, $record->userid); } + } else if ($rating) { $newrating = new object(); $newrating->userid = $USER->id; @@ -79,7 +78,7 @@ if (! insert_record('data_ratings', $newrating)) { error("Could not insert a new rating ($record->id = $rating)"); } - data_update_grades($grade_item, $record->userid); + data_update_grades($data, $record->userid); } } diff --git a/mod/data/version.php b/mod/data/version.php index d049494ac6..c2f82f0b44 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2007060300; +$module->version = 2007060600; $module->requires = 2007060100; // Requires this Moodle version $module->cron = 60; diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index 999183cc7a..080dab16c0 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -32,7 +32,7 @@ function xmldb_forum_upgrade($oldversion=0) { /// $result = result of "/lib/ddllib.php" function calls /// } - if ($result && $oldversion < 2007060300) { + if ($result && $oldversion < 2007060600) { require_once($CFG->dirroot.'/mod/forum/lib.php'); // too much debug output $db->debug = false; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 1f7201eb3e..426f3a4a68 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -134,7 +134,7 @@ function forum_add_instance($forum) { } $forum = stripslashes_recursive($forum); - forum_grade_item_create($forum); + forum_grade_item_update($forum); return $forum->id; } @@ -1107,16 +1107,16 @@ function forum_print_recent_activity($course, $isteacher, $timestart) { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ -function forum_get_user_grades($forumid, $userid=0) { +function forum_get_user_grades($forum, $userid=0) { global $CFG; $user = $userid ? "AND u.id = $userid" : ""; - $sql = "SELECT u.id, avg(fr.rating) AS gradevalue + $sql = "SELECT u.id, u.id AS userid, avg(fr.rating) AS gradevalue FROM {$CFG->prefix}user u, {$CFG->prefix}forum_posts fp, {$CFG->prefix}forum_ratings fr, {$CFG->prefix}forum_discussions fd WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = $forumid + AND fr.userid != u.id AND fd.forum = $forum->id $user GROUP BY u.id"; @@ -1126,28 +1126,22 @@ function forum_get_user_grades($forumid, $userid=0) { /** * Update grades by firing grade_updated event * - * @param object $grade_item null means all forums + * @param object $forum null means all forums * @param int $userid specific user only, 0 mean all */ -function forum_update_grades($grade_item=null, $userid=0, $nullifnone=true) { +function forum_update_grades($forum=null, $userid=0, $nullifnone=true) { global $CFG; - if ($grade_item != null) { - if ($grades = forum_get_user_grades($grade_item->iteminstance, $userid)) { - foreach ($grades as $grade) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $grade->id; - $eventdata->gradevalue = $grade->gradevalue; - events_trigger('grade_updated', $eventdata); - } + if ($forum != null) { + if ($grades = forum_get_user_grades($forum, $userid)) { + grade_update($forum->course, 'mod', 'forum', $forum->id, 0, $grades); } else if ($userid and $nullifnone) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $userid; - $eventdata->gradevalue = NULL; - events_trigger('grade_updated', $eventdata); + $grade = new object(); + $grade->itemid = $forum->id; + $grade->userid = $userid; + $grade->gradevalue = NULL; + grade_update($data->course, 'mod', 'forum', $forum->id, 0, $grade); } } else { @@ -1157,11 +1151,10 @@ function forum_update_grades($grade_item=null, $userid=0, $nullifnone=true) { if ($rs = get_recordset_sql($sql)) { if ($rs->RecordCount() > 0) { while ($forum = rs_fetch_next_record($rs)) { - if (!$forum->assessed) { - continue; // no grading + forum_grade_item_update($forum); + if ($forum->assessed) { + forum_update_grades($forum, 0, false); } - $grade_item = forum_grade_item_get($forum); - forum_update_grades($grade_item, 0, false); } } rs_close($rs); @@ -1170,80 +1163,21 @@ function forum_update_grades($grade_item=null, $userid=0, $nullifnone=true) { } /** - * Return (create if needed) grade item for given forum - * - * @param object $forum object with optional cmidnumber - * @return object grade_item - */ -function forum_grade_item_get($forum) { - if ($items = grade_get_items($forum->course, 'mod', 'forum', $forum->id)) { - if (count($items) > 1) { - debugging('Multiple grade items present!'); - } - $grade_item = reset($items); - - } else { - if (!isset($forum->cmidnumber)) { - if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) { - error("Course Module ID was incorrect"); - } - $forum->cmidnumber = $cm->idnumber; - } - if (!$itemid = forum_grade_item_create($forum)) { - error('Can not create grade item!'); - } - $grade_item = grade_item::fetch('id', $itemid); - } - - return $grade_item; -} - -/** - * Update grade item for given forum + * Create/update grade item for given forum * * @param object $forum object with extra cmidnumber - * @return object grade_item + * @return int 0 if ok */ function forum_grade_item_update($forum) { - $grade_item = forum_grade_item_get($forum); - - $grade_item->name = $forum->name; - $grade_item->idnumber = $forum->cmidnumber; - - if (!$forum->assessed or $forum->scale == 0) { - //how to indicate no grading? - $grade_item->gradetype = GRADE_TYPE_TEXT; - - } else if ($forum->scale > 0) { - $grade_item->gradetype = GRADE_TYPE_VALUE; - $grade_item->grademax = $forum->scale; - $grade_item->grademin = 0; - - } else if ($forum->scale < 0) { - $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = -$forum->scale; + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); } - $grade_item->update(); -} - -/** - * Create grade item for given forum - * - * @param object $forum object with extra cmidnumber - * @return object grade_item - */ -function forum_grade_item_create($forum) { - $params = array('courseid' =>$forum->course, - 'itemtype' =>'mod', - 'itemmodule' =>'forum', - 'iteminstance'=>$forum->id, - 'itemname' =>$forum->name, - 'idnumber' =>$forum->cmidnumber); + $params = array('itemname'=>$forum->name, 'idnumber'=>$forum->cmidnumber); if (!$forum->assessed or $forum->scale == 0) { - //how to indicate no grading? - $params['gradetype'] = GRADE_TYPE_TEXT; + $params['gradetype'] = GRADE_TYPE_NONE; } else if ($forum->scale > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; @@ -1255,8 +1189,7 @@ function forum_grade_item_create($forum) { $params['scaleid'] = -$forum->scale; } - $itemid = grade_create_item($params); - return $itemid; + return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, $params); } /** @@ -1266,11 +1199,10 @@ function forum_grade_item_create($forum) { * @return object grade_item */ function forum_grade_item_delete($forum) { - if ($grade_items = grade_get_items($forum->course, 'mod', 'forum', $forum->id)) { - foreach($grade_items as $grade_item) { - $grade_item->delete(); - } - } + global $CFG; + require_once($CFG->libdir.'/gradelib.php'); + + return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1)); } diff --git a/mod/forum/rate.php b/mod/forum/rate.php index 4d0b9ccb8e..97ba2dfd1b 100644 --- a/mod/forum/rate.php +++ b/mod/forum/rate.php @@ -33,8 +33,6 @@ $context = get_context_instance(CONTEXT_MODULE, $cm->id); require_capability('mod/forum:rate', $context); - $grade_item = forum_grade_item_get($forum); - if ($data = data_submitted()) { $discussionid = false; @@ -64,7 +62,7 @@ if ($rating == FORUM_UNSET_POST_RATING) { delete_records('forum_ratings', 'post', $postid, 'userid', $USER->id); - forum_update_grades($grade_item, $post->userid); + forum_update_grades($forum, $post->userid); } else if ($oldrating = get_record('forum_ratings', 'userid', $USER->id, 'post', $post->id)) { if ($rating != $oldrating->rating) { @@ -73,7 +71,7 @@ if (! update_record('forum_ratings', $oldrating)) { error("Could not update an old rating ($post->id = $rating)"); } - forum_update_grades($grade_item, $post->userid); + forum_update_grades($forum, $post->userid); } } else { @@ -86,7 +84,7 @@ if (! insert_record('forum_ratings', $newrating)) { error("Could not insert a new rating ($postid = $rating)"); } - forum_update_grades($grade_item, $post->userid); + forum_update_grades($forum, $post->userid); } } diff --git a/mod/forum/version.php b/mod/forum/version.php index d049494ac6..c2f82f0b44 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2007060300; +$module->version = 2007060600; $module->requires = 2007060100; // Requires this Moodle version $module->cron = 60; diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index 39323debea..6361e77c0d 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -42,7 +42,7 @@ function xmldb_glossary_upgrade($oldversion=0) { $result = $result && drop_field($table, $field); } - if ($result && $oldversion < 2007060300) { + if ($result && $oldversion < 2007060600) { require_once($CFG->dirroot.'/mod/glossary/lib.php'); // too much debug output $db->debug = false; diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index 3060e67f1d..faea0816de 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -82,7 +82,7 @@ function glossary_add_instance($glossary) { if ($returnid = insert_record("glossary", $glossary)) { $glossary->id = $returnid; $glossary = stripslashes_recursive($glossary); - glossary_grade_item_create($glossary); + glossary_grade_item_update($glossary); } return $returnid; @@ -317,16 +317,16 @@ function glossary_cron () { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ -function glossary_get_user_grades($glossaryid, $userid=0) { +function glossary_get_user_grades($glossary, $userid=0) { global $CFG; $user = $userid ? "AND u.id = $userid" : ""; - $sql = "SELECT u.id, avg(gr.rating) AS gradevalue + $sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS gradevalue FROM {$CFG->prefix}user u, {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_ratings gr WHERE u.id = ge.userid AND ge.id = gr.entryid - AND gr.userid != u.id AND ge.glossaryid = $glossaryid + AND gr.userid != u.id AND ge.glossaryid = $glossary->id $user GROUP BY u.id"; @@ -336,28 +336,22 @@ function glossary_get_user_grades($glossaryid, $userid=0) { /** * Update grades by firing grade_updated event * - * @param object $grade_item null means all glossaries + * @param object $glossary null means all glossaries * @param int $userid specific user only, 0 mean all */ -function glossary_update_grades($grade_item=null, $userid=0, $nullifnone=true) { +function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) { global $CFG; - if ($grade_item != null) { - if ($grades = glossary_get_user_grades($grade_item->iteminstance, $userid)) { - foreach ($grades as $grade) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $grade->id; - $eventdata->gradevalue = $grade->gradevalue; - events_trigger('grade_updated', $eventdata); - } + if ($glossary != null) { + if ($grades = glossary_get_user_grades($glossary, $userid)) { + grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades); } else if ($userid and $nullifnone) { - $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->userid = $userid; - $eventdata->gradevalue = NULL; - events_trigger('grade_updated', $eventdata); + $grade = new object(); + $grade->itemid = $glossary->id; + $grade->userid = $userid; + $grade->gradevalue = NULL; + grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade); } } else { @@ -367,11 +361,10 @@ function glossary_update_grades($grade_item=null, $userid=0, $nullifnone=true) { if ($rs = get_recordset_sql($sql)) { if ($rs->RecordCount() > 0) { while ($glossary = rs_fetch_next_record($rs)) { - if (!$glossary->assessed) { - continue; // no grading + glossary_grade_item_update($glossary); + if ($glossary->assessed) { + glossary_update_grades($glossary, 0, false); } - $grade_item = glossary_grade_item_get($glossary); - glossary_update_grades($grade_item, 0, false); } } rs_close($rs); @@ -380,80 +373,21 @@ function glossary_update_grades($grade_item=null, $userid=0, $nullifnone=true) { } /** - * Return (create if needed) grade item for given glossary - * - * @param object $glossary object with optional cmidnumber - * @return object grade_item - */ -function glossary_grade_item_get($glossary) { - if ($items = grade_get_items($glossary->course, 'mod', 'glossary', $glossary->id)) { - if (count($items) > 1) { - debugging('Multiple grade items present!'); - } - $grade_item = reset($items); - - } else { - if (!isset($glossary->cmidnumber)) { - if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id)) { - error("Course Module ID was incorrect"); - } - $glossary->cmidnumber = $cm->idnumber; - } - if (!$itemid = glossary_grade_item_create($glossary)) { - error('Can not create grade item!'); - } - $grade_item = grade_item::fetch('id', $itemid); - } - - return $grade_item; -} - -/** - * Update grade item for given glossary + * Create/update grade item for given glossary * * @param object $glossary object with extra cmidnumber - * @return object grade_item + * @return int, 0 if ok, error code otherwise */ function glossary_grade_item_update($glossary) { - $grade_item = glossary_grade_item_get($glossary); - - $grade_item->name = $glossary->name; - $grade_item->idnumber = $glossary->cmidnumber; - - if (!$glossary->assessed or $glossary->scale == 0) { - //how to indicate no grading? - $grade_item->gradetype = GRADE_TYPE_TEXT; - - } else if ($glossary->scale > 0) { - $grade_item->gradetype = GRADE_TYPE_VALUE; - $grade_item->grademax = $glossary->scale; - $grade_item->grademin = 0; - - } else if ($glossary->scale < 0) { - $grade_item->gradetype = GRADE_TYPE_SCALE; - $grade_item->scaleid = -$glossary->scale; + global $CFG; + if (!function_exists('grade_update')) { //workaround for buggy PHP versions + require_once($CFG->libdir.'/gradelib.php'); } - $grade_item->update(); -} - -/** - * Create grade item for given glossary - * - * @param object $glossary object with extra cmidnumber - * @return object grade_item - */ -function glossary_grade_item_create($glossary) { - $params = array('courseid' =>$glossary->course, - 'itemtype' =>'mod', - 'itemmodule' =>'glossary', - 'iteminstance'=>$glossary->id, - 'itemname' =>$glossary->name, - 'idnumber' =>$glossary->cmidnumber); + $params = array('itemname'=>$glossary->name, 'idnumber'=>$glossary->cmidnumber); if (!$glossary->assessed or $glossary->scale == 0) { - //how to indicate no grading? - $params['gradetype'] = GRADE_TYPE_TEXT; + $params['gradetype'] = GRADE_TYPE_NONE; } else if ($glossary->scale > 0) { $params['gradetype'] = GRADE_TYPE_VALUE; @@ -465,22 +399,19 @@ function glossary_grade_item_create($glossary) { $params['scaleid'] = -$glossary->scale; } - $itemid = grade_create_item($params); - return $itemid; + return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params); } /** * Delete grade item for given glossary * * @param object $glossary object - * @return object grade_item */ function glossary_grade_item_delete($glossary) { - if ($grade_items = grade_get_items($glossary->course, 'mod', 'glossary', $glossary->id)) { - foreach($grade_items as $grade_item) { - $grade_item->delete(); - } - } + global $CFG; + require_once($CFG->libdir.'/gradelib.php'); + + return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1)); } function glossary_get_participants($glossaryid) { diff --git a/mod/glossary/rate.php b/mod/glossary/rate.php index 245e34bd7c..3d086b7f76 100644 --- a/mod/glossary/rate.php +++ b/mod/glossary/rate.php @@ -36,8 +36,6 @@ require_capability('mod/glossary:rate', $context); } - $grade_item = glossary_grade_item_get($glossary); - if (!empty($_SERVER['HTTP_REFERER'])) { $returnurl = $_SERVER['HTTP_REFERER']; } else { @@ -73,7 +71,7 @@ //Check if we must delete the rate if ($rating == -999) { delete_records('glossary_ratings','userid',$oldrating->userid, 'entryid',$oldrating->entryid); - glossary_update_grades($grade_item, $entry->userid); + glossary_update_grades($glossary, $entry->userid); } else if ($rating != $oldrating->rating) { $oldrating->rating = $rating; @@ -81,7 +79,7 @@ if (! update_record("glossary_ratings", $oldrating)) { error("Could not update an old rating ($entry = $rating)"); } - glossary_update_grades($grade_item, $entry->userid); + glossary_update_grades($glossary, $entry->userid); } } else if ($rating >= 0) { @@ -94,7 +92,7 @@ if (! insert_record("glossary_ratings", $newrating)) { error("Could not insert a new rating ($entry->id = $rating)"); } - glossary_update_grades($grade_item, $entry->userid); + glossary_update_grades($glossary, $entry->userid); } } diff --git a/mod/glossary/version.php b/mod/glossary/version.php index afada32651..965c1cb574 100644 --- a/mod/glossary/version.php +++ b/mod/glossary/version.php @@ -5,7 +5,7 @@ /// This fragment is called by moodle_needs_upgrading() and /admin/index.php ///////////////////////////////////////////////////////////////////////////////// -$module->version = 2007060300; +$module->version = 2007060600; $module->requires = 2007060100; // Requires this Moodle version $module->cron = 0; // Period for cron to check this module (secs) -- 2.39.5