forked from willianmano/moodle-theme_moove
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
429 lines (344 loc) · 13 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?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/>.
/**
* Theme functions.
*
* @package theme_moove
* @copyright 2017 Willian Mano - http://conecti.me
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Inject additional SCSS.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_get_extra_scss($theme) {
$scss = $theme->settings->scss;
$scss .= theme_moove_set_headerimg($theme);
$scss .= theme_moove_set_topfooterimg($theme);
$scss .= theme_moove_set_loginbgimg($theme);
return $scss;
}
/**
* Adds the cover to CSS.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_set_headerimg($theme) {
global $OUTPUT;
$headerimg = $theme->setting_file_url('headerimg', 'headerimg');
if (is_null($headerimg)) {
$headerimg = $OUTPUT->image_url('headerimg', 'theme');
}
$headercss = "#page-site-index.notloggedin #page-header {background-image: url('$headerimg');}";
return $headercss;
}
/**
* Adds the footer image to CSS.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_set_topfooterimg($theme) {
global $OUTPUT;
$topfooterimg = $theme->setting_file_url('topfooterimg', 'topfooterimg');
if (is_null($topfooterimg)) {
$topfooterimg = $OUTPUT->image_url('footer-bg', 'theme');
}
$headercss = "#top-footer {background-image: url('$topfooterimg');}";
return $headercss;
}
/**
* Adds the login page background image to CSS.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_set_loginbgimg($theme) {
global $OUTPUT;
$loginbgimg = $theme->setting_file_url('loginbgimg', 'loginbgimg');
if (is_null($loginbgimg)) {
$loginbgimg = $OUTPUT->image_url('login_bg', 'theme');
}
$headercss = "#page-login-index.moove-login #page-wrapper #page {background-image: url('$loginbgimg');}";
return $headercss;
}
/**
* Returns the main SCSS content.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_get_main_scss_content($theme) {
global $CFG;
$scss = '';
$filename = !empty($theme->settings->preset) ? $theme->settings->preset : null;
$fs = get_file_storage();
$context = context_system::instance();
if ($filename == 'default.scss') {
// We still load the default preset files directly from the boost theme. No sense in duplicating them.
$scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
} else if ($filename == 'plain.scss') {
// We still load the default preset files directly from the boost theme. No sense in duplicating them.
$scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/plain.scss');
} else if ($filename && ($presetfile = $fs->get_file($context->id, 'theme_moove', 'preset', 0, '/', $filename))) {
// This preset file was fetched from the file area for theme_moove and not theme_boost (see the line above).
$scss .= $presetfile->get_content();
} else {
// Safety fallback - maybe new installs etc.
$scss .= file_get_contents($CFG->dirroot . '/theme/boost/scss/preset/default.scss');
}
// Moove scss.
$moovevariables = file_get_contents($CFG->dirroot . '/theme/moove/scss/moove/_variables.scss');
$moove = file_get_contents($CFG->dirroot . '/theme/moove/scss/moove.scss');
// Combine them together.
$allscss = $moovevariables . "\n" . $scss . "\n" . $moove;
return $allscss;
}
/**
* Get SCSS to prepend.
*
* @param theme_config $theme The theme config object.
* @return string
*/
function theme_moove_get_pre_scss($theme) {
$scss = '';
$configurable = [
// Config key => [variableName, ...].
'brandcolor' => ['brand-primary'],
'navbarheadercolor' => 'navbar-header-color',
'navbarbg' => 'navbar-bg',
'navbarbghover' => 'navbar-bg-hover'
];
// Prepend variables first.
foreach ($configurable as $configkey => $targets) {
$value = isset($theme->settings->{$configkey}) ? $theme->settings->{$configkey} : null;
if (empty($value)) {
continue;
}
array_map(function($target) use (&$scss, $value) {
$scss .= '$' . $target . ': ' . $value . ";\n";
}, (array) $targets);
}
// Prepend pre-scss.
if (!empty($theme->settings->scsspre)) {
$scss .= $theme->settings->scsspre;
}
return $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 mixed
*/
function theme_moove_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
$theme = theme_config::load('moove');
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'logo') {
return $theme->setting_file_serve('logo', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'headerimg') {
return $theme->setting_file_serve('headerimg', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'marketing1icon') {
return $theme->setting_file_serve('marketing1icon', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'marketing2icon') {
return $theme->setting_file_serve('marketing2icon', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'marketing3icon') {
return $theme->setting_file_serve('marketing3icon', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'marketing4icon') {
return $theme->setting_file_serve('marketing4icon', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'topfooterimg') {
return $theme->setting_file_serve('topfooterimg', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'loginbgimg') {
return $theme->setting_file_serve('loginbgimg', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'favicon') {
return $theme->setting_file_serve('favicon', $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and preg_match("/^sliderimage[1-9][0-9]?$/", $filearea) !== false) {
return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and preg_match("/^sponsorsimage[1-9][0-9]?$/", $filearea) !== false) {
return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
}
if ($context->contextlevel == CONTEXT_SYSTEM and preg_match("/^clientsimage[1-9][0-9]?$/", $filearea) !== false) {
return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
}
send_file_not_found();
}
/**
* Get theme setting
*
* @param string $setting
* @param bool $format
* @return string
*/
function theme_moove_get_setting($setting, $format = false) {
$theme = theme_config::load('moove');
if (empty($theme->settings->$setting)) {
return false;
}
if (!$format) {
return $theme->settings->$setting;
}
if ($format === 'format_text') {
return format_text($theme->settings->$setting, FORMAT_PLAIN);
}
if ($format === 'format_html') {
return format_text($theme->settings->$setting, FORMAT_HTML, array('trusted' => true, 'noclean' => true));
}
return format_string($theme->settings->$setting);
}
/**
* Extend the Moove navigation
*
* @param flat_navigation $flatnav
*/
function theme_moove_extend_flat_navigation(\flat_navigation $flatnav) {
theme_moove_add_certificatesmenuitem($flatnav);
theme_moove_delete_menuitems($flatnav);
theme_moove_add_coursesections_to_navigation($flatnav);
}
/**
* Add items to flat navigation menu
*
* @param flat_navigation $flatnav
*
*/
function theme_moove_add_certificatesmenuitem(\flat_navigation $flatnav) {
global $COURSE;
try {
if (!theme_moove_has_certificates_plugin()) {
return;
}
$actionurl = new \moodle_url('/theme/moove/certificates.php');
// Course page.
if ($COURSE->id > 1) {
$parentitem = $flatnav->find('competencies', \navigation_node::TYPE_SETTING);
$actionurl = new \moodle_url('/theme/moove/certificates.php', ['id' => $COURSE->id]);
}
if ($COURSE->id == 1 && !$parentitem = $flatnav->find('privatefiles', \navigation_node::TYPE_SETTING)) {
return;
}
if (!is_null($parentitem->parent)) {
$certificatesitemoptions = [
'action' => $actionurl,
'text' => get_string('certificates', 'theme_moove'),
'shorttext' => get_string('certificates', 'theme_moove'),
'icon' => new pix_icon('i/export', ''),
'type' => \navigation_node::TYPE_SETTING,
'key' => 'certificates',
'parent' => $parentitem->parent
];
$certificatesitem = new \flat_navigation_node($certificatesitemoptions, 0);
$flatnav->add($certificatesitem, $parentitem->key);
}
} catch (\coding_exception $e) {
debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace());
} catch (\moodle_exception $e) {
debugging($e->getMessage(), DEBUG_NORMAL, $e->getTrace());
}
}
/**
* Remove items from navigation
*
* @param flat_navigation $flatnav
*/
function theme_moove_delete_menuitems(\flat_navigation $flatnav) {
$itemstodelete = [
'coursehome'
];
foreach ($flatnav as $item) {
if (in_array($item->key, $itemstodelete)) {
$flatnav->remove($item->key);
continue;
}
if (isset($item->parent->key) && $item->parent->key == 'mycourses' &&
isset($item->type) && $item->type == \navigation_node::TYPE_COURSE) {
$flatnav->remove($item->key, \navigation_node::TYPE_COURSE);
}
}
}
/**
* Improve flat navigation menu
*
* @param flat_navigation $flatnav
*/
function theme_moove_add_coursesections_to_navigation(\flat_navigation $flatnav) {
global $PAGE;
$participantsitem = $flatnav->find('participants', \navigation_node::TYPE_CONTAINER);
if (!$participantsitem) {
return;
}
if ($PAGE->course->format != 'singleactivity') {
$coursesectionsoptions = [
'text' => get_string('coursesections', 'theme_moove'),
'shorttext' => get_string('coursesections', 'theme_moove'),
'icon' => new pix_icon('t/viewdetails', ''),
'type' => \navigation_node::COURSE_CURRENT,
'key' => 'course-sections',
'parent' => $participantsitem->parent
];
$coursesections = new \flat_navigation_node($coursesectionsoptions, 0);
foreach ($flatnav as $item) {
if ($item->type == \navigation_node::TYPE_SECTION) {
$coursesections->add_node(new \navigation_node([
'text' => $item->text,
'shorttext' => $item->shorttext,
'icon' => $item->icon,
'type' => $item->type,
'key' => $item->key,
'parent' => $coursesections,
'action' => $item->action
]));
}
}
$flatnav->add($coursesections, $participantsitem->key);
}
$mycourses = $flatnav->find('mycourses', \navigation_node::NODETYPE_LEAF);
if ($mycourses) {
$flatnav->remove($mycourses->key);
$flatnav->add($mycourses, 'privatefiles');
}
}
/**
* Check if a certificate plugin is installed.
*
* @return bool
*/
function theme_moove_has_certificates_plugin() {
$simplecertificate = \core_plugin_manager::instance()->get_plugin_info('mod_simplecertificate');
$customcert = \core_plugin_manager::instance()->get_plugin_info('mod_customcert');
if ($simplecertificate || $customcert) {
return true;
}
return false;
}