]> git.mjollnir.org Git - moodle.git/commitdiff
mechanism to install events
authortoyomoyo <toyomoyo>
Thu, 19 Apr 2007 08:45:30 +0000 (08:45 +0000)
committertoyomoyo <toyomoyo>
Thu, 19 Apr 2007 08:45:30 +0000 (08:45 +0000)
lib/adminlib.php
lib/blocklib.php
lib/db/upgrade.php
lib/eventslib.php [new file with mode: 0755]
lib/setup.php

index fd0be41112f417d3ca8e2fe3ffdabc277796bcd0..c627124417510946cb5cf04b758c9adb7699cb49 100644 (file)
@@ -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 '<hr />';
                 } else {
index ca481f62f31c352ae7cbde3c913da0651d4c92d4..e4501c1803ee113d767b62007f531ee582fedb91 100644 (file)
@@ -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 '<hr />';
                 } else {
index ff708ec084420ebc39c08b870a8bdd8018548c91..c87cee433fa8e5fdefcde051800a221461073ca6 100644 (file)
@@ -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 (executable)
index 0000000..6f44cc6
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+/**
+ * Library of functions for events manipulation.
+ * 
+ * @author Martin Dougiamas and many others
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package moodlecore
+ */
+
+
+/**
+ * Loads the events definitions for the component (from file). If no
+ * events are defined for the component, we simply return an empty array.
+ * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
+ * @return array of capabilities
+ */
+function events_load_def($component) {
+    global $CFG;
+
+    if ($component == 'moodle') {
+        $defpath = $CFG->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
index 57c8587c813e332486f1788e295738472e65f4e1..5aa7d710b2aaa530aeb734eafa756a147fcbfb83 100644 (file)
@@ -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;