Merge branch 'MDL-50645_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Dan Poltawski
2015-07-27 10:36:55 +01:00
5 changed files with 129 additions and 19 deletions
+112 -14
View File
@@ -4843,10 +4843,12 @@ function remove_course_contents($courseid, $showfeedback = true, array $options
// Cleanup the rest of plugins.
$cleanuplugintypes = array('report', 'coursereport', 'format');
$callbacks = get_plugins_with_function('delete_course', 'lib.php');
foreach ($cleanuplugintypes as $type) {
$plugins = get_plugin_list_with_function($type, 'delete_course', 'lib.php');
foreach ($plugins as $plugin => $pluginfunction) {
$pluginfunction($course->id, $showfeedback);
if (!empty($callbacks[$type])) {
foreach ($callbacks[$type] as $pluginfunction) {
$pluginfunction($course->id, $showfeedback);
}
}
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted.get_string('type_'.$type.'_plural', 'plugin'), 'notifysuccess');
@@ -7023,26 +7025,122 @@ function is_valid_plugin_name($name) {
* and the function names as values (e.g. 'report_courselist_hook', 'forum_hook').
*/
function get_plugin_list_with_function($plugintype, $function, $file = 'lib.php') {
global $CFG;
// We don't include here as all plugin types files would be included.
$plugins = get_plugins_with_function($function, $file, false);
if (empty($plugins[$plugintype])) {
return array();
}
$allplugins = core_component::get_plugin_list($plugintype);
// Reformat the array and include the files.
$pluginfunctions = array();
$pluginswithfile = core_component::get_plugin_list_with_file($plugintype, $file, true);
foreach ($pluginswithfile as $plugin => $notused) {
$fullfunction = $plugintype . '_' . $plugin . '_' . $function;
foreach ($plugins[$plugintype] as $pluginname => $functionname) {
if (function_exists($fullfunction)) {
// Function exists with standard name. Store, indexed by frankenstyle name of plugin.
$pluginfunctions[$plugintype . '_' . $plugin] = $fullfunction;
// Check that it has not been removed and the file is still available.
if (!empty($allplugins[$pluginname])) {
} else if ($plugintype === 'mod') {
// For modules, we also allow plugin without full frankenstyle but just starting with the module name.
$shortfunction = $plugin . '_' . $function;
if (function_exists($shortfunction)) {
$pluginfunctions[$plugintype . '_' . $plugin] = $shortfunction;
$filepath = $allplugins[$pluginname] . DIRECTORY_SEPARATOR . $file;
if (file_exists($filepath)) {
include_once($filepath);
$pluginfunctions[$plugintype . '_' . $pluginname] = $functionname;
}
}
}
return $pluginfunctions;
}
/**
* Get a list of all the plugins that define a certain API function in a certain file.
*
* @param string $function the part of the name of the function after the
* frankenstyle prefix. e.g 'hook' if you are looking for functions with
* names like report_courselist_hook.
* @param string $file the name of file within the plugin that defines the
* function. Defaults to lib.php.
* @param bool $include Whether to include the files that contain the functions or not.
* @return array with [plugintype][plugin] = functionname
*/
function get_plugins_with_function($function, $file = 'lib.php', $include = true) {
global $CFG;
$cache = \cache::make('core', 'plugin_functions');
// Including both although I doubt that we will find two functions definitions with the same name.
// Clearning the filename as cache_helper::hash_key only allows a-zA-Z0-9_.
$key = $function . '_' . clean_param($file, PARAM_ALPHA);
if ($pluginfunctions = $cache->get($key)) {
// Checking that the files are still available.
foreach ($pluginfunctions as $plugintype => $plugins) {
$allplugins = \core_component::get_plugin_list($plugintype);
foreach ($plugins as $plugin => $fullpath) {
// Cache might be out of sync with the codebase, skip the plugin if it is not available.
if (empty($allplugins[$plugin])) {
unset($pluginfunctions[$plugintype][$plugin]);
continue;
}
$fileexists = file_exists($allplugins[$plugin] . DIRECTORY_SEPARATOR . $file);
if ($include && $fileexists) {
// Include the files if it was requested.
include_once($allplugins[$plugin] . DIRECTORY_SEPARATOR . $file);
} else if (!$fileexists) {
// If the file is not available any more it should not be returned.
unset($pluginfunctions[$plugintype][$plugin]);
}
}
}
return $pluginfunctions;
}
$pluginfunctions = array();
// To fill the cached. Also, everything should continue working with cache disabled.
$plugintypes = \core_component::get_plugin_types();
foreach ($plugintypes as $plugintype => $unused) {
// We need to include files here.
$pluginswithfile = \core_component::get_plugin_list_with_file($plugintype, $file, true);
foreach ($pluginswithfile as $plugin => $notused) {
$fullfunction = $plugintype . '_' . $plugin . '_' . $function;
$pluginfunction = false;
if (function_exists($fullfunction)) {
// Function exists with standard name. Store, indexed by frankenstyle name of plugin.
$pluginfunction = $fullfunction;
} else if ($plugintype === 'mod') {
// For modules, we also allow plugin without full frankenstyle but just starting with the module name.
$shortfunction = $plugin . '_' . $function;
if (function_exists($shortfunction)) {
$pluginfunction = $shortfunction;
}
}
if ($pluginfunction) {
if (empty($pluginfunctions[$plugintype])) {
$pluginfunctions[$plugintype] = array();
}
$pluginfunctions[$plugintype][$plugin] = $pluginfunction;
}
}
}
$cache->set($key, $pluginfunctions);
return $pluginfunctions;
}
/**
* Lists plugin-like directories within specified directory
*