on-demand release 3.7dev+
[moodle.git] / theme / more / lib.php
CommitLineData
833511a0
FM
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
7c923caf
FM
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 */
35function 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 */
75function 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
833511a0
FM
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 */
101function theme_more_process_css($css, $theme) {
aa6b4129 102 global $OUTPUT;
833511a0
FM
103
104 // Set the background image for the logo.
aa6b4129 105 $logo = $OUTPUT->get_logo_url(null, 100);
833511a0
FM
106 $css = theme_more_set_logo($css, $logo);
107
108 // Set custom CSS.
109 if (!empty($theme->settings->customcss)) {
110 $customcss = $theme->settings->customcss;
111 } else {
112 $customcss = null;
113 }
114 $css = theme_more_set_customcss($css, $customcss);
115
116 return $css;
117}
118
119/**
120 * Adds the logo to CSS.
121 *
122 * @param string $css The CSS.
123 * @param string $logo The URL of the logo.
124 * @return string The parsed CSS
125 */
126function theme_more_set_logo($css, $logo) {
127 $tag = '[[setting:logo]]';
128 $replacement = $logo;
129 if (is_null($replacement)) {
130 $replacement = '';
131 }
132
133 $css = str_replace($tag, $replacement, $css);
134
135 return $css;
136}
137
138/**
139 * Serves any files associated with the theme settings.
140 *
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
149 */
150function theme_more_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
52d57db5
DM
151 if ($context->contextlevel == CONTEXT_SYSTEM &&
152 ($filearea === 'logo' || $filearea === 'smalllogo' || $filearea === 'backgroundimage')) {
833511a0 153 $theme = theme_config::load('more');
bb8ed60a
MS
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';
157 }
7c923caf 158 return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
833511a0
FM
159 } else {
160 send_file_not_found();
161 }
162}
163
164/**
165 * Adds any custom CSS to the CSS before it is cached.
166 *
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.
170 */
171function theme_more_set_customcss($css, $customcss) {
172 $tag = '[[setting:customcss]]';
173 $replacement = $customcss;
174 if (is_null($replacement)) {
175 $replacement = '';
176 }
177
178 $css = str_replace($tag, $replacement, $css);
179
180 return $css;
181}