[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/theme/more/ -> lib.php (source)

   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 More lib.
  19   *
  20   * @package    theme_more
  21   * @copyright  2014 Frédéric Massart
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Extra LESS code to inject.
  27   *
  28   * This will generate some LESS code from the settings used by the user. We cannot use
  29   * the {@link theme_more_less_variables()} here because we need to create selectors or
  30   * alter existing ones.
  31   *
  32   * @param theme_config $theme The theme config object.
  33   * @return string Raw LESS code.
  34   */
  35  function theme_more_extra_less($theme) {
  36      $content = '';
  37      $imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
  38      // Sets the background image, and its settings.
  39      if (!empty($imageurl)) {
  40          $content .= 'body { ';
  41          $content .= "background-image: url('$imageurl');";
  42          if (!empty($theme->settings->backgroundfixed)) {
  43              $content .= 'background-attachment: fixed;';
  44          }
  45          if (!empty($theme->settings->backgroundposition)) {
  46              $content .= 'background-position: ' . str_replace('_', ' ', $theme->settings->backgroundposition) . ';';
  47          }
  48          if (!empty($theme->settings->backgroundrepeat)) {
  49              $content .= 'background-repeat: ' . $theme->settings->backgroundrepeat . ';';
  50          }
  51          $content .= ' }';
  52      }
  53      // If there the user wants a background for the content, we need to make it look consistent,
  54      // therefore we need to round its borders, and adapt the border colour.
  55      if (!empty($theme->settings->contentbackground)) {
  56          $content .= '
  57              #region-main {
  58                  .well;
  59                  background-color: ' . $theme->settings->contentbackground . ';
  60                  border-color: darken(' . $theme->settings->contentbackground . ', 7%);
  61              }';
  62      }
  63      return $content;
  64  }
  65  
  66  /**
  67   * Returns variables for LESS.
  68   *
  69   * We will inject some LESS variables from the settings that the user has defined
  70   * for the theme. No need to write some custom LESS for this.
  71   *
  72   * @param theme_config $theme The theme config object.
  73   * @return array of LESS variables without the @.
  74   */
  75  function theme_more_less_variables($theme) {
  76      $variables = array();
  77      if (!empty($theme->settings->bodybackground)) {
  78          $variables['bodyBackground'] = $theme->settings->bodybackground;
  79      }
  80      if (!empty($theme->settings->textcolor)) {
  81          $variables['textColor'] = $theme->settings->textcolor;
  82      }
  83      if (!empty($theme->settings->linkcolor)) {
  84          $variables['linkColor'] = $theme->settings->linkcolor;
  85      }
  86      if (!empty($theme->settings->secondarybackground)) {
  87          $variables['wellBackground'] = $theme->settings->secondarybackground;
  88      }
  89      return $variables;
  90  }
  91  
  92  /**
  93   * Parses CSS before it is cached.
  94   *
  95   * This function can make alterations and replace patterns within the CSS.
  96   *
  97   * @param string $css The CSS
  98   * @param theme_config $theme The theme config object.
  99   * @return string The parsed CSS The parsed CSS.
 100   */
 101  function theme_more_process_css($css, $theme) {
 102  
 103      // Set the background image for the logo.
 104      $logo = $theme->setting_file_url('logo', 'logo');
 105      $css = theme_more_set_logo($css, $logo);
 106  
 107      // Set custom CSS.
 108      if (!empty($theme->settings->customcss)) {
 109          $customcss = $theme->settings->customcss;
 110      } else {
 111          $customcss = null;
 112      }
 113      $css = theme_more_set_customcss($css, $customcss);
 114  
 115      return $css;
 116  }
 117  
 118  /**
 119   * Adds the logo to CSS.
 120   *
 121   * @param string $css The CSS.
 122   * @param string $logo The URL of the logo.
 123   * @return string The parsed CSS
 124   */
 125  function theme_more_set_logo($css, $logo) {
 126      $tag = '[[setting:logo]]';
 127      $replacement = $logo;
 128      if (is_null($replacement)) {
 129          $replacement = '';
 130      }
 131  
 132      $css = str_replace($tag, $replacement, $css);
 133  
 134      return $css;
 135  }
 136  
 137  /**
 138   * Serves any files associated with the theme settings.
 139   *
 140   * @param stdClass $course
 141   * @param stdClass $cm
 142   * @param context $context
 143   * @param string $filearea
 144   * @param array $args
 145   * @param bool $forcedownload
 146   * @param array $options
 147   * @return bool
 148   */
 149  function theme_more_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
 150      if ($context->contextlevel == CONTEXT_SYSTEM &&
 151              ($filearea === 'logo' || $filearea === 'smalllogo' || $filearea === 'backgroundimage')) {
 152          $theme = theme_config::load('more');
 153          // By default, theme files must be cache-able by both browsers and proxies.
 154          if (!array_key_exists('cacheability', $options)) {
 155              $options['cacheability'] = 'public';
 156          }
 157          return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
 158      } else {
 159          send_file_not_found();
 160      }
 161  }
 162  
 163  /**
 164   * Adds any custom CSS to the CSS before it is cached.
 165   *
 166   * @param string $css The original CSS.
 167   * @param string $customcss The custom CSS to add.
 168   * @return string The CSS which now contains our custom CSS.
 169   */
 170  function theme_more_set_customcss($css, $customcss) {
 171      $tag = '[[setting:customcss]]';
 172      $replacement = $customcss;
 173      if (is_null($replacement)) {
 174          $replacement = '';
 175      }
 176  
 177      $css = str_replace($tag, $replacement, $css);
 178  
 179      return $css;
 180  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1