Merge branch 'MDL-54742-master' of git://github.com/mihailges/moodle
[moodle.git] / theme / more / lib.php
blob0162903e511392ef84b9da582927199400ac9d79
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.
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/>.
17 /**
18 * Theme More lib.
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
25 /**
26 * Extra LESS code to inject.
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.
32 * @param theme_config $theme The theme config object.
33 * @return string Raw LESS code.
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;';
45 if (!empty($theme->settings->backgroundposition)) {
46 $content .= 'background-position: ' . str_replace('_', ' ', $theme->settings->backgroundposition) . ';';
48 if (!empty($theme->settings->backgroundrepeat)) {
49 $content .= 'background-repeat: ' . $theme->settings->backgroundrepeat . ';';
51 $content .= ' }';
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 }';
63 return $content;
66 /**
67 * Returns variables for LESS.
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.
72 * @param theme_config $theme The theme config object.
73 * @return array of LESS variables without the @.
75 function theme_more_less_variables($theme) {
76 $variables = array();
77 if (!empty($theme->settings->bodybackground)) {
78 $variables['bodyBackground'] = $theme->settings->bodybackground;
80 if (!empty($theme->settings->textcolor)) {
81 $variables['textColor'] = $theme->settings->textcolor;
83 if (!empty($theme->settings->linkcolor)) {
84 $variables['linkColor'] = $theme->settings->linkcolor;
86 if (!empty($theme->settings->secondarybackground)) {
87 $variables['wellBackground'] = $theme->settings->secondarybackground;
89 return $variables;
92 /**
93 * Parses CSS before it is cached.
95 * This function can make alterations and replace patterns within the CSS.
97 * @param string $css The CSS
98 * @param theme_config $theme The theme config object.
99 * @return string The parsed CSS The parsed CSS.
101 function theme_more_process_css($css, $theme) {
102 global $OUTPUT;
104 // Set the background image for the logo.
105 $logo = $OUTPUT->get_logo_url(null, 100);
106 $css = theme_more_set_logo($css, $logo);
108 // Set custom CSS.
109 if (!empty($theme->settings->customcss)) {
110 $customcss = $theme->settings->customcss;
111 } else {
112 $customcss = null;
114 $css = theme_more_set_customcss($css, $customcss);
116 return $css;
120 * Adds the logo to CSS.
122 * @param string $css The CSS.
123 * @param string $logo The URL of the logo.
124 * @return string The parsed CSS
126 function theme_more_set_logo($css, $logo) {
127 $tag = '[[setting:logo]]';
128 $replacement = $logo;
129 if (is_null($replacement)) {
130 $replacement = '';
133 $css = str_replace($tag, $replacement, $css);
135 return $css;
139 * Serves any files associated with the theme settings.
141 * @param stdClass $course
142 * @param stdClass $cm
143 * @param context $context
144 * @param string $filearea
145 * @param array $args
146 * @param bool $forcedownload
147 * @param array $options
148 * @return bool
150 function theme_more_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
151 if ($context->contextlevel == CONTEXT_SYSTEM &&
152 ($filearea === 'logo' || $filearea === 'smalllogo' || $filearea === 'backgroundimage')) {
153 $theme = theme_config::load('more');
154 // By default, theme files must be cache-able by both browsers and proxies.
155 if (!array_key_exists('cacheability', $options)) {
156 $options['cacheability'] = 'public';
158 return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
159 } else {
160 send_file_not_found();
165 * Adds any custom CSS to the CSS before it is cached.
167 * @param string $css The original CSS.
168 * @param string $customcss The custom CSS to add.
169 * @return string The CSS which now contains our custom CSS.
171 function theme_more_set_customcss($css, $customcss) {
172 $tag = '[[setting:customcss]]';
173 $replacement = $customcss;
174 if (is_null($replacement)) {
175 $replacement = '';
178 $css = str_replace($tag, $replacement, $css);
180 return $css;