Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/theme/ -> index.php (source)

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

   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          print_error('invaliddevicetype');
  41      }
  42  }
  43  
  44  unset($SESSION->theme);
  45  
  46  if ($reset and confirm_sesskey()) {
  47      theme_reset_all_caches();
  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;
  59  
  60  } else if ($choose && $device && !theme_is_device_locked($device) && !$unsettheme && confirm_sesskey()) {
  61      // Load the theme to make sure it is valid.
  62      $theme = theme_config::load($choose);
  63  
  64      // Get the config argument for the chosen device.
  65      $themename = core_useragent::get_device_type_cfg_var_name($device);
  66      set_config($themename, $theme->name);
  67  
  68      $urlconfirm = new moodle_url('/theme/index.php', array('confirmation' => 1, 'choose' => $choose));
  69      redirect($urlconfirm);
  70  } else if ($device && !theme_is_device_locked($device) && $unsettheme && confirm_sesskey() && ($device != 'default')) {
  71      // Unset the theme and continue.
  72      unset_config(core_useragent::get_device_type_cfg_var_name($device));
  73      $device = '';
  74  }
  75  
  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  
  79  $table = new html_table();
  80  $table->data = array();
  81  $heading = '';
  82  if (!empty($CFG->enabledevicedetection) && empty($device)) {
  83      $heading = get_string('selectdevice', 'admin');
  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  
  89      // Display the device selection screen.
  90      $table->id = 'admindeviceselector';
  91      $table->head = array(get_string('devicetype', 'admin'), get_string('currenttheme', 'admin'), get_string('info'));
  92  
  93      $devices = core_useragent::get_device_type_list();
  94      foreach ($devices as $thedevice) {
  95  
  96          $headingthemename = ''; // To output the picked theme name when needed.
  97          $themename = core_useragent::get_device_type_theme($thedevice);
  98          if (!$themename && $thedevice == 'default') {
  99              $themename = theme_config::DEFAULT_THEME;
 100          }
 101          $themelocked = theme_is_device_locked($thedevice);
 102  
 103          $screenshotcell = $strthemenotselected;
 104          $unsetthemebutton = '';
 105          if ($themename) {
 106              // Check the theme exists.
 107              $themename = clean_param($themename, PARAM_THEME);
 108              if (empty($themename)) {
 109                  // Likely the theme has been deleted.
 110                  unset_config(core_useragent::get_device_type_cfg_var_name($thedevice));
 111              } else {
 112                  $strthemename = get_string('pluginname', 'theme_'.$themename);
 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'));
 117                  // Contents of the screenshot/preview cell.
 118                  $screenshotcell = html_writer::empty_tag('img', array('class' => 'img-fluid',
 119                      'src' => $screenshoturl, 'alt' => $strthemename));
 120                  // Show the name of the picked theme.
 121                  $headingthemename = $OUTPUT->heading($strthemename, 3);
 122              }
 123              // If not default device then show option to unset theme.
 124              if ($thedevice != 'default' && !$themelocked) {
 125                  $unsetthemestr = get_string('unsettheme', 'admin');
 126                  $unsetthemeurl = new moodle_url('/theme/index.php',
 127                      array('device' => $thedevice, 'sesskey' => sesskey(), 'unsettheme' => true));
 128                  $unsetthemebutton = new single_button($unsetthemeurl, $unsetthemestr, 'get');
 129                  $unsetthemebutton = $OUTPUT->render($unsetthemebutton);
 130              }
 131          }
 132  
 133          $deviceurl = new moodle_url('/theme/index.php', array('device' => $thedevice, 'sesskey' => sesskey()));
 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          }
 144  
 145          $table->data[] = array(
 146              $OUTPUT->heading(ucfirst($thedevice), 3),
 147              $screenshotcell,
 148              $headingthemename . $lockwarning . $select . $unsetthemebutton
 149          );
 150      }
 151  } else {
 152      // Either a device has been selected of $CFG->enabledevicedetection is off so display a list
 153      // of themes to select.
 154      $heading = get_string('selecttheme', 'admin', $device);
 155      if (empty($device)) {
 156          // If $CFG->enabledevicedetection is off this will return 'default'.
 157          $device = core_useragent::get_device_type();
 158      }
 159  
 160      $themelocked = theme_is_device_locked($device);
 161      $table->id = 'adminthemeselector';
 162      $table->head = array(get_string('theme'), get_string('info'));
 163  
 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      }
 173  
 174      foreach ($themes as $themename => $themedir) {
 175  
 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) {
 184              // Obsoleted or broken theme, just skip for now.
 185              continue;
 186          }
 187          if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
 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);
 193  
 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();
 198  
 199          // Set up bools whether this theme is chosen either main or legacy.
 200          $ischosentheme = ($themename == core_useragent::get_device_type_theme($device));
 201  
 202          if ($ischosentheme) {
 203              // Is the chosen main theme.
 204              $rowclasses[] = 'selectedtheme';
 205          }
 206  
 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',
 210              array('theme' => $themename, 'image' => 'screenshot', 'component' => 'theme'));
 211          // Contents of the first screenshot/preview cell.
 212          $row[] = html_writer::empty_tag('img', array('class' => 'img-fluid',
 213              'src' => $screenshotpath, 'alt' => $strthemename));
 214          // Contents of the second cell.
 215          $infocell = $OUTPUT->heading($strthemename, 3);
 216  
 217          if ($themelocked) {
 218              $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
 219          }
 220  
 221          // Button to choose this as the main theme or unset this theme for devices other then default.
 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              }
 236          }
 237  
 238          $row[] = $infocell;
 239  
 240          $table->data[$themename] = $row;
 241          $table->rowclasses[$themename] = join(' ', $rowclasses);
 242      }
 243  }
 244  echo $OUTPUT->header('themeselector');
 245  echo $OUTPUT->heading($heading);
 246  
 247  $params = array('sesskey' => sesskey(), 'reset' => 1);
 248  if (!empty($device)) {
 249      $params['device'] = $device;
 250  }
 251  echo $OUTPUT->single_button(new moodle_url('index.php', $params), get_string('themeresetcaches', 'admin'));
 252  
 253  echo html_writer::table($table);
 254  
 255  echo $OUTPUT->footer();