$eventdata->userid = $result['#']['student'][0]['#'];
$eventdata->gradevalue = $result['#']['score'][0]['#'];
+ /* TODO: use grade_update() instead
trigger_event('grade_updated_external', $eventdata);
- echo "<br/>triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue";
+ echo "<br/>triggering event for $eventdata->idnumber... student id is $eventdata->userid and grade is $eventdata->gradevalue";
+ */
}
}
///////////////////////////////////////////////////////////////////////////
-$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
?>
<INDEX NAME="mnethostid_username" UNIQUE="true" FIELDS="mnet_host_id, username"/>
</INDEXES>
</TABLE>
- <TABLE NAME="events_handlers" COMMENT="This table is for storing which components requests what type of event, and the location of the responsible handlers. For example, the grade book can register 'grade_updated' event with a function grade_handler() that should be called event time an 'grade_updated' event is triggered by a module." PREVIOUS="mnet_sso_access_control" NEXT="events_queue">
+ <TABLE NAME="events_handlers" COMMENT="This table is for storing which components requests what type of event, and the location of the responsible handlers. For example, the assignment registers 'grade_updated' event with a function assignment_grade_handler() that should be called event time an 'grade_updated' event is triggered by grade_update() function." PREVIOUS="mnet_sso_access_control" NEXT="events_queue">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" ENUM="false" COMMENT="id of the table, please edit me" NEXT="eventname"/>
<FIELD NAME="eventname" TYPE="char" LENGTH="166" NOTNULL="true" SEQUENCE="false" ENUM="false" COMMENT="name of the event, e.g. 'grade_updated'" PREVIOUS="id" NEXT="handlermodule"/>
/***** 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');
}
// no grade submitted
- if (empty($grade)) {
+ if (empty($grades)) {
return GRADE_UPDATE_OK;
}
}
/// 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'])) {
$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 *****/
/**
}
}
-
-/**
-* 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
}
-/**
- * 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;
-
-}
-
-
?>
$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'
)
);
$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 {
$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);
}
/**
$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;
}
// This fragment is called by /admin/index.php
////////////////////////////////////////////////////////////////////////////////
-$module->version = 2007060600;
+$module->version = 2007060700;
$module->requires = 2007052800; // Requires this Moodle version
$module->cron = 60;
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 {
$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);
}
/**
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));
}
/************************************************************************
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 {
$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);
}
/**
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));
}
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 {
$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);
}
/**
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) {