This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
toolbox.php
1011 lines (895 loc) · 39 KB
/
toolbox.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Essential is a clean and customizable theme.
*
* @package theme_essential
* @copyright 2016 Gareth J Barnard
* @copyright 2015 Gareth J Barnard
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace theme_essential;
defined('MOODLE_INTERNAL') || die;
class toolbox {
protected $corerenderer = null;
protected static $instance;
private function __construct() {
}
public static function get_instance() {
if (!is_object(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Sets the core_renderer class instance so that when purging all caches and 'theme_xxx_process_css' etc.
* the settings are correct.
* @param class core_renderer $core Child object of core_renderer class.
*/
static public function set_core_renderer($core) {
$us = self::get_instance();
// Set only once from the initial calling lib.php process_css function so that subsequent parent calls do not override it.
// Must happen before parents.
if (null === $us->corerenderer) {
$us->corerenderer = $core;
}
}
/**
* Finds the given setting in the theme from the themes' configuration object.
* @param string $setting Setting name.
* @param string $format false|'format_text'|'format_html'.
* @return any false|value of setting.
*/
static public function get_setting($setting, $format = false) {
$us = self::check_corerenderer();
$settingvalue = $us->get_setting($setting);
global $CFG;
require_once($CFG->dirroot . '/lib/weblib.php');
if (empty($settingvalue)) {
return false;
} else if (!$format) {
return $settingvalue;
} else if ($format === 'format_text') {
return format_text($settingvalue, FORMAT_PLAIN);
} else if ($format === 'format_html') {
return format_text($settingvalue, FORMAT_HTML, array('trusted' => true, 'noclean' => true));
} else if ($format === 'format_file_url') {
return self::setting_file_url($setting, $setting);
} else {
return format_string($settingvalue);
}
}
/**
* Gets the setting moodle_url for the given setting value if it exists.
*/
static public function get_setting_moodle_url($setting) {
$us = self::check_corerenderer();
return $us->get_setting_moodle_url($setting);
}
static public function setting_file_url($setting, $filearea) {
$us = self::check_corerenderer();
return $us->setting_file_url($setting, $filearea);
}
static public function pix_url($imagename, $component) {
$us = self::check_corerenderer();
return $us->pix_url($imagename, $component);
}
static public function getfontawesomemarkup($theicon, $classes = array(), $attributes = array(), $content = '') {
$us = self::check_corerenderer();
return $us->getfontawesomemarkup($theicon, $classes, $attributes, $content);
}
/**
* States if course content search can be used.
* @return boolean false|true if course content search can be used.
*/
static public function course_content_search() {
$canwe = false;
if (self::get_setting('coursecontentsearch')) {
global $PAGE;
// MyDashboard uses columns3.php. Change if needed. And 'process_content_search' below.
$essentialsearch = new \moodle_url('index.php');
$essentialsearch->param('sesskey', sesskey());
$inspectorscourerdata = array('data' => array('theme' => $essentialsearch->out(false)));
$PAGE->requires->js_call_amd('theme_essential/inspector_scourer', 'init', $inspectorscourerdata);
if (self::get_setting('searchallcoursecontentenable')) {
\user_preference_allow_ajax_update('theme_essential_courseitemsearchtype', PARAM_INT);
}
$canwe = true;
}
return $canwe;
}
static public function process_content_search() {
$term = \optional_param('term', '', PARAM_TEXT);
if ($term) {
// Autocomplete AJAX call.
global $CFG, $PAGE;
// Might be overkill but would probably stop DOS attack from lots of DB reads.
\require_sesskey();
if ($CFG->forcelogin) {
\require_login();
}
$courserenderer = $PAGE->get_renderer('core', 'course');
echo json_encode($courserenderer->inspector_ajax($term));
die();
}
$pref = \optional_param('pref', '', PARAM_TEXT);
if (($pref) && ($pref == 'courseitemsearchtype')) {
// Autocomplete AJAX user preference call.
global $CFG;
// Might be overkill but would probably stop DOS attack from lots of DB reads.
\require_sesskey();
if ($CFG->forcelogin) {
\require_login();
}
$value = \optional_param('value', '', PARAM_INT);
// Update.
if (($value == 0) || ($value == 1)) {
if (!\set_user_preference('theme_essential_courseitemsearchtype', $value)) {
print_error('errorsettinguserpref');
}
echo 'OK';
} else {
header('HTTP/1.1 406 Not Acceptable');
echo 'Not Acceptable';
}
die();
}
}
static private function check_corerenderer() {
$us = self::get_instance();
if (empty($us->corerenderer)) {
// Use $OUTPUT unless is not a Essential or child core_renderer which can happen on theme switch.
global $OUTPUT;
if (property_exists($OUTPUT, 'essential')) {
$us->corerenderer = $OUTPUT;
} else {
// Use $PAGE->theme->name as will be accurate than $CFG->theme when using URL theme changes.
// Core 'allowthemechangeonurl' setting.
global $PAGE;
$corerenderer = null;
try {
$corerenderer = $PAGE->get_renderer('theme_'.$PAGE->theme->name, 'core');
} catch (\coding_exception $ce) {
// Specialised renderer may not exist in theme. This is not a coding fault. We just need to cope.
$corerenderer = null;
}
// Fallback check.
if (($corerenderer != null) && (property_exists($corerenderer, 'essential'))) {
$us->corerenderer = $corerenderer;
} else {
// Probably during theme switch, '$CFG->theme' will be accurrate.
global $CFG;
try {
$corerenderer = $PAGE->get_renderer('theme_'.$CFG->theme, 'core');
} catch (\coding_exception $ce) {
// Specialised renderer may not exist in theme. This is not a coding fault. We just need to cope.
$corerenderer = null;
}
if (($corerenderer != null) && (property_exists($corerenderer, 'essential'))) {
$us->corerenderer = $corerenderer;
} else {
// Last resort. Hopefully will be fine on next page load for Child themes.
// However '***_process_css' in lib.php will be fine as it sets the correct renderer.
$us->corerenderer = $PAGE->get_renderer('theme_essential', 'core');
}
}
}
}
return $us->corerenderer;
}
/**
* Finds the given tile file in the theme.
* @param string $filename Filename without extension to get.
* @return string Complete path of the file.
*/
static public function get_tile_file($filename) {
$us = self::check_corerenderer();
return $us->get_tile_file($filename);
}
static public function get_categories_list() {
static $catlist = null;
if (empty($catlist)) {
global $DB;
$catlist = $DB->get_records('course_categories', null, 'sortorder', 'id, name, depth, path');
foreach ($catlist as $category) {
$category->parents = array();
if ($category->depth > 1 ) {
$path = preg_split('|/|', $category->path, -1, PREG_SPLIT_NO_EMPTY);
$category->namechunks = array();
foreach ($path as $parentid) {
$category->namechunks[] = $catlist[$parentid]->name;
$category->parents[] = $parentid;
}
$category->parents = array_reverse($category->parents);
} else {
$category->namechunks = array($category->name);
}
}
}
return $catlist;
}
// Report Page Title.
static public function report_page_has_title() {
global $PAGE;
$hastitle = true;
switch ($PAGE->pagetype) {
case 'grade-report-overview-index':
$hastitle = false;
break;
default:
break;
}
return $hastitle;
}
// Page Bottom Region.
static public function has_page_bottom_region() {
global $PAGE;
$hasregion = false;
switch ($PAGE->pagetype) {
case 'admin-plugins':
case 'course-management':
case 'mod-quiz-edit':
$hasregion = true;
break;
case 'mod-assign-view':
// Only apply to 'grading' page.
if (optional_param('action', '', PARAM_TEXT) == 'grading') {
$hasregion = true;
}
break;
default:
break;
}
return $hasregion;
}
static public function showslider() {
global $CFG;
$noslides = self::get_setting('numberofslides');
if ($noslides && (intval($CFG->version) >= 2013111800)) {
$devicetype = \core_useragent::get_device_type(); // In useragent.php.
if (($devicetype == "mobile") && self::get_setting('hideonphone')) {
$noslides = false;
} else if (($devicetype == "tablet") && self::get_setting('hideontablet')) {
$noslides = false;
}
}
return $noslides;
}
static public function render_indicators($numberofslides) {
$indicators = '';
for ($indicatorslideindex = 0; $indicatorslideindex < $numberofslides; $indicatorslideindex++) {
$indicators .= '<li data-target="#essentialCarousel" data-slide-to="'.$indicatorslideindex.'"';
if ($indicatorslideindex == 0) {
$indicators .= ' class="active"';
}
$indicators .= '></li>';
}
return $indicators;
}
static public function render_slide($slideno, $captionoptions) {
$slideurl = self::get_setting('slide'.$slideno.'url');
$slideurltarget = self::get_setting('slide'.$slideno.'target');
$slidetitle = format_string(self::get_setting('slide'.$slideno));
$slidecaption = self::get_setting('slide'.$slideno.'caption', 'format_html');
if ($slideurl) {
// Strip links from the caption to prevent link in a link.
$slidecaption = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $slidecaption);
}
if ($captionoptions == 0) {
$slideextraclass = ' side-caption';
} else {
$slideextraclass = '';
}
$slideextraclass .= ($slideno === 1) ? ' active' : '';
$slideimagealt = strip_tags($slidetitle);
// Get slide image or fallback to default.
$slideimage = self::get_setting('slide'.$slideno.'image');
if ($slideimage) {
$slideimage = self::setting_file_url('slide'.$slideno.'image', 'slide'.$slideno.'image');
} else {
$slideimage = self::pix_url('default_slide', 'theme');
}
if ($slideurl) {
$slidecontent = '<a href="'.$slideurl.'" target="'.$slideurltarget.'" class="item'.$slideextraclass.'">';
} else {
$slidecontent = '<div class="item'.$slideextraclass.'">';
}
if ($captionoptions == 0) {
$slidecontent .= '<div class="container-fluid">';
$slidecontent .= '<div class="row-fluid">';
if ($slidetitle || $slidecaption) {
$slidecontent .= '<div class="span5 the-side-caption">';
$slidecontent .= '<div class="the-side-caption-content">';
$slidecontent .= '<h4>'.$slidetitle.'</h4>';
$slidecontent .= '<div>'.$slidecaption.'</div>';
$slidecontent .= '</div>';
$slidecontent .= '</div>';
$slidecontent .= '<div class="span7">';
} else {
$slidecontent .= '<div class="span10 offset1 nocaption">';
}
$slidecontent .= '<div class="carousel-image-container">';
$slidecontent .= '<img src="'.$slideimage.'" alt="'.$slideimagealt.'" class="carousel-image">';
$slidecontent .= '</div>';
$slidecontent .= '</div>';
$slidecontent .= '</div>';
$slidecontent .= '</div>';
} else {
$nocaption = (!($slidetitle || $slidecaption)) ? ' nocaption' : '';
$slidecontent .= '<div class="carousel-image-container'.$nocaption.'">';
$slidecontent .= '<img src="'.$slideimage.'" alt="'.$slideimagealt.'" class="carousel-image">';
$slidecontent .= '</div>';
// Output title and caption if either is present.
if ($slidetitle || $slidecaption) {
$slidecontent .= '<div class="carousel-caption">';
$slidecontent .= '<div class="carousel-caption-inner">';
$slidecontent .= '<h4>'.$slidetitle.'</h4>';
$slidecontent .= '<div>'.$slidecaption.'</div>';
$slidecontent .= '</div>';
$slidecontent .= '</div>';
}
}
$slidecontent .= ($slideurl) ? '</a>' : '</div>';
return $slidecontent;
}
static public function render_slide_controls($left) {
$strprev = get_string('prev');
$strnext = get_string('next');
if ($left) {
$arrowprev = 'left';
$arrownext = 'right';
} else {
$arrowprev = 'right';
$arrownext = 'left';
}
$prev = '<a class="left carousel-control" href="#essentialCarousel" data-slide="prev" aria-label="'.$strprev.'">';
$prev .= '<span aria-hidden="true" class="fa fa-chevron-circle-'.$arrowprev.'"></span></a>';
$next = '<a class="right carousel-control" href="#essentialCarousel" data-slide="next" aria-label="'.$strnext.'">';
$next .= '<span aria-hidden="true" class="fa fa-chevron-circle-'.$arrownext.'"></span></a>';
return $prev . $next;
}
/**
* Checks if the user is switching colours with a refresh
*
* If they are this updates the users preference in the database
*/
static protected function check_colours_switch() {
$colours = \optional_param('essentialcolours', null, PARAM_ALPHANUM);
if (in_array($colours, array('default', 'alternative1', 'alternative2', 'alternative3', 'alternative4'))) {
\set_user_preference('theme_essential_colours', $colours);
}
}
/**
* Adds the JavaScript for the colour switcher to the page.
*
* The colour switcher is a YUI moodle module that is located in
* theme/udemspl/yui/udemspl/udemspl.js
*
* @param moodle_page $page
*/
static public function initialise_colourswitcher(\moodle_page $page) {
self::check_colours_switch();
\user_preference_allow_ajax_update('theme_essential_colours', PARAM_ALPHANUM);
$page->requires->js_call_amd('theme_essential/coloursswitcher', 'init',
array(array('div' => '#custom_menu_themecolours .dropdown-menu')));
}
/**
* Gets the theme colours the user has selected if enabled or the default if they have never changed.
*
* @param string $default The default theme colors to use.
* @return string The theme colours the user has selected.
*/
static public function get_colours($default = 'default') {
$preference = \get_user_preferences('theme_essential_colours', $default);
foreach (range(1, 4) as $alternativethemenumber) {
if ($preference == 'alternative'.$alternativethemenumber &&
self::get_setting('enablealternativethemecolors'.$alternativethemenumber)) {
return $preference;
}
}
return $default;
}
static public function set_font($css, $type, $fontname) {
$familytag = '[[setting:' . $type . 'font]]';
$facetag = '[[setting:fontfiles' . $type . ']]';
if (empty($fontname)) {
$familyreplacement = 'Verdana';
$facereplacement = '';
} else if (self::get_setting('fontselect') === '3') {
$fontfiles = array();
$fontfileeot = self::setting_file_url('fontfileeot'.$type, 'fontfileeot'.$type);
if (!empty($fontfileeot)) {
$fontfiles[] = "url('".$fontfileeot."?#iefix') format('embedded-opentype')";
}
$fontfilewoff = self::setting_file_url('fontfilewoff'.$type, 'fontfilewoff'.$type);
if (!empty($fontfilewoff)) {
$fontfiles[] = "url('".$fontfilewoff."') format('woff')";
}
$fontfilewofftwo = self::setting_file_url('fontfilewofftwo' . $type, 'fontfilewofftwo'.$type);
if (!empty($fontfilewofftwo)) {
$fontfiles[] = "url('".$fontfilewofftwo."') format('woff2')";
}
$fontfileotf = self::setting_file_url('fontfileotf'.$type, 'fontfileotf'.$type);
if (!empty($fontfileotf)) {
$fontfiles[] = "url('".$fontfileotf."') format('opentype')";
}
$fontfilettf = self::setting_file_url('fontfilettf'.$type, 'fontfilettf'.$type);
if (!empty($fontfilettf)) {
$fontfiles[] = "url('".$fontfilettf."') format('truetype')";
}
$fontfilesvg = self::setting_file_url('fontfilesvg'.$type, 'fontfilesvg'.$type);
if (!empty($fontfilesvg)) {
$fontfiles[] = "url('".$fontfilesvg."') format('svg')";
}
if (!empty($fontfiles)) {
$familyreplacement = '"'.$fontname.'"';
$facereplacement = '@font-face {'.PHP_EOL.'font-family: "'.$fontname.'";'.PHP_EOL;
$facereplacement .= !empty($fontfileeot) ? "src: url('".$fontfileeot."');".PHP_EOL : '';
$facereplacement .= "src: ";
$facereplacement .= implode(",".PHP_EOL." ", $fontfiles);
$facereplacement .= ";".PHP_EOL."}";
} else {
// No files back to default.
$familyreplacement = 'Verdana';
$facereplacement = '';
}
} else {
$familyreplacement = '"'.$fontname.'"';
$facereplacement = '';
}
$css = str_replace($familytag, $familyreplacement, $css);
$css = str_replace($facetag, $facereplacement, $css);
return $css;
}
static public function set_color($css, $themecolor, $tag, $defaultcolour, $alpha = null) {
if (!($themecolor)) {
$replacement = $defaultcolour;
} else {
$replacement = $themecolor;
}
if (!is_null($alpha)) {
$replacement = self::hex2rgba($replacement, $alpha);
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_alternativecolor($css, $type, $customcolor, $defaultcolour, $alpha = null) {
$tag = '[[setting:alternativetheme'.$type.']]';
if (!($customcolor)) {
$replacement = $defaultcolour;
} else {
$replacement = $customcolor;
}
if (!is_null($alpha)) {
$replacement = self::hex2rgba($replacement, $alpha);
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function get_current_category() {
$us = self::check_corerenderer();
return $us->get_current_category();
}
static public function set_categorycoursetitleimages($css) {
$tag = '[[setting:categorycoursetitle]]';
$replacement = '';
if (self::get_setting('enablecategorycti')) {
$categories = self::get_categories_list();
foreach ($categories as $cid => $unused) {
$image = self::get_setting('categoryct'.$cid.'image');
$imageurl = false;
if ($image) {
$imageurl = self::setting_file_url('categoryct'.$cid.'image', 'categoryct'.$cid.'image');
} else {
$imageurlsetting = self::get_setting('categoryctimageurl'.$cid);
if ($imageurlsetting) {
$imageurl = $imageurlsetting;
}
}
if ($imageurl) {
$replacement .= '.categorycti-'.$cid.' {';
$replacement .= 'background-image: url(\''.$imageurl.'\');';
$replacement .= 'height: '.self::get_setting('categorycti'.$cid.'height').'px;';
$replacement .= '}';
$replacement .= '.categorycti-'.$cid.' .coursetitle {';
$replacement .= 'color: '.self::get_setting('categorycti'.$cid.'textcolour').';';
$replacement .= 'background-color: '.self::get_setting('categorycti'.$cid.'textbackgroundcolour').';';
$replacement .= 'opacity: '.self::get_setting('categorycti'.$cid.'textbackgroundopactity').';';
$replacement .= '}';
}
}
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
/**
* Returns the RGB for the given hex.
*
* @param string $hex
* @return array
*/
static private function hex2rgb($hex) {
// From: http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/.
$hex = str_replace("#", "", $hex);
if (strlen($hex) == 3) {
$r = hexdec(substr($hex, 0, 1).substr($hex, 0, 1));
$g = hexdec(substr($hex, 1, 1).substr($hex, 1, 1));
$b = hexdec(substr($hex, 2, 1).substr($hex, 2, 1));
} else {
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
$rgb = array('r' => $r, 'g' => $g, 'b' => $b);
return $rgb; // Returns the rgb as an array.
}
static public function hexadjust($hex, $percentage) {
$percentage = round($percentage / 100, 2);
$rgb = self::hex2rgb($hex);
$r = round($rgb['r'] - ($rgb['r'] * $percentage));
$g = round($rgb['g'] - ($rgb['g'] * $percentage));
$b = round($rgb['b'] - ($rgb['b'] * $percentage));
return '#'.str_pad(dechex(max(0, min(255, $r))), 2, '0', STR_PAD_LEFT)
.str_pad(dechex(max(0, min(255, $g))), 2, '0', STR_PAD_LEFT)
.str_pad(dechex(max(0, min(255, $b))), 2, '0', STR_PAD_LEFT);
}
/**
* Returns the RGBA for the given hex and alpha.
*
* @param string $hex
* @param string $alpha
* @return string
*/
static private function hex2rgba($hex, $alpha) {
$rgba = self::hex2rgb($hex);
$rgba[] = $alpha;
return 'rgba('.implode(", ", $rgba).')'; // Returns the rgba values separated by commas.
}
static public function set_headerbackground($css, $headerbackground) {
$tag = '[[setting:headerbackground]]';
$headerbackgroundstyle = self::get_setting('headerbackgroundstyle');
$replacement = '#page-header {';
$replacement .= 'background-image: url(\'';
if ($headerbackground) {
$replacement .= $headerbackground;
} else {
$replacement .= self::pix_url('bg/header', 'theme');
$headerbackgroundstyle = 'tiled';
}
$replacement .= '\');';
if ($headerbackground) {
$replacement .= 'background-size: contain;';
}
if ($headerbackgroundstyle == 'tiled') {
$replacement .= 'background-repeat: repeat;';
} else {
$replacement .= 'background-repeat: no-repeat;';
$replacement .= 'background-position: center;';
}
$replacement .= '}';
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_pagebackground($css, $pagebackground) {
$tag = '[[setting:pagebackground]]';
if (!($pagebackground)) {
$replacement = 'none';
} else {
$replacement = 'url(\''.$pagebackground.'\')';
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_pagebackgroundstyle($css, $style) {
$tagattach = '[[setting:backgroundattach]]';
$tagrepeat = '[[setting:backgroundrepeat]]';
$tagsize = '[[setting:backgroundsize]]';
$replacementattach = 'fixed';
$replacementrepeat = 'no-repeat';
$replacementsize = 'cover';
if ($style === 'tiled') {
$replacementrepeat = 'repeat';
$replacementsize = 'auto';
} else if ($style === 'stretch') {
$replacementattach = 'scroll';
}
$css = str_replace($tagattach, $replacementattach, $css);
$css = str_replace($tagrepeat, $replacementrepeat, $css);
$css = str_replace($tagsize, $replacementsize, $css);
return $css;
}
static public function set_loginbackground($css, $loginbackground) {
$tag = '[[setting:loginbackground]]';
if (!($loginbackground)) {
$replacement = 'none';
} else {
$replacement = 'url(\''.$loginbackground.'\')';
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_loginbackgroundstyle($css, $style, $opacity) {
$tagopacity = '[[setting:loginbackgroundopacity]]';
$tagsize = '[[setting:loginbackgroundstyle]]';
$replacementsize = 'cover';
if ($style === 'stretch') {
$replacementsize = '100% 100%';
}
$css = str_replace($tagopacity, $opacity, $css);
$css = str_replace($tagsize, $replacementsize, $css);
return $css;
}
static public function set_marketingheight($css, $marketingheight, $marketingimageheight) {
$tag = '[[setting:marketingheight]]';
$mhreplacement = $marketingheight;
if (!($mhreplacement)) {
$mhreplacement = 100;
}
$css = str_replace($tag, $mhreplacement.'px', $css);
$tag = '[[setting:marketingheightwithbutton]]';
$mhreplacement += 32;
$css = str_replace($tag, $mhreplacement.'px', $css);
$tag = '[[setting:marketingimageheight]]';
$mihreplacement = $marketingimageheight;
if (!($mihreplacement)) {
$mihreplacement = 100;
}
$css = str_replace($tag, $mihreplacement.'px', $css);
$tag = '[[setting:marketingheightwithimage]]';
$replacement = $mhreplacement + $mihreplacement;
if (!($replacement)) {
$replacement = 200;
}
$css = str_replace($tag, $replacement.'px', $css);
$tag = '[[setting:marketingheightwithimagewithbutton]]';
$replacement += 32;
$css = str_replace($tag, $replacement.'px', $css);
return $css;
}
static public function set_marketingimage($css, $marketingimage, $setting) {
$tag = '[[setting:'.$setting.']]';
if (!($marketingimage)) {
$replacement = 'none';
} else {
$replacement = 'url(\''.$marketingimage.'\')';
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_customcss($css, $customcss) {
$tag = '[[setting:customcss]]';
$replacement = $customcss;
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_integer($css, $setting, $integer, $default) {
$tag = '[[setting:'.$setting.']]';
if (!($integer)) {
$replacement = $default;
} else {
$replacement = $integer;
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
static public function set_pagewidth($css, $pagewidth) {
$tag = '[[setting:pagewidth]]';
$imagetag = '[[setting:pagewidthimage]]';
$replacement = $pagewidth;
if (!($replacement)) {
$replacement = '1200';
}
if ($replacement == "100") {
$css = str_replace($tag, $replacement.'%', $css);
$css = str_replace($imagetag, '90'.'%', $css);
} else {
$css = str_replace($tag, $replacement.'px', $css);
$css = str_replace($imagetag, $replacement.'px', $css);
}
return $css;
}
/**
* States if the browser is not IE9 or less.
*/
static public function not_lte_ie9() {
$properties = self::ie_properties();
if (!is_array($properties)) {
return true;
}
// We have properties, it is a version of IE, so is it greater than 9?
return ($properties['version'] > 9.0);
}
/**
* States if the browser is IE9 or less.
*/
static public function lte_ie9() {
$properties = self::ie_properties();
if (!is_array($properties)) {
return false;
}
// We have properties, it is a version of IE, so is it greater than 9?
return ($properties['version'] <= 9.0);
}
/**
* States if the browser is IE by returning properties, otherwise false.
*/
static protected function ie_properties() {
$properties = \core_useragent::check_ie_properties(); // In /lib/classes/useragent.php.
if (!is_array($properties)) {
return false;
} else {
return $properties;
}
}
static public function compile_properties($themename, $array = true) {
global $CFG, $DB;
$props = array();
$themeprops = $DB->get_records('config_plugins', array('plugin' => 'theme_'.$themename));
if ($array) {
$props['moodle_version'] = $CFG->version;
// Put the theme version next so that it will be at the top of the table.
foreach ($themeprops as $themeprop) {
if ($themeprop->name == 'version') {
$props['theme_version'] = $themeprop->value;
unset($themeprops[$themeprop->id]);
break;
}
}
foreach ($themeprops as $themeprop) {
$props[$themeprop->name] = $themeprop->value;
}
} else {
$data = new \stdClass();
$data->id = 0;
$data->value = $CFG->version;
$props['moodle_version'] = $data;
// Convert 'version' to 'theme_version'.
foreach ($themeprops as $themeprop) {
if ($themeprop->name == 'version') {
$data = new \stdClass();
$data->id = $themeprop->id;
$data->name = 'theme_version';
$data->value = $themeprop->value;
$props['theme_version'] = $data;
unset($themeprops[$themeprop->id]);
break;
}
}
foreach ($themeprops as $themeprop) {
$data = new \stdClass();
$data->id = $themeprop->id;
$data->value = $themeprop->value;
$props[$themeprop->name] = $data;
}
}
return $props;
}
static public function put_properties($themename, $props) {
global $DB;
// Get the current properties as a reference and for theme version information.
$currentprops = self::compile_properties($themename, false);
// Build the report.
$report = get_string('putpropertyreport', 'theme_essential').PHP_EOL;
$report .= get_string('putpropertyproperties', 'theme_essential').' \'Moodle\' '.
get_string('putpropertyversion', 'theme_essential').' '.$props['moodle_version'].'.'.PHP_EOL;
unset($props['moodle_version']);
$report .= get_string('putpropertyour', 'theme_essential').' \'Moodle\' '.
get_string('putpropertyversion', 'theme_essential').' '.$currentprops['moodle_version']->value.'.'.PHP_EOL;
unset($currentprops['moodle_version']);
$report .= get_string('putpropertyproperties', 'theme_essential').' \''.ucfirst($themename).'\' '.
get_string('putpropertyversion', 'theme_essential').' '.$props['theme_version'].'.'.PHP_EOL;
unset($props['theme_version']);
$report .= get_string('putpropertyour', 'theme_essential').' \''.ucfirst($themename).'\' '.
get_string('putpropertyversion', 'theme_essential').' '.$currentprops['theme_version']->value.'.'.PHP_EOL.PHP_EOL;
unset($currentprops['theme_version']);
// Pre-process files - using 'theme_essential_pluginfile' in lib.php as a reference.
// TODO: refactor into one method for both this and that.
$filestoreport = '';
$preprocessfilesettings = array('logo', 'headerbackground', 'pagebackground', 'favicon', 'iphoneicon',
'iphoneretinaicon', 'ipadicon', 'ipadretinaicon', 'loginbackground');
$fonttypes = array('eot', 'otf', 'svg', 'ttf', 'woff', 'woff2');
foreach ($fonttypes as $fonttype) {
$preprocessfilesettings[] = 'fontfile'.$fonttype.'heading';
$preprocessfilesettings[] = 'fontfile'.$fonttype.'body';
}
// Only 3 marketing spots and no setting for the number.
$preprocessfilesettings = array_merge($preprocessfilesettings, array('marketing1image', 'marketing2image', 'marketing3image'));
// Slide show.
for ($propslide = 1; $propslide <= $props['numberofslides']; $propslide++) {
$preprocessfilesettings[] = 'slide'.$propslide.'image';
}
// Process the file properties.
foreach ($preprocessfilesettings as $preprocessfilesetting) {
self::put_prop_file_preprocess($preprocessfilesetting, $props, $filestoreport);
unset($currentprops[$preprocessfilesetting]);
}
// Course title images are complex and related to the category id of the installation, so ignore!
if ((!empty($props['enablecategorycti'])) || (!empty($props['enablecategoryctics']))) {
$report .= get_string('putpropertiesignorecti', 'theme_essential').PHP_EOL.PHP_EOL;
}
$ctikeys = array(
'enablecategorycti',
'enablecategoryctics',
'ctioverrideheight',
'ctioverridetextcolour',
'ctioverridetextbackgroundcolour',
'ctioverridetextbackgroundopacity');
foreach ($ctikeys as $ctikey) {
unset($props[$ctikey]);
unset($currentprops[$ctikey]);
}
$propskeys = array_keys($props);
foreach ($propskeys as $propkey) {
if (preg_match('#^categoryct#', $propkey) === 1) {
unset($props[$propkey]);
}
}
$currentpropkeys = array_keys($currentprops);
foreach ($currentpropkeys as $currentpropkey) {
if (preg_match('#^categoryct#', $currentpropkey) === 1) {
unset($currentprops[$currentpropkey]);
}
}
if ($filestoreport) {
$report .= get_string('putpropertiesreportfiles', 'theme_essential').PHP_EOL.$filestoreport.PHP_EOL;
}
// Need to ignore and report on any unknown settings.
$report .= get_string('putpropertiessettingsreport', 'theme_essential').PHP_EOL;
$changed = '';
$unchanged = '';
$added = '';
$ignored = '';
$settinglog = '';
foreach ($props as $propkey => $propvalue) {
$settinglog = '\''.$propkey.'\' '.get_string('putpropertiesvalue', 'theme_essential').' \''.$propvalue.'\'';
if (array_key_exists($propkey, $currentprops)) {
if ($propvalue != $currentprops[$propkey]->value) {
$settinglog .= ' '.get_string('putpropertiesfrom', 'theme_essential').' \''.$currentprops[$propkey]->value.'\'';
$changed .= $settinglog.'.'.PHP_EOL;
$DB->update_record('config_plugins', array('id' => $currentprops[$propkey]->id, 'value' => $propvalue), true);
} else {
$unchanged .= $settinglog.'.'.PHP_EOL;
}
} else if (preg_match('#^slide#', $propkey) === 1) {
$DB->insert_record('config_plugins', array(
'plugin' => 'theme_'.$themename, 'name' => $propkey, 'value' => $propvalue), true);
$added .= $settinglog.'.'.PHP_EOL;
} else {
$ignored .= $settinglog.'.'.PHP_EOL;
}
}
if (!empty($changed)) {
$report .= get_string('putpropertieschanged', 'theme_essential').PHP_EOL.$changed.PHP_EOL;
}
if (!empty($added)) {
$report .= get_string('putpropertiesadded', 'theme_essential').PHP_EOL.$added.PHP_EOL;
}
if (!empty($unchanged)) {
$report .= get_string('putpropertiesunchanged', 'theme_essential').PHP_EOL.$unchanged.PHP_EOL;
}
if (!empty($ignored)) {
$report .= get_string('putpropertiesignored', 'theme_essential').PHP_EOL.$ignored.PHP_EOL;
}