MDL-9516 update_grade(); now required $source of grading to allow modification of original grades in activities through grade_updated event

This commit is contained in:
skodak 2007-06-06 23:04:24 +00:00
parent 13127313d3
commit b67ec72f57
10 changed files with 69 additions and 149 deletions

View File

@ -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 "<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";
*/
}
}

View File

@ -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
?>

View File

@ -1191,7 +1191,7 @@
<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"/>

View File

@ -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;
}
?>

View File

@ -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'
)
);

View File

@ -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;
}

View File

@ -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;

View File

@ -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));
}
/************************************************************************

View File

@ -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));
}

View File

@ -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) {