From 0856223c505494386c57d473fdd877cd14c776d9 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Thu, 19 Apr 2007 08:45:30 +0000 Subject: [PATCH] mechanism to install events --- lib/adminlib.php | 6 ++ lib/blocklib.php | 4 ++ lib/db/upgrade.php | 1 + lib/eventslib.php | 152 +++++++++++++++++++++++++++++++++++++++++++++ lib/setup.php | 3 +- 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100755 lib/eventslib.php diff --git a/lib/adminlib.php b/lib/adminlib.php index fd0be41112..c627124417 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -114,6 +114,8 @@ function upgrade_plugins($type, $dir, $return) { if (!update_capabilities($type.'/'.$plug)) { error('Could not set up the capabilities for '.$module->name.'!'); } + events_update_definition($type.'/'.$plug); + notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess'); } else { notify('Installing '. $plugin->name .' FAILED!'); @@ -151,6 +153,7 @@ function upgrade_plugins($type, $dir, $return) { if (!update_capabilities($type.'/'.$plug)) { error('Could not update '.$plugin->name.' capabilities!'); } + events_update_definition($type.'/'.$plug); notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess'); } else { notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!'); @@ -303,6 +306,7 @@ function upgrade_activity_modules($return) { if (!update_capabilities('mod/'.$module->name)) { error('Could not update '.$module->name.' capabilities!'); } + events_update_definition('mod/'.$module->name); $updated_modules = true; @@ -337,6 +341,8 @@ function upgrade_activity_modules($return) { if (!update_capabilities('mod/'.$module->name)) { error('Could not set up the capabilities for '.$module->name.'!'); } + + events_update_definition('mod/'.$module->name); notify(get_string('modulesuccess', '', $module->name), 'notifysuccess'); echo '
'; } else { diff --git a/lib/blocklib.php b/lib/blocklib.php index ca481f62f3..e4501c1803 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -1270,6 +1270,8 @@ function upgrade_blocks_plugins($continueto) { if (!update_capabilities($component)) { error('Could not update '.$block->name.' capabilities!'); } + + events_update_definition($component); notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess'); } else { notify('Upgrading block '. $block->name .' from '. $currblock->version .' to '. $block->version .' FAILED!'); @@ -1326,6 +1328,8 @@ function upgrade_blocks_plugins($continueto) { if (!update_capabilities($component)) { notify('Could not set up '.$block->name.' capabilities!'); } + + events_update_definition($component); notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess'); echo '
'; } else { diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index ff708ec084..c87cee433f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -756,6 +756,7 @@ function xmldb_main_upgrade($oldversion=0) { /// Adding keys to table events_handlers $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->addKeyInfo('eventname-handlermodule', XMLDB_KEY_UNIQUE, array('eventname', 'handlermodule')); /// Adding indexes to table events_handlers $table->addIndexInfo('eventname', XMLDB_INDEX_NOTUNIQUE, array('eventname')); diff --git a/lib/eventslib.php b/lib/eventslib.php new file mode 100755 index 0000000000..6f44cc6c7b --- /dev/null +++ b/lib/eventslib.php @@ -0,0 +1,152 @@ +libdir.'/db/events.php'; + $varprefix = 'moodle'; + } else { + $compparts = explode('/', $component); + + if ($compparts[0] == 'block') { + // Blocks are an exception. Blocks directory is 'blocks', and not + // 'block'. So we need to jump through hoops. + $defpath = $CFG->dirroot.'/'.$compparts[0]. + 's/'.$compparts[1].'/db/events.php'; + $varprefix = $compparts[0].'_'.$compparts[1]; + } else if ($compparts[0] == 'format') { + // Similar to the above, course formats are 'format' while they + // are stored in 'course/format'. + $defpath = $CFG->dirroot.'/course/'.$component.'/db/events.php'; + $varprefix = $compparts[0].'_'.$compparts[1]; + } else { + $defpath = $CFG->dirroot.'/'.$component.'/db/events.php'; + $varprefix = str_replace('/', '_', $component); + } + } + $events = array(); + + if (file_exists($defpath)) { + require($defpath); + $events = ${$varprefix.'_events'}; + } + + return $events; +} + +/** + * Gets the capabilities that have been cached in the database for this + * component. + * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' + * @return array of events + */ +function get_cached_events($component='moodle') { + if ($component == 'moodle') { + $storedevents = get_records_select('events_handlers', + "handlermodule LIKE 'moodle/%'"); + } else { + $storedevents = get_records_select('events_handlers', + "handlermodule LIKE '$component%'"); + } + + if (!empty($storedevents)) { + foreach ($storedevents as $storedevent) { + $eventname = $storedevent->eventname; + // not needed for comparisons + unset($storedevent->handlermodule); + unset($storedevent->id); + unset($storedevent->eventname); + $cachedevents[$eventname] = (array)$storedevent; + } + return $cachedevents; + } +} + + +/** + * Updates the capabilities table with the component capability definitions. + * If no parameters are given, the function updates the core moodle + * capabilities. + * + * Note that the absence of the db/access.php capabilities definition file + * will cause any stored capabilities for the component to be removed from + * the database. + * + * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' + * @return boolean + */ + +function events_update_definition($component='moodle') { + + $storedevents = array(); + + $fileevents = events_load_def($component); + // load event definitions from db tables + // if we detect an event being already stored, we discard from this array later + // the remaining needs to be removed + + $cachedevents = get_cached_events($component); + + /// compare the 2 arrays, and make adjustments + /// array_udiff is php 5 only =( + + if ($fileevents) { + foreach ($fileevents as $eventname => $fileevent) { + if (!empty($cachedevents[$eventname])) { + if ($cachedevents[$eventname] == $fileevent) { + unset($cachedevents[$eventname]); + continue; // breaks the cachedevents loop + } + } + // if we are here, no break is called, file event is new + $event = new object; + $event->eventname = $eventname; + $event->handlermodule = $component; + $event->handlerfile = $fileevent['handlerfile']; + $event->handlerfunction = $fileevent['handlerfunction']; + insert_record('events_handlers', $event); + } + } + + // clean up the left overs + // delete from db + events_cleanup($component, $cachedevents); + + return true; +} + +/** + * Deletes cached events that are no longer needed by the component. + * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results' + * @param $chachedevents - array of the cached events definitions that will be + * @return int - number of deprecated capabilities that have been removed + */ +function events_cleanup($component, $cachedevents) { + $deletecount = 0; + if ($cachedevents) { + foreach ($cachedevents as $eventname => $cachedevent) { + if (delete_records('events_handlers', 'eventname', $eventname, 'handlermodule', $component)) { + $deletecount++; + } + } + } + return $deletecount; +} + +?> \ No newline at end of file diff --git a/lib/setup.php b/lib/setup.php index 57c8587c81..5aa7d710b2 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -193,7 +193,8 @@ global $HTTPSPAGEREQUIRED; require_once($CFG->libdir .'/accesslib.php'); // Access control functions require_once($CFG->libdir .'/deprecatedlib.php'); // Deprecated functions included for backward compatibility require_once($CFG->libdir .'/moodlelib.php'); // Other general-purpose functions - + require_once($CFG->libdir .'/eventslib.php'); // Events functions + /// Disable errors for now - needed for installation when debug enabled in config.php if (isset($CFG->debug)) { $originalconfigdebug = $CFG->debug; -- 2.39.5