Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/theme/ -> index.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

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