MDL-51177 core: Ignore built files in stylelint
[moodle.git] / lib / classes / plugininfo / mod.php
blobd936fbc69ebadf8cd38982a40e519c174f0f39fd
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 plugin info.
20 * @package core
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();
30 /**
31 * Class for activity modules
33 class mod extends base {
34 /**
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() {
39 global $DB;
40 return $DB->get_records_menu('modules', array('visible'=>1), 'name ASC', 'name, name AS val');
43 /**
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
49 * @param string $name
50 * @return mixed
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);
63 } else {
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()) {
79 return;
82 if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
83 return;
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.
91 if ($settings) {
92 $ADMIN->add($parentnodename, $settings);
96 /**
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') {
104 return false;
105 } else {
106 return true;
111 * Return URL used for management of plugins of this type.
112 * @return moodle_url
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.
121 * @return string
123 public function get_uninstall_extra_warning() {
124 global $DB;
126 if (!$module = $DB->get_record('modules', array('name'=>$this->name))) {
127 return '';
130 if (!$count = $DB->count_records('course_modules', array('module'=>$module->id))) {
131 return '';
134 $sql = "SELECT COUNT('x')
135 FROM (
136 SELECT course
137 FROM {course_modules}
138 WHERE module = :mid
139 GROUP BY course
140 ) c";
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.
152 * @private
154 public function uninstall_cleanup() {
155 global $DB, $CFG;
157 if (!$module = $DB->get_record('modules', array('name' => $this->name))) {
158 parent::uninstall_cleanup();
159 return;
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}
175 WHERE module=?)",
176 array($module->id));
178 // Delete all the course module records.
179 $DB->delete_records('course_modules', array('module' => $module->id));
181 // Delete module contexts.
182 if ($coursemods) {
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();