forked from trema-tech/moodle-theme_trema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
338 lines (287 loc) · 11.6 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lib file.
*
* @package theme_trema
* @copyright 2019-2024 Trema - {@link https://trema.tech/}
* @copyright 2023-2024 TNG Consulting Inc. - {@link https://www.tngconsulting.ca/}
* @author Rodrigo Mady
* @author Trevor Furtado
* @author Michael Milette
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Get SCSS to prepend.
*
* @param theme_config $theme
* The theme config object.
* @return string
*/
function theme_trema_get_pre_scss($theme) {
global $CFG;
$scss = '';
$scss .= file_get_contents("$CFG->dirroot/theme/trema/scss/defaultvariables.scss");
$configurable = [
// Target SCSS variable name => Trema theme setting.
'primary' => 'primarycolor',
'secondary' => 'secondarycolor',
'body-bg-color' => 'bodybackgroundcolor',
'body-font-family' => 'sitefont',
'h1-font-family' => 'h1font',
'hx-font-family' => 'hxfont',
'text-transform' => 'texttransform',
'banner-title-font-family' => 'bannertitlesfont',
'banner-title-transform' => 'bannertitletransform',
'banner-title-spacing' => 'bannertitlespacing',
'banner-text-align' => 'frontpagebannercontentalign',
'banner-height' => 'bannerheight',
'custom-menu-alignment' => 'custommenualignment',
'links-decoration' => 'linkdecoration',
'dropdown-bg-color' => 'bodybackgroundcolor',
'footer-opacity' => 'footeropacity',
];
// Prepend variables first.
foreach ($configurable as $scssvar => $themesetting) {
if (isset($theme->settings->{$themesetting})) {
$value = $theme->settings->{$themesetting};
} else {
continue;
}
$scss .= '$' . $scssvar . ': ' . $value . ";\n";
}
// ....
// Colors
// ....
{
// Background color of page header.
$headerbgcolor = get_config('theme_trema', 'headerbgcolor');
if (strpos('$#', substr($headerbgcolor, 0, 1)) === false) {
$headerbgcolor = get_config('theme_trema', $headerbgcolor);
}
$scss .= '$header-bg-color: ' . $headerbgcolor . " !default;\n";
// Background color of Log In button in page header.
$loginbtnbgcolor = get_config('theme_trema', 'loginbtnbgcolor');
if (strpos('$#', substr($loginbtnbgcolor, 0, 1)) === false) {
$loginbtnbgcolor = get_config('theme_trema', $loginbtnbgcolor);
}
$scss .= '$loginbtn-bg-color: ' . $loginbtnbgcolor . " !default;\n";
// Background color of drawers.
$drawerbgcolor = get_config('theme_trema', 'drawerbgcolor');
if (strpos('$#', substr($drawerbgcolor, 0, 1)) === false) {
$drawerbgcolor = get_config('theme_trema', $drawerbgcolor);
}
$scss .= '$drawer-bg-color: ' . $drawerbgcolor . " !default;\n";
// Background color of footer.
$footerbgcolor = get_config('theme_trema', 'footerbgcolor');
if (strpos('$#', substr($footerbgcolor, 0, 1)) === false) {
$footerbgcolor = get_config('theme_trema', $footerbgcolor);
}
$scss .= '$footer-bg-color: ' . $footerbgcolor . " !default;\n";
}
// ....
// Fonts
// ....
$fonts = [
'Arial, Helvetica, sans-serif' => 'Arial',
'Verdana, Tahoma, sans-serif' => 'Verdana',
'"Times New Roman", Times, serif' => 'TimesNewRoman',
'Georgia, serif' => 'Georgia',
'"Courier New", Courier, monospace' => 'CourierNew',
'Alegreya, serif' => 'Alegreya',
'"CrimsonText", serif' => 'CrimsonText',
'"EBGaramond", sans-serif' => 'EBGaramond',
'Lato, sans-serif' => 'Lato',
'Montserrat, sans-serif' => 'Montserrat',
'"NotoSans", sans-serif' => 'NotoSans',
'"OpenSans", sans-serif' => 'OpenSans',
'"PlayfairDisplay", serif' => 'PlayfairDisplay',
'Poppins, sans-serif' => 'Poppins',
'Roboto, Arial, Helvetica, sans-serif' => 'Roboto',
];
$scss .= '$bodyfontfile: "' . $fonts[$theme->settings->sitefont] . '";' . PHP_EOL;
$scss .= '$bannertitlesfontfile: "' . $fonts[$theme->settings->bannertitlesfont] . '";' . PHP_EOL;
$scss .= '$h1fontfile: "' . $fonts[$theme->settings->h1font] . '";' . PHP_EOL;
$scss .= '$hxfontfile: "' . $fonts[$theme->settings->hxfont] . '";' . PHP_EOL;
// ....
// Show/hide User profile fields.
// ....
$fields = [];
// Section: General.
$fields['showprofileemaildisplay'] = '#fitem_id_maildisplay'; // Email display.
if (empty($theme->settings->showmoodlenetprofile)) {
$fields['showmoodlenetprofile'] = '#fitem_id_moodlenetprofile'; // MoodleNet Profile.
}
$fields['showprofilecity'] = '#fitem_id_city'; // City.
$fields['showprofilecountry'] = '#fitem_id_country'; // Country.
$fields['showprofiletimezone'] = '#fitem_id_timezone'; // Timezone.
$fields['showprofiledescription'] = '#fitem_id_description_editor'; // Description.
// Section: User Picture.
$fields['showprofilepictureofuser'] = '#id_moodle_picture';
// Section: Additional Names.
$fields['showprofileadditionalnames'] = '#id_moodle_additional_names';
// Section: Interests.
$fields['showprofileinterests'] = '#id_moodle_interests';
// Section: Optional.
$fields['showprofileoptional'] = '#id_moodle_optional';
// MoodleNet profile ID.
$fields['showprofilemoodlenetprofile'] = '#fitem_id_moodlenetprofile';
// ID number.
$fields['showprofileidnumber'] = '#fitem_id_idnumber';
// Institution.
$fields['showprofileinstitution'] = '#fitem_id_institution';
// Department.
$fields['showprofiledepartment'] = '#fitem_id_department';
// Phone.
$fields['showprofilephone1'] = '#fitem_id_phone1';
// Mobile phone.
$fields['showprofilephone2'] = '#fitem_id_phone2';
// Address.
$fields['showprofileaddress'] = '#fitem_id_address';
//
// Show/hide other elements.
//
// Activity module icons.
$fields['showactivityicons'] = '.page-header-image,.activityiconcontainer.courseicon';
// Login form.
$fields['loginshowloginform'] = '#login, .loginform .login-form, .login-form-forgotpassword form-group';
// User menu - Hide the Logout link.
$fields['showumlogoutlink'] = '#carousel-item-main a:last-of-type, #carousel-item-main .dropdown-divider:last-of-type';
// Links to Moodle 'Page' activities on Frontpage unless in edit mode on the front page.
$fields['showfrontpagelinkstopages'] = '#page-site-index:not(.editing) #page-content .modtype_page';
// Moodle branding.
$fields['showbranding'] = '.sitelink,.footer-section.p-3:not(.border-bottom)';
$customscss = '';
// Automatically hide guest login button if Auto-login Guests is enabled and Guest Login button is visible.
if (!empty($CFG->autologinguests) && !empty($CFG->guestloginbutton)) {
$customscss .= '#guestlogin,';
}
// ....
// Combine all of the fields that we need to hide.
// ....
foreach ($fields as $setting => $field) {
if (empty($theme->settings->$setting)) {
$customscss .= $field . ',' . PHP_EOL;
}
}
// If there is something to hide, hide it.
if (!empty($customscss)) {
$scss .= $customscss . 'displaynone {display: none;}';
}
// ....
// Login page
// ....
// Background image.
$backgroundimageurl = $theme->setting_file_url('loginbackgroundimage', 'loginbackgroundimage');
if ($theme->settings->loginpagestyle == 'image' && !empty($backgroundimageurl)) {
$scss .= "\$login-backgroundimage: '$backgroundimageurl';\n";
} else {
$scss .= "\$login-backgroundimage: '[[pix:theme|frontpage/banner]]';\n";
}
// Not image in settings.
if ($theme->settings->loginpagestyle !== 'image') {
$scss .= "body.pagelayout-login #page-wrapper { background-image: none; }\n";
}
// ....
// Other settings.
// ....
// Softness: Rounding some corners.
$scss .= '$softness: ' . (!empty($theme->settings->softness) ? '.4rem' : '0') . ";\n";
// ....
// Prepend pre-scss.
// ....
if (! empty($theme->settings->scsspre)) {
$scss .= $theme->settings->scsspre;
}
return $scss;
}
/**
* Load the main SCSS and the frontpage banner.
*
* @param theme_config $theme
* The theme config object.
* @return string
*/
function theme_trema_get_main_scss_content($theme) {
global $CFG, $SITE;
$scss = '';
$filename = !empty($theme->settings->preset) ? $theme->settings->preset : 'default.scss';
$scss .= file_get_contents("$CFG->dirroot/theme/trema/scss/preset/{$filename}");
if (empty($theme->settings->enabletremalines)) {
$scss .= "%border-frequency { &:before, &:after { content: none !important;}}";
}
// Frontpage banner.
if (!empty($theme->settings->frontpageenabledarkoverlay)) {
$darkoverlay = "url([[pix:theme|frontpage/overlay]]),";
} else {
$darkoverlay = "";
}
if ($frontpagebannerurl = $theme->setting_file_url('frontpagebanner', 'frontpagebanner')) {
$scss .= "#frontpage-banner {background-image: $darkoverlay url('$frontpagebannerurl');}";
} else {
$scss .= "#frontpage-banner {background-image: $darkoverlay url([[pix:theme|frontpage/banner]]);}";
}
// Compensate for long site names.
$sitenamefontscale = strlen($SITE->shortname) < 36 ? '1' : '0.75';
$scss .= '$sitename-font-scale: ' . $sitenamefontscale . ";\n";
return $scss;
}
/**
* Inject additional SCSS for images.
*
* @param theme_config $theme Theme config object.
* @return string
*/
function theme_trema_get_extra_scss($theme) {
$scss = '';
// Sets the background image and its settings.
$imageurl = $theme->setting_file_url('backgroundimage', 'backgroundimage');
if (!empty($imageurl)) {
$scss .= '@media (min-width: 768px) { ';
$scss .= ' body { ';
$scss .= ' background-image: url("' . $imageurl . '"); ';
$scss .= ' background-size: cover; ';
$scss .= ' background-attachment: fixed; ';
$scss .= ' } ';
$scss .= "}\n";
}
// Always return the background image with the scss when we have it.
return !empty($theme->settings->scss) ? "{$theme->settings->scss} \n {$scss}" : $scss;
}
/**
* Serves any files associated with the theme settings.
*
* @param stdClass $course
* @param stdClass $cm
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options
* @return bool
*/
function theme_trema_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
$theme = theme_config::load('trema');
// By default, theme files must be cache-able by both browsers and proxies.
if (! array_key_exists('cacheability', $options)) {
$options['cacheability'] = 'public';
}
return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
} else {
send_file_not_found();
}
}