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_contentbank
|
|
|
21 |
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace core\plugininfo;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Class for contentbank plugins
|
|
|
28 |
*
|
|
|
29 |
* @package core_contentbank
|
|
|
30 |
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class contenttype extends base {
|
|
|
34 |
|
|
|
35 |
public static function plugintype_supports_disabling(): bool {
|
|
|
36 |
return true;
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Defines if there should be a way to uninstall the plugin via the administration UI.
|
|
|
41 |
*
|
|
|
42 |
* @return bool
|
|
|
43 |
*/
|
|
|
44 |
public function is_uninstall_allowed() {
|
|
|
45 |
return true;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Get the name for the settings section.
|
|
|
50 |
*
|
|
|
51 |
* @return string
|
|
|
52 |
*/
|
|
|
53 |
public function get_settings_section_name() {
|
|
|
54 |
return 'contentbanksetting' . $this->name;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Load the global settings for a particular contentbank plugin (if there are any)
|
|
|
59 |
*
|
|
|
60 |
* @param \part_of_admin_tree $adminroot
|
|
|
61 |
* @param string $parentnodename
|
|
|
62 |
* @param bool $hassiteconfig
|
|
|
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 |
/** @var \admin_root $ADMIN */
|
|
|
67 |
$ADMIN = $adminroot; // May be used in settings.php.
|
|
|
68 |
$plugininfo = $this; // Also can be used inside settings.php
|
|
|
69 |
$contenttype = $this; // Also to be used inside settings.php.
|
|
|
70 |
|
|
|
71 |
if (!$this->is_installed_and_upgraded()) {
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if (!$hassiteconfig) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
$section = $this->get_settings_section_name();
|
|
|
80 |
|
|
|
81 |
$settings = null;
|
|
|
82 |
if (file_exists($this->full_path('settings.php'))) {
|
|
|
83 |
$settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
|
|
|
84 |
include($this->full_path('settings.php')); // This may also set $settings to null.
|
|
|
85 |
}
|
|
|
86 |
if ($settings) {
|
|
|
87 |
$ADMIN->add($parentnodename, $settings);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Return URL used for management of plugins of this type.
|
|
|
93 |
* @return \moodle_url
|
|
|
94 |
*/
|
|
|
95 |
public static function get_manage_url() {
|
|
|
96 |
return new \moodle_url('/admin/settings.php', array('section' => 'managecontentbanktypes'));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Gathers and returns the information about all plugins of the given type
|
|
|
102 |
*
|
|
|
103 |
* @param string $type the name of the plugintype, eg. mod, auth or workshopform
|
|
|
104 |
* @param string $typerootdir full path to the location of the plugin dir
|
|
|
105 |
* @param string $typeclass the name of the actually called class
|
|
|
106 |
* @param \core_plugin_manager $pluginman the plugin manager calling this method
|
|
|
107 |
* @return array of plugintype classes, indexed by the plugin name
|
|
|
108 |
*/
|
|
|
109 |
public static function get_plugins($type, $typerootdir, $typeclass, $pluginman) {
|
|
|
110 |
global $CFG;
|
|
|
111 |
|
|
|
112 |
$contents = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
|
|
|
113 |
if (!empty($CFG->contentbank_plugins_sortorder)) {
|
|
|
114 |
$order = explode(',', $CFG->contentbank_plugins_sortorder);
|
|
|
115 |
$order = array_merge(array_intersect($order, array_keys($contents)),
|
|
|
116 |
array_diff(array_keys($contents), $order));
|
|
|
117 |
} else {
|
|
|
118 |
$order = array_keys($contents);
|
|
|
119 |
}
|
|
|
120 |
$sortedcontents = array();
|
|
|
121 |
foreach ($order as $contentname) {
|
|
|
122 |
$sortedcontents[$contentname] = $contents[$contentname];
|
|
|
123 |
}
|
|
|
124 |
return $sortedcontents;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Finds all enabled plugins, the result may include missing plugins.
|
|
|
129 |
* @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
|
|
|
130 |
*/
|
|
|
131 |
public static function get_enabled_plugins() {
|
|
|
132 |
global $CFG;
|
|
|
133 |
|
|
|
134 |
$plugins = \core_plugin_manager::instance()->get_installed_plugins('contenttype');
|
|
|
135 |
|
|
|
136 |
if (!$plugins) {
|
|
|
137 |
return array();
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
$plugins = array_keys($plugins);
|
|
|
141 |
// Order the plugins.
|
|
|
142 |
if (!empty($CFG->contentbank_plugins_sortorder)) {
|
|
|
143 |
$order = explode(',', $CFG->contentbank_plugins_sortorder);
|
|
|
144 |
$order = array_merge(array_intersect($order, $plugins),
|
|
|
145 |
array_diff($plugins, $order));
|
|
|
146 |
} else {
|
|
|
147 |
$order = $plugins;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Filter to return only enabled plugins.
|
|
|
151 |
$enabled = array();
|
|
|
152 |
foreach ($order as $plugin) {
|
|
|
153 |
$disabled = get_config('contentbank_' . $plugin, 'disabled');
|
|
|
154 |
if (empty($disabled)) {
|
|
|
155 |
$enabled[$plugin] = $plugin;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
return $enabled;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public static function enable_plugin(string $pluginname, int $enabled): bool {
|
|
|
162 |
$haschanged = false;
|
|
|
163 |
|
|
|
164 |
$plugin = 'contentbank_' . $pluginname;
|
|
|
165 |
$oldvalue = get_config($plugin, 'disabled');
|
|
|
166 |
$disabled = !$enabled;
|
|
|
167 |
// Only set value if there is no config setting or if the value is different from the previous one.
|
|
|
168 |
if ($oldvalue == false && $disabled) {
|
|
|
169 |
set_config('disabled', $disabled, $plugin);
|
|
|
170 |
$haschanged = true;
|
|
|
171 |
} else if ($oldvalue != false && !$disabled) {
|
|
|
172 |
unset_config('disabled', $plugin);
|
|
|
173 |
$haschanged = true;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($haschanged) {
|
|
|
177 |
add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
|
|
|
178 |
\core_plugin_manager::reset_caches();
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return $haschanged;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Optional extra warning before uninstallation adding number of existing contenttype contents.
|
|
|
186 |
*
|
|
|
187 |
* @return string
|
|
|
188 |
*/
|
|
|
189 |
public function get_uninstall_extra_warning() {
|
|
|
190 |
global $DB;
|
|
|
191 |
|
|
|
192 |
$contentcount = $DB->count_records('contentbank_content', ['contenttype' => "contenttype_$this->name"]);
|
|
|
193 |
if (!$contentcount) {
|
|
|
194 |
return '';
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
$message = get_string('contenttypeuninstalling',
|
|
|
198 |
'core_admin',
|
|
|
199 |
(object)['count' => $contentcount, 'type' => $this->displayname]
|
|
|
200 |
);
|
|
|
201 |
|
|
|
202 |
return $message;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Pre-uninstall hook.
|
|
|
207 |
*
|
|
|
208 |
* This is intended for disabling of plugin, some DB table purging, etc.
|
|
|
209 |
*
|
|
|
210 |
* NOTE: to be called from uninstall_plugin() only.
|
|
|
211 |
*/
|
|
|
212 |
public function uninstall_cleanup() {
|
|
|
213 |
global $DB;
|
|
|
214 |
|
|
|
215 |
$records = $DB->get_records('contentbank_content', ['contenttype' => 'contenttype_'.$this->name]);
|
|
|
216 |
$contenttypename = 'contenttype_'.$this->name;
|
|
|
217 |
$contenttypeclass = "\\$contenttypename\\contenttype";
|
|
|
218 |
foreach ($records as $record) {
|
|
|
219 |
$context = \context::instance_by_id($record->contextid, MUST_EXIST);
|
|
|
220 |
$contenttype = new $contenttypeclass($context);
|
|
|
221 |
$contentclass = "\\$contenttypename\\content";
|
|
|
222 |
$content = new $contentclass($record);
|
|
|
223 |
$contenttype->delete_content($content);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
parent::uninstall_cleanup();
|
|
|
227 |
}
|
|
|
228 |
}
|