From 9a8f974a3acc590d7b62633ae557c3672ca8cbc7 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 1 Oct 2021 11:51:17 +0200 Subject: [PATCH] MDL-72720 mod: Implement enable_plugin() method --- admin/modules.php | 39 ++++---------------------- lib/classes/plugininfo/mod.php | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/admin/modules.php b/admin/modules.php index e76a8e11a3c..5bfe9cd2477 100644 --- a/admin/modules.php +++ b/admin/modules.php @@ -30,42 +30,17 @@ /// If data submitted, then process and store. if (!empty($hide) and confirm_sesskey()) { - if (!$module = $DB->get_record("modules", array("name"=>$hide))) { - print_error('moduledoesnotexist', 'error'); - } - $DB->set_field("modules", "visible", "0", array("id"=>$module->id)); // Hide main module - // Remember the visibility status in visibleold - // and hide... - $sql = "UPDATE {course_modules} - SET visibleold=visible, visible=0 - WHERE module=?"; - $DB->execute($sql, array($module->id)); - // Increment course.cacherev for courses where we just made something invisible. - // This will force cache rebuilding on the next request. - increment_revision_number('course', 'cacherev', - "id IN (SELECT DISTINCT course - FROM {course_modules} - WHERE visibleold=1 AND module=?)", - array($module->id)); - core_plugin_manager::reset_caches(); + $class = \core_plugin_manager::resolve_plugininfo_class('mod'); + $class::enable_plugin($hide, false); + admin_get_root(true, false); // settings not required - only pages redirect(new moodle_url('/admin/modules.php')); } if (!empty($show) and confirm_sesskey()) { - if (!$module = $DB->get_record("modules", array("name"=>$show))) { - print_error('moduledoesnotexist', 'error'); - } - $DB->set_field("modules", "visible", "1", array("id"=>$module->id)); // Show main module - $DB->set_field('course_modules', 'visible', '1', array('visibleold'=>1, 'module'=>$module->id)); // Get the previous saved visible state for the course module. - // Increment course.cacherev for courses where we just made something visible. - // This will force cache rebuilding on the next request. - increment_revision_number('course', 'cacherev', - "id IN (SELECT DISTINCT course - FROM {course_modules} - WHERE visible=1 AND module=?)", - array($module->id)); - core_plugin_manager::reset_caches(); + $class = \core_plugin_manager::resolve_plugininfo_class('mod'); + $class::enable_plugin($show, true); + admin_get_root(true, false); // settings not required - only pages redirect(new moodle_url('/admin/modules.php')); } @@ -163,5 +138,3 @@ $table->print_html(); echo $OUTPUT->footer(); - - diff --git a/lib/classes/plugininfo/mod.php b/lib/classes/plugininfo/mod.php index d936fbc69eb..ce8ebccf1dd 100644 --- a/lib/classes/plugininfo/mod.php +++ b/lib/classes/plugininfo/mod.php @@ -40,6 +40,57 @@ class mod extends base { return $DB->get_records_menu('modules', array('visible'=>1), 'name ASC', 'name, name AS val'); } + public static function enable_plugin(string $pluginname, int $enabled): bool { + global $DB; + + if (!$module = $DB->get_record('modules', ['name' => $pluginname])) { + throw new \moodle_exception('moduledoesnotexist', 'error'); + } + + $haschanged = false; + + // Only set visibility if it's different from the current value. + if ($module->visible != $enabled) { + // Set module visibility. + $DB->set_field('modules', 'visible', $enabled, ['id' => $module->id]); + $haschanged = true; + + if ($enabled) { + // Revert the previous saved visible state for the course module. + $DB->set_field('course_modules', 'visible', '1', ['visibleold' => 1, 'module' => $module->id]); + + // Increment course.cacherev for courses where we just made something visible. + // This will force cache rebuilding on the next request. + increment_revision_number('course', 'cacherev', + "id IN (SELECT DISTINCT course + FROM {course_modules} + WHERE visible = 1 AND module = ?)", + [$module->id] + ); + } else { + // Remember the visibility status in visibleold and hide. + $sql = "UPDATE {course_modules} + SET visibleold = visible, visible = 0 + WHERE module = ?"; + $DB->execute($sql, [$module->id]); + // Increment course.cacherev for courses where we just made something invisible. + // This will force cache rebuilding on the next request. + increment_revision_number('course', 'cacherev', + 'id IN (SELECT DISTINCT course + FROM {course_modules} + WHERE visibleold = 1 AND module = ?)', + [$module->id] + ); + } + + // Include this information into config changes table. + add_to_config_log('mod_visibility', $module->visible, $enabled, $pluginname); + \core_plugin_manager::reset_caches(); + } + + return $haschanged; + } + /** * Magic method getter, redirects to read only values. *