MDL-68889 block_recentlyaccessedcourses: small viewport issues
[moodle.git] / theme / index.php
CommitLineData
b7009474 1<?php
b7009474 2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
574909ef
PS
18 * This page provides the Administration -> ... -> Theme selector UI.
19 *
20 * @package core
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b7009474 22 */
23
1fcf0ca8 24require_once(__DIR__ . '/../config.php');
b7009474 25require_once($CFG->libdir . '/adminlib.php');
26
aff24313 27$choose = optional_param('choose', '', PARAM_PLUGIN);
78946b9b 28$reset = optional_param('reset', 0, PARAM_BOOL);
37959dd4 29$device = optional_param('device', '', PARAM_TEXT);
bff67f49 30$unsettheme = optional_param('unsettheme', 0, PARAM_BOOL);
8dabb720 31$confirmation = optional_param('confirmation', 0, PARAM_BOOL);
b7009474 32
33admin_externalpage_setup('themeselector');
34
e5824bb9 35if (!empty($device)) {
c3d2fbf9
SH
36 // Make sure the device requested is valid.
37 $devices = core_useragent::get_device_type_list();
e5824bb9 38 if (!in_array($device, $devices)) {
c3d2fbf9 39 // The provided device isn't a valid device throw an error.
e5824bb9
SH
40 print_error('invaliddevicetype');
41 }
42}
43
b7009474 44unset($SESSION->theme);
45
78946b9b
PS
46if ($reset and confirm_sesskey()) {
47 theme_reset_all_caches();
8dabb720 48} else if ($choose && $confirmation) {
49
50 $theme = theme_config::load($choose);
51 echo $OUTPUT->header();
52 echo $OUTPUT->heading(get_string('themesaved'));
53 echo $OUTPUT->box_start();
54 echo format_text(get_string('choosereadme', 'theme_'.$theme->name), FORMAT_MOODLE);
55 echo $OUTPUT->box_end();
56 echo $OUTPUT->continue_button($CFG->wwwroot . '/theme/index.php');
57 echo $OUTPUT->footer();
58 exit;
78946b9b 59
f1ecad60 60} else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
e5824bb9
SH
61 // Load the theme to make sure it is valid.
62 $theme = theme_config::load($choose);
f1ecad60 63
e5824bb9 64 // Get the config argument for the chosen device.
c3d2fbf9 65 $themename = core_useragent::get_device_type_cfg_var_name($device);
37959dd4 66 set_config($themename, $theme->name);
8ba55c01 67
8dabb720 68 $urlconfirm = new moodle_url('/theme/index.php', array('confirmation' => 1, 'choose' => $choose));
69 redirect($urlconfirm);
f1ecad60 70} else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
c3d2fbf9
SH
71 // Unset the theme and continue.
72 unset_config(core_useragent::get_device_type_cfg_var_name($device));
bff67f49 73 $device = '';
b7009474 74}
75
37959dd4
AF
76// Otherwise, show either a list of devices, or is enabledevicedetection set to no or a
77// device is specified show a list of themes.
78
fd09397e 79$table = new html_table();
e5824bb9 80$table->data = array();
604cb179 81$heading = '';
7bff5441 82if (!empty($CFG->enabledevicedetection) && empty($device)) {
604cb179 83 $heading = get_string('selectdevice', 'admin');
e5824bb9
SH
84 // Display a list of devices that a user can select a theme for.
85
86 $strthemenotselected = get_string('themenoselected', 'admin');
87 $strthemeselect = get_string('themeselect', 'admin');
88
c3d2fbf9 89 // Display the device selection screen.
e5824bb9 90 $table->id = 'admindeviceselector';
604cb179 91 $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
37959dd4 92
c3d2fbf9 93 $devices = core_useragent::get_device_type_list();
32b0c696 94 foreach ($devices as $thedevice) {
37959dd4 95
c3d2fbf9
SH
96 $headingthemename = ''; // To output the picked theme name when needed.
97 $themename = core_useragent::get_device_type_theme($thedevice);
32b0c696 98 if (!$themename && $thedevice == 'default') {
37959dd4
AF
99 $themename = theme_config::DEFAULT_THEME;
100 }
f1ecad60 101 $themelocked = theme_is_device_locked($thedevice);
37959dd4 102
e5824bb9 103 $screenshotcell = $strthemenotselected;
bff67f49 104 $unsetthemebutton = '';
37959dd4 105 if ($themename) {
c3d2fbf9 106 // Check the theme exists.
e5824bb9
SH
107 $themename = clean_param($themename, PARAM_THEME);
108 if (empty($themename)) {
c3d2fbf9
SH
109 // Likely the theme has been deleted.
110 unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
e5824bb9
SH
111 } else {
112 $strthemename = get_string('pluginname', 'theme_'.$themename);
c3d2fbf9
SH
113 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
114 // not the current one.
115 $screenshoturl = new moodle_url('/theme/image.php',
116 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
e5824bb9 117 // Contents of the screenshot/preview cell.
4b58fc61 118 $screenshotcell = html_writer::empty_tag('img', array('class' => 'img-responsive img-fluid',
119 'src' => $screenshoturl, 'alt' => $strthemename));
c3d2fbf9 120 // Show the name of the picked theme.
43c741b9 121 $headingthemename = $OUTPUT->heading($strthemename, 3);
e5824bb9 122 }
bff67f49 123 // If not default device then show option to unset theme.
f1ecad60 124 if ($thedevice != 'default' && !$themelocked) {
bff67f49 125 $unsetthemestr = get_string('unsettheme', 'admin');
c3d2fbf9
SH
126 $unsetthemeurl = new moodle_url('/theme/index.php',
127 array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
bff67f49
RT
128 $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
129 $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
130 }
37959dd4
AF
131 }
132
32b0c696 133 $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
f1ecad60
RW
134
135 $select = '';
136 if (!$themelocked) {
137 $select = $OUTPUT->render(new single_button($deviceurl, $strthemeselect, 'get'));
138 }
139
140 $lockwarning = '';
141 if ($themelocked) {
142 $lockwarning = html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
143 }
37959dd4 144
e5824bb9 145 $table->data[] = array(
32b0c696 146 $OUTPUT->heading(ucfirst($thedevice), 3),
e5824bb9 147 $screenshotcell,
f1ecad60 148 $headingthemename . $lockwarning . $select . $unsetthemebutton
e5824bb9 149 );
78946b9b 150 }
e5824bb9
SH
151} else {
152 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
153 // of themes to select.
604cb179 154 $heading = get_string('selecttheme', 'admin', $device);
e5824bb9 155 if (empty($device)) {
c3d2fbf9
SH
156 // If $CFG->enabledevicedetection is off this will return 'default'.
157 $device = core_useragent::get_device_type();
ee8df661 158 }
b7009474 159
f1ecad60 160 $themelocked = theme_is_device_locked($device);
e5824bb9
SH
161 $table->id = 'adminthemeselector';
162 $table->head = array(get_string('theme'), get_string('info'));
37959dd4 163
f1ecad60
RW
164 $themes = array();
165 if ($themelocked) {
166 $heading = get_string('currenttheme', 'admin');
167 $themename = theme_get_locked_theme_for_device($device);
168 $themedirectory = core_component::get_plugin_directory('theme', $themename);
169 $themes[$themename] = $themedirectory;
170 } else {
171 $themes = core_component::get_plugin_list('theme');
172 }
78946b9b 173
e5824bb9 174 foreach ($themes as $themename => $themedir) {
37959dd4 175
e5824bb9
SH
176 // Load the theme config.
177 try {
178 $theme = theme_config::load($themename);
179 } catch (Exception $e) {
180 // Bad theme, just skip it for now.
181 continue;
182 }
183 if ($themename !== $theme->name) {
c3d2fbf9 184 // Obsoleted or broken theme, just skip for now.
e5824bb9
SH
185 continue;
186 }
e625feaf 187 if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
e5824bb9
SH
188 // The theme doesn't want to be shown in the theme selector and as theme
189 // designer mode is switched off we will respect that decision.
190 continue;
191 }
192 $strthemename = get_string('pluginname', 'theme_'.$themename);
74df4357 193
e5824bb9
SH
194 // Build the table row, and also a list of items to go in the second cell.
195 $row = array();
196 $infoitems = array();
197 $rowclasses = array();
b7009474 198
c3d2fbf9
SH
199 // Set up bools whether this theme is chosen either main or legacy.
200 $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
b7009474 201
e5824bb9 202 if ($ischosentheme) {
c3d2fbf9 203 // Is the chosen main theme.
e5824bb9
SH
204 $rowclasses[] = 'selectedtheme';
205 }
0d399dc5 206
c3d2fbf9
SH
207 // Link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes,
208 // not the current one.
209 $screenshotpath = new moodle_url('/theme/image.php',
0ef2631d 210 array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
e5824bb9 211 // Contents of the first screenshot/preview cell.
4b58fc61 212 $row[] = html_writer::empty_tag('img', array('class' => 'img-responsive img-fluid',
213 'src' => $screenshotpath, 'alt' => $strthemename));
e5824bb9
SH
214 // Contents of the second cell.
215 $infocell = $OUTPUT->heading($strthemename, 3);
ebb19aba 216
f1ecad60
RW
217 if ($themelocked) {
218 $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
219 }
220
c3d2fbf9 221 // Button to choose this as the main theme or unset this theme for devices other then default.
f1ecad60
RW
222 if (!$themelocked) {
223 if (($ischosentheme) && ($device != 'default')) {
224 $unsetthemestr = get_string('unsettheme', 'admin');
225 $unsetthemeurl = new moodle_url('/theme/index.php',
226 array('device' => $device, 'unsettheme' => true, 'sesskey' => sesskey()));
227 $unsetbutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
228 $infocell .= $OUTPUT->render($unsetbutton);
229 } else if ((!$ischosentheme)) {
230 $setthemestr = get_string('usetheme');
231 $setthemeurl = new moodle_url('/theme/index.php',
232 array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey()));
233 $setthemebutton = new single_button($setthemeurl, $setthemestr, 'get');
234 $infocell .= $OUTPUT->render($setthemebutton);
235 }
36ff8d05 236 }
b7009474 237
e5824bb9 238 $row[] = $infocell;
edda2b9a 239
e5824bb9
SH
240 $table->data[$themename] = $row;
241 $table->rowclasses[$themename] = join(' ', $rowclasses);
242 }
b7009474 243}
604cb179
RT
244echo $OUTPUT->header('themeselector');
245echo $OUTPUT->heading($heading);
246
32b0c696
GB
247$params = array('sesskey' => sesskey(), 'reset' => 1);
248if (!empty($device)) {
249 $params['device'] = $device;
250}
251echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
b7009474 252
16be8974 253echo html_writer::table($table);
b7009474 254
e625feaf 255echo $OUTPUT->footer();