-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_course_overview_campus.php
executable file
·1392 lines (1200 loc) · 79.7 KB
/
block_course_overview_campus.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/>.
/**
* Block "course overview (campus)"
*
* @package block_course_overview_campus
* @copyright 2013 Alexander Bias, Ulm University <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__) . '/lib.php');
class block_course_overview_campus extends block_base {
// Variable which will hold the plugin's config
public $coc_config;
function init() {
$this->title = get_string('pluginname', 'block_course_overview_campus');
}
function specialization() {
global $coc_config;
$coc_config = get_config('block_course_overview_campus');
$this->title = format_string($coc_config->blocktitle);
}
function applicable_formats() {
return array('my-index' => true, 'my' => true, 'site-index' => true);
}
function has_config() {
return true;
}
function instance_allow_multiple() {
return false;
}
function instance_can_be_hidden() {
return false;
}
function get_content() {
global $coc_config, $USER, $CFG, $DB, $PAGE, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
/********************************************************************************/
/*** PREPROCESSING ***/
/********************************************************************************/
// Check if the configured term dates make sense, if not disable term filter
if (!block_course_overview_campus_check_term_config($coc_config)) {
$coc_config->termcoursefilter = false;
}
// Process GET parameters
$hidecourse = optional_param('coc-hidecourse', 0, PARAM_INT);
$showcourse = optional_param('coc-showcourse', 0, PARAM_INT);
$hidenews = optional_param('coc-hidenews', 0, PARAM_INT);
$shownews = optional_param('coc-shownews', 0, PARAM_INT);
$manage = optional_param('coc-manage', 0, PARAM_BOOL);
$term = optional_param('coc-term', null, PARAM_TEXT);
$category = optional_param('coc-category', null, PARAM_TEXT);
$toplevelcategory = optional_param('coc-toplevelcategory', null, PARAM_TEXT);
$teacher = optional_param('coc-teacher', null, PARAM_TEXT);
// Set displaying preferences when set by GET parameters
if ($coc_config->enablehidecourses) {
if ($hidecourse != 0) {
set_user_preference('block_course_overview_campus-hidecourse-'.$hidecourse, 1);
}
if ($showcourse != 0) {
set_user_preference('block_course_overview_campus-hidecourse-'.$showcourse, 0);
}
}
if ($coc_config->enablecoursenews) {
if ($hidenews != 0) {
set_user_preference('block_course_overview_campus-hidenews-'.$hidenews, 1);
}
if ($shownews != 0) {
set_user_preference('block_course_overview_campus-hidenews-'.$shownews, 0);
}
}
// Set and remember term filter if GET parameter is present
if ($term != null) {
$selectedterm = $term;
set_user_preference('block_course_overview_campus-selectedterm', $term);
}
// Or set term filter based on user preference with default term fallback if activated
else if ($coc_config->defaultterm == true) {
$selectedterm = get_user_preferences('block_course_overview_campus-selectedterm', 'currentterm');
}
// Or set term filter based on user preference with 'all' terms fallback
else {
$selectedterm = get_user_preferences('block_course_overview_campus-selectedterm', 'all');
}
// Set and remember parent category filter if GET parameter is present
if ($category != null) {
$selectedcategory = $category;
set_user_preference('block_course_overview_campus-selectedcategory', $category);
}
// Or set parent category filter based on user preference with 'all' categories fallback
else {
$selectedcategory = get_user_preferences('block_course_overview_campus-selectedcategory', 'all');
}
// Set and remember top level category filter if GET parameter is present
if ($toplevelcategory != null) {
$selectedtoplevelcategory = $toplevelcategory;
set_user_preference('block_course_overview_campus-selectedtoplevelcategory', $toplevelcategory);
}
// Or set top level category filter based on user preference with 'all' categories fallback
else {
$selectedtoplevelcategory = get_user_preferences('block_course_overview_campus-selectedtoplevelcategory', 'all');
}
// Set and remember teacher filter if GET parameter is present
if ($teacher != null) {
$selectedteacher = $teacher;
set_user_preference('block_course_overview_campus-selectedteacher', $teacher);
}
// Or set teacher filter based on user preference with 'all' teachers fallback
else {
$selectedteacher = get_user_preferences('block_course_overview_campus-selectedteacher', 'all');
}
// Get my courses in alphabetical order
$courses = enrol_get_my_courses('id, shortname', 'fullname ASC');
// Remove frontpage course, if enrolled, from courses list
$site = get_site();
if (array_key_exists($site->id, $courses)) {
unset($courses[$site->id]);
}
/********************************************************************************/
/*** PROCESS MY COURSES ***/
/********************************************************************************/
// No, I don't have any courses -> content is only a placeholder message
if (empty($courses)) {
$content = get_string('nocourses', 'block_course_overview_campus');
}
// Yes, I have courses
else {
// Start output buffer
ob_start();
// Get lastaccess of my courses to support course news
if ($coc_config->enablecoursenews) {
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
}
else {
$courses[$c->id]->lastaccess = 0;
}
}
}
// Get course news from my courses
if ($coc_config->enablecoursenews) {
$coursenews = block_course_overview_campus_get_overviews($courses, $coc_config->skipcoursenews);
}
// Get all course categories for later use
$coursecategories = $DB->get_records('course_categories');
// Get teacher roles for later use
if (!empty($coc_config->teacherroles)) {
$teacherroles = explode(',', $coc_config->teacherroles);
}
else {
$teacherroles = array();
}
// Create empty filter for activated filters
if ($coc_config->termcoursefilter == true) {
$filterterms = array();
}
if ($coc_config->categorycoursefilter == true) {
$filtercategories = array();
}
if ($coc_config->toplevelcategorycoursefilter == true) {
$filtertoplevelcategories = array();
}
if ($coc_config->teachercoursefilter == true) {
$filterteachers = array();
}
// Create counter for hidden courses
if ($coc_config->enablehidecourses) {
$hiddencourses = 0;
}
// Create string to remember courses for JS processing
$js_courseslist = ' ';
// Create string to remember course news for JS processing
if ($coc_config->enablecoursenews) {
$js_coursenewslist = ' ';
}
// Now iterate over courses and collect data about my courses
foreach ($courses as $c) {
// Get course context
$context = context_course::instance($c->id);
// Collect information about my courses and populate filters with data about my courses
// Term information
if ($coc_config->termcoursefilter == true || $coc_config->secondrowshowtermname == true) {
// Create object for bufferung course term information
$courseterm = new stdClass();
// If course start date is undefined, set course term to "other"
if ($c->startdate == 0) {
$courseterm->id = 'other';
$courseterm->name = get_string('other', 'block_course_overview_campus');
}
// If course start date is available, if timeless courses are enabled and if course start date is before timeless course threshold, set course term to "timeless"
else if ($coc_config->timelesscourses == true && date('Y', $c->startdate) < $coc_config->timelesscoursesthreshold) {
$courseterm->id = 'timeless';
$courseterm->name = format_string($coc_config->timelesscoursesname);
}
// If course start date is available, distinguish between term modes
// "Academic year" mode
else if ($coc_config->termmode == 1) {
// Prepare date information
$coursestartyday = usergetdate($c->startdate)['yday'];
$coursestartyear = usergetdate($c->startdate)['year'];
$term1startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term1startday)[0], explode('-', $coc_config->term1startday)[1]))['yday'];
// If term starts on January 1st, set course term to course start date's year
if ($coc_config->term1startday == '01-01') {
$courseterm->id = $coursestartyear;
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, $coursestartyear);
}
// If term doesn't start on January 1st and course start date's day comes on or after term start day, set course term to course start date's year + next year
else if ($coursestartyday >= $term1startyday) {
$courseterm->id = $coursestartyear.'-'.($coursestartyear+1);
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, $coursestartyear, ($coursestartyear+1));
}
// If term doesn't start on January 1st and course start date's day comes before term start day, set course term to course start date's year + former year
else {
$courseterm->id = ($coursestartyear-1).'-'.$coursestartyear;
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, ($coursestartyear-1), $coursestartyear);
}
// Discard date information
unset($courseyday, $courseyear, $term1startday);
}
// "Semester" mode
else if ($coc_config->termmode == 2) {
// Prepare date information
$coursestartyday = usergetdate($c->startdate)['yday'];
$coursestartyear = usergetdate($c->startdate)['year'];
$term1startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term1startday)[0], explode('-', $coc_config->term1startday)[1]))['yday'];
$term2startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term2startday)[0], explode('-', $coc_config->term2startday)[1]))['yday'];
// If course start date's day comes before first term start day, set course term to second term of former year
if ($coursestartyday < $term1startyday) {
$courseterm->id = ($coursestartyear-1).'-2';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term2name, ($coursestartyear-1), $coursestartyear);
}
// If course start date's day comes on or after first term start day but before second term start day, set course term to first term of current year
else if ($coursestartyday >= $term1startyday && $coursestartyday < $term2startyday) {
$courseterm->id = $coursestartyear.'-1';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, $coursestartyear);
}
// If course start date's day comes on or after second term start day, set course term to second term of current year
else {
$courseterm->id = $coursestartyear.'-2';
// If first term does start on January 1st, suffix name with single year, otherwise suffix name with double year
if ($coc_config->term1startday == '01-01') {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term2name, $coursestartyear);
}
else {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term2name, $coursestartyear, ($coursestartyear+1));
}
}
// Discard date information
unset($courseyday, $courseyear, $term1startday, $term2startday);
}
// "Tertial" mode
else if ($coc_config->termmode == 3) {
// Prepare date information
$coursestartyday = usergetdate($c->startdate)['yday'];
$coursestartyear = usergetdate($c->startdate)['year'];
$term1startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term1startday)[0], explode('-', $coc_config->term1startday)[1]))['yday'];
$term2startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term2startday)[0], explode('-', $coc_config->term2startday)[1]))['yday'];
$term3startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term3startday)[0], explode('-', $coc_config->term3startday)[1]))['yday'];
// If course start date's day comes before first term start day, set course term to third term of former year
if ($coursestartyday < $term1startyday) {
$courseterm->id = ($coursestartyear-1).'-3';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term3name, ($coursestartyear-1), $coursestartyear);
}
// If course start date's day comes on or after first term start day but before second term start day, set course term to first term of current year
else if ($coursestartyday >= $term1startyday && $coursestartyday < $term2startyday) {
$courseterm->id = $coursestartyear.'-1';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, $coursestartyear);
}
// If course start date's day comes on or after second term start day but before third term start day, set course term to second term of current year
else if ($coursestartyday >= $term2startyday && $coursestartyday < $term3startyday) {
$courseterm->id = $coursestartyear.'-2';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term2name, $coursestartyear);
}
// If course start date's day comes on or after third term start day, set course term to third term of current year
else {
$courseterm->id = $coursestartyear.'-3';
// If first term does start on January 1st, suffix name with single year, otherwise suffix name with double year
if ($coc_config->term1startday == '01-01') {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term3name, $coursestartyear);
}
else {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term3name, $coursestartyear, ($coursestartyear+1));
}
}
// Discard date information
unset($courseyday, $courseyear, $term1startday, $term2startday, $term3startday);
}
// "Trimester" mode
else if ($coc_config->termmode == 4) {
// Prepare date information
$coursestartyday = usergetdate($c->startdate)['yday'];
$coursestartyear = usergetdate($c->startdate)['year'];
$term1startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term1startday)[0], explode('-', $coc_config->term1startday)[1]))['yday'];
$term2startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term2startday)[0], explode('-', $coc_config->term2startday)[1]))['yday'];
$term3startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term3startday)[0], explode('-', $coc_config->term3startday)[1]))['yday'];
$term4startyday = usergetdate(make_timestamp($coursestartyear, explode('-', $coc_config->term4startday)[0], explode('-', $coc_config->term4startday)[1]))['yday'];
// If course start date's day comes before first term start day, set course term to fourth term of former year
if ($coursestartyday < $term1startyday) {
$courseterm->id = ($coursestartyear-1).'-4';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term4name, ($coursestartyear-1), $coursestartyear);
}
// If course start date's day comes on or after first term start day but before second term start day, set course term to first term of current year
else if ($coursestartyday >= $term1startyday && $coursestartyday < $term2startyday) {
$courseterm->id = $coursestartyear.'-1';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term1name, $coursestartyear);
}
// If course start date's day comes on or after second term start day but before third term start day, set course term to second term of current year
else if ($coursestartyday >= $term2startyday && $coursestartyday < $term3startyday) {
$courseterm->id = $coursestartyear.'-2';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term2name, $coursestartyear);
}
// If course start date's day comes on or after third term start day but before fourth term start day, set course term to third term of current year
else if ($coursestartyday >= $term3startyday && $coursestartyday < $term4startyday) {
$courseterm->id = $coursestartyear.'-3';
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term3name, $coursestartyear);
}
// If course start date's day comes on or after fourth term start day, set course term to fourth term of current year
else {
$courseterm->id = $coursestartyear.'-4';
// If first term does start on January 1st, suffix name with single year, otherwise suffix name with double year
if ($coc_config->term1startday == '01-01') {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term4name, $coursestartyear);
}
else {
$courseterm->name = block_course_overview_campus_get_term_displayname($coc_config->term4name, $coursestartyear, ($coursestartyear+1));
}
}
// Discard date information
unset($courseyday, $courseyear, $term1startday, $term2startday, $term3startday, $term4startday);
}
// This should never happen
else {
print_error('error');
}
// Remember course term for later use
$c->term = $courseterm->id;
$c->termname = format_string($courseterm->name);
}
// Term filter
if ($coc_config->termcoursefilter == true) {
// Add course term to filter list
$filterterms[$courseterm->id] = $courseterm->name;
}
// Parent category information
if ($coc_config->categorycoursefilter == true || $coc_config->secondrowshowcategoryname == true || $coc_config->toplevelcategorycoursefilter == true || $coc_config->secondrowshowtoplevelcategoryname == true) {
// Get course parent category name from array of all category names
$coursecategory = $coursecategories[$c->category];
// Remember course parent category name for later use
$c->categoryname = format_string($coursecategory->name);
$c->categoryid = $coursecategory->id;
// Get course top level category name from array of all category names
$coursecategorypath = explode('/', $coursecategory->path);
$coursetoplevelcategoryid = $coursecategorypath[1];
$coursetoplevelcategory = $coursecategories[$coursetoplevelcategoryid];
// Remember course top level category name for later use
$c->toplevelcategoryname = format_string($coursetoplevelcategory->name);
$c->toplevelcategoryid = $coursetoplevelcategory->id;
}
// Parent category filter
if ($coc_config->categorycoursefilter == true) {
// Merge homonymous categories into one category if configured
if ($coc_config->mergehomonymouscategories == true) {
// Check if course category name is already present in the category filter array
if ($othercategoryid = array_search($c->categoryname, $filtercategories)) {
// If yes and if course category is different than the already present category (same name, but different id), modify course category id to equal the already present category id (poor hack, but functional)
if ($othercategoryid != $c->categoryid) {
$c->categoryid = $othercategoryid;
}
}
}
// Add course parent category name to filter list
$filtercategories[$c->categoryid] = $c->categoryname;
}
// Top level category filter
if ($coc_config->toplevelcategorycoursefilter == true) {
// Add course top level category name to filter list
$filtertoplevelcategories[$c->toplevelcategoryid] = $c->toplevelcategoryname;
}
// Teacher information
if ($coc_config->teachercoursefilter == true || $coc_config->secondrowshowteachername == true) {
// Get course teachers based on global teacher roles
if (count($teacherroles) > 0) {
// Get all user name fields for SQL query in a proper way
$allnames = get_all_user_name_fields(true, 'u');
// Check if we have to check for suspended teachers
if ($coc_config->teacherroleshidesuspended == 1) {
// Build extra where clause for SQL query
$now = round(time(), -2); // improves db caching
$extrawhere = 'ue.status = '.ENROL_USER_ACTIVE.' AND e.status = '.ENROL_INSTANCE_ENABLED.' AND ue.timestart < '.$now.' AND (ue.timeend = 0 OR ue.timeend > '.$now.')';
}
// Check if we have to include teacher roles from parent contexts
// If yes
if ($coc_config->teacherrolesparent == 1) {
// If we have to check for suspended teachers
if ($coc_config->teacherroleshidesuspended == 1) {
$courseteachers = get_role_users($teacherroles, $context, true, 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname', false, '', '', '', $extrawhere);
}
else {
$courseteachers = get_role_users($teacherroles, $context, true, 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname');
}
}
// If no
else if ($coc_config->teacherrolesparent == 2) {
// If we have to check for suspended teachers
if ($coc_config->teacherroleshidesuspended == 1) {
$courseteachers = get_role_users($teacherroles, $context, false, 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname', false, '', '', '', $extrawhere);
}
else {
$courseteachers = get_role_users($teacherroles, $context, false, 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname');
}
}
// If depending on moodle/course:reviewotherusers capability
else if ($coc_config->teacherrolesparent == 3) {
// If we have to check for suspended teachers
if ($coc_config->teacherroleshidesuspended == 1) {
$courseteachers = get_role_users($teacherroles, $context, has_capability('moodle/course:reviewotherusers', $context), 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname', false, '', '', '', $extrawhere);
}
else {
$courseteachers = get_role_users($teacherroles, $context, has_capability('moodle/course:reviewotherusers', $context), 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname');
}
}
// Should not happen
else {
$courseteachers = get_role_users($teacherroles, $context, true, 'ra.id, u.id, '.$allnames.', r.sortorder', 'u.lastname, u.firstname');
}
}
else {
$courseteachers = array();
}
// Remember course teachers for later use
$c->teachers = $courseteachers;
}
// Teacher filter
if ($coc_config->teachercoursefilter == true) {
// Add all course teacher's names to filter list
if ($coc_config->teachercoursefilter == true) {
foreach ($courseteachers as $ct) {
$filterteachers[$ct->id] = $ct->lastname.', '.$ct->firstname;
}
}
}
// Check if this course should be shown or not
if ($coc_config->enablehidecourses) {
$courses[$c->id]->hidecourse = get_user_preferences('block_course_overview_campus-hidecourse-'.$c->id, 0);
if ($courses[$c->id]->hidecourse == 1) {
$hiddencourses++;
}
}
// Check if this course should show news or not
if ($coc_config->enablecoursenews) {
$courses[$c->id]->hidenews = get_user_preferences('block_course_overview_campus-hidenews-'.$c->id, $coc_config->coursenewsdefault);
}
// Re-sort courses to list courses in which I have a teacher role first if configured - First step: Removing the courses
if ($coc_config->prioritizemyteachedcourses) {
// Check if user is teacher in this course
if (array_key_exists($USER->id, $courseteachers)) {
// Remember the course
$myteachercourses[] = $c;
// Remove the course from the courses array
unset($courses[$c->id]);
}
}
}
// Re-sort courses to list courses in which I have a teacher role first if configured - Last step: Adding the courses again
if ($coc_config->prioritizemyteachedcourses && isset ($myteachercourses) && count($myteachercourses) > 0) {
// Add the courses again at the beginning of the courses array
$courses = $myteachercourses + $courses;
}
// Replace and remember currentterm placeholder with precise term based on my courses
if ($coc_config->termcoursefilter == true && $selectedterm == 'currentterm') {
// Distinguish between term modes
// "Academic year" mode
if ($coc_config->termmode == '1') {
// If term starts on January 1st and there are courses this year, set selected term to this year
if ($coc_config->term1startday == '1' && isset($filterterms[date('Y')])) {
$selectedterm = date('Y');
}
// If term doesn't start on January 1st and current day comes on or after term start day and there are courses this term, set selected term to this year + next year
else if (intval(date('z')) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) && isset($filterterms[date('Y').'-'.(date('Y')+1)])) {
$selectedterm = date('Y').'-'.(date('Y')+1);
}
// If term doesn't start on January 1st and current day comes before term start day and there are courses this term, set selected term to this year + former year
else if (isset($filterterms[(date('Y')-1).'-'.date('Y')])) {
$selectedterm = (date('Y')-1).'-'.date('Y');
}
// Otherwise set selected term to the latest (but not future) term possible
else {
$selectedterm = 'all';
arsort($filterterms);
foreach ($filterterms as $t) {
if ($t != 'other' && $t != 'timeless' && intval(substr($t, 0, 4)) <= intval(date('Y'))) {
$selectedterm = $t;
break;
}
}
}
}
// "Semester" mode
else if ($coc_config->termmode == '2') {
// If current day comes before first term start day and there are courses this term, set selected term to second term of former year
if (intval(date('z')) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) && isset($filterterms[(date('Y')-1).'-2'])) {
$selectedterm = (date('Y')-1).'-2';
}
// If current day comes on or after first term start day but before second term start day and there are courses this term, set selected term to first term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term2startday))) &&
isset($filterterms[date('Y').'-1'])) {
$selectedterm = date('Y').'-1';
}
// If course start date's day comes on or after second term start day and there are courses this term, set selected term to second term of current year
else if (isset($filterterms[date('Y').'-2'])) {
$selectedterm = date('Y').'-2';
}
// Otherwise set selected term to the latest (but not future) term possible
else {
$selectedterm = 'all';
krsort($filterterms);
foreach ($filterterms as $t => $n) {
if ($t != 'other' && $t != 'timeless' && intval(substr($t, 0, 4)) <= intval(date('Y'))) {
$selectedterm = $t;
break;
}
}
}
}
// "Tertial" mode
else if ($coc_config->termmode == '3') {
// If current day comes before first term start day and there are courses this term, set selected term to third term of former year
if (intval(date('z')) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) && isset($filterterms[(date('Y')-1).'-3'])) {
$selectedterm = (date('Y')-1).'-2';
}
// If current day comes on or after first term start day but before second term start day and there are courses this term, set selected term to first term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term2startday))) &&
isset($filterterms[date('Y').'-1'])) {
$selectedterm = date('Y').'-1';
}
// If current day comes on or after second term start day but before third term start day and there are courses this term, set selected term to second term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term2startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term3startday))) &&
isset($filterterms[date('Y').'-2'])) {
$selectedterm = date('Y').'-2';
}
// If course start date's day comes on or after third term start day and there are courses this term, set selected term to third term of current year
else if (isset($filterterms[date('Y').'-3'])) {
$selectedterm = date('Y').'-3';
}
// Otherwise set selected term to the latest (but not future) term possible
else {
$selectedterm = 'all';
krsort($filterterms);
foreach ($filterterms as $t => $n) {
if ($t != 'other' && $t != 'timeless' && intval(substr($t, 0, 4)) <= intval(date('Y'))) {
$selectedterm = $t;
break;
}
}
}
}
// "Trimester" mode
else if ($coc_config->termmode == '4') {
// If current day comes before first term start day and there are courses this term, set selected term to fourth term of former year
if (intval(date('z')) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) && isset($filterterms[(date('Y')-1).'-4'])) {
$selectedterm = (date('Y')-1).'-2';
}
// If current day comes on or after first term start day but before second term start day and there are courses this term, set selected term to first term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term1startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term2startday))) &&
isset($filterterms[date('Y').'-1'])) {
$selectedterm = date('Y').'-1';
}
// If current day comes on or after second term start day but before third term start day and there are courses this term, set selected term to second term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term2startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term3startday))) &&
isset($filterterms[date('Y').'-2'])) {
$selectedterm = date('Y').'-2';
}
// If current day comes on or after third term start day but before fourth term start day and there are courses this term, set selected term to third term of current year
else if (intval(date('z', $c->startdate)) >= intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term3startday))) &&
intval(date('z', $c->startdate)) < intval(date('z', strtotime(date('Y', $c->startdate).'-'.$coc_config->term4startday))) &&
isset($filterterms[date('Y').'-3'])) {
$selectedterm = date('Y').'-3';
}
// If course start date's day comes on or after fourth term start day and there are courses this term, set selected term to fourth term of current year
else if (isset($filterterms[date('Y').'-4'])) {
$selectedterm = date('Y').'-4';
}
// Otherwise set selected term to the latest (but not future) term possible
else {
$selectedterm = 'all';
krsort($filterterms);
foreach ($filterterms as $t => $n) {
if ($t != 'other' && $t != 'timeless' && intval(substr($t, 0, 4)) <= intval(date('Y'))) {
$selectedterm = $t;
break;
}
}
}
}
// This should never happen
else {
print_error('error');
}
// Remember selected term
set_user_preference('block_course_overview_campus-selectedterm', $selectedterm);
}
/********************************************************************************/
/*** GENERATE OUTPUT FOR FILTER ***/
/********************************************************************************/
// Show filter form if any filter is activated and if hidden courses management isn't active
if ((!$coc_config->enablehidecourses || $manage == false) && ($coc_config->categorycoursefilter == true || $coc_config->toplevelcategorycoursefilter == true || $coc_config->termcoursefilter == true || $coc_config->teachercoursefilter == true)) {
// Calculate CSS class for filter divs
$filtercount = 0;
if ($coc_config->termcoursefilter == true) {
$filtercount++;
}
if ($coc_config->teachercoursefilter == true) {
$filtercount++;
}
if ($coc_config->categorycoursefilter == true) {
$filtercount++;
}
if ($coc_config->toplevelcategorycoursefilter == true) {
$filtercount++;
}
if ($filtercount == 1) {
$filterwidth = 'span12 col-md-12'; // class 'span12' is used for Bootstrapbase and will be ignored by Boost
}
else if ($filtercount == 2) {
$filterwidth = 'span6 col-md-6'; // class 'span6' is used for Bootstrapbase and will be ignored by Boost
}
else if ($filtercount == 3) {
$filterwidth = 'span4 col-md-4'; // class 'span4' is used for Bootstrapbase and will be ignored by Boost
}
else if ($filtercount == 4) {
$filterwidth = 'span3 col-md-6 col-lg-3'; // class 'span3' is used for Bootstrapbase and will be ignored by Boost
}
else {
$filterwidth = 'span12 col-md-12'; // class 'span12' is used for Bootstrapbase and will be ignored by Boost
}
// Start section and form
echo '<div id="coc-filterlist" class="container-fluid"><div class="row"><form method="post" action="">';
// Show term filter
if ($coc_config->termcoursefilter == true) {
echo '<div class="coc-filter '.$filterwidth.' m-b-1">';
// Show filter description
if ($coc_config->termcoursefilterdisplayname != '') {
echo '<label for="coc-filterterm">'.format_string($coc_config->termcoursefilterdisplayname).'</label>';
}
// Show filter widget
echo '<select name="coc-term" id="coc-filterterm" class="input-block-level form-control">'; // class 'input-block-level' is used for Bootstrapbase and will be ignored by Boost
// Remember in this variable if selected term was displayed or not
$selectedtermdisplayed = false;
// Sort term filter alphabetically in reverse order
krsort($filterterms);
// Print "All terms" option
if ($selectedterm == 'all') {
echo '<option value="all" selected>'.get_string('all', 'block_course_overview_campus').'</option> ';
$selectedtermdisplayed = true;
}
else {
echo '<option value="all">'.get_string('all', 'block_course_overview_campus').'</option> ';
}
// Print each term in filter as an option item and select selected term
foreach ($filterterms as $t => $n) {
// If iterated term is selected term
if ($selectedterm == $t) {
// Handle "other" term option
if ($selectedterm == 'other') {
echo '<option selected value="other">'.get_string('other', 'block_course_overview_campus').'</option> ';
$selectedtermdisplayed = true;
}
// Handle "timeless" term option
else if ($selectedterm == 'timeless') {
echo '<option selected value="timeless">'.format_string($coc_config->timelesscoursesname).'</option> ';
$selectedtermdisplayed = true;
}
else {
echo '<option selected value="'.$t.'">'.format_string($n).'</option> ';
$selectedtermdisplayed = true;
}
}
// If iterated term isn't selected term
else {
// Handle "other" term option
if ($t == 'other') {
echo '<option value="other">'.get_string('other', 'block_course_overview_campus').'</option> ';
}
// Handle "timeless" term option
else if ($t == 'timeless') {
echo '<option value="timeless">'.format_string($coc_config->timelesscoursesname).'</option> ';
}
else {
echo '<option value="'.$t.'">'.format_string($n).'</option> ';
}
}
}
echo '</select>';
// If selected term couldn't be displayed, select all terms and save the new selection. In this case, no option item is marked as selected, but that's ok as the "all" item is at the top
if (!$selectedtermdisplayed) {
$selectedterm = 'all';
set_user_preference('block_course_overview_campus-selectedterm', $selectedterm);
}
echo '</div>';
}
// Show top level category filter
if ($coc_config->toplevelcategorycoursefilter == true) {
echo '<div class="coc-filter '.$filterwidth.' m-b-1">';
// Show filter description
if ($coc_config->toplevelcategorycoursefilterdisplayname != '') {
echo '<label for="coc-filtertoplevelcategory">'.format_string($coc_config->toplevelcategorycoursefilterdisplayname).'</label>';
}
// Show filter widget
echo '<select name="coc-toplevelcategory" id="coc-filtertoplevelcategory" class="input-block-level form-control">'; // class 'input-block-level' is used for Bootstrapbase and will be ignored by Boost
// Remember in this variable if selected top level category was displayed or not
$selectedtoplevelcategorydisplayed = false;
// Sort top level category filter by category sort order
// Create empty array for sorted categories
$filtertoplevelcategoriessortorder = array();
// Fetch full category information for each category
foreach ($filtertoplevelcategories as $ftl_key => $ftl_value) {
$filtertoplevelcategoriesfullinfo[] = $coursecategories[$ftl_key];
}
// Sort full category information array by sortorder
$success = usort($filtertoplevelcategoriesfullinfo, "block_course_overview_campus_compare_categories");
// If sorting was not successful, return old array
if (!$success) {
return $filtertoplevelcategories;
}
// If sorting was successful, return new array with same data structure like the old one
else {
$filtertoplevelcategories = array();
foreach ($filtertoplevelcategoriesfullinfo as $ftl) {
$filtertoplevelcategories[$ftl->id] = format_string($ftl->name);
}
}
// Print "All categories" option
if ($selectedtoplevelcategory == 'all') {
echo '<option value="all" selected>'.get_string('all', 'block_course_overview_campus').'</option> ';
$selectedtoplevelcategorydisplayed = true;
}
else {
echo '<option value="all">'.get_string('all', 'block_course_overview_campus').'</option> ';
}
// Print each top level category in filter as an option item and select selected top level category
foreach ($filtertoplevelcategories as $value => $cat) {
// If iterated top level category is selected top level category
if ($selectedtoplevelcategory == $value) {
echo '<option selected value="'.$value.'">'.$cat.'</option> ';
$selectedtoplevelcategorydisplayed = true;
}
// If iterated top level category isn't selected top level category
else {
echo '<option value="'.$value.'">'.$cat.'</option> ';
}
}
echo '</select>';
// If selected top level category couldn't be displayed, select all categories and save the new selection. In this case, no option item is marked as selected, but that's ok as the "all" item is at the top
if (!$selectedtoplevelcategorydisplayed) {
$selectedtoplevelcategory = 'all';
set_user_preference('block_course_overview_campus-selectedtoplevelcategory', $selectedtoplevelcategory);
}
echo '</div>';
}
// Show parent category filter
if ($coc_config->categorycoursefilter == true) {
echo '<div class="coc-filter '.$filterwidth.' m-b-1">';
// Show filter description
if ($coc_config->categorycoursefilterdisplayname != '') {
echo '<label for="coc-filtercategory">'.format_string($coc_config->categorycoursefilterdisplayname).'</label>';
}
// Show filter widget
echo '<select name="coc-category" id="coc-filtercategory" class="input-block-level form-control">'; // class 'input-block-level' is used for Bootstrapbase and will be ignored by Boost
// Remember in this variable if selected parent category was displayed or not
$selectedcategorydisplayed = false;
// Sort parent category filter alphabetically
natcasesort($filtercategories);
// Print "All categories" option
if ($selectedcategory == 'all') {
echo '<option value="all" selected>'.get_string('all', 'block_course_overview_campus').'</option> ';
$selectedcategorydisplayed = true;
}
else {
echo '<option value="all">'.get_string('all', 'block_course_overview_campus').'</option> ';
}
// Print each parent category in filter as an option item and select selected parent category
foreach ($filtercategories as $value => $cat) {
// If iterated parent category is selected parent category
if ($selectedcategory == $value) {
echo '<option selected value="'.$value.'">'.$cat.'</option> ';
$selectedcategorydisplayed = true;
}
// If iterated parent category isn't selected parent category
else {
echo '<option value="'.$value.'">'.$cat.'</option> ';
}
}
echo '</select>';
// If selected parent category couldn't be displayed, select all categories and save the new selection. In this case, no option item is marked as selected, but that's ok as the "all" item is at the top
if (!$selectedcategorydisplayed) {
$selectedcategory = 'all';
set_user_preference('block_course_overview_campus-selectedcategory', $selectedcategory);
}
echo '</div>';
}
// Show teacher filter
if ($coc_config->teachercoursefilter == true) {
echo '<div class="coc-filter '.$filterwidth.' m-b-1">';
// Show filter description
if ($coc_config->teachercoursefilterdisplayname != '') {
echo '<label for="coc-filterteacher">'.format_string($coc_config->teachercoursefilterdisplayname).'</label>';
}
// Show filter widget
echo '<select name="coc-teacher" id="coc-filterteacher" class="input-block-level form-control">'; // class 'input-block-level' is used for Bootstrapbase and will be ignored by Boost
// Remember in this variable if selected teacher was displayed or not
$selectedteacherdisplayed = false;
// Sort teacher filter alphabetically
natcasesort($filterteachers);
// Print "All teachers" option
if ($selectedteacher == 'all') {
echo '<option value="all" selected>'.get_string('all', 'block_course_overview_campus').'</option> ';
$selectedteacherdisplayed = true;
}
else {
echo '<option value="all">'.get_string('all', 'block_course_overview_campus').'</option> ';
}
// Print each teacher in filter as an option item and select selected teacher
foreach ($filterteachers as $id => $t) {
// If iterated teacher is selected teacher
if ($selectedteacher == $id) {
echo '<option selected value="'.$id.'">'.$t.'</option> ';
$selectedteacherdisplayed = true;
}
else {
echo '<option value="'.$id.'">'.$t.'</option> ';
}
}
echo '</select>';
// If selected teacher couldn't be displayed, select all teachers and save the new selection. In this case, no option item is marked as selected, but that's ok as the "all" item is at the top
if (!$selectedteacherdisplayed) {
$selectedteacher = 'all';
set_user_preference('block_course_overview_campus-selectedteacher', $selectedteacher);
}
echo '</div>';
}
// End section and form
echo '</form></div></div>';
// Show submit button for Non-JavaScript interaction
echo '<div id="coc-filtersubmit" class="container-fluid m-b-1"><div class="row"><input type="submit" value="'.get_string('submitfilter', 'block_course_overview_campus').'" class="btn btn-primary" /></div></div>';
}