From 96a16c14a0d4389da5d64ab417f626734730add6 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Tue, 5 Oct 2021 16:16:44 +0200 Subject: [PATCH] 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). --- admin/repository.php | 16 ++++------------ lib/classes/plugininfo/repository.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/admin/repository.php b/admin/repository.php index 278912eee3d..97c54efcc85 100644 --- a/admin/repository.php +++ b/admin/repository.php @@ -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); diff --git a/lib/classes/plugininfo/repository.php b/lib/classes/plugininfo/repository.php index 689edde3c91..7635d57f7e9 100644 --- a/lib/classes/plugininfo/repository.php +++ b/lib/classes/plugininfo/repository.php @@ -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; }