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 text filters
32
 */
33
class filter extends base {
34
 
35
    public static function plugintype_supports_disabling(): bool {
36
        return true;
37
    }
38
 
39
    public function init_display_name() {
40
        if (!get_string_manager()->string_exists('filtername', $this->component)) {
41
            $this->displayname = '[filtername,' . $this->component . ']';
42
        } else {
43
            $this->displayname = get_string('filtername', $this->component);
44
        }
45
    }
46
 
47
    /**
48
     * Finds all enabled plugins, the result may include missing plugins.
49
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
50
     */
51
    public static function get_enabled_plugins() {
52
        global $DB, $CFG;
53
        require_once("$CFG->libdir/filterlib.php");
54
 
55
        $enabled = array();
56
        $filters = $DB->get_records_select('filter_active', "active <> :disabled AND contextid = :contextid", array(
57
            'disabled' => TEXTFILTER_DISABLED, 'contextid' => \context_system::instance()->id), 'filter ASC', 'id, filter');
58
        foreach ($filters as $filter) {
59
            $enabled[$filter->filter] = $filter->filter;
60
        }
61
 
62
        return $enabled;
63
    }
64
 
65
    /**
66
     * Enable or disable a plugin.
67
     * When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
68
     *
69
     * @param string $pluginname The plugin name to enable/disable.
70
     * @param int $enabled Whether the pluginname should be TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
71
     *
72
     * @return bool It always return true because we don't know if the value has changed or not. That way, we guarantee any action
73
     * required if it's changed will be executed.
74
     */
75
    public static function enable_plugin(string $pluginname, int $enabled): bool {
76
        global $CFG;
77
        require_once("$CFG->libdir/filterlib.php");
78
        require_once("$CFG->libdir/weblib.php");
79
 
80
        filter_set_global_state($pluginname, $enabled);
81
        if ($enabled == TEXTFILTER_DISABLED) {
82
            filter_set_applies_to_strings($pluginname, false);
83
        }
84
 
85
        reset_text_filters_cache();
86
        \core_plugin_manager::reset_caches();
87
 
88
        return true;
89
    }
90
 
91
    /**
92
     * Returns current status for a pluginname.
93
     *
94
     * Filters have different values for enabled/disabled plugins so the current value needs to be calculated in a
95
     * different way than the default method in the base class.
96
     *
97
     * @param string $pluginname The plugin name to check.
98
     * @return int The current status (enabled, disabled...) of $pluginname.
99
     */
100
    public static function get_enabled_plugin(string $pluginname): int {
101
        global $DB, $CFG;
102
        require_once("$CFG->libdir/filterlib.php");
103
 
104
        $conditions = ['filter' => $pluginname, 'contextid' => \context_system::instance()->id];
105
        $record = $DB->get_record('filter_active', $conditions, 'active');
106
 
107
        return $record ? (int) $record->active : TEXTFILTER_DISABLED;
108
    }
109
 
110
    public function get_settings_section_name() {
111
        return 'filtersetting' . $this->name;
112
    }
113
 
114
    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
115
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
116
        /** @var \admin_root $ADMIN */
117
        $ADMIN = $adminroot; // May be used in settings.php.
118
        $plugininfo = $this; // Also can be used inside settings.php.
119
        $filter = $this;     // Also can be used inside settings.php.
120
 
121
        if (!$this->is_installed_and_upgraded()) {
122
            return;
123
        }
124
 
125
        if (!$hassiteconfig) {
126
            return;
127
        }
128
        if (file_exists($this->full_path('settings.php'))) {
129
            $fullpath = $this->full_path('settings.php');
130
        } else if (file_exists($this->full_path('filtersettings.php'))) {
131
            $fullpath = $this->full_path('filtersettings.php');
132
        } else {
133
            return;
134
        }
135
 
136
        $section = $this->get_settings_section_name();
137
        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
138
        include($fullpath); // This may also set $settings to null.
139
 
140
        if ($settings) {
141
            $ADMIN->add($parentnodename, $settings);
142
        }
143
    }
144
 
145
    public function is_uninstall_allowed() {
146
        return true;
147
    }
148
 
149
    /**
150
     * Return URL used for management of plugins of this type.
151
     * @return moodle_url
152
     */
153
    public static function get_manage_url() {
154
        return new moodle_url('/admin/filters.php');
155
    }
156
 
157
    /**
158
     * Pre-uninstall hook.
159
     *
160
     * This is intended for disabling of plugin, some DB table purging, etc.
161
     *
162
     * NOTE: to be called from uninstall_plugin() only.
163
     * @private
164
     */
165
    public function uninstall_cleanup() {
166
        global $DB, $CFG;
167
 
168
        $DB->delete_records('filter_active', array('filter' => $this->name));
169
        $DB->delete_records('filter_config', array('filter' => $this->name));
170
 
171
        if (empty($CFG->filterall)) {
172
            $stringfilters = array();
173
        } else if (!empty($CFG->stringfilters)) {
174
            $stringfilters = explode(',', $CFG->stringfilters);
175
            $stringfilters = array_combine($stringfilters, $stringfilters);
176
        } else {
177
            $stringfilters = array();
178
        }
179
 
180
        unset($stringfilters[$this->name]);
181
 
182
        set_config('stringfilters', implode(',', $stringfilters));
183
        set_config('filterall', !empty($stringfilters));
184
 
185
        parent::uninstall_cleanup();
186
    }
187
}