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
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
21
 * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
22
 * @package    core
23
 */
24
namespace core\plugininfo;
25
 
26
use admin_settingpage;
27
use core_plugin_manager;
28
use moodle_url;
29
use part_of_admin_tree;
30
 
31
/**
32
 * Class for dataformats
33
 *
34
 * @package    core
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
36
 * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
37
 */
38
class dataformat extends base {
39
 
40
    public static function plugintype_supports_disabling(): bool {
41
        return true;
42
    }
43
 
44
    /**
45
     * Display name
46
     */
47
    public function init_display_name() {
48
        if (!get_string_manager()->string_exists('dataformat', $this->component)) {
49
            $this->displayname = '[dataformat,' . $this->component . ']';
50
        } else {
51
            $this->displayname = get_string('dataformat', $this->component);
52
        }
53
    }
54
 
55
    /**
56
     * Given a list of dataformat types, return them sorted according to site configuration (if set)
57
     *
58
     * @param string[] $formats List of formats, ['csv', 'pdf', etc]
59
     * @return string[] List of formats according to configured sort, ['csv', 'odf', etc]
60
     */
61
    private static function get_plugins_sortorder(array $formats): array {
62
        global $CFG;
63
 
64
        if (!empty($CFG->dataformat_plugins_sortorder)) {
65
            $order = explode(',', $CFG->dataformat_plugins_sortorder);
66
            $order = array_merge(array_intersect($order, $formats), array_diff($formats, $order));
67
        } else {
68
            $order = $formats;
69
        }
70
 
71
        return $order;
72
    }
73
 
74
    /**
75
     * Gathers and returns the information about all plugins of the given type
76
     *
77
     * @param string $type the name of the plugintype, eg. mod, auth or workshopform
78
     * @param string $typerootdir full path to the location of the plugin dir
79
     * @param string $typeclass the name of the actually called class
80
     * @param core_plugin_manager $pluginman the plugin manager calling this method
81
     * @return array of plugintype classes, indexed by the plugin name
82
     */
83
    public static function get_plugins($type, $typerootdir, $typeclass, $pluginman) {
84
        $formats = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
85
 
86
        $order = static::get_plugins_sortorder(array_keys($formats));
87
        $sortedformats = array();
88
        foreach ($order as $formatname) {
89
            $sortedformats[$formatname] = $formats[$formatname];
90
        }
91
        return $sortedformats;
92
    }
93
 
94
    /**
95
     * Finds all enabled plugins, the result may include missing plugins.
96
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
97
     */
98
    public static function get_enabled_plugins() {
99
        $plugins = core_plugin_manager::instance()->get_installed_plugins('dataformat');
100
        if (!$plugins) {
101
            return array();
102
        }
103
 
104
        $order = static::get_plugins_sortorder(array_keys($plugins));
105
        $enabled = array();
106
        foreach ($order as $formatname) {
107
            $disabled = get_config('dataformat_' . $formatname, 'disabled');
108
            if (empty($disabled)) {
109
                $enabled[$formatname] = $formatname;
110
            }
111
        }
112
        return $enabled;
113
    }
114
 
115
    public static function enable_plugin(string $pluginname, int $enabled): bool {
116
        $haschanged = false;
117
 
118
        $plugin = 'dataformat_' . $pluginname;
119
        $oldvalue = get_config($plugin, 'disabled');
120
        $disabled = !$enabled;
121
        // Only set value if there is no config setting or if the value is different from the previous one.
122
        if ($oldvalue == false && $disabled) {
123
            set_config('disabled', $disabled, $plugin);
124
            $haschanged = true;
125
        } else if ($oldvalue != false && !$disabled) {
126
            unset_config('disabled', $plugin);
127
            $haschanged = true;
128
        }
129
 
130
        if ($haschanged) {
131
            add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
132
            \core_plugin_manager::reset_caches();
133
        }
134
 
135
        return $haschanged;
136
    }
137
 
138
    /**
139
     * Returns the node name used in admin settings menu for this plugin settings (if applicable)
140
     *
141
     * @return null|string node name or null if plugin does not create settings node (default)
142
     */
143
    public function get_settings_section_name() {
144
        return 'dataformatsetting' . $this->name;
145
    }
146
 
147
    /**
148
     * Loads plugin settings to the settings tree
149
     *
150
     * This function usually includes settings.php file in plugins folder.
151
     * Alternatively it can create a link to some settings page (instance of admin_externalpage)
152
     *
153
     * @param \part_of_admin_tree $adminroot
154
     * @param string $parentnodename
155
     * @param bool $hassiteconfig whether the current user has moodle/site:config capability
156
     */
157
    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
158
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
159
        /** @var \admin_root $ADMIN */
160
        $ADMIN = $adminroot; // May be used in settings.php.
161
        $plugininfo = $this; // Also can be used inside settings.php.
162
        $dataformat = $this;     // Also can be used inside settings.php.
163
 
164
        if (!$this->is_installed_and_upgraded()) {
165
            return;
166
        }
167
 
168
        if (!$hassiteconfig) {
169
            return;
170
        }
171
        if (file_exists($this->full_path('settings.php'))) {
172
            $fullpath = $this->full_path('settings.php');
173
        } else if (file_exists($this->full_path('dataformatsettings.php'))) {
174
            $fullpath = $this->full_path('dataformatsettings.php');
175
        } else {
176
            return;
177
        }
178
 
179
        $section = $this->get_settings_section_name();
180
        $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
181
        include($fullpath); // This may also set $settings to null.
182
 
183
        if ($settings) {
184
            $ADMIN->add($parentnodename, $settings);
185
        }
186
    }
187
 
188
    /**
189
     * dataformats can be uninstalled
190
     *
191
     * @return bool
192
     */
193
    public function is_uninstall_allowed() {
194
        return true;
195
    }
196
 
197
    /**
198
     * Return URL used for management of plugins of this type.
199
     * @return moodle_url
200
     */
201
    public static function get_manage_url() {
202
        return new moodle_url('/admin/settings.php?section=managedataformats');
203
    }
204
 
205
}