MDL-72720 repository: Implement enable_plugin() method

This one was tricky because it works slightly different and requires
a repository instance will be created if it doesn't exist.
For now I've implemented the easiest approach because it's not
possible to create instances (some extra parameters should be added
to the method in order to support it).
This commit is contained in:
Sara Arjona
2021-10-15 13:00:04 +02:00
parent bdab64dc61
commit 96a16c14a0
2 changed files with 30 additions and 12 deletions
+4 -12
View File
@@ -183,23 +183,15 @@ if (($action == 'edit') || ($action == 'new')) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$repositorytype = repository::get_type_by_typename($repository);
if (empty($repositorytype)) {
print_error('invalidplugin', 'repository', '', $repository);
}
$repositorytype->update_visibility(true);
core_plugin_manager::reset_caches();
$class = \core_plugin_manager::resolve_plugininfo_class('repository');
$class::enable_plugin($repository, true);
$return = true;
} else if ($action == 'hide') {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$repositorytype = repository::get_type_by_typename($repository);
if (empty($repositorytype)) {
print_error('invalidplugin', 'repository', '', $repository);
}
$repositorytype->update_visibility(false);
core_plugin_manager::reset_caches();
$class = \core_plugin_manager::resolve_plugininfo_class('repository');
$class::enable_plugin($repository, false);
$return = true;
} else if ($action == 'delete') {
$repositorytype = repository::get_type_by_typename($repository);
+26
View File
@@ -40,6 +40,32 @@ class repository extends base {
return $DB->get_records_menu('repository', null, 'type ASC', 'type, type AS val');
}
public static function enable_plugin(string $pluginname, int $enabled): bool {
global $DB;
$haschanged = false;
$repositorytype = \repository::get_type_by_typename($pluginname);
if (empty($repositorytype)) {
throw new \moodle_exception('invalidplugin', 'repository', '', $pluginname);
}
// The visibility will be only changed if the repository exists in database. Otherwise, this method won't do anything.
if ($plugin = $DB->get_record('repository', ['type' => $pluginname])) {
$oldvalue = $plugin->visible;
// Only set visibility if it's different from the current value.
if ($oldvalue != $enabled) {
$haschanged = true;
$repositorytype->update_visibility($enabled);
// Include this information into config changes table.
add_to_config_log('repository_visibility', $oldvalue, $enabled, $pluginname);
\core_plugin_manager::reset_caches();
}
}
return $haschanged;
}
public function get_settings_section_name() {
return 'repositorysettings'.$this->name;
}