forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
2637 lines (2326 loc) · 106 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
*/
/**
* 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_STARTING_WEEKDAY - has since been deprecated please call calendar_get_starting_weekday() instead
*
* @deprecated Moodle 2.0 MDL-24284- please do not use this function any more.
* @todo MDL-31132 This will be deleted in Moodle 2.3.
* @see calendar_get_starting_weekday()
*/
define('CALENDAR_STARTING_WEEKDAY', CALENDAR_DEFAULT_STARTING_WEEKDAY);
/**
* Return the days of the week
*
* @return array array of days
*/
function calendar_get_days() {
return array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
}
/**
* Gets the first day of the week
*
* Used to be define('CALENDAR_STARTING_WEEKDAY', blah);
*
* @return int
*/
function calendar_get_starting_weekday() {
global $CFG;
if (isset($CFG->calendar_startwday)) {
$firstday = $CFG->calendar_startwday;
} else {
$firstday = get_string('firstdayofweek', 'langconfig');
}
if(!is_numeric($firstday)) {
return CALENDAR_DEFAULT_STARTING_WEEKDAY;
} else {
return intval($firstday) % 7;
}
}
/**
* Generates the HTML for a miniature calendar
*
* @param array $courses list of course
* @param array $groups list of group
* @param array $users user's info
* @param int $cal_month calendar month in numeric, default is set to false
* @param int $cal_year calendar month in numeric, default is set to false
* @return string $content return html table for mini calendar
*/
function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
global $CFG, $USER, $OUTPUT;
$display = new stdClass;
$display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
$display->maxwday = $display->minwday + 6;
$content = '';
if(!empty($cal_month) && !empty($cal_year)) {
$thisdate = usergetdate(time()); // Date and time the user sees at his location
if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) {
// Navigated to this month
$date = $thisdate;
$display->thismonth = true;
} else {
// Navigated to other month, let's do a nice trick and save us a lot of work...
if(!checkdate($cal_month, 1, $cal_year)) {
$date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
$display->thismonth = true;
} else {
$date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
$display->thismonth = false;
}
}
} else {
$date = usergetdate(time()); // Date and time the user sees at his location
$display->thismonth = true;
}
// Fill in the variables we 're going to use, nice and tidy
list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
$display->maxdays = calendar_days_in_month($m, $y);
if (get_user_timezone_offset() < 99) {
// We 'll keep these values as GMT here, and offset them when the time comes to query the db
$display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
$display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
} else {
// no timezone info specified
$display->tstart = mktime(0, 0, 0, $m, 1, $y);
$display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
}
$startwday = dayofweek(1, $m, $y);
// Align the starting weekday to fall in our display range
// This is simple, not foolproof.
if($startwday < $display->minwday) {
$startwday += 7;
}
// TODO: THIS IS TEMPORARY CODE!
// [pj] I was just reading through this and realized that I when writing this code I was probably
// asking for trouble, as all these time manipulations seem to be unnecessary and a simple
// make_timestamp would accomplish the same thing. So here goes a test:
//$test_start = make_timestamp($y, $m, 1);
//$test_end = make_timestamp($y, $m, $display->maxdays, 23, 59, 59);
//if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) {
//notify('Failed assertion in calendar/lib.php line 126; display->tstart = '.$display->tstart.', dst_offset = '.dst_offset_on($display->tstart).', usertime = '.usertime($display->tstart).', make_t = '.$test_start);
//}
//if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) {
//notify('Failed assertion in calendar/lib.php line 130; display->tend = '.$display->tend.', dst_offset = '.dst_offset_on($display->tend).', usertime = '.usertime($display->tend).', make_t = '.$test_end);
//}
// Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
$events = calendar_get_events(
usertime($display->tstart) - dst_offset_on($display->tstart),
usertime($display->tend) - dst_offset_on($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 (!groups_course_module_visible($cm)) {
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.
// Arguments passed by reference.
//calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
//Accessibility: added summary and <abbr> elements.
$days_title = calendar_get_days();
$summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear')));
$summary = get_string('tabledata', 'access', $summary);
$content .= '<table class="minicalendar calendartable" summary="'.$summary.'">'; // Begin table
$content .= '<tr class="weekdays">'; // Header row: day names
// Print out the names of the weekdays
$days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
// This uses the % operator to get the correct weekday no matter what shift we have
// applied to the $display->minwday : $display->maxwday range from the default 0 : 6
$content .= '<th scope="col"><abbr title="'. get_string($days_title[$i % 7], 'calendar') .'">'.
get_string($days[$i % 7], 'calendar') ."</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
for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
if($dayweek > $display->maxwday) {
// We need to change week (table row)
$content .= '</tr><tr>';
$dayweek = $display->minwday;
}
// Reset vars
$cell = '';
if ($weekend & (1 << ($dayweek % 7))) {
// Weekend. This is true no matter what the exact range is.
$class = 'weekend day';
} else {
// Normal working day.
$class = 'day';
}
// Special visual fx if an event is defined
if(isset($eventsbyday[$day])) {
$class .= ' hasevent';
$hrefparams['view'] = 'day';
$dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $hrefparams), $day, $m, $y);
$popupcontent = '';
foreach($eventsbyday[$day] as $eventid) {
if (!isset($events[$eventid])) {
continue;
}
$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 = 'c/site';
} else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
$popupicon = 'c/course';
} else if ($event->groupid) { // Group event
$popupicon = 'c/group';
} else if ($event->userid) { // User event
$popupicon = 'c/user';
}
$dayhref->set_anchor('event_'.$event->id);
$popupcontent .= html_writer::start_tag('div');
$popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
$popupcontent .= html_writer::link($dayhref, format_string($event->name, true));
$popupcontent .= html_writer::end_tag('div');
}
//Accessibility: functionality moved to calendar_get_popup.
if($display->thismonth && $day == $d) {
$popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
} else {
$popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
}
// 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 = '<a href="'.(string)$dayhref.'" '.$popup.'>'.$day.'</a>';
} 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;
}
}
// Special visual fx for today
//Accessibility: hidden text for today, and popup.
if($display->thismonth && $day == $d) {
$class .= ' today';
$today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort'));
if(! isset($eventsbyday[$day])) {
$class .= ' eventnone';
$popup = calendar_get_popup(true, false);
$cell = '<a href="#" '.$popup.'>'.$day.'</a>';
}
$cell = get_accesshide($today.' ').$cell;
}
// Just display it
if(!empty($class)) {
$class = ' class="'.$class.'"';
}
$content .= '<td'.$class.'>'.$cell."</td>\n";
}
// 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
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 of eventid for the calendar_tooltip popup window/layout
*/
function calendar_get_popup($is_today, $event_timestart, $popupcontent='') {
global $PAGE;
static $popupcount;
if ($popupcount === null) {
$popupcount = 1;
}
$popupcaption = '';
if($is_today) {
$popupcaption = get_string('today', 'calendar').' ';
}
if (false === $event_timestart) {
$popupcaption .= userdate(time(), get_string('strftimedayshort'));
$popupcontent = get_string('eventnone', 'calendar');
} else {
$popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort')));
}
$id = 'calendar_tooltip_'.$popupcount;
$PAGE->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.add_event', array(array('eventId'=>$id,'title'=>$popupcaption, 'content'=>$popupcontent)));
$popupcount++;
return 'id="'.$id.'"';
}
/**
* 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 (!coursemodule_visible_for_user($cm)) {
continue;
}
}
if ($event->modulename == 'assignment'){
// create calendar_event to test edit_event capability
// this new event will also prevent double creation of calendar_event object
$checkevent = new calendar_event($event);
// TODO: rewrite this hack somehow
if (!calendar_edit_event_allowed($checkevent)){ // cannot manage entries, eg. student
if (!$assignment = $DB->get_record('assignment', array('id'=>$event->instance))) {
// print_error("invalidid", 'assignment');
continue;
}
// assign assignment to assignment object to use hidden_is_hidden method
require_once($CFG->dirroot.'/mod/assignment/lib.php');
if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) {
continue;
}
require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php');
$assignmentclass = 'assignment_'.$assignment->assignmenttype;
$assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm);
if ($assignmentinstance->description_is_hidden()){//force not to show description before availability
$event->description = get_string('notavailableyet', 'assignment');
}
}
}
}
if ($processed >= $display->maxevents) {
break;
}
$event->time = calendar_format_event_time($event, $now, $hrefparams);
$output[] = $event;
++$processed;
}
}
return $output;
}
/**
* 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) . '';
$context = get_context_instance(CONTEXT_COURSE, $module->course);
$fullname = format_string($coursecache[$module->course]->fullname, true, array('context' => $context));
$event->icon = '<img height="16" width="16" src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" style="vertical-align: middle;" />';
$event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
$event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$fullname.'</a>';
$event->cmid = $module->id;
} else if($event->courseid == SITEID) { // Site event
$event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/site') . '" alt="'.get_string('globalevent', 'calendar').'" style="vertical-align: middle;" />';
$event->cssclass = 'calendar_event_global';
} else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
calendar_get_course_cached($coursecache, $event->courseid);
$context = get_context_instance(CONTEXT_COURSE, $event->courseid);
$fullname = format_string($coursecache[$event->courseid]->fullname, true, array('context' => $context));
$event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/course') . '" alt="'.get_string('courseevent', 'calendar').'" style="vertical-align: middle;" />';
$event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$fullname.'</a>';
$event->cssclass = 'calendar_event_course';
} else if ($event->groupid) { // Group event
$event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/group') . '" alt="'.get_string('groupevent', 'calendar').'" style="vertical-align: middle;" />';
$event->cssclass = 'calendar_event_group';
} else if($event->userid) { // User event
$event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/user') . '" alt="'.get_string('userevent', 'calendar').'" style="vertical-align: middle;" />';
$event->cssclass = 'calendar_event_user';
}
return $event;
}
/**
* Prints a calendar event
*
* @deprecated Moodle 2.0 - MDL-22887 please do not use this function any more.
* @todo MDL-31133 - will be removed in Moodle 2.3
* @see core_calendar_renderer event function
*/
function calendar_print_event($event, $showactions=true) {
global $CFG, $USER, $OUTPUT, $PAGE;
debugging('calendar_print_event is deprecated please update your code', DEBUG_DEVELOPER);
$renderer = $PAGE->get_renderer('core_calendar');
if (!($event instanceof calendar_event)) {
$event = new calendar_event($event);
}
echo $renderer->event($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 = '';
// Quick test
if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
return array();
}
if(is_array($users) && !empty($users)) {
// Events from a number of users
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)';
} else if(is_numeric($users)) {
// Events from one user
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)';
} 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)) {
// Events from a number of groups
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' groupid IN ('.implode(',', $groups).')';
} else if(is_numeric($groups)) {
// Events from one group
if(!empty($whereclause)) $whereclause .= ' OR ';
$whereclause .= ' groupid = '.$groups;
} 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)) {
if(!empty($whereclause)) {
$whereclause .= ' OR';
}
$whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))';
} else if(is_numeric($courses)) {
// One course
if(!empty($whereclause)) $whereclause .= ' OR';
$whereclause .= ' (groupid = 0 AND courseid = '.$courses.')';
} 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, null, 'timestart');
if ($events === false) {
$events = array();
}
return $events;
}
/**
* 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 $CFG;
$content = '';
if(!isset($data['d'])) {
$data['d'] = 1;
}
// Ensure course id passed if relevant
// Required due to changes in view/lib.php mainly (calendar_session_vars())
$courseid = '';
if (!empty($data['id'])) {
$courseid = '&course='.$data['id'];
}
if(!checkdate($data['m'], $data['d'], $data['y'])) {
$time = time();
}
else {
$time = make_timestamp($data['y'], $data['m'], $data['d']);
}
$date = usergetdate($time);
$data['m'] = $date['mon'];
$data['y'] = $date['year'];
//Accessibility: calendar block controls, replaced <table> with <div>.
//$nexttext = link_arrow_right(get_string('monthnext', 'access'), $url='', $accesshide=true);
//$prevtext = link_arrow_left(get_string('monthprev', 'access'), $url='', $accesshide=true);
switch($type) {
case 'frontpage':
list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
$nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'index.php?', 0, $nextmonth, $nextyear, $accesshide=true);
$prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'index.php?', 0, $prevmonth, $prevyear, true);
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']);
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':
list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
$nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'view.php?id='.$data['id'].'&', 0, $nextmonth, $nextyear, $accesshide=true);
$prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'view.php?id='.$data['id'].'&', 0, $prevmonth, $prevyear, true);
$calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']);
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')), 1, $data['m'], $data['y']);
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')), 1, $data['m'], $data['y']);
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':
list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
$prevdate = make_timestamp($prevyear, $prevmonth, 1);
$nextdate = make_timestamp($nextyear, $nextmonth, 1);
$prevlink = calendar_get_link_previous(userdate($prevdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $prevmonth, $prevyear);
$nextlink = calendar_get_link_next(userdate($nextdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $nextmonth, $nextyear);
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><h1 class="current">'.userdate($time, get_string('strftimemonthyear'))."</h1>";
$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();
$data['d'] = $date['mday']; // Just for convenience
$prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1));
$nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1));
$prevname = calendar_wday_name($days[$prevdate['wday']]);
$nextname = calendar_wday_name($days[$nextdate['wday']]);
$prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&', $prevdate['mday'], $prevdate['mon'], $prevdate['year']);
$nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&', $nextdate['mday'], $nextdate['mon'], $nextdate['year']);
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;
}
/**
* Get the controls filter for calendar.
*
* Filter is used to hide calendar info from the display page
*
* @param moodle_url $returnurl return-url for filter controls
* @return string $content return filter controls in html
*/
function calendar_filter_controls(moodle_url $returnurl) {
global $CFG, $USER, $OUTPUT;
$groupevents = true;
$id = optional_param( 'id',0,PARAM_INT );
$seturl = new moodle_url('/calendar/set.php', array('return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey()));
$content = '<table>';
$content .= '<tr>';
$seturl->param('var', 'showglobal');
if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) {
$content .= '<td class="eventskey calendar_event_global" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hideglobal', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hideglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
} else {
$content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showglobal', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
}
$seturl->param('var', 'showcourses');
if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) {
$content .= '<td class="eventskey calendar_event_course" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hidecourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
} else {
$content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_showcourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showcourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
}
if (isloggedin() && !isguestuser()) {
$content .= "</tr>\n<tr>";
if ($groupevents) {
// This course MIGHT have group events defined, so show the filter
$seturl->param('var', 'showgroups');
if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) {
$content .= '<td class="eventskey calendar_event_group" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hidegroups', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hidegroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
} else {
$content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showgroups', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showgroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
}
} else {
// This course CANNOT have group events, so lose the filter
$content .= '<td style="width: 11px;"></td><td> </td>'."\n";
}
$seturl->param('var', 'showuser');
if (calendar_show_event_type(CALENDAR_EVENT_USER)) {
$content .= '<td class="eventskey calendar_event_user" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hideuser', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
} else {
$content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showuser', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".$seturl."'".'" /></td>';
$content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
}
}
$content .= "</tr>\n</table>\n";
return $content;
}
/**
* Return the representation day
*
* @param int $tstamp Timestamp in GMT
* @param int $now current Unix timestamp
* @param bool $usecommonwords
* @return string the formatted date/time
*/
function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
static $shortformat;
if(empty($shortformat)) {
$shortformat = get_string('strftimedayshort');
}
if($now === false) {
$now = time();
}
// To have it in one place, if a change is needed
$formal = userdate($tstamp, $shortformat);
$datestamp = usergetdate($tstamp);
$datenow = usergetdate($now);
if($usecommonwords == false) {
// We don't want words, just a date
return $formal;
}
else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
// Today