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

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

Ported code to xenial

File size: 4.5 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, admin_externalpage;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Class for text filters
32 */
33class filter extends base {
34
35    public function init_display_name() {
36        if (!get_string_manager()->string_exists('filtername', $this->component)) {
37            $this->displayname = '[filtername,' . $this->component . ']';
38        } else {
39            $this->displayname = get_string('filtername', $this->component);
40        }
41    }
42
43    /**
44     * Finds all enabled plugins, the result may include missing plugins.
45     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
46     */
47    public static function get_enabled_plugins() {
48        global $DB, $CFG;
49        require_once("$CFG->libdir/filterlib.php");
50
51        $enabled = array();
52        $filters = $DB->get_records_select('filter_active', "active <> :disabled", array('disabled'=>TEXTFILTER_DISABLED), 'filter ASC', 'id, filter');
53        foreach ($filters as $filter) {
54            $enabled[$filter->filter] = $filter->filter;
55        }
56
57        return $enabled;
58    }
59
60    public function get_settings_section_name() {
61        return 'filtersetting' . $this->name;
62    }
63
64    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
65        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
66        $ADMIN = $adminroot; // May be used in settings.php.
67        $plugininfo = $this; // Also can be used inside settings.php.
68        $filter = $this;     // Also can be used inside settings.php.
69
70        if (!$this->is_installed_and_upgraded()) {
71            return;
72        }
73
74        if (!$hassiteconfig) {
75            return;
76        }
77        if (file_exists($this->full_path('settings.php'))) {
78            $fullpath = $this->full_path('settings.php');
79        } else if (file_exists($this->full_path('filtersettings.php'))) {
80            $fullpath = $this->full_path('filtersettings.php');
81        } else {
82            return;
83        }
84
85        $section = $this->get_settings_section_name();
86        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
87        include($fullpath); // This may also set $settings to null.
88
89        if ($settings) {
90            $ADMIN->add($parentnodename, $settings);
91        }
92    }
93
94    public function is_uninstall_allowed() {
95        return true;
96    }
97
98    /**
99     * Return URL used for management of plugins of this type.
100     * @return moodle_url
101     */
102    public static function get_manage_url() {
103        return new moodle_url('/admin/filters.php');
104    }
105
106    /**
107     * Pre-uninstall hook.
108     *
109     * This is intended for disabling of plugin, some DB table purging, etc.
110     *
111     * NOTE: to be called from uninstall_plugin() only.
112     * @private
113     */
114    public function uninstall_cleanup() {
115        global $DB, $CFG;
116
117        $DB->delete_records('filter_active', array('filter' => $this->name));
118        $DB->delete_records('filter_config', array('filter' => $this->name));
119
120        if (empty($CFG->filterall)) {
121            $stringfilters = array();
122        } else if (!empty($CFG->stringfilters)) {
123            $stringfilters = explode(',', $CFG->stringfilters);
124            $stringfilters = array_combine($stringfilters, $stringfilters);
125        } else {
126            $stringfilters = array();
127        }
128
129        unset($stringfilters[$this->name]);
130
131        set_config('stringfilters', implode(',', $stringfilters));
132        set_config('filterall', !empty($stringfilters));
133
134        parent::uninstall_cleanup();
135    }
136}
Note: See TracBrowser for help on using the repository browser.