weekly release 5.0dev
[moodle.git] / theme / boost / lib.php
blob755fc42623a753f22cceec1566b7f68137758680
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 functions.
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
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Post process the CSS tree.
30 * @param string $tree The CSS tree.
31 * @param theme_config $theme The theme config object.
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();
40 /**
41 * Inject additional SCSS.
43 * @param theme_config $theme The theme config object.
44 * @return string
46 function theme_boost_get_extra_scss($theme) {
47 $content = '';
48 $imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
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 .= ' } }';
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 .= ' }';
66 // Always return the background image with the scss when we have it.
67 return !empty($theme->settings->scss) ? "{$theme->settings->scss} \n {$content}" : $content;
70 /**
71 * Serves any files associated with the theme settings.
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
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';
90 return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
91 } else {
92 send_file_not_found();
96 /**
97 * Get the current user preferences that are available
99 * @return array[]
101 function theme_boost_user_preferences(): array {
102 return [
103 'drawer-open-block' => [
104 'type' => PARAM_BOOL,
105 'null' => NULL_NOT_ALLOWED,
106 'default' => false,
107 'permissioncallback' => [core_user::class, 'is_current_user'],
109 'drawer-open-index' => [
110 'type' => PARAM_BOOL,
111 'null' => NULL_NOT_ALLOWED,
112 'default' => true,
113 'permissioncallback' => [core_user::class, 'is_current_user'],
119 * Returns the main SCSS content.
121 * @param theme_config $theme The theme config object.
122 * @return string
124 function theme_boost_get_main_scss_content($theme) {
125 global $CFG;
127 $scss = '';
128 $filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
129 $fs = get_file_storage();
131 $context = context_system::instance();
132 if ($filename == 'default.scss') {
133 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
134 } else if ($filename == 'plain.scss') {
135 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
136 } else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_boost', 'preset', 0, '/', $filename))) {
137 $scss .= $presetfile->get_content();
138 } else {
139 // Safety fallback - maybe new installs etc.
140 $scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
143 return $scss;
147 * Get compiled css.
149 * @return string compiled css
151 function theme_boost_get_precompiled_css() {
152 global $CFG;
153 return file_get_contents($CFG->dirroot . '/theme/boost/style/moodle.css');
157 * Get SCSS to prepend.
159 * @param theme_config $theme The theme config object.
160 * @return string
162 function theme_boost_get_pre_scss($theme) {
163 global $CFG;
165 $scss = '';
166 $configurable = [
167 // Config key => [variableName, ...].
168 'brandcolor' => ['primary'],
171 // Prepend variables first.
172 foreach ($configurable as $configkey => $targets) {
173 $value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
174 if (empty($value)) {
175 continue;
177 array_map(function($target) use (&$scss, $value) {
178 $scss .= '$' . $target . ': ' . $value . ";\n";
179 }, (array) $targets);
182 // Prepend pre-scss.
183 if (!empty($theme->settings->scsspre)) {
184 $scss .= $theme->settings->scsspre;
187 return $scss;