1441 |
ariadna |
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 |
namespace core\plugininfo;
|
|
|
18 |
|
|
|
19 |
use core_plugin_manager;
|
|
|
20 |
use moodle_url;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* AI placement plugin info class.
|
|
|
24 |
*
|
|
|
25 |
* @package core
|
|
|
26 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class aiplacement extends base {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Should there be a way to uninstall the plugin via the administration UI.
|
|
|
33 |
*
|
|
|
34 |
* By default, uninstallation is allowed.
|
|
|
35 |
*
|
|
|
36 |
* @return bool
|
|
|
37 |
*/
|
|
|
38 |
public function is_uninstall_allowed(): bool {
|
|
|
39 |
return true;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* This plugintype supports its plugins being disabled.
|
|
|
44 |
*
|
|
|
45 |
* @return bool
|
|
|
46 |
*/
|
|
|
47 |
public static function plugintype_supports_disabling(): bool {
|
|
|
48 |
return true;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Returns the node name used in admin settings menu for this plugin settings.
|
|
|
53 |
*
|
|
|
54 |
* @return string node name.
|
|
|
55 |
*/
|
|
|
56 |
public function get_settings_section_name(): string {
|
|
|
57 |
return $this->type . '_' . $this->name;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Loads plugin settings to the settings tree.
|
|
|
62 |
*
|
|
|
63 |
* @param \part_of_admin_tree $adminroot
|
|
|
64 |
* @param string $parentnodename
|
|
|
65 |
* @param bool $hassiteconfig whether the current user has moodle/site:config capability
|
|
|
66 |
*/
|
|
|
67 |
public function load_settings(
|
|
|
68 |
\part_of_admin_tree $adminroot,
|
|
|
69 |
$parentnodename,
|
|
|
70 |
$hassiteconfig,
|
|
|
71 |
): void {
|
|
|
72 |
global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
|
|
|
73 |
/** @var \admin_root $ADMIN */
|
|
|
74 |
$ADMIN = $adminroot; // May be used in settings.php.
|
|
|
75 |
$plugininfo = $this; // Also can be used inside settings.php.
|
|
|
76 |
|
|
|
77 |
if (!$this->is_installed_and_upgraded()) {
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if (!$hassiteconfig) {
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$section = $this->get_settings_section_name();
|
|
|
86 |
|
|
|
87 |
// Load the specific settings.
|
|
|
88 |
$settings = new \core_ai\admin\admin_settingspage_provider(
|
|
|
89 |
name: $section,
|
|
|
90 |
visiblename: $this->displayname,
|
|
|
91 |
req_capability: 'moodle/site:config',
|
|
|
92 |
hidden: true,
|
|
|
93 |
);
|
|
|
94 |
if (file_exists($this->full_path('settings.php'))) {
|
|
|
95 |
include($this->full_path('settings.php')); // This may also set $settings to null.
|
|
|
96 |
// Show the save changes button between the specific settings and the actions table.
|
|
|
97 |
$settings->add(new \admin_setting_savebutton("{$section}/savebutton"));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// Load the actions table.
|
|
|
101 |
if (file_exists($this->full_path('setting_actions.php'))) {
|
|
|
102 |
include($this->full_path('setting_actions.php')); // This may also set $settings to null.
|
|
|
103 |
} else {
|
|
|
104 |
$settings->add(new \admin_setting_heading("{$section}/generals",
|
|
|
105 |
new \lang_string('placementactionsettings', 'core_ai'),
|
|
|
106 |
new \lang_string('placementactionsettings_desc', 'core_ai')));
|
|
|
107 |
// Load the setting table of actions that this provider supports.
|
|
|
108 |
$settings->add(new \core_ai\admin\admin_setting_action_manager(
|
|
|
109 |
$section,
|
|
|
110 |
\core_ai\table\aiplacement_action_management_table::class,
|
|
|
111 |
'manageaiplacements',
|
|
|
112 |
new \lang_string('manageaiproviders', 'core_ai'),
|
|
|
113 |
));
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($settings) {
|
|
|
117 |
$ADMIN->add($parentnodename, $settings);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Return URL used for management of plugins of this type.
|
|
|
123 |
*
|
|
|
124 |
* @return moodle_url
|
|
|
125 |
*/
|
|
|
126 |
public static function get_manage_url(): moodle_url {
|
|
|
127 |
return new moodle_url('/admin/settings.php', [
|
|
|
128 |
'section' => 'aiplacement',
|
|
|
129 |
]);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Enable or disable a plugin.
|
|
|
134 |
* When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
|
|
|
135 |
*
|
|
|
136 |
* @param string $pluginname The plugin name to enable/disable.
|
|
|
137 |
* @param int $enabled Whether the pluginname should be enabled (1) or not (0).
|
|
|
138 |
* @return bool Whether $pluginname has been updated or not.
|
|
|
139 |
*/
|
|
|
140 |
public static function enable_plugin(string $pluginname, int $enabled): bool {
|
|
|
141 |
$plugin = "aiplacement_$pluginname";
|
|
|
142 |
$oldvalue = self::is_plugin_enabled($pluginname);
|
|
|
143 |
$newvalue = (bool)$enabled;
|
|
|
144 |
|
|
|
145 |
if ($oldvalue !== $newvalue) {
|
|
|
146 |
if ($newvalue) {
|
|
|
147 |
set_config('enabled', $enabled, $plugin);
|
|
|
148 |
} else {
|
|
|
149 |
unset_config('enabled', $plugin);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
add_to_config_log('enabled', $oldvalue, $newvalue, $plugin);
|
|
|
153 |
core_plugin_manager::reset_caches();
|
|
|
154 |
return true;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
return false;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Finds all enabled plugins, the result may include missing plugins.
|
|
|
162 |
*
|
|
|
163 |
* @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown.
|
|
|
164 |
*/
|
|
|
165 |
public static function get_enabled_plugins(): ?array {
|
|
|
166 |
$pluginmanager = core_plugin_manager::instance();
|
|
|
167 |
$plugins = $pluginmanager->get_installed_plugins('aiplacement');
|
|
|
168 |
|
|
|
169 |
if (!$plugins) {
|
|
|
170 |
return [];
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
$plugins = array_keys($plugins);
|
|
|
174 |
|
|
|
175 |
// Filter to return only enabled plugins.
|
|
|
176 |
$enabled = [];
|
|
|
177 |
foreach ($plugins as $plugin) {
|
|
|
178 |
if (self::is_plugin_enabled($plugin)) {
|
|
|
179 |
$enabled[$plugin] = $plugin;
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
return $enabled;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Check if a provider plugin is enabled in config.
|
|
|
187 |
*
|
|
|
188 |
* @param string $plugin The plugin to check.
|
|
|
189 |
* @return bool Return true if enabled.
|
|
|
190 |
*/
|
|
|
191 |
public static function is_plugin_enabled(string $plugin): bool {
|
|
|
192 |
$config = get_config("aiplacement_$plugin", 'enabled');
|
|
|
193 |
return $config == 1;
|
|
|
194 |
}
|
|
|
195 |
}
|