3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This page prvides the Administration -> ... -> Theme selector UI.
22 require_once(dirname(__FILE__
) . '/../config.php');
23 require_once($CFG->libdir
. '/adminlib.php');
25 $choose = optional_param('choose', '', PARAM_PLUGIN
);
26 $reset = optional_param('reset', 0, PARAM_BOOL
);
27 $device = optional_param('device', '', PARAM_TEXT
);
29 admin_externalpage_setup('themeselector');
31 if (!empty($device)) {
32 // Make sure the device requested is valid
33 $devices = get_device_type_list();
34 if (!in_array($device, $devices)) {
35 // The provided device isn't a valid device throw an error
36 print_error('invaliddevicetype');
40 unset($SESSION->theme
);
42 if ($reset and confirm_sesskey()) {
43 theme_reset_all_caches();
45 } else if ($choose && $device && confirm_sesskey()) {
47 // Load the theme to make sure it is valid.
48 $theme = theme_config
::load($choose);
49 // Get the config argument for the chosen device.
50 $themename = get_device_cfg_var_name($device);
51 set_config($themename, $theme->name
);
53 // Create a new page for the display of the themes readme.
54 // This ensures that the readme page is shown using the new theme.
55 $confirmpage = new moodle_page();
56 $confirmpage->set_context($PAGE->context
);
57 $confirmpage->set_url($PAGE->url
);
58 $confirmpage->set_pagelayout($PAGE->pagelayout
);
59 $confirmpage->set_pagetype($PAGE->pagetype
);
60 $confirmpage->set_title($PAGE->title
);
61 $confirmpage->set_heading($PAGE->heading
);
63 // Get the core renderer for the new theme.
64 $output = $confirmpage->get_renderer('core');
66 echo $output->header();
67 echo $output->heading(get_string('themesaved'));
68 echo $output->box_start();
69 echo format_text(get_string('choosereadme', 'theme_'.$theme->name
), FORMAT_MOODLE
);
70 echo $output->box_end();
71 echo $output->continue_button($CFG->wwwroot
. '/theme/index.php');
72 echo $output->footer();
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.
79 echo $OUTPUT->header('themeselector');
80 echo $OUTPUT->heading(get_string('themes'));
82 echo $OUTPUT->single_button(new moodle_url('index.php', array('sesskey' => sesskey(), 'reset' => 1)), get_string('themeresetcaches', 'admin'));
84 $table = new html_table();
85 $table->data
= array();
86 if (!empty($CFG->enabledevicedetection
) && empty($device)) {
87 // Display a list of devices that a user can select a theme for.
89 $strthemenotselected = get_string('themenoselected', 'admin');
90 $strthemeselect = get_string('themeselect', 'admin');
92 // Display the device selection screen
93 $table->id
= 'admindeviceselector';
94 $table->head
= array(get_string('devicetype', 'admin'), get_string('theme'), get_string('info'));
96 $devices = get_device_type_list();
97 foreach ($devices as $device) {
99 $headingthemename = ''; // To output the picked theme name when needed
100 $themename = get_selected_theme_for_device_type($device);
101 if (!$themename && $device == 'default') {
102 $themename = theme_config
::DEFAULT_THEME
;
105 $screenshotcell = $strthemenotselected;
107 // Check the theme exists
108 $themename = clean_param($themename, PARAM_THEME
);
109 if (empty($themename)) {
110 // Likely the theme has been deleted
111 unset_config(get_device_cfg_var_name($device));
113 $strthemename = get_string('pluginname', 'theme_'.$themename);
114 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
115 $screenshoturl = new moodle_url('/theme/image.php', array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
116 // Contents of the screenshot/preview cell.
117 $screenshotcell = html_writer
::empty_tag('img', array('src' => $screenshoturl, 'alt' => $strthemename));
118 // Show the name of the picked theme
119 $headingthemename = $OUTPUT->heading($strthemename, 3);
123 $deviceurl = new moodle_url('/theme/index.php', array('device' => $device, 'sesskey' => sesskey()));
124 $select = new single_button($deviceurl, $strthemeselect, 'get');
126 $table->data
[] = array(
129 $headingthemename . $OUTPUT->render($select)
133 // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
134 // of themes to select.
136 if (empty($device)) {
137 // if $CFG->enabledevicedetection is off this will return 'default'
138 $device = get_device_type();
141 $table->id
= 'adminthemeselector';
142 $table->head
= array(get_string('theme'), get_string('info'));
144 $themes = get_plugin_list('theme');
146 foreach ($themes as $themename => $themedir) {
148 // Load the theme config.
150 $theme = theme_config
::load($themename);
151 } catch (Exception
$e) {
152 // Bad theme, just skip it for now.
155 if ($themename !== $theme->name
) {
156 //obsoleted or broken theme, just skip for now
159 if (empty($CFG->themedesignermode
) && $theme->hidefromselector
) {
160 // The theme doesn't want to be shown in the theme selector and as theme
161 // designer mode is switched off we will respect that decision.
164 $strthemename = get_string('pluginname', 'theme_'.$themename);
166 // Build the table row, and also a list of items to go in the second cell.
168 $infoitems = array();
169 $rowclasses = array();
171 // Set up bools whether this theme is chosen either main or legacy
172 $ischosentheme = ($themename == get_selected_theme_for_device_type($device));
174 if ($ischosentheme) {
175 // Is the chosen main theme
176 $rowclasses[] = 'selectedtheme';
179 // link to the screenshot, now mandatory - the image path is hardcoded because we need image from other themes, not the current one
180 $screenshotpath = new moodle_url('/theme/image.php', array('theme'=>$themename, 'image'=>'screenshot', 'component'=>'theme'));
181 // Contents of the first screenshot/preview cell.
182 $row[] = html_writer
::empty_tag('img', array('src'=>$screenshotpath, 'alt'=>$strthemename));
183 // Contents of the second cell.
184 $infocell = $OUTPUT->heading($strthemename, 3);
186 // Button to choose this as the main theme
187 $maintheme = new single_button(new moodle_url('/theme/index.php', array('device' => $device, 'choose' => $themename, 'sesskey' => sesskey())), get_string('usetheme'), 'get');
188 $maintheme->disabled
= $ischosentheme;
189 $infocell .= $OUTPUT->render($maintheme);
193 $table->data
[$themename] = $row;
194 $table->rowclasses
[$themename] = join(' ', $rowclasses);
198 echo html_writer
::table($table);
200 echo $OUTPUT->footer();