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 * This page provides the Administration -> ... -> Theme selector UI.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(__DIR__
. '/../config.php');
25 require_once($CFG->libdir
. '/adminlib.php');
27 $choose = optional_param('choose', '', PARAM_PLUGIN
);
28 $reset = optional_param('reset', 0, PARAM_BOOL
);
29 $confirmation = optional_param('confirmation', 0, PARAM_BOOL
);
31 admin_externalpage_setup('themeselector');
33 unset($SESSION->theme
);
35 $PAGE->set_primary_active_tab('siteadminnode');
36 $PAGE->navbar
->add(get_string('themeselector', 'admin'), $PAGE->url
);
39 if ($reset and confirm_sesskey()) {
40 theme_reset_all_caches();
44 if (!empty($choose) && confirm_sesskey()) {
46 // Load the theme to make sure it is valid.
47 $theme = theme_config
::load($choose);
49 if ($theme instanceof \theme_config
) {
50 set_config('theme', $theme->name
);
51 $notifytype = 'success';
52 $notifymessage = get_string('themesaved');
54 $notifytype = 'error';
55 $notifymessage = get_string('error');
58 // Redirect with notification.
59 redirect(new moodle_url('/theme/index.php'), $notifymessage, null, $notifytype);
62 $table = new html_table();
64 $table->id
= 'adminthemeselector';
65 $table->head
= [get_string('theme'), get_string('info')];
66 $table->align
= ['left', 'left'];
68 $themes = core_component
::get_plugin_list('theme');
70 // Loop through available themes.
71 foreach ($themes as $themename => $themedir) {
74 $theme = theme_config
::load($themename);
75 } catch (Exception
$e) {
76 // Bad theme, just skip it for now.
79 if ($themename !== $theme->name
) {
80 // Obsoleted or broken theme, just skip for now.
83 if (empty($CFG->themedesignermode
) && $theme->hidefromselector
) {
84 // The theme doesn't want to be shown in the theme selector and as theme
85 // designer mode is switched off we will respect that decision.
89 // Build the table rows.
92 $strthemename = get_string('pluginname', 'theme_'.$themename);
94 // Screenshot/preview cell.
95 $screenshotpath = new moodle_url('/theme/image.php', ['theme' => $themename, 'image' => 'screenshot', 'component' => 'theme']);
96 $row[] = html_writer
::empty_tag('img', ['class' => 'img-fluid', 'src' => $screenshotpath, 'alt' => $strthemename]);
99 $infocell = $OUTPUT->heading($strthemename, 3);
101 // Is this the current theme?
102 if ($themename == $CFG->theme
) {
103 $rowclasses[] = 'selectedtheme';
105 // Button to choose this as the main theme.
106 $setthemestr = get_string('usetheme');
107 $setthemeurl = new moodle_url('/theme/index.php', ['choose' => $themename, 'sesskey' => sesskey()]);
108 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'post');
109 $infocell .= html_writer
::div($OUTPUT->render($setthemebutton), 'mt-2');
113 $table->data
[$themename] = $row;
114 $table->rowclasses
[$themename] = join(' ', $rowclasses);
115 $table->responsive
= false;
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading(get_string('themeselector', 'admin'));
122 // Reset theme caches button.
123 $reseturl = new moodle_url('index.php', ['sesskey' => sesskey(), 'reset' => 1]);
124 echo $OUTPUT->single_button($reseturl, get_string('themeresetcaches', 'admin'), 'post');
126 // Render main table.
127 echo html_writer
::table($table);
128 echo $OUTPUT->footer();