MDL-72720 portfolio: Implement enable_plugin() method

This one was tricky because it works slightly different and requires
a portfolio instance will be created if it doesn't exist.
For now I've implemented the easiest approach but, in the future,
it might be improved adding also the "deleted" status to the
enable_plugin() method, to completely remove it (current
implementation only switch status from "Enable but hidden" to
"Enable and visible").
This commit is contained in:
Sara Arjona 2021-10-05 12:06:39 +02:00
parent a7570bfcb6
commit 14e492bebb
2 changed files with 38 additions and 9 deletions

View File

@ -102,20 +102,15 @@ if (($action == 'edit') || ($action == 'new')) {
require_sesskey();
$instance = portfolio_instance($portfolio);
$current = $instance->get('visible');
if (empty($current) && $instance->instance_sanity_check()) {
print_error('cannotsetvisible', 'portfolio', $baseurl);
}
$plugin = $instance->get('plugin');
if ($action == 'show') {
$visible = 1;
} else {
$visible = 0;
}
$instance->set('visible', $visible);
$instance->save();
core_plugin_manager::reset_caches();
$class = \core_plugin_manager::resolve_plugininfo_class('portfolio');
$class::enable_plugin($plugin, $visible);
$return = true;
} else if ($action == 'delete') {
$instance = portfolio_instance($portfolio);
@ -246,4 +241,3 @@ if ($return) {
}
echo $OUTPUT->footer();

View File

@ -48,6 +48,41 @@ class portfolio extends base {
return $enabled;
}
public static function enable_plugin(string $pluginname, int $enabled): bool {
global $DB, $CFG;
require_once($CFG->libdir . '/portfoliolib.php');
$haschanged = false;
$oldvalue = null;
$data = ['visible' => $enabled];
if ($plugin = $DB->get_record('portfolio_instance', ['plugin' => $pluginname])) {
$instance = portfolio_instance($plugin->id);
$oldvalue = $instance->get('visible');
if (empty($oldvalue) && $instance->instance_sanity_check()) {
throw new \moodle_exception('cannotsetvisible', 'portfolio');
}
// Only set visibility if it's different from the current value.
if ($oldvalue != $enabled) {
$haschanged = true;
$instance->set('visible', $enabled);
$instance->save();
}
} else {
$haschanged = true;
portfolio_static_function($pluginname, 'create_instance', $pluginname, $pluginname, $data);
}
if ($haschanged) {
// Include this information into config changes table.
add_to_config_log('portfolio_visibility', $oldvalue, $enabled, $pluginname);
\core_plugin_manager::reset_caches();
}
return $haschanged;
}
/**
* Return URL used for management of plugins of this type.
* @return moodle_url