Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * Returns the main SCSS content.
  98   *
  99   * @param theme_config $theme The theme config object.
 100   * @return string
 101   */
 102  function theme_boost_get_main_scss_content($theme) {
 103      global $CFG;
 104  
 105      $scss = '';
 106      $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
 107      $fs = get_file_storage();
 108  
 109      $context = context_system::instance();
 110      if ($filename == 'default.scss') {
 111          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 112      } else if ($filename == 'plain.scss') {
 113          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
 114      } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
 115          $scss .= $presetfile->get_content();
 116      } else {
 117          // Safety fallback - maybe new installs etc.
 118          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 119      }
 120  
 121      return $scss;
 122  }
 123  
 124  /**
 125   * Get compiled css.
 126   *
 127   * @return string compiled css
 128   */
 129  function theme_boost_get_precompiled_css() {
 130      global $CFG;
 131      return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
 132  }
 133  
 134  /**
 135   * Get SCSS to prepend.
 136   *
 137   * @param theme_config $theme The theme config object.
 138   * @return array
 139   */
 140  function theme_boost_get_pre_scss($theme) {
 141      global $CFG;
 142  
 143      $scss = '';
 144      $configurable = [
 145          // Config key => [variableName, ...].
 146          'brandcolor' => ['primary'],
 147      ];
 148  
 149      // Prepend variables first.
 150      foreach ($configurable as $configkey => $targets) {
 151          $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
 152          if (empty($value)) {
 153              continue;
 154          }
 155          array_map(function($target) use (&$scss, $value) {
 156              $scss .= '$' . $target . ': ' . $value . ";\n";
 157          }, (array) $targets);
 158      }
 159  
 160      // Prepend pre-scss.
 161      if (!empty($theme->settings->scsspre)) {
 162          $scss .= $theme->settings->scsspre;
 163      }
 164  
 165      return $scss;
 166  }