Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/theme/ -> index.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  $confirmation = optional_param('confirmation', 0, PARAM_BOOL);
  30  
  31  admin_externalpage_setup('themeselector');
  32  
  33  unset($SESSION->theme);
  34  
  35  $PAGE->set_primary_active_tab('siteadminnode');
  36  $PAGE->navbar->add(get_string('themeselector', 'admin'), $PAGE->url);
  37  
  38  // Clear theme cache.
  39  if ($reset and confirm_sesskey()) {
  40      theme_reset_all_caches();
  41  }
  42  
  43  $definedinconfig = array_key_exists('theme', $CFG->config_php_settings);
  44  if ($definedinconfig) {
  45      $forcedthemename = get_string('pluginname', 'theme_'.$CFG->theme);
  46      // Show a notification that the theme is defined in config.php.
  47      \core\notification::info(get_string('themedefinedinconfigphp', 'admin', $forcedthemename));
  48  }
  49  
  50  // Change theme.
  51  if (!$definedinconfig && !empty($choose) && confirm_sesskey()) {
  52  
  53      // Load the theme to make sure it is valid.
  54      $theme = theme_config::load($choose);
  55  
  56      if ($theme instanceof \theme_config) {
  57          set_config('theme', $theme->name);
  58          $notifytype = 'success';
  59          $notifymessage = get_string('themesaved');
  60      } else {
  61          $notifytype = 'error';
  62          $notifymessage = get_string('error');
  63      }
  64  
  65      // Redirect with notification.
  66      redirect(new moodle_url('/theme/index.php'), $notifymessage, null, $notifytype);
  67  }
  68  
  69  $table = new html_table();
  70  $table->data = [];
  71  $table->id = 'adminthemeselector';
  72  $table->head = [get_string('theme'), get_string('info')];
  73  $table->align = ['left', 'left'];
  74  
  75  $themes = core_component::get_plugin_list('theme');
  76  
  77  // Loop through available themes.
  78  foreach ($themes as $themename => $themedir) {
  79  
  80      try {
  81          $theme = theme_config::load($themename);
  82      } catch (Exception $e) {
  83          // Bad theme, just skip it for now.
  84          continue;
  85      }
  86      if ($themename !== $theme->name) {
  87          // Obsoleted or broken theme, just skip for now.
  88          continue;
  89      }
  90      if (empty($CFG->themedesignermode) && $theme->hidefromselector) {
  91          // The theme doesn't want to be shown in the theme selector and as theme
  92          // designer mode is switched off we will respect that decision.
  93          continue;
  94      }
  95  
  96      // Build the table rows.
  97      $row = [];
  98      $rowclasses = [];
  99      $strthemename = get_string('pluginname', 'theme_'.$themename);
 100  
 101      // Screenshot/preview cell.
 102      $screenshotpath = new moodle_url('/theme/image.php', ['theme' => $themename, 'image' => 'screenshot', 'component' => 'theme']);
 103      $row[] = html_writer::empty_tag('img', ['class' => 'img-fluid', 'src' => $screenshotpath, 'alt' => $strthemename]);
 104  
 105      // Info cell.
 106      $infocell = $OUTPUT->heading($strthemename, 3);
 107  
 108      // Is this the current theme?
 109      if ($themename == $CFG->theme) {
 110          $rowclasses[] = 'selectedtheme';
 111          if ($definedinconfig) {
 112              $infocell .= html_writer::div(get_string('configoverride', 'admin'), 'alert alert-info');
 113          }
 114      } else if (!$definedinconfig) {
 115          // Button to choose this as the main theme.
 116          $setthemestr = get_string('usetheme');
 117          $setthemeurl = new moodle_url('/theme/index.php', ['choose' => $themename, 'sesskey' => sesskey()]);
 118          $setthemebutton = new single_button($setthemeurl, $setthemestr, 'post');
 119          $infocell .= html_writer::div($OUTPUT->render($setthemebutton), 'mt-2');
 120      }
 121      $row[] = $infocell;
 122  
 123      $table->data[$themename] = $row;
 124      $table->rowclasses[$themename] = join(' ', $rowclasses);
 125      $table->responsive = false;
 126  }
 127  
 128  // Show heading.
 129  echo $OUTPUT->header();
 130  echo $OUTPUT->heading(get_string('themeselector', 'admin'));
 131  
 132  // Reset theme caches button.
 133  $reseturl = new moodle_url('index.php', ['sesskey' => sesskey(), 'reset' => 1]);
 134  echo $OUTPUT->single_button($reseturl, get_string('themeresetcaches', 'admin'), 'post');
 135  
 136  // Render main table.
 137  echo html_writer::table($table);
 138  echo $OUTPUT->footer();