From 967f222f55aaa29ad7465cd5781782e51239606a Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Fri, 4 May 2007 02:15:46 +0000 Subject: [PATCH] MDL-9510, cron job support of legacy grade functions in modules. Implementation is not finished as the gradebook grade_added event is not complete --- admin/cron.php | 6 ++++++ lib/gradelib.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/admin/cron.php b/admin/cron.php index b703bc292a..6f33caca30 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -380,6 +380,12 @@ } } } + + // attemps to grab grades from third party/non-stard mods, or mods with no event + // implemented for 1.9 and above. + mtrace("Grabbing grades from modules if required..."); + include_once($CFG->dirroot.'/lib/gradelib.php'); + grades_grab_grades(); //Unset session variables and destroy it @session_unset(); diff --git a/lib/gradelib.php b/lib/gradelib.php index f190c83f47..fb9a627c59 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -154,4 +154,56 @@ function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) { return $count; } + +/* + * 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 event_trigger() function can be called to initiate + * an event as usual and copy/ *upgrade the data in the gradebook tables. + */ +function grades_grab_grades() { + + global $CFG, $db; + + if (!$mods = get_list_of_plugins('mod') ) { + error('No modules installed!'); + } + + foreach ($mods as $mod) { + + if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it + continue; + } + + $fullmod = $CFG->dirroot .'/mod/'. $mod; + + // include the module lib once + if (file_exists($fullmod.'/lib.php')) { + include_once($fullmod.'/lib.php'); + // look for mod_grades() function - old grade book pulling function + // to see if module supports grades, and check for event registration status + $gradefunc = $mod.'_grades'; + // if this mod has grades, but grade_added event is not registered + // then we need to pull grades into the new gradebook + if (function_exists($gradefunc) && !event_is_registered($mod, $gradefunc)) { + // get all instance of the mod + $module = get_record('modules', 'name', $mod); + if ($module && $modinstances = get_records('course_modules', 'module', $module->id)) { + foreach ($modinstances as $modinstance) { + // for each instance, call the xxx_grades() function + if ($grades = $gradefunc($modinstance->instance)) { + foreach ($grades->grades as $userid=>$usergrade) { + // make the grade_added eventdata + // missing grade event trigger + // trigger_event('grade_added', $eventdata); + } + } + } + } + } + } + } +} + ?> -- 2.39.5