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.

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   * 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 .= 'body { ';
  53          $content .= "background-image: url('$imageurl'); background-size: cover;";
  54          $content .= ' }';
  55      }
  56  
  57      // Always return the background image with the scss when we have it.
  58      return !empty($theme->settings->scss) ? $theme->settings->scss . ' ' . $content : $content;
  59  }
  60  
  61  /**
  62   * Serves any files associated with the theme settings.
  63   *
  64   * @param stdClass $course
  65   * @param stdClass $cm
  66   * @param context $context
  67   * @param string $filearea
  68   * @param array $args
  69   * @param bool $forcedownload
  70   * @param array $options
  71   * @return bool
  72   */
  73  function theme_boost_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
  74      if ($context->contextlevel == CONTEXT_SYSTEM && ($filearea === 'logo' || $filearea === 'backgroundimage')) {
  75          $theme = theme_config::load('boost');
  76          // By default, theme files must be cache-able by both browsers and proxies.
  77          if (!array_key_exists('cacheability', $options)) {
  78              $options['cacheability'] = 'public';
  79          }
  80          return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
  81      } else {
  82          send_file_not_found();
  83      }
  84  }
  85  
  86  /**
  87   * Returns the main SCSS content.
  88   *
  89   * @param theme_config $theme The theme config object.
  90   * @return string
  91   */
  92  function theme_boost_get_main_scss_content($theme) {
  93      global $CFG;
  94  
  95      $scss = '';
  96      $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
  97      $fs = get_file_storage();
  98  
  99      $context = context_system::instance();
 100      if ($filename == 'default.scss') {
 101          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 102      } else if ($filename == 'plain.scss') {
 103          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
 104      } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
 105          $scss .= $presetfile->get_content();
 106      } else {
 107          // Safety fallback - maybe new installs etc.
 108          $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
 109      }
 110  
 111      return $scss;
 112  }
 113  
 114  /**
 115   * Get compiled css.
 116   *
 117   * @return string compiled css
 118   */
 119  function theme_boost_get_precompiled_css() {
 120      global $CFG;
 121      return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
 122  }
 123  
 124  /**
 125   * Get SCSS to prepend.
 126   *
 127   * @param theme_config $theme The theme config object.
 128   * @return array
 129   */
 130  function theme_boost_get_pre_scss($theme) {
 131      global $CFG;
 132  
 133      $scss = '';
 134      $configurable = [
 135          // Config key => [variableName, ...].
 136          'brandcolor' => ['primary'],
 137      ];
 138  
 139      // Prepend variables first.
 140      foreach ($configurable as $configkey => $targets) {
 141          $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
 142          if (empty($value)) {
 143              continue;
 144          }
 145          array_map(function($target) use (&$scss, $value) {
 146              $scss .= '$' . $target . ': ' . $value . ";\n";
 147          }, (array) $targets);
 148      }
 149  
 150      // Prepend pre-scss.
 151      if (!empty($theme->settings->scsspre)) {
 152          $scss .= $theme->settings->scsspre;
 153      }
 154  
 155      return $scss;
 156  }