Skip to content

Commit

Permalink
MDL-72720 repository: Implement enable_plugin() method
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sarjona committed Oct 15, 2021
1 parent bdab64d commit 96a16c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
16 changes: 4 additions & 12 deletions admin/repository.php
Expand Up @@ -183,23 +183,15 @@ function repository_action_url($repository) {
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);
Expand Down
26 changes: 26 additions & 0 deletions lib/classes/plugininfo/repository.php
Expand Up @@ -40,6 +40,32 @@ public static function get_enabled_plugins() {
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;
}
Expand Down

0 comments on commit 96a16c1

Please sign in to comment.