diff --git a/admin/blocks.php b/admin/blocks.php index 7b168ffde3a..b7c794b9195 100644 --- a/admin/blocks.php +++ b/admin/blocks.php @@ -30,26 +30,28 @@ $strprotect = get_string('blockprotect', 'admin'); $strunprotect = get_string('blockunprotect', 'admin'); -/// If data submitted, then process and store. + // If data submitted, then process and store. if (!empty($hide) && confirm_sesskey()) { - if (!$block = $DB->get_record('block', array('id'=>$hide))) { - print_error('blockdoesnotexist', 'error'); + if (!$block = $DB->get_record('block', ['id' => $hide])) { + throw new \moodle_exception('blockdoesnotexist', 'error'); } - $DB->set_field('block', 'visible', '0', array('id'=>$block->id)); // Hide block - add_to_config_log('block_visibility', $block->visible, '0', $block->name); - core_plugin_manager::reset_caches(); - admin_get_root(true, false); // settings not required - only pages + + $class = \core_plugin_manager::resolve_plugininfo_class('block'); + $class::enable_plugin($block->name, false); + // Settings not required - only pages. + admin_get_root(true, false); } if (!empty($show) && confirm_sesskey() ) { - if (!$block = $DB->get_record('block', array('id'=>$show))) { - print_error('blockdoesnotexist', 'error'); + if (!$block = $DB->get_record('block', ['id' => $show])) { + throw new \moodle_exception('blockdoesnotexist', 'error'); } - $DB->set_field('block', 'visible', '1', array('id'=>$block->id)); // Show block - add_to_config_log('block_visibility', $block->visible, '1', $block->name); - core_plugin_manager::reset_caches(); - admin_get_root(true, false); // settings not required - only pages + + $class = \core_plugin_manager::resolve_plugininfo_class('block'); + $class::enable_plugin($block->name, true); + // Settings not required - only pages. + admin_get_root(true, false); } if (!empty($protect) && confirm_sesskey()) { @@ -234,5 +236,3 @@ } echo $OUTPUT->footer(); - - diff --git a/lib/classes/plugininfo/block.php b/lib/classes/plugininfo/block.php index bce3f997844..860699959f1 100644 --- a/lib/classes/plugininfo/block.php +++ b/lib/classes/plugininfo/block.php @@ -41,6 +41,29 @@ class block extends base { return $DB->get_records_menu('block', array('visible'=>1), 'name ASC', 'name, name AS val'); } + public static function enable_plugin(string $pluginname, int $enabled): bool { + global $DB; + + if (!$block = $DB->get_record('block', ['name' => $pluginname])) { + throw new \moodle_exception('blockdoesnotexist', 'error'); + } + + $haschanged = false; + + // Only set visibility if it's different from the current value. + if ($block->visible != $enabled) { + // Set block visibility. + $DB->set_field('block', 'visible', $enabled, ['id' => $block->id]); + $haschanged = true; + + // Include this information into config changes table. + add_to_config_log('block_visibility', $block->visible, $enabled, $pluginname); + \core_plugin_manager::reset_caches(); + } + + return $haschanged; + } + /** * Magic method getter, redirects to read only values. *