Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
24
namespace core\plugininfo;
25
 
26
use admin_settingpage;
27
use moodle_url;
28
use part_of_admin_tree;
29
 
30
/**
31
 * Class for activity modules
32
 */
33
class mod extends base {
34
 
35
    public static function plugintype_supports_disabling(): bool {
36
        return true;
37
    }
38
 
39
    /**
40
     * Finds all enabled plugins, the result may include missing plugins.
41
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
42
     */
43
    public static function get_enabled_plugins() {
44
        global $DB;
45
        return $DB->get_records_menu('modules', array('visible'=>1), 'name ASC', 'name, name AS val');
46
    }
47
 
48
    public static function enable_plugin(string $pluginname, int $enabled): bool {
49
        global $DB;
50
 
51
        if (!$module = $DB->get_record('modules', ['name' => $pluginname])) {
52
            throw new \moodle_exception('moduledoesnotexist', 'error');
53
        }
54
 
55
        $haschanged = false;
56
 
57
        // Only set visibility if it's different from the current value.
58
        if ($module->visible != $enabled) {
59
            if ($enabled && component_callback_exists("mod_{$pluginname}", 'pre_enable_plugin_actions')) {
60
                // This callback may be used to perform actions that must be completed prior to enabling a plugin.
61
                // Example of this may include:
62
                // - making a configuration change
63
                // - adding an alert
64
                // - checking a pre-requisite
65
                //
66
                // If the return value is falsy, then the change will be prevented.
67
                if (!component_callback("mod_{$pluginname}", 'pre_enable_plugin_actions')) {
68
                    return false;
69
                }
70
            }
71
            // Set module visibility.
72
            $DB->set_field('modules', 'visible', $enabled, ['id' => $module->id]);
73
            $haschanged = true;
74
 
75
            if ($enabled) {
76
                // Revert the previous saved visible state for the course module.
77
                $DB->set_field('course_modules', 'visible', '1', ['visibleold' => 1, 'module' => $module->id]);
78
 
79
                // Increment course.cacherev for courses where we just made something visible.
80
                // This will force cache rebuilding on the next request.
81
                increment_revision_number('course', 'cacherev',
82
                    "id IN (SELECT DISTINCT course
83
                                       FROM {course_modules}
84
                                      WHERE visible = 1 AND module = ?)",
85
                    [$module->id]
86
                );
87
            } else {
88
                // Remember the visibility status in visibleold and hide.
89
                $sql = "UPDATE {course_modules}
90
                           SET visibleold = visible, visible = 0
91
                         WHERE module = ?";
92
                $DB->execute($sql, [$module->id]);
93
                // Increment course.cacherev for courses where we just made something invisible.
94
                // This will force cache rebuilding on the next request.
95
                increment_revision_number('course', 'cacherev',
96
                    'id IN (SELECT DISTINCT course
97
                                       FROM {course_modules}
98
                                      WHERE visibleold = 1 AND module = ?)',
99
                    [$module->id]
100
                );
101
            }
102
 
103
            // Include this information into config changes table.
104
            add_to_config_log('mod_visibility', $module->visible, $enabled, $pluginname);
105
            \core_plugin_manager::reset_caches();
106
        }
107
 
108
        return $haschanged;
109
    }
110
 
111
    /**
112
     * Magic method getter, redirects to read only values.
113
     *
114
     * For module plugins we pretend the object has 'visible' property for compatibility
115
     * with plugins developed for Moodle version below 2.4
116
     *
117
     * @param string $name
118
     * @return mixed
119
     */
120
    public function __get($name) {
121
        if ($name === 'visible') {
122
            debugging('This is now an instance of plugininfo_mod, please use $module->is_enabled() instead of $module->visible', DEBUG_DEVELOPER);
123
            return ($this->is_enabled() !== false);
124
        }
125
        return parent::__get($name);
126
    }
127
 
128
    public function init_display_name() {
129
        if (get_string_manager()->string_exists('pluginname', $this->component)) {
130
            $this->displayname = get_string('pluginname', $this->component);
131
        } else {
132
            $this->displayname = get_string('modulename', $this->component);
133
        }
134
    }
135
 
136
    public function get_settings_section_name() {
137
        return 'modsetting' . $this->name;
138
    }
