Skip to content

Commit

Permalink
MDL-39087 Add new helper methods to the plugin_manager API
Browse files Browse the repository at this point in the history
These are mainly intended for callers that had to iterate over
get_plugins() result manually.
  • Loading branch information
mudrd8mz committed Apr 11, 2013
1 parent c6f4c88 commit d7d48b4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
3 changes: 1 addition & 2 deletions admin/courseformats.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@


$return = new moodle_url('/admin/settings.php', array('section' => 'manageformats')); $return = new moodle_url('/admin/settings.php', array('section' => 'manageformats'));


$allplugins = plugin_manager::instance()->get_plugins(); $formatplugins = plugin_manager::instance()->get_plugins_of_type('format');
$formatplugins = $allplugins['format'];
$sortorder = array_flip(array_keys($formatplugins)); $sortorder = array_flip(array_keys($formatplugins));


if (!isset($formatplugins[$formatname])) { if (!isset($formatplugins[$formatname])) {
Expand Down
6 changes: 2 additions & 4 deletions lib/adminlib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6071,8 +6071,7 @@ public function is_related($query) {
if (parent::is_related($query)) { if (parent::is_related($query)) {
return true; return true;
} }
$allplugins = plugin_manager::instance()->get_plugins(); $formats = plugin_manager::instance()->get_plugins_of_type('format');
$formats = $allplugins['format'];
foreach ($formats as $format) { foreach ($formats as $format) {
if (strpos($format->component, $query) !== false || if (strpos($format->component, $query) !== false ||
strpos(textlib::strtolower($format->displayname), $query) !== false) { strpos(textlib::strtolower($format->displayname), $query) !== false) {
Expand All @@ -6095,8 +6094,7 @@ public function output_html($data, $query='') {
$return = $OUTPUT->heading(new lang_string('courseformats'), 3, 'main'); $return = $OUTPUT->heading(new lang_string('courseformats'), 3, 'main');
$return .= $OUTPUT->box_start('generalbox formatsui'); $return .= $OUTPUT->box_start('generalbox formatsui');


$allplugins = plugin_manager::instance()->get_plugins(); $formats = plugin_manager::instance()->get_plugins_of_type('format');
$formats = $allplugins['format'];


// display strings // display strings
$txt = get_strings(array('settings', 'name', 'enable', 'disable', 'up', 'down', 'default', 'delete')); $txt = get_strings(array('settings', 'name', 'enable', 'disable', 'up', 'down', 'default', 'delete'));
Expand Down
3 changes: 1 addition & 2 deletions lib/editor/tinymce/settings.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
unset($settings); unset($settings);


require_once("$CFG->libdir/pluginlib.php"); require_once("$CFG->libdir/pluginlib.php");
$allplugins = plugin_manager::instance()->get_plugins(); foreach (plugin_manager::instance()->get_plugins_of_type('tinymce') as $plugin) {
foreach ($allplugins['tinymce'] as $plugin) {
$plugin->load_settings($ADMIN, 'editortinymce', $hassiteconfig); $plugin->load_settings($ADMIN, 'editortinymce', $hassiteconfig);
} }


Expand Down
63 changes: 61 additions & 2 deletions lib/pluginlib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ public function get_plugin_types($fullpaths = true) {
return $this->reorder_plugin_types(get_plugin_types($fullpaths)); return $this->reorder_plugin_types(get_plugin_types($fullpaths));
} }


/**
* Returns list of known plugins of the given type
*
* This method returns the subset of the tree returned by {@link self::get_plugins()}.
* If the given type is not known, empty array is returned.
*
* @param string $type plugin type, e.g. 'mod' or 'workshopallocation'
* @param bool $disablecache force reload, cache can be used otherwise
* @return array (string)plugin name (e.g. 'workshop') => corresponding subclass of {@link plugininfo_base}
*/
public function get_plugins_of_type($type, $disablecache=false) {

$plugins = $this->get_plugins($disablecache);

if (!isset($plugins[$type])) {
return array();
}

return $plugins[$type];
}

/** /**
* Returns a tree of known plugins and information about them * Returns a tree of known plugins and information about them
* *
Expand Down Expand Up @@ -161,6 +182,41 @@ public function get_plugins($disablecache=false) {
return $this->pluginsinfo; return $this->pluginsinfo;
} }


/**
* Returns list of all known subplugins of the given plugin
*
* For plugins that do not provide subplugins (i.e. there is no support for it),
* empty array is returned.
*
* @param string $component full component name, e.g. 'mod_workshop'
* @param bool $disablecache force reload, cache can be used otherwise
* @return array (string) component name (e.g. 'workshopallocation_random') => subclass of {@link plugininfo_base}
*/
public function get_subplugins_of_plugin($component, $disablecache=false) {

$pluginfo = $this->get_plugin_info($component, $disablecache);

if (is_null($pluginfo)) {
return array();
}

$subplugins = $this->get_subplugins($disablecache);

if (!isset($subplugins[$pluginfo->component])) {
return array();
}

$list = array();

foreach ($subplugins[$pluginfo->component] as $subdata) {
foreach ($this->get_plugins_of_type($subdata->type) as $subpluginfo) {
$list[$subpluginfo->component] = $subpluginfo;
}
}

return $list;
}

/** /**
* Returns list of plugins that define their subplugins and the information * Returns list of plugins that define their subplugins and the information
* about them from the db/subplugins.php file. * about them from the db/subplugins.php file.
Expand Down Expand Up @@ -294,12 +350,15 @@ public function plugintype_name_plural($type) {
} }


/** /**
* Returns information about the known plugin, or null
*
* @param string $component frankenstyle component name. * @param string $component frankenstyle component name.
* @param bool $disablecache force reload, cache can be used otherwise
* @return plugininfo_base|null the corresponding plugin information. * @return plugininfo_base|null the corresponding plugin information.
*/ */
public function get_plugin_info($component) { public function get_plugin_info($component, $disablecache=false) {
list($type, $name) = $this->normalize_component($component); list($type, $name) = $this->normalize_component($component);
$plugins = $this->get_plugins(); $plugins = $this->get_plugins($disablecache);
if (isset($plugins[$type][$name])) { if (isset($plugins[$type][$name])) {
return $plugins[$type][$name]; return $plugins[$type][$name];
} else { } else {
Expand Down
25 changes: 25 additions & 0 deletions lib/tests/pluginlib_test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@ public function test_plugin_manager_instance() {
$this->assertTrue($pluginman instanceof testable_plugin_manager); $this->assertTrue($pluginman instanceof testable_plugin_manager);
} }


public function test_get_plugins_of_type() {
$pluginman = testable_plugin_manager::instance();
$mods = $pluginman->get_plugins_of_type('mod');
$this->assertEquals('array', gettype($mods));
$this->assertEquals(2, count($mods));
$this->assertTrue($mods['foo'] instanceof testable_plugininfo_mod);
$this->assertTrue($mods['bar'] instanceof testable_plugininfo_mod);
$foolishes = $pluginman->get_plugins_of_type('foolish');
$this->assertEquals(1, count($foolishes));
$this->assertTrue($foolishes['frog'] instanceof testable_pluginfo_foolish);
$unknown = $pluginman->get_plugins_of_type('muhehe');
$this->assertSame(array(), $unknown);
}

public function test_get_plugins() { public function test_get_plugins() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins(); $plugins = $pluginman->get_plugins();
$this->assertEquals('array', gettype($plugins));
$this->assertTrue(isset($plugins['mod']['foo'])); $this->assertTrue(isset($plugins['mod']['foo']));
$this->assertTrue(isset($plugins['mod']['bar'])); $this->assertTrue(isset($plugins['mod']['bar']));
$this->assertTrue(isset($plugins['foolish']['frog'])); $this->assertTrue(isset($plugins['foolish']['frog']));
Expand All @@ -60,6 +75,16 @@ public function test_get_plugins() {
$this->assertTrue($plugins['foolish']['frog'] instanceof testable_pluginfo_foolish); $this->assertTrue($plugins['foolish']['frog'] instanceof testable_pluginfo_foolish);
} }


public function test_get_subplugins_of_plugin() {
$pluginman = testable_plugin_manager::instance();
$this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_missing'));
$this->assertSame(array(), $pluginman->get_subplugins_of_plugin('mod_bar'));
$foosubs = $pluginman->get_subplugins_of_plugin('mod_foo');
$this->assertEquals('array', gettype($foosubs));
$this->assertEquals(1, count($foosubs));
$this->assertTrue($foosubs['foolish_frog'] instanceof testable_pluginfo_foolish);
}

public function test_get_subplugins() { public function test_get_subplugins() {
$pluginman = testable_plugin_manager::instance(); $pluginman = testable_plugin_manager::instance();
$subplugins = $pluginman->get_subplugins(); $subplugins = $pluginman->get_subplugins();
Expand Down
2 changes: 2 additions & 0 deletions lib/upgrade.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ information provided here is intended especially for developers.
* Additional (optional) param $onlyactive has been added to get_enrolled_users, count_enrolled_users * Additional (optional) param $onlyactive has been added to get_enrolled_users, count_enrolled_users
functions to get information for only active (excluding suspended enrolments) users. Included two functions to get information for only active (excluding suspended enrolments) users. Included two
helper functions extract_suspended_users, get_suspended_userids to extract suspended user information. helper functions extract_suspended_users, get_suspended_userids to extract suspended user information.
* The plugin_manager class now provides two new helper methods for getting information
about known plugins: get_plugins_of_type() and get_subplugins_of_plugin().


Database (DML) layer: Database (DML) layer:
* $DB->sql_empty() is deprecated, you have to use sql parameters with empty values instead, * $DB->sql_empty() is deprecated, you have to use sql parameters with empty values instead,
Expand Down

0 comments on commit d7d48b4

Please sign in to comment.