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  2017 Damyon Wiese
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core\plugininfo;
25
 
26
/**
27
 * Class for document converter plugins
28
 *
29
 * @package    core
30
 * @copyright  2017 Damyon Wiese
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class fileconverter extends base {
34
 
35
    public static function plugintype_supports_disabling(): bool {
36
        return true;
37
    }
38
 
39
    /**
40
     * Should there be a way to uninstall the plugin via the administration UI.
41
     *
42
     * Uninstallation is allowed for fileconverter plugins.
43
     *
44
     * @return bool
45
     */
46
    public function is_uninstall_allowed() {
47
        return true;
48
    }
49
 
50
    /**
51
     * Get the name for the settings section.
52
     *
53
     * @return string
54
     */
55
    public function get_settings_section_name() {
56
        return 'fileconverter' . $this->name;
57
    }
58
 
59
    /**
60
     * Load the global settings for a particular availability plugin (if there are any)
61
     *
62
     * @param \part_of_admin_tree $adminroot
63
     * @param string $parentnodename
64
     * @param bool $hassiteconfig
65
     */
66
    public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
67
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
68
        /** @var \admin_root $ADMIN */
69
        $ADMIN = $adminroot; // May be used in settings.php.
70
        $plugininfo = $this; // Also can be used inside settings.php.
71
 
72
        if (!$this->is_installed_and_upgraded()) {
73
            return;
74
        }
75
 
76
        if (!$hassiteconfig) {
77
            return;
78
        }
79
 
80
        $section = $this->get_settings_section_name();
81
 
82
        $settings = null;
83
        if (file_exists($this->full_path('settings.php'))) {
84
            $settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
85
            include($this->full_path('settings.php')); // This may also set $settings to null.
86
        }
87
        if ($settings) {
88
            $ADMIN->add($parentnodename, $settings);
89
        }
90
    }
91
 
92
    /**
93
     * Return URL used for management of plugins of this type.
94
     * @return \moodle_url
95
     */
96
    public static function get_manage_url() {
97
        return new \moodle_url('/admin/settings.php', array('section' => 'managefileconverterplugins'));
98
    }
99
 
100
    /**
101
     * Finds all enabled plugins, the result may include missing plugins.
102
     *
103
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
104
     */
105
    public static function get_enabled_plugins() {
106
        global $CFG;
107
 
108
        $order = (!empty($CFG->converter_plugins_sortorder)) ? explode(',', $CFG->converter_plugins_sortorder) : [];
109
        if ($order) {
110
            $plugins = \core_plugin_manager::instance()->get_installed_plugins('fileconverter');
111
            $order = array_intersect($order, array_keys($plugins));
112
        }
113
 
114
        return array_combine($order, $order);
115
    }
116
 
117
    public static function enable_plugin(string $pluginname, int $enabled): bool {
118
        global $CFG;
119
 
120
        $haschanged = false;
121
        $plugins = [];
122
        if (!empty($CFG->converter_plugins_sortorder)) {
123
            $plugins = array_flip(explode(',', $CFG->converter_plugins_sortorder));
124
        }
125
        // Only set visibility if it's different from the current value.
126
        if ($enabled && !array_key_exists($pluginname, $plugins)) {
127
            $plugins[$pluginname] = $pluginname;
128
            $haschanged = true;
129
        } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
130
            unset($plugins[$pluginname]);
131
            $haschanged = true;
132
        }
133
 
134
        if ($haschanged) {
135
            add_to_config_log('converter_plugins_sortorder', !$enabled, $enabled, $pluginname);
136
            self::set_enabled_plugins(array_flip($plugins));
137
        }
138
 
139
        return $haschanged;
140
    }
141
 
142
    /**
143
     * Sets the current plugin as enabled or disabled
144
     * When enabling tries to guess the sortorder based on default rank returned by the plugin.
145
     * @param bool $newstate
146
     */
147
    public function set_enabled($newstate = true) {
148
        self::enable_plugin($this->name, $newstate);
149
    }
150
 
151
    /**
152
     * Set the list of enabled converter players in the specified sort order
153
     * To be used when changing settings or in unit tests
154
     * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
155
     */
156
    public static function set_enabled_plugins($list) {
157
        if (empty($list)) {
158
            $list = [];
159
        } else if (!is_array($list)) {
160
            $list = explode(',', $list);
161
        }
162
        if ($list) {
163
            $plugins = \core_plugin_manager::instance()->get_installed_plugins('fileconverter');
164
            $list = array_intersect($list, array_keys($plugins));
165
        }
166
        set_config('converter_plugins_sortorder', join(',', $list));
167
        \core_plugin_manager::reset_caches();
168
    }
169
 
170
    /**
171
     * Returns a string describing the formats this engine can converter from / to.
172
     *
173
     * @return string
174
     */
175
    public function get_supported_conversions() {
176
        $classname = self::get_classname($this->name);
177
        if (class_exists($classname)) {
178
            $object = new $classname();
179
            return $object->get_supported_conversions();
180
        }
181
        return '';
182
    }
183
 
184
    /**
185
     * Return the class name for the plugin.
186
     *
187
     * @param   string $plugin
188
     * @return  string
189
     */
190
    public static function get_classname($plugin) {
191
        return "\\fileconverter_{$plugin}\\converter";
192
    }
193
}