139
 
140
    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
141
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
142
        /** @var \admin_root $ADMIN */
143
        $ADMIN = $adminroot; // May be used in settings.php.
144
        $plugininfo = $this; // Also can be used inside settings.php.
145
        $module = $this;     // Also can be used inside settings.php.
146
 
147
        if (!$this->is_installed_and_upgraded()) {
148
            return;
149
        }
150
 
151
        if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
152
            return;
153
        }
154
 
155
        $section = $this->get_settings_section_name();
156
 
157
        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
158
        include($this->full_path('settings.php')); // This may also set $settings to null.
159
 
160
        if ($settings) {
161
            $ADMIN->add($parentnodename, $settings);
162
        }
163
    }
164
 
165
    /**
166
     * Allow all activity modules but Forum to be uninstalled.
167
     *
168
     * This exception for the Forum has been hard-coded in Moodle since ages,
169
     * we may want to re-think it one day.
170
     */
171
    public function is_uninstall_allowed() {
172
        if ($this->name === 'forum') {
173
            return false;
174
        } else {
175
            return true;
176
        }
177
    }
178
 
179
    /**
180
     * Return URL used for management of plugins of this type.
181
     * @return moodle_url
182
     */
183
    public static function get_manage_url() {
184
        return new moodle_url('/admin/modules.php');
185
    }
186
 
187
    /**
188
     * Return warning with number of activities and number of affected courses.
189
     *
190
     * @return string
191
     */
192
    public function get_uninstall_extra_warning() {
193
        global $DB;
194
 
195
        if (!$module = $DB->get_record('modules', array('name'=>$this->name))) {
196
            return '';
197
        }
198
 
199
        if (!$count = $DB->count_records('course_modules', array('module'=>$module->id))) {
200
            return '';
201
        }
202
 
203
        $sql = "SELECT COUNT('x')
204
                  FROM (
205
                    SELECT course
206
                      FROM {course_modules}
207
                     WHERE module = :mid
208
                  GROUP BY course
209
                  ) c";
210
        $courses = $DB->count_records_sql($sql, array('mid'=>$module->id));
211
 
212
        return '<p>'.get_string('uninstallextraconfirmmod', 'core_plugin', array('instances'=>$count, 'courses'=>$courses)).'</p>';
213
    }
214
 
215
    /**
216
     * Pre-uninstall hook.
217
     *
218
     * This is intended for disabling of plugin, some DB table purging, etc.
219
     *
220
     * NOTE: to be called from uninstall_plugin() only.
221
     * @private
222
     */
223
    public function uninstall_cleanup() {
224
        global $DB, $CFG;
225
 
226
        if (!$module = $DB->get_record('modules', array('name' => $this->name))) {
227
            parent::uninstall_cleanup();
228
            return;
229
        }
230
 
231
        // Delete all the relevant instances from all course sections.
232
        if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) {
233
            foreach ($coursemods as $coursemod) {
234
                // Do not verify results, there is not much we can do anyway.
235
                delete_mod_from_section($coursemod->id, $coursemod->section);
236
            }
237
        }
238
 
239
        // Increment course.cacherev for courses that used this module.
240
        // This will force cache rebuilding on the next request.
241
        increment_revision_number('course', 'cacherev',
242
            "id IN (SELECT DISTINCT course
243
                      FROM {course_modules}
244
                     WHERE module=?)",
245
            array($module->id));
246
 
247
        // Delete all the course module records.
248
        $DB->delete_records('course_modules', array('module' => $module->id));
249
 
250
        // Delete module contexts.
251
        if ($coursemods) {
252
            foreach ($coursemods as $coursemod) {
253
                \context_helper::delete_instance(CONTEXT_MODULE, $coursemod->id);
254
            }
255
        }
256
 
257
        // Delete the module entry itself.
258
        $DB->delete_records('modules', array('name' => $module->name));
259
 
260
        // Cleanup the gradebook.
261
        require_once($CFG->libdir.'/gradelib.php');
262
        grade_uninstalled_module($module->name);
263
 
264
        // Do not look for legacy $module->name . '_uninstall any more,
265
        // they should have migrated to db/uninstall.php by now.
266
 
267
        parent::uninstall_cleanup();
268
    }
269
}