source: moodle/trunk/fuentes/lib/classes/plugininfo/mod.php @ 1331

Last change on this file since 1331 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 6.7 KB
Line 
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.
13//
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/>.
16
17/**
18 * Defines classes used for plugin info.
19 *
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
23 */
24namespace core\plugininfo;
25
26use moodle_url, part_of_admin_tree, admin_settingpage;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Class for activity modules
32 */
33class 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
37     */
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');
41    }
42
43    /**
44     * Magic method getter, redirects to read only values.
45     *
46     * For module plugins we pretend the object has 'visible' property for compatibility
47     * with plugins developed for Moodle version below 2.4
48     *
49     * @param string $name
50     * @return mixed
51     */
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);
56        }
57        return parent::__get($name);
58    }
59
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);
65        }
66    }
67
68    public function get_settings_section_name() {
69        return 'modsetting' . $this->name;
70    }
71
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.
77
78        if (!$this->is_installed_and_upgraded()) {
79            return;
80        }
81
82        if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
83            return;
84        }
85
86        $section = $this->get_settings_section_name();
87
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.
90
91        if ($settings) {
92            $ADMIN->add($parentnodename, $settings);
93        }
94    }
95
96    /**
97     * Allow all activity modules but Forum to be uninstalled.
98     *
99     * This exception for the Forum has been hard-coded in Moodle since ages,
100     * we may want to re-think it one day.
101     */
102    public function is_uninstall_allowed() {
103        if ($this->name === 'forum') {
104            return false;
105        } else {
106            return true;
107        }
108    }
109
110    /**
111     * Return URL used for management of plugins of this type.
112     * @return moodle_url
113     */
114    public static function get_manage_url() {
115        return new moodle_url('/admin/modules.php');
116    }
117
118    /**
119     * Return warning with number of activities and number of affected courses.
120     *
121     * @return string
122     */
123    public function get_uninstall_extra_warning() {
124        global $DB;
125
126        if (!$module = $DB->get_record('modules', array('name'=>$this->name))) {
127            return '';
128        }
129
130        if (!$count = $DB->count_records('course_modules', array('module'=>$module->id))) {
131            return '';
132        }
133
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));
142
143        return '<p>'.get_string('uninstallextraconfirmmod', 'core_plugin', array('instances'=>$count, 'courses'=>$courses)).'</p>';
144    }
145
146    /**
147     * Pre-uninstall hook.
148     *
149     * This is intended for disabling of plugin, some DB table purging, etc.
150     *
151     * NOTE: to be called from uninstall_plugin() only.
152     * @private
153     */
154    public function uninstall_cleanup() {
155        global $DB, $CFG;
156
157        if (!$module = $DB->get_record('modules', array('name' => $this->name))) {
158            parent::uninstall_cleanup();
159            return;
160        }
161
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);
167            }
168        }
169
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));
177
178        // Delete all the course module records.
179        $DB->delete_records('course_modules', array('module' => $module->id));
180
181        // Delete module contexts.
182        if ($coursemods) {
183            foreach ($coursemods as $coursemod) {
184                \context_helper::delete_instance(CONTEXT_MODULE, $coursemod->id);
185            }
186        }
187
188        // Delete the module entry itself.
189        $DB->delete_records('modules', array('name' => $module->name));
190
191        // Cleanup the gradebook.
192        require_once($CFG->libdir.'/gradelib.php');
193        grade_uninstalled_module($module->name);
194
195        // Do not look for legacy $module->name . '_uninstall any more,
196        // they should have migrated to db/uninstall.php by now.
197
198        parent::uninstall_cleanup();
199    }
200}
Note: See TracBrowser for help on using the repository browser.