MDL-20636 fix most of the remaining codechecker issues in mod/quiz and lib/questionli...
[moodle.git] / lib / pluginlib.php
blob17e829849f2c4cbfcc2c569e39cef925035d6e2c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Defines classes used for plugins management
20 * This library provides a unified interface to various plugin types in
21 * Moodle. It is mainly used by the plugins management admin page and the
22 * plugins check page during the upgrade.
24 * @package core
25 * @subpackage admin
26 * @copyright 2011 David Mudrak <david@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 /**
33 * Singleton class providing general plugins management functionality
35 class plugin_manager {
37 /** the plugin is shipped with standard Moodle distribution */
38 const PLUGIN_SOURCE_STANDARD = 'std';
39 /** the plugin is added extension */
40 const PLUGIN_SOURCE_EXTENSION = 'ext';
42 /** the plugin uses neither database nor capabilities, no versions */
43 const PLUGIN_STATUS_NODB = 'nodb';
44 /** the plugin is up-to-date */
45 const PLUGIN_STATUS_UPTODATE = 'uptodate';
46 /** the plugin is about to be installed */
47 const PLUGIN_STATUS_NEW = 'new';
48 /** the plugin is about to be upgraded */
49 const PLUGIN_STATUS_UPGRADE = 'upgrade';
50 /** the version at the disk is lower than the one already installed */
51 const PLUGIN_STATUS_DOWNGRADE = 'downgrade';
52 /** the plugin is installed but missing from disk */
53 const PLUGIN_STATUS_MISSING = 'missing';
55 /** @var plugin_manager holds the singleton instance */
56 protected static $singletoninstance;
57 /** @var array of raw plugins information */
58 protected $pluginsinfo = null;
59 /** @var array of raw subplugins information */
60 protected $subpluginsinfo = null;
62 /**
63 * Direct initiation not allowed, use the factory method {@link self::instance()}
65 * @todo we might want to specify just a single plugin type to work with
67 protected function __construct() {
68 $this->get_plugins(true);
71 /**
72 * Sorry, this is singleton
74 protected function __clone() {
77 /**
78 * Factory method for this class
80 * @return plugin_manager the singleton instance
82 public static function instance() {
83 global $CFG;
85 if (is_null(self::$singletoninstance)) {
86 self::$singletoninstance = new self();
88 return self::$singletoninstance;
91 /**
92 * Returns a tree of known plugins and information about them
94 * @param bool $disablecache force reload, cache can be used otherwise
95 * @return array
97 public function get_plugins($disablecache=false) {
99 if ($disablecache or is_null($this->pluginsinfo)) {
100 $this->pluginsinfo = array();
101 $plugintypes = get_plugin_types();
102 foreach ($plugintypes as $plugintype => $plugintyperootdir) {
103 if (in_array($plugintype, array('base', 'general'))) {
104 throw new coding_exception('Illegal usage of reserved word for plugin type');
106 if (class_exists('plugintype_' . $plugintype)) {
107 $plugintypeclass = 'plugintype_' . $plugintype;
108 } else {
109 $plugintypeclass = 'plugintype_general';
111 if (!in_array('plugintype_interface', class_implements($plugintypeclass))) {
112 throw new coding_exception('Class ' . $plugintypeclass . ' must implement plugintype_interface');
114 $plugins = call_user_func(array($plugintypeclass, 'get_plugins'), $plugintype, $plugintyperootdir, $plugintypeclass);
115 $this->pluginsinfo[$plugintype] = $plugins;
119 return $this->pluginsinfo;
123 * Returns list of plugins that define their subplugins and information about them
125 * At the moment, only activity modules can define subplugins.
127 * @param double $disablecache force reload, cache can be used otherwise
128 * @return array
130 public function get_subplugins($disablecache=false) {
132 if ($disablecache or is_null($this->subpluginsinfo)) {
133 $this->subpluginsinfo = array();
134 $mods = get_plugin_list('mod');
135 foreach ($mods as $mod => $moddir) {
136 $modsubplugins = array();
137 if (file_exists($moddir . '/db/subplugins.php')) {
138 include($moddir . '/db/subplugins.php');
139 foreach ($subplugins as $subplugintype => $subplugintyperootdir) {
140 $subplugin = new stdClass();
141 $subplugin->type = $subplugintype;
142 $subplugin->typerootdir = $subplugintyperootdir;
143 $modsubplugins[$subplugintype] = $subplugin;
145 $this->subpluginsinfo['mod_' . $mod] = $modsubplugins;
150 return $this->subpluginsinfo;
154 * Returns the name of the plugin that defines the given subplugin type
156 * If the given subplugin type is not actually a subplugin, returns false.
158 * @param string $subplugintype the name of subplugin type, eg. workshopform or quiz
159 * @return false|string the name of the parent plugin, eg. mod_workshop
161 public function get_parent_of_subplugin($subplugintype) {
163 $parent = false;
164 foreach ($this->get_subplugins() as $pluginname => $subplugintypes) {
165 if (isset($subplugintypes[$subplugintype])) {
166 $parent = $pluginname;
167 break;
171 return $parent;
175 * Returns a localized name of a given plugin
177 * @param string $plugin name of the plugin, eg mod_workshop or auth_ldap
178 * @return string
180 public function plugin_name($plugin) {
181 list($type, $name) = normalize_component($plugin);
182 return $this->pluginsinfo[$type][$name]->displayname;
186 * Returns a localized name of a plugin type in plural form
188 * Most plugin types define their names in core_plugin lang file. In case of subplugins,
189 * we try to ask the parent plugin for the name. In the worst case, we will return
190 * the value of the passed $type parameter.
192 * @param string $type the type of the plugin, e.g. mod or workshopform
193 * @return string
195 public function plugintype_name_plural($type) {
197 if (get_string_manager()->string_exists('type_' . $type . '_plural', 'core_plugin')) {
198 // for most plugin types, their names are defined in core_plugin lang file
199 return get_string('type_' . $type . '_plural', 'core_plugin');
201 } else if ($parent = $this->get_parent_of_subplugin($type)) {
202 // if this is a subplugin, try to ask the parent plugin for the name
203 if (get_string_manager()->string_exists('subplugintype_' . $type . '_plural', $parent)) {
204 return $this->plugin_name($parent) . ' / ' . get_string('subplugintype_' . $type . '_plural', $parent);
205 } else {
206 return $this->plugin_name($parent) . ' / ' . $type;
209 } else {
210 return $type;
215 * Defines a white list of all plugins shipped in the standard Moodle distribution
217 * @return false|array array of standard plugins or false if the type is unknown
219 public static function standard_plugins_list($type) {
220 static $standard_plugins = array(
222 'assignment' => array(
223 'offline', 'online', 'upload', 'uploadsingle'
226 'auth' => array(
227 'cas', 'db', 'email', 'fc', 'imap', 'ldap', 'manual', 'mnet',
228 'nntp', 'nologin', 'none', 'pam', 'pop3', 'radius',
229 'shibboleth', 'webservice'
232 'block' => array(
233 'activity_modules', 'admin_bookmarks', 'blog_menu',
234 'blog_recent', 'blog_tags', 'calendar_month',
235 'calendar_upcoming', 'comments', 'community',
236 'completionstatus', 'course_list', 'course_overview',
237 'course_summary', 'feedback', 'glossary_random', 'html',
238 'login', 'mentees', 'messages', 'mnet_hosts', 'myprofile',
239 'navigation', 'news_items', 'online_users', 'participants',
240 'private_files', 'quiz_results', 'recent_activity',
241 'rss_client', 'search', 'search_forums', 'section_links',
242 'selfcompletion', 'settings', 'site_main_menu',
243 'social_activities', 'tag_flickr', 'tag_youtube', 'tags'
246 'coursereport' => array(
247 'completion', 'log', 'outline', 'participation', 'progress', 'stats'
250 'datafield' => array(
251 'checkbox', 'date', 'file', 'latlong', 'menu', 'multimenu',
252 'number', 'picture', 'radiobutton', 'text', 'textarea', 'url'
255 'datapreset' => array(
256 'imagegallery'
259 'editor' => array(
260 'textarea', 'tinymce'
263 'enrol' => array(
264 'authorize', 'category', 'cohort', 'database', 'flatfile',
265 'guest', 'imsenterprise', 'ldap', 'manual', 'meta', 'mnet',
266 'paypal', 'self'
269 'filter' => array(
270 'activitynames', 'algebra', 'censor', 'emailprotect',
271 'emoticon', 'mediaplugin', 'multilang', 'tex', 'tidy',
272 'urltolink', 'mod_data', 'mod_glossary'
275 'format' => array(
276 'scorm', 'social', 'topics', 'weeks'
279 'gradeexport' => array(
280 'ods', 'txt', 'xls', 'xml'
283 'gradeimport' => array(
284 'csv', 'xml'
287 'gradereport' => array(
288 'grader', 'outcomes', 'overview', 'user'
291 'local' => array(
294 'message' => array(
295 'email', 'jabber', 'popup'
298 'mnetservice' => array(
299 'enrol'
302 'mod' => array(
303 'assignment', 'chat', 'choice', 'data', 'feedback', 'folder',
304 'forum', 'glossary', 'imscp', 'label', 'lesson', 'page',
305 'quiz', 'resource', 'scorm', 'survey', 'url', 'wiki', 'workshop'
308 'plagiarism' => array(
311 'portfolio' => array(
312 'boxnet', 'download', 'flickr', 'googledocs', 'mahara', 'picasa'
315 'profilefield' => array(
316 'checkbox', 'datetime', 'menu', 'text', 'textarea'
319 'qformat' => array(
320 'aiken', 'blackboard', 'blackboard_six', 'examview', 'gift',
321 'learnwise', 'missingword', 'multianswer', 'qti_two', 'webct',
322 'xhtml', 'xml'
325 'qtype' => array(
326 'calculated', 'calculatedmulti', 'calculatedsimple',
327 'description', 'essay', 'match', 'missingtype', 'multianswer',
328 'multichoice', 'numerical', 'random', 'randomsamatch',
329 'shortanswer', 'truefalse'
332 'quiz' => array(
333 'grading', 'overview', 'responses', 'statistics'
336 'report' => array(
337 'backups', 'capability', 'configlog', 'courseoverview',
338 'customlang', 'log', 'profiling', 'questioninstances',
339 'security', 'spamcleaner', 'stats', 'unittest', 'unsuproles'
342 'repository' => array(
343 'alfresco', 'boxnet', 'coursefiles', 'dropbox', 'filesystem',
344 'flickr', 'flickr_public', 'googledocs', 'local', 'merlot',
345 'picasa', 'recent', 's3', 'upload', 'url', 'user', 'webdav',
346 'wikimedia', 'youtube'
349 'theme' => array(
350 'anomaly', 'arialist', 'base', 'binarius', 'boxxie', 'brick',
351 'canvas', 'formal_white', 'formfactor', 'fusion',
352 'leatherbound', 'magazine', 'nimble', 'nonzero', 'overlay',
353 'serenity', 'sky_high', 'splash', 'standard', 'standardold'
356 'webservice' => array(
357 'amf', 'rest', 'soap', 'xmlrpc'
360 'workshopallocation' => array(
361 'manual', 'random'
364 'workshopeval' => array(
365 'best'
368 'workshopform' => array(
369 'accumulative', 'comments', 'numerrors', 'rubric'
373 if (isset($standard_plugins[$type])) {
374 return $standard_plugins[$type];
376 } else {
377 return false;
383 * All classes that represent a plugin of some type must implement this interface
385 interface plugintype_interface {
388 * Gathers and returns the information about all plugins of the given type
390 * Passing the parameter $typeclass allows us to reach the same effect as with the
391 * late binding in PHP 5.3. Once PHP 5.3 is required, we can refactor this to use
392 * {@example $plugin = new static();} instead of {@example $plugin = new $typeclass()}
394 * @param string $type the name of the plugintype, eg. mod, auth or workshopform
395 * @param string $typerootdir full path to the location of the plugin dir
396 * @param string $typeclass the name of the actually called class
397 * @return array of plugintype classes, indexed by the plugin name
399 public static function get_plugins($type, $typerootdir, $typeclass);
402 * Sets $displayname property to a localized name of the plugin
404 * @return void
406 public function set_display_name();
409 * Sets $versiondisk property to a numerical value representing the
410 * version of the plugin's source code.
412 * If the value is null after calling this method, either the plugin
413 * does not use versioning (typically does not have any database
414 * data) or is missing from disk.
416 * @return void
418 public function set_version_disk();
421 * Sets $versiondb property to a numerical value representing the
422 * currently installed version of the plugin.
424 * If the value is null after calling this method, either the plugin
425 * does not use versioning (typically does not have any database
426 * data) or has not been installed yet.
428 * @return void
430 public function set_version_db();
433 * Sets $versionrequires property to a numerical value representing
434 * the version of Moodle core that this plugin requires.
436 * @return void
438 public function set_version_requires();
441 * Sets $source property to one of plugin_manager::PLUGIN_SOURCE_xxx
442 * constants.
444 * If the property's value is null after calling this method, then
445 * the type of the plugin has not been recognized and you should throw
446 * an exception.
448 * @return void
450 public function set_source();
453 * Returns true if the plugin is shipped with the official distribution
454 * of the current Moodle version, false otherwise.
456 * @return bool
458 public function is_standard();
461 * Returns the status of the plugin
463 * @return string one of plugin_manager::PLUGIN_STATUS_xxx constants
465 public function get_status();
468 * Returns the information about plugin availability
470 * True means that the plugin is enabled. False means that the plugin is
471 * disabled. Null means that the information is not available, or the
472 * plugin does not support configurable availability or the availability
473 * can not be changed.
475 * @return null|bool
477 public function is_enabled();
480 * Returns the URL of the plugin settings screen
482 * Null value means that the plugin either does not have the settings screen
483 * or its location is not available via this library.
485 * @return null|moodle_url
487 public function get_settings_url();
490 * Returns the URL of the screen where this plugin can be uninstalled
492 * Visiting that URL must be safe, that is a manual confirmation is needed
493 * for actual uninstallation of the plugin. Null value means that the
494 * plugin either does not support uninstallation, or does not require any
495 * database cleanup or the location of the screen is not available via this
496 * library.
498 * @return null|moodle_url
500 public function get_uninstall_url();
503 * Returns relative directory of the plugin with heading '/'
505 * @example /mod/workshop
506 * @return string
508 public function get_dir();
512 * Defines public properties that all plugintype classes must have
513 * and provides default implementation of required methods.
515 abstract class plugintype_base {
517 /** @var string the plugintype name, eg. mod, auth or workshopform */
518 public $type;
519 /** @var string full path to the location of all the plugins of this type */
520 public $typerootdir;
521 /** @var string the plugin name, eg. assignment, ldap */
522 public $name;
523 /** @var string the localized plugin name */
524 public $displayname;
525 /** @var string the plugin source, one of plugin_manager::PLUGIN_SOURCE_xxx constants */
526 public $source;
527 /** @var fullpath to the location of this plugin */
528 public $rootdir;
529 /** @var int|string the version of the plugin's source code */
530 public $versiondisk;
531 /** @var int|string the version of the installed plugin */
532 public $versiondb;
533 /** @var int|float|string required version of Moodle core */
534 public $versionrequires;
535 /** @var int number of instances of the plugin - not supported yet */
536 public $instances;
537 /** @var int order of the plugin among other plugins of the same type - not supported yet */
538 public $sortorder;
541 * @see plugintype_interface::get_plugins()
543 public static function get_plugins($type, $typerootdir, $typeclass) {
545 // get the information about plugins at the disk
546 $plugins = get_plugin_list($type);
547 $ondisk = array();
548 foreach ($plugins as $pluginname => $pluginrootdir) {
549 $plugin = new $typeclass();
550 $plugin->type = $type;
551 $plugin->typerootdir = $typerootdir;
552 $plugin->name = $pluginname;
553 $plugin->rootdir = $pluginrootdir;
555 $plugin->set_display_name();
556 $plugin->set_version_disk();
557 $plugin->set_version_db();
558 $plugin->set_version_requires();
559 $plugin->set_source();
561 $ondisk[$pluginname] = $plugin;
563 return $ondisk;
567 * @see plugintype_interface::set_display_name()
569 public function set_display_name() {
570 if (! get_string_manager()->string_exists('pluginname', $this->type . '_' . $this->name)) {
571 $this->displayname = '[pluginname,' . $this->type . '_' . $this->name . ']';
572 } else {
573 $this->displayname = get_string('pluginname', $this->type . '_' . $this->name);
578 * @see plugintype_interface::set_version_disk()
580 public function set_version_disk() {
582 if (empty($this->rootdir)) {
583 return;
586 $versionfile = $this->rootdir . '/version.php';
588 if (is_readable($versionfile)) {
589 include($versionfile);
590 if (isset($plugin->version)) {
591 $this->versiondisk = $plugin->version;
597 * @see plugintype_interface::set_version_db()
599 public function set_version_db() {
601 if ($ver = self::get_version_from_config_plugins($this->type . '_' . $this->name)) {
602 $this->versiondb = $ver;
607 * @see plugintype_interface::set_version_requires()
609 public function set_version_requires() {
611 if (empty($this->rootdir)) {
612 return;
615 $versionfile = $this->rootdir . '/version.php';
617 if (is_readable($versionfile)) {
618 include($versionfile);
619 if (isset($plugin->requires)) {
620 $this->versionrequires = $plugin->requires;
626 * @see plugintype_interface::set_source()
628 public function set_source() {
630 $standard = plugin_manager::standard_plugins_list($this->type);
632 if ($standard !== false) {
633 $standard = array_flip($standard);
634 if (isset($standard[$this->name])) {
635 $this->source = plugin_manager::PLUGIN_SOURCE_STANDARD;
636 } else {
637 $this->source = plugin_manager::PLUGIN_SOURCE_EXTENSION;
643 * @see plugintype_interface::is_standard()
645 public function is_standard() {
646 return $this->source === plugin_manager::PLUGIN_SOURCE_STANDARD;
650 * @see plugintype_interface::get_status()
652 public function get_status() {
654 if (is_null($this->versiondb) and is_null($this->versiondisk)) {
655 return plugin_manager::PLUGIN_STATUS_NODB;
657 } else if (is_null($this->versiondb) and !is_null($this->versiondisk)) {
658 return plugin_manager::PLUGIN_STATUS_NEW;
660 } else if (!is_null($this->versiondb) and is_null($this->versiondisk)) {
661 return plugin_manager::PLUGIN_STATUS_MISSING;
663 } else if ((string)$this->versiondb === (string)$this->versiondisk) {
664 return plugin_manager::PLUGIN_STATUS_UPTODATE;
666 } else if ($this->versiondb < $this->versiondisk) {
667 return plugin_manager::PLUGIN_STATUS_UPGRADE;
669 } else if ($this->versiondb > $this->versiondisk) {
670 return plugin_manager::PLUGIN_STATUS_DOWNGRADE;
672 } else {
673 // $version = pi(); and similar funny jokes - hopefully Donald E. Knuth will never contribute to Moodle ;-)
674 throw new coding_exception('Unable to determine plugin state, check the plugin versions');
679 * @see plugintype_interface::is_enabled()
681 public function is_enabled() {
682 return null;
686 * @see plugintype_interface::get_settings_url()
688 public function get_settings_url() {
689 return null;
693 * @see plugintype_interface::get_uninstall_url()
695 public function get_uninstall_url() {
696 return null;
700 * @see plugintype_interface::get_dir()
702 public function get_dir() {
703 global $CFG;
705 return substr($this->rootdir, strlen($CFG->dirroot));
709 * Provides access to plugin versions from {config_plugins}
711 * @param string $plugin plugin name
712 * @param double $disablecache optional, defaults to false
713 * @return int|false the stored value or false if not found
715 protected function get_version_from_config_plugins($plugin, $disablecache=false) {
716 global $DB;
717 static $pluginversions = null;
719 if (is_null($pluginversions) or $disablecache) {
720 $pluginversions = $DB->get_records_menu('config_plugins', array('name' => 'version'), 'plugin', 'plugin,value');
723 if (!array_key_exists($plugin, $pluginversions)) {
724 return false;
727 return $pluginversions[$plugin];
732 * General class for all plugin types that do not have their own class
734 class plugintype_general extends plugintype_base implements plugintype_interface {
739 * Class for page side blocks
741 class plugintype_block extends plugintype_base implements plugintype_interface {
744 * @see plugintype_interface::get_plugins()
746 public static function get_plugins($type, $typerootdir, $typeclass) {
748 // get the information about blocks at the disk
749 $blocks = parent::get_plugins($type, $typerootdir, $typeclass);
751 // add blocks missing from disk
752 $blocksinfo = self::get_blocks_info();
753 foreach ($blocksinfo as $blockname => $blockinfo) {
754 if (isset($blocks[$blockname])) {
755 continue;
757 $plugin = new $typeclass();
758 $plugin->type = $type;
759 $plugin->typerootdir = $typerootdir;
760 $plugin->name = $blockname;
761 $plugin->rootdir = null;
762 $plugin->displayname = $blockname;
763 $plugin->versiondb = $blockinfo->version;
764 $plugin->set_source();
766 $blocks[$blockname] = $plugin;
769 return $blocks;
773 * @see plugintype_interface::set_display_name()
775 public function set_display_name() {
777 if (get_string_manager()->string_exists('pluginname', 'block_' . $this->name)) {
778 $this->displayname = get_string('pluginname', 'block_' . $this->name);
780 } else if (($block = block_instance($this->name)) !== false) {
781 $this->displayname = $block->get_title();
783 } else {
784 parent::set_display_name();
789 * @see plugintype_interface::set_version_db()
791 public function set_version_db() {
792 global $DB;
794 $blocksinfo = self::get_blocks_info();
795 if (isset($blocksinfo[$this->name]->version)) {
796 $this->versiondb = $blocksinfo[$this->name]->version;
801 * @see plugintype_interface::is_enabled()
803 public function is_enabled() {
805 $blocksinfo = self::get_blocks_info();
806 if (isset($blocksinfo[$this->name]->visible)) {
807 if ($blocksinfo[$this->name]->visible) {
808 return true;
809 } else {
810 return false;
812 } else {
813 return parent::is_enabled();
818 * @see plugintype_interface::get_settings_url()
820 public function get_settings_url() {
822 if (($block = block_instance($this->name)) === false) {
823 return parent::get_settings_url();
825 } else if ($block->has_config()) {
826 if (!empty($this->rootdir) and file_exists($this->rootdir . '/settings.php')) {
827 return new moodle_url('/admin/settings.php', array('section' => 'blocksetting' . $this->name));
828 } else {
829 $blocksinfo = self::get_blocks_info();
830 return new moodle_url('/admin/block.php', array('block' => $blocksinfo[$this->name]->id));
833 } else {
834 return parent::get_settings_url();
839 * @see plugintype_interface::get_uninstall_url()
841 public function get_uninstall_url() {
843 $blocksinfo = self::get_blocks_info();
844 return new moodle_url('/admin/blocks.php', array('delete' => $blocksinfo[$this->name]->id, 'sesskey' => sesskey()));
848 * Provides access to the records in {block} table
850 * @param bool $disablecache do not use internal static cache
851 * @return array array of stdClasses
853 protected static function get_blocks_info($disablecache=false) {
854 global $DB;
855 static $blocksinfocache = null;
857 if (is_null($blocksinfocache) or $disablecache) {
858 $blocksinfocache = $DB->get_records('block', null, 'name', 'name,id,version,visible');
861 return $blocksinfocache;
866 * Class for text filters
868 class plugintype_filter extends plugintype_base implements plugintype_interface {
871 * @see plugintype_interface::get_plugins()
873 public static function get_plugins($type, $typerootdir, $typeclass) {
874 global $CFG, $DB;
876 $filters = array();
878 // get the list of filters from both /filter and /mod location
879 $installed = filter_get_all_installed();
881 foreach ($installed as $filterlegacyname => $displayname) {
882 $plugin = new $typeclass();
883 $plugin->type = $type;
884 $plugin->typerootdir = $typerootdir;
885 $plugin->name = self::normalize_legacy_name($filterlegacyname);
886 $plugin->rootdir = $CFG->dirroot . '/' . $filterlegacyname;
887 $plugin->displayname = $displayname;
889 $plugin->set_version_disk();
890 $plugin->set_version_db();
891 $plugin->set_version_requires();
892 $plugin->set_source();
894 $filters[$plugin->name] = $plugin;
897 $globalstates = self::get_global_states();
899 if ($DB->get_manager()->table_exists('filter_active')) {
900 // if we're upgrading from 1.9, the table does not exist yet
901 // if it does, make sure that all installed filters are registered
902 $needsreload = false;
903 foreach (array_keys($installed) as $filterlegacyname) {
904 if (!isset($globalstates[self::normalize_legacy_name($filterlegacyname)])) {
905 filter_set_global_state($filterlegacyname, TEXTFILTER_DISABLED);
906 $needsreload = true;
909 if ($needsreload) {
910 $globalstates = self::get_global_states(true);
914 // make sure that all registered filters are installed, just in case
915 foreach ($globalstates as $name => $info) {
916 if (!isset($filters[$name])) {
917 // oops, there is a record in filter_active but the filter is not installed
918 $plugin = new $typeclass();
919 $plugin->type = $type;
920 $plugin->typerootdir = $typerootdir;
921 $plugin->name = $name;
922 $plugin->rootdir = $CFG->dirroot . '/' . $info->legacyname;
923 $plugin->displayname = $info->legacyname;
925 $plugin->set_version_db();
927 if (is_null($plugin->versiondb)) {
928 // this is a hack to stimulate 'Missing from disk' error
929 // because $plugin->versiondisk will be null !== false
930 $plugin->versiondb = false;
933 $filters[$plugin->name] = $plugin;
937 return $filters;
941 * @see plugintype_interface::set_display_name()
943 public function set_display_name() {
944 // do nothing, the name is set in self::get_plugins()
948 * @see plugintype_interface::set_version_disk()
950 public function set_version_disk() {
952 if (strpos($this->name, 'mod_') === 0) {
953 // filters bundled with modules do not use versioning
954 return;
957 return parent::set_version_disk();
961 * @see plugintype_interface::set_version_requires()
963 public function set_version_requires() {
965 if (strpos($this->name, 'mod_') === 0) {
966 // filters bundled with modules do not use versioning
967 return;
970 return parent::set_version_requires();
974 * @see plugintype_interface::is_enabled()
976 public function is_enabled() {
978 $globalstates = self::get_global_states();
980 foreach ($globalstates as $filterlegacyname => $info) {
981 $name = self::normalize_legacy_name($filterlegacyname);
982 if ($name === $this->name) {
983 if ($info->active == TEXTFILTER_DISABLED) {
984 return false;
985 } else {
986 // it may be 'On' or 'Off, but available'
987 return null;
992 return null;
996 * @see plugintype_interface::get_settings_url()
998 public function get_settings_url() {
1000 $globalstates = self::get_global_states();
1001 $legacyname = $globalstates[$this->name]->legacyname;
1002 if (filter_has_global_settings($legacyname)) {
1003 return new moodle_url('/admin/settings.php', array('section' => 'filtersetting' . str_replace('/', '', $legacyname)));
1004 } else {
1005 return null;
1010 * @see plugintype_interface::get_uninstall_url()
1012 public function get_uninstall_url() {
1014 if (strpos($this->name, 'mod_') === 0) {
1015 return null;
1016 } else {
1017 $globalstates = self::get_global_states();
1018 $legacyname = $globalstates[$this->name]->legacyname;
1019 return new moodle_url('/admin/filters.php', array('sesskey' => sesskey(), 'filterpath' => $legacyname, 'action' => 'delete'));
1024 * Convert legacy filter names like 'filter/foo' or 'mod/bar' into frankenstyle
1026 * @param string $legacyfiltername legacy filter name
1027 * @return string frankenstyle-like name
1029 protected static function normalize_legacy_name($legacyfiltername) {
1031 $name = str_replace('/', '_', $legacyfiltername);
1032 if (strpos($name, 'filter_') === 0) {
1033 $name = substr($name, 7);
1034 if (empty($name)) {
1035 throw new coding_exception('Unable to determine filter name: ' . $legacyfiltername);
1039 return $name;
1043 * Provides access to the results of {@link filter_get_global_states()}
1044 * but indexed by the normalized filter name
1046 * The legacy filter name is available as ->legacyname property.
1048 * @param bool $disablecache
1049 * @return array
1051 protected static function get_global_states($disablecache=false) {
1052 global $DB;
1053 static $globalstatescache = null;
1055 if ($disablecache or is_null($globalstatescache)) {
1057 if (!$DB->get_manager()->table_exists('filter_active')) {
1058 // we're upgrading from 1.9 and the table used by {@link filter_get_global_states()}
1059 // does not exist yet
1060 $globalstatescache = array();
1062 } else {
1063 foreach (filter_get_global_states() as $legacyname => $info) {
1064 $name = self::normalize_legacy_name($legacyname);
1065 $filterinfo = new stdClass();
1066 $filterinfo->legacyname = $legacyname;
1067 $filterinfo->active = $info->active;
1068 $filterinfo->sortorder = $info->sortorder;
1069 $globalstatescache[$name] = $filterinfo;
1074 return $globalstatescache;
1079 * Class for activity modules
1081 class plugintype_mod extends plugintype_base implements plugintype_interface {
1084 * @see plugintype_interface::get_plugins()
1086 public static function get_plugins($type, $typerootdir, $typeclass) {
1088 // get the information about plugins at the disk
1089 $modules = parent::get_plugins($type, $typerootdir, $typeclass);
1091 // add modules missing from disk
1092 $modulesinfo = self::get_modules_info();
1093 foreach ($modulesinfo as $modulename => $moduleinfo) {
1094 if (isset($modules[$modulename])) {
1095 continue;
1097 $plugin = new $typeclass();
1098 $plugin->type = $type;
1099 $plugin->typerootdir = $typerootdir;
1100 $plugin->name = $modulename;
1101 $plugin->rootdir = null;
1102 $plugin->displayname = $modulename;
1103 $plugin->versiondb = $moduleinfo->version;
1104 $plugin->set_source();
1106 $modules[$modulename] = $plugin;
1109 return $modules;
1113 * @see plugintype_interface::set_display_name()
1115 public function set_display_name() {
1116 if (get_string_manager()->string_exists('pluginname', $this->type . '_' . $this->name)) {
1117 $this->displayname = get_string('pluginname', $this->type . '_' . $this->name);
1118 } else {
1119 $this->displayname = get_string('modulename', $this->type . '_' . $this->name);
1124 * @see plugintype_interface::set_version_disk()
1126 public function set_version_disk() {
1128 if (empty($this->rootdir)) {
1129 return;
1132 $versionfile = $this->rootdir . '/version.php';
1134 if (is_readable($versionfile)) {
1135 include($versionfile);
1136 if (isset($module->version)) {
1137 $this->versiondisk = $module->version;
1143 * @see plugintype_interface::set_version_db()
1145 public function set_version_db() {
1146 global $DB;
1148 $modulesinfo = self::get_modules_info();
1149 if (isset($modulesinfo[$this->name]->version)) {
1150 $this->versiondb = $modulesinfo[$this->name]->version;
1155 * @see plugintype_interface::set_version_requires()
1157 public function set_version_requires() {
1159 if (empty($this->rootdir)) {
1160 return;
1163 $versionfile = $this->rootdir . '/version.php';
1165 if (is_readable($versionfile)) {
1166 include($versionfile);
1167 if (isset($module->requires)) {
1168 $this->versionrequires = $module->requires;
1174 * @see plugintype_interface::is_enabled()
1176 public function is_enabled() {
1178 $modulesinfo = self::get_modules_info();
1179 if (isset($modulesinfo[$this->name]->visible)) {
1180 if ($modulesinfo[$this->name]->visible) {
1181 return true;
1182 } else {
1183 return false;
1185 } else {
1186 return parent::is_enabled();
1191 * @see plugintype_interface::get_settings_url()
1193 public function get_settings_url() {
1195 if (!empty($this->rootdir) and (file_exists($this->rootdir . '/settings.php') or file_exists($this->rootdir . '/settingstree.php'))) {
1196 return new moodle_url('/admin/settings.php', array('section' => 'modsetting' . $this->name));
1197 } else {
1198 return parent::get_settings_url();
1203 * @see plugintype_interface::get_uninstall_url()
1205 public function get_uninstall_url() {
1207 if ($this->name !== 'forum') {
1208 return new moodle_url('/admin/modules.php', array('delete' => $this->name, 'sesskey' => sesskey()));
1209 } else {
1210 return null;
1215 * Provides access to the records in {modules} table
1217 * @param bool $disablecache do not use internal static cache
1218 * @return array array of stdClasses
1220 protected static function get_modules_info($disablecache=false) {
1221 global $DB;
1222 static $modulesinfocache = null;
1224 if (is_null($modulesinfocache) or $disablecache) {
1225 $modulesinfocache = $DB->get_records('modules', null, 'name', 'name,id,version,visible');
1228 return $modulesinfocache;
1233 * Class for question types
1235 class plugintype_qtype extends plugintype_base implements plugintype_interface {
1238 * @see plugintype_interface::set_display_name()
1240 public function set_display_name() {
1241 $this->displayname = get_string($this->name, 'qtype_' . $this->name);
1246 * Class for question formats
1248 class plugintype_qformat extends plugintype_base implements plugintype_interface {
1251 * @see plugintype_interface::set_display_name()
1253 public function set_display_name() {
1254 $this->displayname = get_string($this->name, 'qformat_' . $this->name);
1259 * Class for authentication plugins
1261 class plugintype_auth extends plugintype_base implements plugintype_interface {
1264 * @see plugintype_interface::is_enabled()
1266 public function is_enabled() {
1267 global $CFG;
1268 /** @var null|array list of enabled authentication plugins */
1269 static $enabled = null;
1271 if (in_array($this->name, array('nologin', 'manual'))) {
1272 // these two are always enabled and can't be disabled
1273 return null;
1276 if (is_null($enabled)) {
1277 $enabled = explode(',', $CFG->auth);
1280 return isset($enabled[$this->name]);
1284 * @see plugintype_interface::get_settings_url()
1286 public function get_settings_url() {
1288 if (!empty($this->rootdir) and file_exists($this->rootdir . '/settings.php')) {
1289 return new moodle_url('/admin/settings.php', array('section' => 'authsetting' . $this->name));
1290 } else {
1291 return new moodle_url('/admin/auth_config.php', array('auth' => $this->name));
1297 * Class for enrolment plugins
1299 class plugintype_enrol extends plugintype_base implements plugintype_interface {
1302 * We do not actually need whole enrolment classes here so we do not call
1303 * {@link enrol_get_plugins()}. Note that this may produce slightly different
1304 * results, for example if the enrolment plugin does not contain lib.php
1305 * but it is listed in $CFG->enrol_plugins_enabled
1307 * @see plugintype_interface::is_enabled()
1309 public function is_enabled() {
1310 global $CFG;
1311 /** @var null|array list of enabled enrolment plugins */
1312 static $enabled = null;
1314 if (is_null($enabled)) {
1315 $enabled = explode(',', $CFG->enrol_plugins_enabled);
1318 return isset($enabled[$this->name]);
1322 * @see plugintype_interface::get_settings_url()
1324 public function get_settings_url() {
1326 if ($this->is_enabled() or (!empty($this->rootdir) and file_exists($this->rootdir . '/settings.php'))) {
1327 return new moodle_url('/admin/settings.php', array('section' => 'enrolsettings' . $this->name));
1328 } else {
1329 return parent::get_settings_url();
1334 * @see plugintype_interface::get_uninstall_url()
1336 public function get_uninstall_url() {
1337 return new moodle_url('/admin/enrol.php', array('action' => 'uninstall', 'enrol' => $this->name, 'sesskey' => sesskey()));
1342 * Class for messaging processors
1344 class plugintype_message extends plugintype_base implements plugintype_interface {
1347 * @see plugintype_interface::get_settings_url()
1349 public function get_settings_url() {
1351 if ($this->name === 'jabber') {
1352 return new moodle_url('/admin/settings.php', array('section' => 'jabber'));
1355 if ($this->name === 'email') {
1356 return new moodle_url('/admin/settings.php', array('section' => 'mail'));
1363 * Class for repositories
1365 class plugintype_repository extends plugintype_base implements plugintype_interface {
1368 * @see plugintype_interface::is_enabled()
1370 public function is_enabled() {
1372 $enabled = self::get_enabled_repositories();
1374 return isset($enabled[$this->name]);
1378 * @see plugintype_interface::get_settings_url()
1380 public function get_settings_url() {
1382 if ($this->is_enabled()) {
1383 return new moodle_url('/admin/repository.php', array('sesskey' => sesskey(), 'action' => 'edit', 'repos' => $this->name));
1384 } else {
1385 return parent::get_settings_url();
1390 * Provides access to the records in {repository} table
1392 * @param bool $disablecache do not use internal static cache
1393 * @return array array of stdClasses
1395 protected static function get_enabled_repositories($disablecache=false) {
1396 global $DB;
1397 static $repositories = null;
1399 if (is_null($repositories) or $disablecache) {
1400 $repositories = $DB->get_records('repository', null, 'type', 'type,visible,sortorder');
1403 return $repositories;
1408 * Class for portfolios
1410 class plugintype_portfolio extends plugintype_base implements plugintype_interface {
1413 * @see plugintype_interface::is_enabled()
1415 public function is_enabled() {
1417 $enabled = self::get_enabled_portfolios();
1419 return isset($enabled[$this->name]);
1423 * Provides access to the records in {portfolio_instance} table
1425 * @param bool $disablecache do not use internal static cache
1426 * @return array array of stdClasses
1428 protected static function get_enabled_portfolios($disablecache=false) {
1429 global $DB;
1430 static $portfolios = null;
1432 if (is_null($portfolios) or $disablecache) {
1433 $portfolios = array();
1434 $instances = $DB->get_recordset('portfolio_instance', null, 'plugin');
1435 foreach ($instances as $instance) {
1436 if (isset($portfolios[$instance->plugin])) {
1437 if ($instance->visible) {
1438 $portfolios[$instance->plugin]->visible = $instance->visible;
1440 } else {
1441 $portfolios[$instance->plugin] = $instance;
1446 return $portfolios;
1451 * Class for themes
1453 class plugintype_theme extends plugintype_base implements plugintype_interface {
1456 * @see plugintype_interface::is_enabled()
1458 public function is_enabled() {
1459 global $CFG;
1461 if ((!empty($CFG->theme) and $CFG->theme === $this->name) or
1462 (!empty($CFG->themelegacy) and $CFG->themelegacy === $this->name)) {
1463 return true;
1464 } else {
1465 return parent::is_enabled();
1471 * Class representing an MNet service
1473 class plugintype_mnetservice extends plugintype_base implements plugintype_interface {
1476 * @see plugintype_interface::is_enabled()
1478 public function is_enabled() {
1479 global $CFG;
1481 if (empty($CFG->mnet_dispatcher_mode) || $CFG->mnet_dispatcher_mode !== 'strict') {
1482 return false;
1483 } else {
1484 return parent::is_enabled();