From b67ec72f57e2cb428b25518c4d9e4c41821f5956 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 6 Jun 2007 23:04:24 +0000 Subject: [PATCH] MDL-9516 update_grade(); now required $source of grading to allow modification of original grades in activities through grade_updated event --- grade/import/xml/index.php | 4 +- lib/db/events.php | 20 +---- lib/db/install.xml | 2 +- lib/gradelib.php | 141 ++++++++++------------------------- mod/assignment/db/events.php | 12 +-- mod/assignment/lib.php | 13 ++-- mod/assignment/version.php | 2 +- mod/data/lib.php | 8 +- mod/forum/lib.php | 8 +- mod/glossary/lib.php | 8 +- 10 files changed, 69 insertions(+), 149 deletions(-) diff --git a/grade/import/xml/index.php b/grade/import/xml/index.php index 4111ae0eeb..36ce58859b 100755 --- a/grade/import/xml/index.php +++ b/grade/import/xml/index.php @@ -60,8 +60,10 @@ if ( $formdata = $mform->get_data() ) { $eventdata->userid = $result['#']['student'][0]['#']; $eventdata->gradevalue = $result['#']['score'][0]['#']; + /* TODO: use grade_update() instead trigger_event('grade_updated_external', $eventdata); - echo "
triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue"; + echo "
triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue"; + */ } } diff --git a/lib/db/events.php b/lib/db/events.php index 5def23b9d7..d4a782d25f 100644 --- a/lib/db/events.php +++ b/lib/db/events.php @@ -26,24 +26,6 @@ /////////////////////////////////////////////////////////////////////////// -$handlers = array ( -/* - * Grades created/modified outside of activities (import, gradebook overrides, etc.) - * - * required parameters (object or array): - * 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 - */ - 'grade_update_request' => array ( - 'handlerfile' => '/lib/gradelib.php', - 'handlerfunction' => 'grade_handler', - 'schedule' => 'instant' - ) -); +$handlers = array (); // no handlers for now in core ?> diff --git a/lib/db/install.xml b/lib/db/install.xml index 7634a0fd62..cd11c8a7ec 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1191,7 +1191,7 @@ - +
diff --git a/lib/gradelib.php b/lib/gradelib.php index 34b1b2ada0..40c62c309f 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -69,7 +69,7 @@ require_once($CFG->libdir . '/grade/grade_tree.php'); /***** PUBLIC GRADE API *****/ -function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grade=NULL, $itemdetails=NULL) { +function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) { // only following grade_item properties can be changed/used in this function $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'deleted'); @@ -146,7 +146,7 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum } // no grade submitted - if (empty($grade)) { + if (empty($grades)) { return GRADE_UPDATE_OK; } @@ -157,18 +157,14 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum } /// Finally start processing of grades - if (is_object($grade)) { - $grades = array($grade); + if (is_object($grades)) { + $grades = array($grades); } else { - if (array_key_exists('userid', $grade)) { - $grades = array($grade); - } else { - $grades = $grade; + if (array_key_exists('userid', $grades)) { + $grades = array($grades); } } - unset($grade); - foreach ($grades as $grade) { $grade = (array)$grade; if (empty($grade['userid'])) { @@ -205,16 +201,46 @@ function grade_update($courseid, $itemtype, $itemmodule, $iteminstance, $itemnum $rawgrade->insert(); } - //trigger grade_updated event notification + // trigger grade_updated event notification $eventdata = new object(); - $eventdata->itemid = $grade_item->id; - $eventdata->grade = $grade; + $eventdata->source = $source; + $eventdata->itemid = $grade_item->id; + $eventdata->courseid = $grade_item->courseid; + $eventdata->itemtype = $grade_item->itemtype; + $eventdata->itemmodule = $grade_item->itemmodule; + $eventdata->iteminstance = $grade_item->iteminstance; + $eventdata->itemnumber = $grade_item->itemnumber; + $eventdata->idnumber = $grade_item->idnumber; + $eventdata->grade = $grade; events_trigger('grade_updated', $eventdata); } return GRADE_UPDATE_OK; } + +/** +* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not. +* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked. +* 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 +* return the locked state of the specific grade. +* +* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc +* @param string $itemmodule 'forum, 'quiz', 'csv' etc +* @param int $iteminstance id of the item module +* @param int $itemnumber Optional number of the item to check +* @param int $userid ID of the user who owns the grade +* @return boolean Whether the grade is locked or not +*/ +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); +} + + /***** END OF PUBLIC API *****/ /** @@ -281,28 +307,6 @@ function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_ } } - -/** -* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not. -* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked. -* 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 -* return the locked state of the specific grade. -* -* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc -* @param string $itemmodule 'forum, 'quiz', 'csv' etc -* @param int $iteminstance id of the item module -* @param int $itemnumber Optional number of the item to check -* @param int $userid ID of the user who owns the grade -* @return boolean Whether the grade is locked or not -*/ -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. * The search is further restricted, so that only grade_items that have needs_update == TRUE @@ -518,71 +522,4 @@ function standardise_score($gradevalue, $source_min, $source_max, $target_min, $ } -/** - * 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 - * @return boolean success - * - */ -function grade_handler($eventdata) { - $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 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 - $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; - - } else if (count($grade_items) == 1) { - $grade_item = reset($grade_items); - unset($grade_items); //release memory - - } else { - debugging('More than one grade item matched, grade update request failed'); - return true; - } - - // !! TODO: whitelist only some types such as 'import'(?) 'manual'(?) and ignore the rest!! - if ($grade_item->itemtype == 'mod') { - // 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['feedback'])) { - $grade->feedback = $eventdata['feedback']; - } - 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 bdc5610740..2fdb74ea94 100644 --- a/mod/assignment/db/events.php +++ b/mod/assignment/db/events.php @@ -28,13 +28,13 @@ $handlers = array ( -/* - * Grades created/modified outside of activities (import, gradebook overrides, etc.) - * see description in lib/db/events.php - */ - 'grade_updated_external' => array ( + /* + * Grade created or modified notification. + * See event format in lib/gradelib.php function update_grade() + */ + 'grade_updated' => array ( 'handlerfile' => '/mod/assignment/lib.php', - 'handlerfunction' => 'assignment_grade_update_handler', + 'handlerfunction' => 'assignment_grade_handler', 'schedule' => 'instant' ) ); diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 95b40f9c1d..a123823808 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -1843,7 +1843,7 @@ function assignment_update_grades($assignment=null, $userid=0, $nullifnone=true) $grades[$k]->gradevalue = null; } } - grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades); + grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, $grades); } } else { @@ -1895,7 +1895,7 @@ function assignment_grade_item_update($assignment) { $params['gradetype'] = GRADE_TYPE_NONE; } - return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, $params); + return grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, $params); } /** @@ -1912,19 +1912,18 @@ function assignment_grade_item_delete($assignment) { $assignment->courseid = $assignment->course; } - return grade_update($assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted'=>1)); + return grade_update('mod/assignment', $assignment->courseid, 'mod', 'assignment', $assignment->id, 0, NULL, array('deleted'=>1)); } /** - * Something wants to change the grade from outside using "grade_updated_external" event. - * Final static method - do not override! + * Something wants to change the grade from outside using "grade_updated" event. * - * see eventdata description in lib/db/events.php */ -function assignment_grade_update_handler($eventdata) { +function assignment_grade_handler($eventdata) { global $CFG, $USER; //TODO: ... + // check source to prevent infinite loops ;-) return true; } diff --git a/mod/assignment/version.php b/mod/assignment/version.php index 01272114fa..bf82d41dc2 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 = 2007060600; +$module->version = 2007060700; $module->requires = 2007052800; // Requires this Moodle version $module->cron = 60; diff --git a/mod/data/lib.php b/mod/data/lib.php index ef10f9afb6..a5c92e9cf4 100755 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -769,14 +769,14 @@ function data_update_grades($data=null, $userid=0, $nullifnone=true) { if ($data != null) { if ($grades = data_get_user_grades($data, $userid)) { - grade_update($data->course, 'mod', 'data', $data->id, 0, $grades); + grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grades); } else if ($userid and $nullifnone) { $grade = new object(); $grade->itemid = $data->id; $grade->userid = $userid; $grade->gradevalue = NULL; - grade_update($data->course, 'mod', 'data', $data->id, 0, $grade); + grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, $grade); } } else { @@ -824,7 +824,7 @@ function data_grade_item_update($data) { $params['scaleid'] = -$data->scale; } - return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, $params); + return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, $params); } /** @@ -837,7 +837,7 @@ function data_grade_item_delete($data) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); - return grade_update($data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1)); + return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1)); } /************************************************************************ diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 426f3a4a68..a0303bdb6d 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1134,14 +1134,14 @@ function forum_update_grades($forum=null, $userid=0, $nullifnone=true) { if ($forum != null) { if ($grades = forum_get_user_grades($forum, $userid)) { - grade_update($forum->course, 'mod', 'forum', $forum->id, 0, $grades); + grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, $grades); } else if ($userid and $nullifnone) { $grade = new object(); $grade->itemid = $forum->id; $grade->userid = $userid; $grade->gradevalue = NULL; - grade_update($data->course, 'mod', 'forum', $forum->id, 0, $grade); + grade_update('mod/forum', $data->course, 'mod', 'forum', $forum->id, 0, $grade); } } else { @@ -1189,7 +1189,7 @@ function forum_grade_item_update($forum) { $params['scaleid'] = -$forum->scale; } - return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, $params); + return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, NULL, $params); } /** @@ -1202,7 +1202,7 @@ function forum_grade_item_delete($forum) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); - return grade_update($forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1)); + return grade_update('mod/forum', $forum->course, 'mod', 'forum', $forum->id, 0, NULL, array('deleted'=>1)); } diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index faea0816de..7147c130af 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -344,14 +344,14 @@ function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) { if ($glossary != null) { if ($grades = glossary_get_user_grades($glossary, $userid)) { - grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades); + grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades); } else if ($userid and $nullifnone) { $grade = new object(); $grade->itemid = $glossary->id; $grade->userid = $userid; $grade->gradevalue = NULL; - grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade); + grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grade); } } else { @@ -399,7 +399,7 @@ function glossary_grade_item_update($glossary) { $params['scaleid'] = -$glossary->scale; } - return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params); + return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, $params); } /** @@ -411,7 +411,7 @@ function glossary_grade_item_delete($glossary) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); - return grade_update($glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1)); + return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1)); } function glossary_get_participants($glossaryid) { -- 2.39.5