2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Defines classes used for plugin info.
21 * @copyright 2011 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core\plugininfo
;
26 use moodle_url
, part_of_admin_tree
, admin_settingpage
;
28 defined('MOODLE_INTERNAL') ||
die();
31 * Class for activity modules
33 class mod
extends base
{
35 * Finds all enabled plugins, the result may include missing plugins.
36 * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
38 public static function get_enabled_plugins() {
40 return $DB->get_records_menu('modules', array('visible'=>1), 'name ASC', 'name, name AS val');
44 * Magic method getter, redirects to read only values.
46 * For module plugins we pretend the object has 'visible' property for compatibility
47 * with plugins developed for Moodle version below 2.4
52 public function __get($name) {
53 if ($name === 'visible') {
54 debugging('This is now an instance of plugininfo_mod, please use $module->is_enabled() instead of $module->visible', DEBUG_DEVELOPER
);
55 return ($this->is_enabled() !== false);
57 return parent
::__get($name);
60 public function init_display_name() {
61 if (get_string_manager()->string_exists('pluginname', $this->component
)) {
62 $this->displayname
= get_string('pluginname', $this->component
);
64 $this->displayname
= get_string('modulename', $this->component
);
68 public function get_settings_section_name() {
69 return 'modsetting' . $this->name
;
72 public function load_settings(part_of_admin_tree
$adminroot, $parentnodename, $hassiteconfig) {
73 global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
74 $ADMIN = $adminroot; // May be used in settings.php.
75 $plugininfo = $this; // Also can be used inside settings.php.
76 $module = $this; // Also can be used inside settings.php.
78 if (!$this->is_installed_and_upgraded()) {
82 if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
86 $section = $this->get_settings_section_name();
88 $settings = new admin_settingpage($section, $this->displayname
, 'moodle/site:config', $this->is_enabled() === false);
89 include($this->full_path('settings.php')); // This may also set $settings to null.
92 $ADMIN->add($parentnodename, $settings);
97 * Allow all activity modules but Forum to be uninstalled.
99 * This exception for the Forum has been hard-coded in Moodle since ages,
100 * we may want to re-think it one day.
102 public function is_uninstall_allowed() {
103 if ($this->name
=== 'forum') {
111 * Return URL used for management of plugins of this type.
114 public static function get_manage_url() {
115 return new moodle_url('/admin/modules.php');
119 * Return warning with number of activities and number of affected courses.
123 public function get_uninstall_extra_warning() {
126 if (!$module = $DB->get_record('modules', array('name'=>$this->name
))) {
130 if (!$count = $DB->count_records('course_modules', array('module'=>$module->id
))) {
134 $sql = "SELECT COUNT('x')
137 FROM {course_modules}
141 $courses = $DB->count_records_sql($sql, array('mid'=>$module->id
));
143 return '<p>'.get_string('uninstallextraconfirmmod', 'core_plugin', array('instances'=>$count, 'courses'=>$courses)).'</p>';
147 * Pre-uninstall hook.
149 * This is intended for disabling of plugin, some DB table purging, etc.
151 * NOTE: to be called from uninstall_plugin() only.
154 public function uninstall_cleanup() {
157 if (!$module = $DB->get_record('modules', array('name' => $this->name
))) {
158 parent
::uninstall_cleanup();
162 // Delete all the relevant instances from all course sections.
163 if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id
))) {
164 foreach ($coursemods as $coursemod) {
165 // Do not verify results, there is not much we can do anyway.
166 delete_mod_from_section($coursemod->id
, $coursemod->section
);
170 // Increment course.cacherev for courses that used this module.
171 // This will force cache rebuilding on the next request.
172 increment_revision_number('course', 'cacherev',
173 "id IN (SELECT DISTINCT course
174 FROM {course_modules}
178 // Delete all the course module records.
179 $DB->delete_records('course_modules', array('module' => $module->id
));
181 // Delete module contexts.
183 foreach ($coursemods as $coursemod) {
184 \context_helper
::delete_instance(CONTEXT_MODULE
, $coursemod->id
);
188 // Delete the module entry itself.
189 $DB->delete_records('modules', array('name' => $module->name
));
191 // Cleanup the gradebook.
192 require_once($CFG->libdir
.'/gradelib.php');
193 grade_uninstalled_module($module->name
);
195 // Do not look for legacy $module->name . '_uninstall any more,
196 // they should have migrated to db/uninstall.php by now.
198 parent
::uninstall_cleanup();