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.

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   * Theme functions.
  19   *
  20   * @package    theme_boost
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Post process the CSS tree.
  29   *
  30   * @param string $tree The CSS tree.
  31   * @param theme_config $theme The theme config object.
  32   */
  33  function theme_boost_css_tree_post_processor($tree, $theme) {
  34      error_log('theme_boost_css_tree_post_processor() is deprecated. Required' .
  35          'prefixes for Bootstrap are now in theme/boost/scss/moodle/prefixes.scss');
  36      $prefixer = new theme_boost\autoprefixer($tree);
  37      $prefixer->prefix();
  38  }
  39  
  40  /**
  41   * Inject additional SCSS.
  42   *
  43   * @param theme_config $theme The theme config object.
  44   * @return string
  45   */
  46  function theme_boost_get_extra_scss($theme) {
  47      $content = '';
  48      $imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
  49  
  50      // Sets the background image, and its settings.
  51      if (!empty($imageurl)) {
  52          $content .= '@media (min-width: 768px) {';
  53          $content .= 'body { ';
  54          $content .= "background-image: url('$imageurl'); background-size: cover;";
  55          $content .= ' } }';
  56      }
  57  
  58      // Sets the login background image.
  59      $loginbackgroundimageurl = $theme->setting_file_url('loginbackgroundimage', 'loginbackgroundimage');
  60      if (!empty($loginbackgroundimageurl)) {
  61          $content .= 'body.pagelayout-login #page { ';
  62          $content .= "background-image: url('$loginbackgroundimageurl'); background-size: cover;";
  63          $content .= ' }';
  64      }
  65  
  66      // Always return the background image with the scss when we have it.
  67      return !empty($theme->settings->scss) ? $theme->settings->scss . ' ' . $content : $content;
  68  }
  69  
  70  /**
  71   * Serves any files associated with the theme settings.
  72   *
  73   * @param stdClass $course
  74   * @param stdClass $cm
  75   * @param context $context
  76   * @param string $filearea
  77   * @param array $args
  78   * @param bool $forcedownload
  79   * @param array $options
  80   * @return bool
  81   */
  82  function theme_boost_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
  83      if ($context->contextlevel == CONTEXT_SYSTEM && ($filearea === 'logo' || $filearea === 'backgroundimage' ||
  84          $filearea === 'loginbackgroundimage')) {
  85          $theme = theme_config::load('boost');
  86          // By default, theme files must be cache-able by both browsers and proxies.
  87          if (!array_key_exists('cacheability', $options)) {
  88              $options['cacheability'] = 'public';
  89          }
  90          return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
  91      } else {
  92          send_file_not_found();
  93      }
  94  }
  95  
  96  /**
  97   * Get the current user preferences that are available
  98   *
  99   * @return array[]
 100   */
 101  function theme_boost_user_preferences(): array {
 102      return [
 103          'drawer-open-block' => [
 104              'type' => PARAM_BOOL,
 105              'null' => NULL_NOT_ALLOWED,
 106              'default' => false,
 107              'permissioncallback' => [core_user::class, 'is_current_user'],
 108          ],
 109          'drawer-open-index' => [
 110              'type' => PARAM_BOOL,
 111              'null' => NULL_NOT_ALLOWED,
 112              'default' => true,
 113              'permissioncallback' => [core_user::class, 'is_current_user'],
 114          ],
 115      ];
 116  }
 117  
 118  /**
 119   * Returns the main SCSS content.
 120   *
 121   * @param theme_config $theme The theme config object.
 122   * @return string
 123   */
 124  function theme_boost_get_main_scss_content($theme) {
 125      global $CFG;
 126  
 127      $scss = '';
 128      $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
 129      $fs = get_file_storage();
 130  
 131      $context = context_system::instance();
 132      if ($filename == 'default.scss') {
 133          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 134      } else if ($filename == 'plain.scss') {
 135          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
 136      } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
 137          $scss .= $presetfile->get_content();
 138      } else {
 139          // Safety fallback - maybe new installs etc.
 140          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 141      }
 142  
 143      return $scss;
 144  }
 145  
 146  /**
 147   * Get compiled css.
 148   *
 149   * @return string compiled css
 150   */
 151  function theme_boost_get_precompiled_css() {
 152      global $CFG;
 153      return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
 154  }
 155  
 156  /**
 157   * Get SCSS to prepend.
 158   *
 159   * @param theme_config $theme The theme config object.
 160   * @return string
 161   */
 162  function theme_boost_get_pre_scss($theme) {
 163      global $CFG;
 164  
 165      $scss = '';
 166      $configurable = [
 167          // Config key => [variableName, ...].
 168          'brandcolor' => ['primary'],
 169      ];
 170  
 171      // Prepend variables first.
 172      foreach ($configurable as $configkey => $targets) {
 173          $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
 174          if (empty($value)) {
 175              continue;
 176          }
 177          array_map(function($target) use (&$scss, $value) {
 178              $scss .= '$' . $target . ': ' . $value . ";\n";
 179          }, (array) $targets);
 180      }
 181  
 182      // Prepend pre-scss.
 183      if (!empty($theme->settings->scsspre)) {
 184          $scss .= $theme->settings->scsspre;
 185      }
 186  
 187      return $scss;
 188  }