forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
3302 lines (2924 loc) · 127 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
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/>.
/**
* Calendar extension
*
* @package core_calendar
* @copyright 2004 Greek School Network (http://www.sch.gr), Jon Papaioannou,
* Avgoustos Tsinakos
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
/**
* These are read by the administration component to provide default values
*/
/**
* CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD - default value of upcoming event preference
*/
define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
/**
* CALENDAR_DEFAULT_UPCOMING_MAXEVENTS - default value to display the maximum number of upcoming event
*/
define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
/**
* CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday
*/
define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1);
// This is a packed bitfield: day X is "weekend" if $field & (1 << X) is true
// Default value = 65 = 64 + 1 = 2^6 + 2^0 = Saturday & Sunday
/**
* CALENDAR_DEFAULT_WEEKEND - default value for weekend (Saturday & Sunday)
*/
define('CALENDAR_DEFAULT_WEEKEND', 65);
/**
* CALENDAR_URL - path to calendar's folder
*/
define('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
/**
* CALENDAR_TF_24 - Calendar time in 24 hours format
*/
define('CALENDAR_TF_24', '%H:%M');
/**
* CALENDAR_TF_12 - Calendar time in 12 hours format
*/
define('CALENDAR_TF_12', '%I:%M %p');
/**
* CALENDAR_EVENT_GLOBAL - Global calendar event types
*/
define('CALENDAR_EVENT_GLOBAL', 1);
/**
* CALENDAR_EVENT_COURSE - Course calendar event types
*/
define('CALENDAR_EVENT_COURSE', 2);
/**
* CALENDAR_EVENT_GROUP - group calendar event types
*/
define('CALENDAR_EVENT_GROUP', 4);
/**
* CALENDAR_EVENT_USER - user calendar event types
*/
define('CALENDAR_EVENT_USER', 8);
/**
* CALENDAR_IMPORT_FROM_FILE - import the calendar from a file
*/
define('CALENDAR_IMPORT_FROM_FILE', 0);
/**
* CALENDAR_IMPORT_FROM_URL - import the calendar from a URL
*/
define('CALENDAR_IMPORT_FROM_URL', 1);
/**
* CALENDAR_IMPORT_EVENT_UPDATED - imported event was updated
*/
define('CALENDAR_IMPORT_EVENT_UPDATED', 1);
/**
* CALENDAR_IMPORT_EVENT_INSERTED - imported event was added by insert
*/
define('CALENDAR_IMPORT_EVENT_INSERTED', 2);
/**
* CALENDAR_SUBSCRIPTION_UPDATE - Used to represent update action for subscriptions in various forms.
*/
define('CALENDAR_SUBSCRIPTION_UPDATE', 1);
/**
* CALENDAR_SUBSCRIPTION_REMOVE - Used to represent remove action for subscriptions in various forms.
*/
define('CALENDAR_SUBSCRIPTION_REMOVE', 2);
/**
* Return the days of the week
*
* @return array array of days
*/
function calendar_get_days() {
$calendartype = \core_calendar\type_factory::get_calendar_instance();
return $calendartype->get_weekdays();
}
/**
* Get the subscription from a given id
*
* @since Moodle 2.5
* @param int $id id of the subscription
* @return stdClass Subscription record from DB
* @throws moodle_exception for an invalid id
*/
function calendar_get_subscription($id) {
global $DB;
$cache = cache::make('core', 'calendar_subscriptions');
$subscription = $cache->get($id);
if (empty($subscription)) {
$subscription = $DB->get_record('event_subscriptions', array('id' => $id), '*', MUST_EXIST);
// cache the data.
$cache->set($id, $subscription);
}
return $subscription;
}
/**
* Gets the first day of the week
*
* Used to be define('CALENDAR_STARTING_WEEKDAY', blah);
*
* @return int
*/
function calendar_get_starting_weekday() {
$calendartype = \core_calendar\type_factory::get_calendar_instance();
return $calendartype->get_starting_weekday();
}
/**
* Generates the HTML for a miniature calendar
*
* @param array $courses list of course to list events from
* @param array $groups list of group
* @param array $users user's info
* @param int|bool $calmonth calendar month in numeric, default is set to false
* @param int|bool $calyear calendar month in numeric, default is set to false
* @param string|bool $placement the place/page the calendar is set to appear - passed on the the controls function
* @param int|bool $courseid id of the course the calendar is displayed on - passed on the the controls function
* @param int $time the unixtimestamp representing the date we want to view, this is used instead of $calmonth
* and $calyear to support multiple calendars
* @return string $content return html table for mini calendar
*/
function calendar_get_mini($courses, $groups, $users, $calmonth = false, $calyear = false, $placement = false,
$courseid = false, $time = 0) {
global $CFG, $OUTPUT, $PAGE;
// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$display = new stdClass;
// Assume we are not displaying this month for now.
$display->thismonth = false;
$content = '';
// Do this check for backwards compatibility. The core should be passing a timestamp rather than month and year.
// If a month and year are passed they will be in Gregorian.
if (!empty($calmonth) && !empty($calyear)) {
// Ensure it is a valid date, else we will just set it to the current timestamp.
if (checkdate($calmonth, 1, $calyear)) {
$time = make_timestamp($calyear, $calmonth, 1);
} else {
$time = time();
}
$date = usergetdate($time);
if ($calmonth == $date['mon'] && $calyear == $date['year']) {
$display->thismonth = true;
}
// We can overwrite date now with the date used by the calendar type, if it is not Gregorian, otherwise
// there is no need as it is already in Gregorian.
if ($calendartype->get_name() != 'gregorian') {
$date = $calendartype->timestamp_to_date_array($time);
}
} else if (!empty($time)) {
// Get the specified date in the calendar type being used.
$date = $calendartype->timestamp_to_date_array($time);
$thisdate = $calendartype->timestamp_to_date_array(time());
if ($date['month'] == $thisdate['month'] && $date['year'] == $thisdate['year']) {
$display->thismonth = true;
// If we are the current month we want to set the date to the current date, not the start of the month.
$date = $thisdate;
}
} else {
// Get the current date in the calendar type being used.
$time = time();
$date = $calendartype->timestamp_to_date_array($time);
$display->thismonth = true;
}
list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display.
// Get Gregorian date for the start of the month.
$gregoriandate = $calendartype->convert_to_gregorian($date['year'], $date['mon'], 1);
// Store the gregorian date values to be used later.
list($gy, $gm, $gd, $gh, $gmin) = array($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
$gregoriandate['hour'], $gregoriandate['minute']);
// Get the max number of days in this month for this calendar type.
$display->maxdays = calendar_days_in_month($m, $y);
// Get the starting week day for this month.
$startwday = dayofweek(1, $m, $y);
// Get the days in a week.
$daynames = calendar_get_days();
// Store the number of days in a week.
$numberofdaysinweek = $calendartype->get_num_weekdays();
// Set the min and max weekday.
$display->minwday = calendar_get_starting_weekday();
$display->maxwday = $display->minwday + ($numberofdaysinweek - 1);
// These are used for DB queries, so we want unixtime, so we need to use Gregorian dates.
$display->tstart = make_timestamp($gy, $gm, $gd, $gh, $gmin, 0);
$display->tend = $display->tstart + ($display->maxdays * DAYSECS) - 1;
// Align the starting weekday to fall in our display range
// This is simple, not foolproof.
if ($startwday < $display->minwday) {
$startwday += $numberofdaysinweek;
}
// Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
$events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses);
// Set event course class for course events
if (!empty($events)) {
foreach ($events as $eventid => $event) {
if (!empty($event->modulename)) {
$cm = get_coursemodule_from_instance($event->modulename, $event->instance);
if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
unset($events[$eventid]);
}
}
}
}
// This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
// possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
// will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
// arguments to this function.
$hrefparams = array();
if(!empty($courses)) {
$courses = array_diff($courses, array(SITEID));
if(count($courses) == 1) {
$hrefparams['course'] = reset($courses);
}
}
// We want to have easy access by day, since the display is on a per-day basis.
calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
// Accessibility: added summary and <abbr> elements.
$summary = get_string('calendarheading', 'calendar', userdate($display->tstart, get_string('strftimemonthyear')));
// Begin table.
$content .= '<table class="minicalendar calendartable" summary="' . $summary . '">';
if (($placement !== false) && ($courseid !== false)) {
$content .= '<caption>'. calendar_top_controls($placement, array('id' => $courseid, 'time' => $time)) .'</caption>';
}
$content .= '<tr class="weekdays">'; // Header row: day names
// Print out the names of the weekdays.
for ($i = $display->minwday; $i <= $display->maxwday; ++$i) {
$pos = $i % $numberofdaysinweek;
$content .= '<th scope="col"><abbr title="'. $daynames[$pos]['fullname'] .'">'.
$daynames[$pos]['shortname'] ."</abbr></th>\n";
}
$content .= '</tr><tr>'; // End of day names; prepare for day numbers
// For the table display. $week is the row; $dayweek is the column.
$dayweek = $startwday;
// Paddding (the first week may have blank days in the beginning)
for($i = $display->minwday; $i < $startwday; ++$i) {
$content .= '<td class="dayblank"> </td>'."\n";
}
$weekend = CALENDAR_DEFAULT_WEEKEND;
if (isset($CFG->calendar_weekend)) {
$weekend = intval($CFG->calendar_weekend);
}
// Now display all the calendar
$daytime = strtotime('-1 day', $display->tstart);
for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
$cellattributes = array();
$daytime = strtotime('+1 day', $daytime);
if($dayweek > $display->maxwday) {
// We need to change week (table row)
$content .= '</tr><tr>';
$dayweek = $display->minwday;
}
// Reset vars.
if ($weekend & (1 << ($dayweek % $numberofdaysinweek))) {
// Weekend. This is true no matter what the exact range is.
$class = 'weekend day';
} else {
// Normal working day.
$class = 'day';
}
if (isset($eventsbyday[$day])) {
// There is at least one event on this day.
$class .= ' hasevent';
$hrefparams['view'] = 'day';
$dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL . 'view.php', $hrefparams), 0, 0, 0, $daytime);
$popupcontent = '';
foreach($eventsbyday[$day] as $eventid) {
if (!isset($events[$eventid])) {
continue;
}
$event = new calendar_event($events[$eventid]);
$popupalt = '';
$component = 'moodle';
if (!empty($event->modulename)) {
$popupicon = 'icon';
$popupalt = $event->modulename;
$component = $event->modulename;
} else if ($event->courseid == SITEID) { // Site event.
$popupicon = 'i/siteevent';
} else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event.
$popupicon = 'i/courseevent';
} else if ($event->groupid) { // Group event.
$popupicon = 'i/groupevent';
} else { // Must be a user event.
$popupicon = 'i/userevent';
}
$dayhref->set_anchor('event_'.$event->id);
$popupcontent .= html_writer::start_tag('div');
$popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
$name = format_string($event->name, true);
// Show ical source if needed.
if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
$a = new stdClass();
$a->name = $name;
$a->source = $event->subscription->name;
$name = get_string('namewithsource', 'calendar', $a);
}
$popupcontent .= html_writer::link($dayhref, $name);
$popupcontent .= html_writer::end_tag('div');
}
if ($display->thismonth && $day == $d) {
$popupdata = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
} else {
$popupdata = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
}
$cellattributes = array_merge($cellattributes, $popupdata);
// Class and cell content
if(isset($typesbyday[$day]['startglobal'])) {
$class .= ' calendar_event_global';
} else if(isset($typesbyday[$day]['startcourse'])) {
$class .= ' calendar_event_course';
} else if(isset($typesbyday[$day]['startgroup'])) {
$class .= ' calendar_event_group';
} else if(isset($typesbyday[$day]['startuser'])) {
$class .= ' calendar_event_user';
}
$cell = html_writer::link($dayhref, $day);
} else {
$cell = $day;
}
$durationclass = false;
if (isset($typesbyday[$day]['durationglobal'])) {
$durationclass = ' duration_global';
} else if(isset($typesbyday[$day]['durationcourse'])) {
$durationclass = ' duration_course';
} else if(isset($typesbyday[$day]['durationgroup'])) {
$durationclass = ' duration_group';
} else if(isset($typesbyday[$day]['durationuser'])) {
$durationclass = ' duration_user';
}
if ($durationclass) {
$class .= ' duration '.$durationclass;
}
// If event has a class set then add it to the table day <td> tag
// Note: only one colour for minicalendar
if(isset($eventsbyday[$day])) {
foreach($eventsbyday[$day] as $eventid) {
if (!isset($events[$eventid])) {
continue;
}
$event = $events[$eventid];
if (!empty($event->class)) {
$class .= ' '.$event->class;
}
break;
}
}
if ($display->thismonth && $day == $d) {
// The current cell is for today - add appropriate classes and additional information for styling.
$class .= ' today';
$today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort'));
if (!isset($eventsbyday[$day])) {
$class .= ' eventnone';
$popupdata = calendar_get_popup(true, false);
$cellattributes = array_merge($cellattributes, $popupdata);
$cell = html_writer::link('#', $day);
}
$cell = get_accesshide($today . ' ') . $cell;
}
// Just display it
$cellattributes['class'] = $class;
$content .= html_writer::tag('td', $cell, $cellattributes);
}
// Paddding (the last week may have blank days at the end)
for($i = $dayweek; $i <= $display->maxwday; ++$i) {
$content .= '<td class="dayblank"> </td>';
}
$content .= '</tr>'; // Last row ends
$content .= '</table>'; // Tabular display of days ends
static $jsincluded = false;
if (!$jsincluded) {
$PAGE->requires->yui_module('moodle-calendar-info', 'Y.M.core_calendar.info.init');
$jsincluded = true;
}
return $content;
}
/**
* Gets the calendar popup
*
* It called at multiple points in from calendar_get_mini.
* Copied and modified from calendar_get_mini.
*
* @param bool $is_today false except when called on the current day.
* @param mixed $event_timestart $events[$eventid]->timestart, OR false if there are no events.
* @param string $popupcontent content for the popup window/layout.
* @return string eventid for the calendar_tooltip popup window/layout.
*/
function calendar_get_popup($today = false, $timestart, $popupcontent = '') {
global $PAGE;
$popupcaption = '';
if ($today) {
$popupcaption = get_string('today', 'calendar') . ' ';
}
if (false === $timestart) {
$popupcaption .= userdate(time(), get_string('strftimedayshort'));
$popupcontent = get_string('eventnone', 'calendar');
} else {
$popupcaption .= get_string('eventsfor', 'calendar', userdate($timestart, get_string('strftimedayshort')));
}
return array(
'data-core_calendar-title' => $popupcaption,
'data-core_calendar-popupcontent' => $popupcontent,
);
}
/**
* Gets the calendar upcoming event
*
* @param array $courses array of courses
* @param array|int|bool $groups array of groups, group id or boolean for all/no group events
* @param array|int|bool $users array of users, user id or boolean for all/no user events
* @param int $daysinfuture number of days in the future we 'll look
* @param int $maxevents maximum number of events
* @param int $fromtime start time
* @return array $output array of upcoming events
*/
function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
global $CFG, $COURSE, $DB;
$display = new stdClass;
$display->range = $daysinfuture; // How many days in the future we 'll look
$display->maxevents = $maxevents;
$output = array();
// Prepare "course caching", since it may save us a lot of queries
$coursecache = array();
$processed = 0;
$now = time(); // We 'll need this later
$usermidnighttoday = usergetmidnight($now);
if ($fromtime) {
$display->tstart = $fromtime;
} else {
$display->tstart = $usermidnighttoday;
}
// This works correctly with respect to the user's DST, but it is accurate
// only because $fromtime is always the exact midnight of some day!
$display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1;
// Get the events matching our criteria
$events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses);
// This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
// possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
// will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
// arguments to this function.
$hrefparams = array();
if(!empty($courses)) {
$courses = array_diff($courses, array(SITEID));
if(count($courses) == 1) {
$hrefparams['course'] = reset($courses);
}
}
if ($events !== false) {
$modinfo = get_fast_modinfo($COURSE);
foreach($events as $event) {
if (!empty($event->modulename)) {
if ($event->courseid == $COURSE->id) {
if (isset($modinfo->instances[$event->modulename][$event->instance])) {
$cm = $modinfo->instances[$event->modulename][$event->instance];
if (!$cm->uservisible) {
continue;
}
}
} else {
if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) {
continue;
}
if (!\core_availability\info_module::is_user_visible($cm, 0, false)) {
continue;
}
}
}
if ($processed >= $display->maxevents) {
break;
}
$event->time = calendar_format_event_time($event, $now, $hrefparams);
$output[] = $event;
++$processed;
}
}
return $output;
}
/**
* Get a HTML link to a course.
*
* @param int $courseid the course id
* @return string a link to the course (as HTML); empty if the course id is invalid
*/
function calendar_get_courselink($courseid) {
if (!$courseid) {
return '';
}
calendar_get_course_cached($coursecache, $courseid);
$context = context_course::instance($courseid);
$fullname = format_string($coursecache[$courseid]->fullname, true, array('context' => $context));
$url = new moodle_url('/course/view.php', array('id' => $courseid));
$link = html_writer::link($url, $fullname);
return $link;
}
/**
* Add calendar event metadata
*
* @param stdClass $event event info
* @return stdClass $event metadata
*/
function calendar_add_event_metadata($event) {
global $CFG, $OUTPUT;
//Support multilang in event->name
$event->name = format_string($event->name,true);
if(!empty($event->modulename)) { // Activity event
// The module name is set. I will assume that it has to be displayed, and
// also that it is an automatically-generated event. And of course that the
// fields for get_coursemodule_from_instance are set correctly.
$module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance);
if ($module === false) {
return;
}
$modulename = get_string('modulename', $event->modulename);
if (get_string_manager()->string_exists($event->eventtype, $event->modulename)) {
// will be used as alt text if the event icon
$eventtype = get_string($event->eventtype, $event->modulename);
} else {
$eventtype = '';
}
$icon = $OUTPUT->pix_url('icon', $event->modulename) . '';
$event->icon = '<img src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" class="icon" />';
$event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
$event->courselink = calendar_get_courselink($module->course);
$event->cmid = $module->id;
} else if($event->courseid == SITEID) { // Site event
$event->icon = '<img src="'.$OUTPUT->pix_url('i/siteevent') . '" alt="'.get_string('globalevent', 'calendar').'" class="icon" />';
$event->cssclass = 'calendar_event_global';
} else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
$event->icon = '<img src="'.$OUTPUT->pix_url('i/courseevent') . '" alt="'.get_string('courseevent', 'calendar').'" class="icon" />';
$event->courselink = calendar_get_courselink($event->courseid);
$event->cssclass = 'calendar_event_course';
} else if ($event->groupid) { // Group event
if ($group = calendar_get_group_cached($event->groupid)) {
$groupname = format_string($group->name, true, context_course::instance($group->courseid));
} else {
$groupname = '';
}
$event->icon = html_writer::empty_tag('image', array('src' => $OUTPUT->pix_url('i/groupevent'),
'alt' => get_string('groupevent', 'calendar'), 'title' => $groupname, 'class' => 'icon'));
$event->courselink = calendar_get_courselink($event->courseid) . ', ' . $groupname;
$event->cssclass = 'calendar_event_group';
} else if($event->userid) { // User event
$event->icon = '<img src="'.$OUTPUT->pix_url('i/userevent') . '" alt="'.get_string('userevent', 'calendar').'" class="icon" />';
$event->cssclass = 'calendar_event_user';
}
return $event;
}
/**
* Get calendar events
*
* @param int $tstart Start time of time range for events
* @param int $tend End time of time range for events
* @param array|int|boolean $users array of users, user id or boolean for all/no user events
* @param array|int|boolean $groups array of groups, group id or boolean for all/no group events
* @param array|int|boolean $courses array of courses, course id or boolean for all/no course events
* @param boolean $withduration whether only events starting within time range selected
* or events in progress/already started selected as well
* @param boolean $ignorehidden whether to select only visible events or all events
* @return array $events of selected events or an empty array if there aren't any (or there was an error)
*/
function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
global $DB;
$whereclause = '';
$params = array();
// Quick test
if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
return array();
}
if ((is_array($users) && !empty($users)) or is_numeric($users)) {
// Events from a number of users
if(!empty($whereclause)) $whereclause .= ' OR';
list($insqlusers, $inparamsusers) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
$whereclause .= " (userid $insqlusers AND courseid = 0 AND groupid = 0)";
$params = array_merge($params, $inparamsusers);
} else if($users === true) {
// Events from ALL users
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)';
} else if($users === false) {
// No user at all, do nothing
}
if ((is_array($groups) && !empty($groups)) or is_numeric($groups)) {
// Events from a number of groups
if(!empty($whereclause)) $whereclause .= ' OR';
list($insqlgroups, $inparamsgroups) = $DB->get_in_or_equal($groups, SQL_PARAMS_NAMED);
$whereclause .= " groupid $insqlgroups ";
$params = array_merge($params, $inparamsgroups);
} else if($groups === true) {
// Events from ALL groups
if(!empty($whereclause)) $whereclause .= ' OR ';
$whereclause .= ' groupid != 0';
}
// boolean false (no groups at all): we don't need to do anything
if ((is_array($courses) && !empty($courses)) or is_numeric($courses)) {
if(!empty($whereclause)) $whereclause .= ' OR';
list($insqlcourses, $inparamscourses) = $DB->get_in_or_equal($courses, SQL_PARAMS_NAMED);
$whereclause .= " (groupid = 0 AND courseid $insqlcourses)";
$params = array_merge($params, $inparamscourses);
} else if ($courses === true) {
// Events from ALL courses
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' (groupid = 0 AND courseid != 0)';
}
// Security check: if, by now, we have NOTHING in $whereclause, then it means
// that NO event-selecting clauses were defined. Thus, we won't be returning ANY
// events no matter what. Allowing the code to proceed might return a completely
// valid query with only time constraints, thus selecting ALL events in that time frame!
if(empty($whereclause)) {
return array();
}
if($withduration) {
$timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend;
}
else {
$timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend;
}
if(!empty($whereclause)) {
// We have additional constraints
$whereclause = $timeclause.' AND ('.$whereclause.')';
}
else {
// Just basic time filtering
$whereclause = $timeclause;
}
if ($ignorehidden) {
$whereclause .= ' AND visible = 1';
}
$events = $DB->get_records_select('event', $whereclause, $params, 'timestart');
if ($events === false) {
$events = array();
}
return $events;
}
/** Get calendar events by id
*
* @since Moodle 2.5
* @param array $eventids list of event ids
* @return array Array of event entries, empty array if nothing found
*/
function calendar_get_events_by_id($eventids) {
global $DB;
if (!is_array($eventids) || empty($eventids)) {
return array();
}
list($wheresql, $params) = $DB->get_in_or_equal($eventids);
$wheresql = "id $wheresql";
return $DB->get_records_select('event', $wheresql, $params);
}
/**
* Get control options for Calendar
*
* @param string $type of calendar
* @param array $data calendar information
* @return string $content return available control for the calender in html
*/
function calendar_top_controls($type, $data) {
global $PAGE, $OUTPUT;
// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$content = '';
// Ensure course id passed if relevant.
$courseid = '';
if (!empty($data['id'])) {
$courseid = '&course='.$data['id'];
}
// If we are passing a month and year then we need to convert this to a timestamp to
// support multiple calendars. No where in core should these be passed, this logic
// here is for third party plugins that may use this function.
if (!empty($data['m']) && !empty($date['y'])) {
if (!isset($data['d'])) {
$data['d'] = 1;
}
if (!checkdate($data['m'], $data['d'], $data['y'])) {
$time = time();
} else {
$time = make_timestamp($data['y'], $data['m'], $data['d']);
}
} else if (!empty($data['time'])) {
$time = $data['time'];
} else {
$time = time();
}
// Get the date for the calendar type.
$date = $calendartype->timestamp_to_date_array($time);
$urlbase = $PAGE->url;
// We need to get the previous and next months in certain cases.
if ($type == 'frontpage' || $type == 'course' || $type == 'month') {
$prevmonth = calendar_sub_month($date['mon'], $date['year']);
$prevmonthtime = $calendartype->convert_to_gregorian($prevmonth[1], $prevmonth[0], 1);
$prevmonthtime = make_timestamp($prevmonthtime['year'], $prevmonthtime['month'], $prevmonthtime['day'],
$prevmonthtime['hour'], $prevmonthtime['minute']);
$nextmonth = calendar_add_month($date['mon'], $date['year']);
$nextmonthtime = $calendartype->convert_to_gregorian($nextmonth[1], $nextmonth[0], 1);
$nextmonthtime = make_timestamp($nextmonthtime['year'], $nextmonthtime['month'], $nextmonthtime['day'],
$nextmonthtime['hour'], $nextmonthtime['minute']);
}
switch ($type) {
case 'frontpage':
$prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), $urlbase, false, false, false, true, $prevmonthtime);
$nextlink = calendar_get_link_next(get_string('monthnext', 'access'), $urlbase, false, false, false, true, $nextmonthtime);
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time);
if (!empty($data['id'])) {
$calendarlink->param('course', $data['id']);
}
if (right_to_left()) {
$left = $nextlink;
$right = $prevlink;
} else {
$left = $prevlink;
$right = $nextlink;
}
$content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
$content .= $left.'<span class="hide"> | </span>';
$content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current'));
$content .= '<span class="hide"> | </span>'. $right;
$content .= "<span class=\"clearer\"><!-- --></span>\n";
$content .= html_writer::end_tag('div');
break;
case 'course':
$prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), $urlbase, false, false, false, true, $prevmonthtime);
$nextlink = calendar_get_link_next(get_string('monthnext', 'access'), $urlbase, false, false, false, true, $nextmonthtime);
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time);
if (!empty($data['id'])) {
$calendarlink->param('course', $data['id']);
}
if (right_to_left()) {
$left = $nextlink;
$right = $prevlink;
} else {
$left = $prevlink;
$right = $nextlink;
}
$content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
$content .= $left.'<span class="hide"> | </span>';
$content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current'));
$content .= '<span class="hide"> | </span>'. $right;
$content .= "<span class=\"clearer\"><!-- --></span>";
$content .= html_writer::end_tag('div');
break;
case 'upcoming':
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'upcoming')), false, false, false, $time);
if (!empty($data['id'])) {
$calendarlink->param('course', $data['id']);
}
$calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
$content .= html_writer::tag('div', $calendarlink, array('class'=>'centered'));
break;
case 'display':
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view' => 'month')), false, false, false, $time);
if (!empty($data['id'])) {
$calendarlink->param('course', $data['id']);
}
$calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
$content .= html_writer::tag('h3', $calendarlink);
break;
case 'month':
$prevlink = calendar_get_link_previous(userdate($prevmonthtime, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', false, false, false, false, $prevmonthtime);
$nextlink = calendar_get_link_next(userdate($nextmonthtime, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', false, false, false, false, $nextmonthtime);
if (right_to_left()) {
$left = $nextlink;
$right = $prevlink;
} else {
$left = $prevlink;
$right = $nextlink;
}
$content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
$content .= $left . '<span class="hide"> | </span>';
$content .= $OUTPUT->heading(userdate($time, get_string('strftimemonthyear')), 2, 'current');
$content .= '<span class="hide"> | </span>' . $right;
$content .= '<span class="clearer"><!-- --></span>';
$content .= html_writer::end_tag('div')."\n";
break;
case 'day':
$days = calendar_get_days();
$prevtimestamp = strtotime('-1 day', $time);
$nexttimestamp = strtotime('+1 day', $time);
$prevdate = $calendartype->timestamp_to_date_array($prevtimestamp);
$nextdate = $calendartype->timestamp_to_date_array($nexttimestamp);
$prevname = $days[$prevdate['wday']]['fullname'];
$nextname = $days[$nextdate['wday']]['fullname'];
$prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&', false, false, false, false, $prevtimestamp);
$nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&', false, false, false, false, $nexttimestamp);
if (right_to_left()) {
$left = $nextlink;
$right = $prevlink;
} else {
$left = $prevlink;
$right = $nextlink;
}
$content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
$content .= $left;
$content .= '<span class="hide"> | </span><span class="current">'.userdate($time, get_string('strftimedaydate')).'</span>';
$content .= '<span class="hide"> | </span>'. $right;
$content .= "<span class=\"clearer\"><!-- --></span>";
$content .= html_writer::end_tag('div')."\n";
break;
}
return $content;
}
/**
* Formats a filter control element.
*
* @param moodle_url $url of the filter
* @param int $type constant defining the type filter
* @return string html content of the element
*/
function calendar_filter_controls_element(moodle_url $url, $type) {
global $OUTPUT;
switch ($type) {
case CALENDAR_EVENT_GLOBAL:
$typeforhumans = 'global';
$class = 'calendar_event_global';
break;
case CALENDAR_EVENT_COURSE:
$typeforhumans = 'course';
$class = 'calendar_event_course';
break;
case CALENDAR_EVENT_GROUP:
$typeforhumans = 'groups';
$class = 'calendar_event_group';
break;
case CALENDAR_EVENT_USER:
$typeforhumans = 'user';
$class = 'calendar_event_user';
break;
}
if (calendar_show_event_type($type)) {
$icon = $OUTPUT->pix_icon('t/hide', get_string('hide'));
$str = get_string('hide'.$typeforhumans.'events', 'calendar');
} else {
$icon = $OUTPUT->pix_icon('t/show', get_string('show'));
$str = get_string('show'.$typeforhumans.'events', 'calendar');