2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Theme selector page for admin use.
21 * @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../config.php');
26 require_once($CFG->libdir
. '/adminlib.php');
28 $choose = optional_param('choose', '', PARAM_PLUGIN
);
29 $reset = optional_param('reset', 0, PARAM_BOOL
);
30 $confirmation = optional_param('confirmation', 0, PARAM_BOOL
);
32 admin_externalpage_setup('themeselector');
34 unset($SESSION->theme
);
36 $PAGE->set_primary_active_tab('siteadminnode');
37 $PAGE->navbar
->add(get_string('themeselector', 'admin'), $PAGE->url
);
38 $PAGE->set_pagelayout('standard');
41 if ($reset && confirm_sesskey()) {
42 theme_reset_all_caches();
45 $definedinconfig = array_key_exists('theme', $CFG->config_php_settings
);
46 if ($definedinconfig) {
47 $forcedthemename = get_string('pluginname', 'theme_'.$CFG->theme
);
48 // Show a notification that the theme is defined in config.php.
49 \core\notification
::info(get_string('themedefinedinconfigphp', 'admin', $forcedthemename));
53 if (!$definedinconfig && !empty($choose) && confirm_sesskey()) {
55 // Load the theme to make sure it is valid.
56 $theme = theme_config
::load($choose);
58 if ($theme instanceof \theme_config
) {
59 set_config('theme', $theme->name
);
60 $notifytype = 'success';
61 $notifymessage = get_string('themesaved');
63 $notifytype = 'error';
64 $notifymessage = get_string('error');
67 // Redirect with notification.
68 redirect(new moodle_url('/admin/themeselector.php'), $notifymessage, null, $notifytype);
72 echo $OUTPUT->header();
74 // Prepare data for rendering.
77 $currentthemeindex = 0;
78 $themes = core_component
::get_plugin_list('theme');
80 // Loop through available themes.
81 foreach ($themes as $themename => $themedir) {
84 $theme = theme_config
::load($themename);
85 } catch (Exception
$e) {
86 // Bad theme, just skip it for now.
89 if ($themename !== $theme->name
) {
90 // Obsoleted or broken theme, just skip for now.
93 if (empty($CFG->themedesignermode
) && $theme->hidefromselector
) {
94 // The theme doesn't want to be shown in the theme selector and as theme
95 // designer mode is switched off we will respect that decision.
99 // All params for modal use are set here, except for 'choosereadme' (description).
100 // That string can be long. We will fetch it with JS as opposed to passing it as an attribute.
103 // The 'name' param is formatted and should not to be confused with 'choose'.
104 $themedata['name'] = get_string('pluginname', 'theme_'.$themename);;
105 $themedata['choose'] = $themename;
107 // Image to display for previewing.
108 $image = new moodle_url('/theme/image.php', ['theme' => $themename, 'image' => 'screenshot', 'component' => 'theme']);
109 $themedata['image'] = $image;
111 // Is this the current theme?
112 if ($themename === $CFG->theme
) {
113 $themedata['current'] = true;
114 $currentthemeindex = $index;
115 } else if (!$definedinconfig) {
117 $actionurl = new moodle_url('/admin/themeselector.php');
118 $themedata['actionurl'] = $actionurl;
119 $themedata['sesskey'] = sesskey();
123 $settingspath = "$themedir/settings.php";
124 if (file_exists($settingspath)) {
125 $section = "themesetting{$themename}";
126 $settingsurl = new moodle_url('/admin/settings.php', ['section' => $section]);
127 $themedata['settingsurl'] = $settingsurl;
130 // Link to the theme usage report if override enabled and it is being used in at least one context.
131 if (\core\output\theme_usage
::is_theme_used_in_any_context($themename) === \core\output\theme_usage
::THEME_IS_USED
) {
132 $reporturl = new moodle_url($CFG->wwwroot
. '/report/themeusage/index.php');
133 $reporturl->params(['themechoice' => $themename]);
134 $themedata['reporturl'] = $reporturl->out(false);
137 $data[$index] = $themedata;
141 // Reorder the array to always have the current theme first.
142 if (isset($data[$currentthemeindex])) {
143 $currenttheme = $data[$currentthemeindex];
144 unset($data[$currentthemeindex]);
145 array_unshift($data, $currenttheme);
148 // Show theme selector.
149 $renderable = new \core_admin\output\theme_selector
($data, $definedinconfig);
150 $renderer = $PAGE->get_renderer('core', 'admin');
151 echo $renderer->theme_selector_list($renderable);
154 echo $OUTPUT->footer();