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  2018 Toni Barbera {@link http://www.moodle.org}
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
 
29
/**
30
 * Class for admin tool plugins
31
 *
32
 * @package    core
33
 * @copyright  2018 Toni Barbera {@link http://www.moodle.org}
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class customfield extends base {
37
 
38
    public static function plugintype_supports_disabling(): bool {
39
        return true;
40
    }
41
 
42
    /**
43
     * Allow uninstall
44
     * @return bool
45
     */
46
    public function is_uninstall_allowed() {
47
        return true;
48
    }
49
 
50
    /**
51
     * Return URL used for management of plugins of this type.
52
     * @return moodle_url
53
     */
54
    public static function get_manage_url() {
55
        return new moodle_url('/admin/settings.php', array('section' => 'managecustomfields'));
56
    }
57
 
58
    /**
59
     * Enabled plugins
60
     * @return array|null
61
     */
62
    public static function get_enabled_plugins() {
63
        global $DB;
64
 
65
        // Get all available plugins.
66
        $plugins = \core_plugin_manager::instance()->get_installed_plugins('customfield');
67
        if (!$plugins) {
68
            return array();
69
        }
70
 
71
        // Check they are enabled using get_config (which is cached and hopefully fast).
72
        $enabled = array();
73
        foreach ($plugins as $plugin => $version) {
74
            $disabled = get_config('customfield_' . $plugin, 'disabled');
75
            if (empty($disabled)) {
76
                $enabled[$plugin] = $plugin;
77
            }
78
        }
79
 
80
        return $enabled;
81
    }
82
 
83
    public static function enable_plugin(string $pluginname, int $enabled): bool {
84
        $haschanged = false;
85
 
86
        $plugin = 'customfield_' . $pluginname;
87
        $oldvalue = get_config($plugin, 'disabled');
88
        $disabled = !$enabled;
89
        // Only set value if there is no config setting or if the value is different from the previous one.
90
        if ($oldvalue == false && $disabled) {
91
            set_config('disabled', $disabled, $plugin);
92
            $haschanged = true;
93
        } else if ($oldvalue != false && !$disabled) {
94
            unset_config('disabled', $plugin);
95
            $haschanged = true;
96
        }
97
 
98
        if ($haschanged) {
99
            add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
100
            \core_plugin_manager::reset_caches();
101
        }
102
 
103
        return $haschanged;
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
     */
113
    public function uninstall_cleanup() {
114
        global $DB;
115
        $DB->delete_records_select('customfield_data',
116
            'fieldid IN (SELECT f.id FROM {customfield_field} f WHERE f.type = ?)', [$this->name]);
117
        $DB->delete_records('customfield_field', ['type' => $this->name]);
118
        parent::uninstall_cleanup();
119
    }
120
 
121
    /**
122
     * Setting section name
123
     *
124
     * @return null|string
125
     */
126
    public function get_settings_section_name() {
127
        return 'customfieldsetting' . $this->name;
128
    }
129
 
130
    /**
131
     * Load the global settings for a particular availability plugin (if there are any)
132
     *
133
     * @param \part_of_admin_tree $adminroot
134
     * @param string $parentnodename
135
     * @param bool $hassiteconfig
136
     */
137
    public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
138
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
139
        /** @var \admin_root $ADMIN */
140
        $ADMIN = $adminroot; // May be used in settings.php.
141
        $plugininfo = $this; // Also can be used inside settings.php
142
        $availability = $this; // Also to be used inside settings.php.
143
 
144
        if (!$this->is_installed_and_upgraded()) {
145
            return;
146
        }
147
 
148
        if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
149
            return;
150
        }
151
 
152
        $section = $this->get_settings_section_name();
153
 
154
        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
155
        include($this->full_path('settings.php')); // This may also set $settings to null.
156
 
157
        if ($settings) {
158
            $ADMIN->add($parentnodename, $settings);
159
        }
160
    }
161
}