Fix for bug 1755:
[moodle.git] / calendar / lib.php
CommitLineData
7423f116 1<?php // $Id$
2
3/////////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Calendar extension //
8// //
9// Copyright (C) 2003-2004 Greek School Network www.sch.gr //
10// //
11// Designed by: //
12// Avgoustos Tsinakos (tsinakos@uom.gr) //
13// Jon Papaioannou (pj@uom.gr) //
14// //
15// Programming and development: //
16// Jon Papaioannou (pj@uom.gr) //
17// //
18// For bugs, suggestions, etc contact: //
19// Jon Papaioannou (pj@uom.gr) //
20// //
21// The current module was developed at the University of Macedonia //
22// (www.uom.gr) under the funding of the Greek School Network (www.sch.gr) //
23// The aim of this project is to provide additional and improved //
24// functionality to the Asynchronous Distance Education service that the //
25// Greek School Network deploys. //
26// //
27// This program is free software; you can redistribute it and/or modify //
28// it under the terms of the GNU General Public License as published by //
29// the Free Software Foundation; either version 2 of the License, or //
30// (at your option) any later version. //
31// //
32// This program is distributed in the hope that it will be useful, //
33// but WITHOUT ANY WARRANTY; without even the implied warranty of //
34// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
35// GNU General Public License for more details: //
36// //
37// http://www.gnu.org/copyleft/gpl.html //
38// //
39/////////////////////////////////////////////////////////////////////////////
40
ed151929 41$firstday = get_string('firstdayofweek');
42if(!is_numeric($firstday)) {
43 define ('CALENDAR_STARTING_WEEKDAY', 1);
44}
45else {
46 define ('CALENDAR_STARTING_WEEKDAY', intval($firstday) % 7);
47}
48
7423f116 49define ('SECS_IN_DAY', 86400);
91a09745 50define ('CALENDAR_UPCOMING_DAYS', 21);
7423f116 51define ('CALENDAR_UPCOMING_MAXEVENTS', 10);
7423f116 52define ('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
1b0ebe79 53define ('CALENDAR_TF_24', '%H:%M');
54define ('CALENDAR_TF_12', '%I:%M %p');
7423f116 55
bd119567 56// Initialize the session variables here to be sure
57calendar_session_vars();
58
7423f116 59function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
60 global $CFG, $USER;
61
4e17c6f3 62 $display = &New stdClass;
7423f116 63 $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
64 $display->maxwday = $display->minwday + 6;
65
66 $content = '';
67
68 if(!empty($cal_month) && !empty($cal_year)) {
69 $thisdate = usergetdate(time()); // Date and time the user sees at his location
70 if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) {
71 // Navigated to this month
72 $date = $thisdate;
73 $display->thismonth = true;
74 }
75 else {
76 // Navigated to other month, let's do a nice trick and save us a lot of work...
77 if(!checkdate($cal_month, 1, $cal_year)) {
78 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
79 $display->thismonth = true;
80 }
81 else {
82 $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
83 $display->thismonth = false;
84 }
85 }
86 }
87 else {
88 $date = usergetdate(time()); // Date and time the user sees at his location
89 $display->thismonth = true;
90 }
91
92 // Fill in the variables we 're going to use, nice and tidy
93 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
94 $display->maxdays = calendar_days_in_month($m, $y);
95
96 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
97 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
98 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
99
100 $startwday = gmdate('w', $display->tstart); // $display->tstart is already GMT, so don't use date(): messes with server's TZ
101
102 // Align the starting weekday to fall in our display range
103 // This is simple, not foolproof.
104 if($startwday < $display->minwday) {
105 $startwday += 7;
106 }
107
108 // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
109 $whereclause = calendar_sql_where(usertime($display->tstart), usertime($display->tend), $users, $groups, $courses);
110
111 if($whereclause === false) {
112 $events = array();
113 }
114 else {
115 $events = get_records_select('event', $whereclause);
116 }
117
c635dcda 118 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
119 // possibly removing courseid 1 from $courses, there is only one course left, then clicking on a day in the month
120 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
121 // arguments to this function.
122
7bd1677c 123 $morehref = '';
124 if(!empty($courses)) {
125 $courses = array_diff($courses, array(1));
126 if(count($courses) == 1) {
127 $morehref = '&amp;course='.reset($courses);
128 }
c635dcda 129 }
130
7423f116 131 // We want to have easy access by day, since the display is on a per-day basis.
132 // Arguments passed by reference.
7b38bfa6 133 //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
134 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday);
7423f116 135
92668ad2 136 $content .= '<table class="calendarmini">'; // Begin table
137 $content .= '<thead><tr>'; // Header row: day names
7423f116 138
139 // Print out the names of the weekdays
140 $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
141 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
142 // This uses the % operator to get the correct weekday no matter what shift we have
143 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
92668ad2 144 $content .= '<td>'.get_string($days[$i % 7], 'calendar').'</td>';
7423f116 145 }
146
92668ad2 147 $content .= '</tr></thead><tbody><tr>'; // End of day names; prepare for day numbers
7423f116 148
149 // For the table display. $week is the row; $dayweek is the column.
7423f116 150 $dayweek = $startwday;
151
152 // Paddding (the first week may have blank days in the beginning)
153 for($i = $display->minwday; $i < $startwday; ++$i) {
92668ad2 154 $content .= '<td>&nbsp;</td>'."\n";
7423f116 155 }
156
d56d4e23 157 $strftimetimedayshort = get_string('strftimedayshort');
158
7423f116 159 // Now display all the calendar
160 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
161 if($dayweek > $display->maxwday) {
162 // We need to change week (table row)
d56d4e23 163 $content .= '</tr><tr>';
7423f116 164 $dayweek = $display->minwday;
7423f116 165 }
92668ad2 166
7423f116 167 // Reset vars
7423f116 168 $cell = '';
169 if($dayweek % 7 == 0 || $dayweek % 7 == 6) {
170 // Weekend. This is true no matter what the exact range is.
92668ad2 171 $class = 'cal_weekend';
7423f116 172 }
173 else {
174 // Normal working day.
92668ad2 175 $class = '';
7423f116 176 }
177
178 // Special visual fx if an event is defined
179 if(isset($eventsbyday[$day])) {
c635dcda 180 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $day, $m, $y);
f434283f 181
7423f116 182 // OverLib popup
183 $popupcontent = '';
184 foreach($eventsbyday[$day] as $eventid) {
41d30a8e 185 if (!isset($events[$eventid])) {
186 continue;
4e17c6f3 187 }
41d30a8e 188 $event = $events[$eventid];
41d30a8e 189 if(!empty($event->modulename)) {
190 $popupicon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
191 $popupalt = $event->modulename;
192
193 } else if ($event->courseid == 1) { // Site event
194 $popupicon = $CFG->pixpath.'/c/site.gif';
195 $popupalt = '';
196 } else if ($event->courseid > 1 and empty($event->groupid)) { // Course event
197 $popupicon = $CFG->pixpath.'/c/course.gif';
c9b05b32 198 $popupalt = '';
41d30a8e 199 } else if ($event->groupid) { // Group event
200 $popupicon = $CFG->pixpath.'/c/group.gif';
201 $popupalt = '';
202 } else if ($event->userid) { // User event
203 $popupicon = $CFG->pixpath.'/c/user.gif';
204 $popupalt = '';
4e17c6f3 205 }
9cb8879b 206 $popupcontent .= '<div><img height="16" width="16" src="'.$popupicon.'" style="vertical-align: middle; margin-right: 4px;" alt="'.$popupalt.'" /><a href="'.$dayhref.'">'.$event->name.'</a></div>';
7423f116 207 }
208
d56d4e23 209 $popupcaption = get_string('eventsfor', 'calendar', userdate($events[$eventid]->timestart, $strftimetimedayshort));
9cb8879b 210 $popupcontent = str_replace("'", "\'", htmlspecialchars($popupcontent));
7423f116 211 $popup = 'onmouseover="return overlib(\''.$popupcontent.'\', CAPTION, \''.$popupcaption.'\');" onmouseout="return nd();"';
212
213 // Class and cell content
214 if(isset($typesbyday[$day]['startglobal'])) {
215 $class .= ' cal_event_global';
216 }
217 else if(isset($typesbyday[$day]['startcourse'])) {
218 $class .= ' cal_event_course';
219 }
220 else if(isset($typesbyday[$day]['startgroup'])) {
221 $class .= ' cal_event_group';
222 }
223 else if(isset($typesbyday[$day]['startuser'])) {
224 $class .= ' cal_event_user';
225 }
f434283f 226 $cell = '<strong><a href="'.$dayhref.'" '.$popup.'">'.$day.'</a></strong>';
7423f116 227 }
228 else {
229 $cell = $day;
230 }
231
232 if(isset($typesbyday[$day]['durationglobal'])) {
233 $class .= ' cal_duration_global';
234 }
235 else if(isset($typesbyday[$day]['durationcourse'])) {
236 $class .= ' cal_duration_course';
237 }
238 else if(isset($typesbyday[$day]['durationgroup'])) {
239 $class .= ' cal_duration_group';
240 }
241 else if(isset($typesbyday[$day]['durationuser'])) {
242 $class .= ' cal_duration_user';
243 }
244
245 // Special visual fx for today
246 if($display->thismonth && $day == $d) {
247 $class .= ' cal_today';
248 }
249
250 // Just display it
92668ad2 251 if(!empty($class)) {
d56d4e23 252 $class = ' class="'.$class.'"';
92668ad2 253 }
254 $content .= '<td'.$class.'>'.$cell."</td>\n";
7423f116 255 }
256
257 // Paddding (the last week may have blank days at the end)
258 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
92668ad2 259 $content .= '<td>&nbsp;</td>';
7423f116 260 }
261 $content .= '</tr>'; // Last row ends
262
92668ad2 263 $content .= '</tbody></table>'; // Tabular display of days ends
7423f116 264
265 return $content;
266}
267
9958a08c 268function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
7423f116 269 global $CFG;
270
c635dcda 271 $display = &New stdClass;
7423f116 272 $display->range = $daysinfuture; // How many days in the future we 'll look
273 $display->maxevents = $maxevents;
274
275 $output = array();
276
277 // Prepare "course caching", since it may save us a lot of queries
278 $coursecache = array();
279
280 $processed = 0;
281 $now = time(); // We 'll need this later
9d567178 282 $usermidnighttoday = usergetmidnight($now);
7423f116 283
9958a08c 284 if ($fromtime) {
285 $display->tstart = $fromtime;
286 } else {
9d567178 287 $display->tstart = $usermidnighttoday;
9958a08c 288 }
7423f116 289
290 // This effectively adds as many days as needed, and the final SECS_IN_DAY - 1
291 // serves to cover the duration until the end of the final day. We could
292 // just do another gmmktime() and an addition, but this is "faster" :)
293 $display->tend = $display->tstart + (SECS_IN_DAY * $display->range) - 1;
294
295 // Get the events matching our criteria
296 $whereclause = calendar_sql_where($display->tstart, $display->tend, $users, $groups, $courses);
9958a08c 297 if ($whereclause === false) {
7423f116 298 $events = false;
9958a08c 299 } else {
7423f116 300 $whereclause .= ' ORDER BY timestart'; // We want them this way
301 $events = get_records_select('event', $whereclause);
302 }
303
c635dcda 304 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
305 // possibly removing courseid 1 from $courses, there is only one course left, then clicking on a day in the month
306 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
307 // arguments to this function.
308
7bd1677c 309 $morehref = '';
310 if(!empty($courses)) {
311 $courses = array_diff($courses, array(1));
312 if(count($courses) == 1) {
313 $morehref = '&amp;course='.reset($courses);
314 }
c635dcda 315 }
316
7423f116 317 if($events !== false) {
318 foreach($events as $event) {
9d567178 319
320 if($processed >= $display->maxevents) {
321 break;
322 }
7423f116 323
7423f116 324 $startdate = usergetdate($event->timestart);
325 $enddate = usergetdate($event->timestart + $event->timeduration);
9d567178 326 $usermidnightstart = usergetmidnight($event->timestart);
7423f116 327
328 if($event->timeduration) {
329 // To avoid doing the math if one IF is enough :)
9d567178 330 $usermidnightend = usergetmidnight($event->timestart + $event->timeduration);
7423f116 331 }
332 else {
9d567178 333 $usermidnightend = $usermidnightstart;
7423f116 334 }
335
7423f116 336 // OK, now to get a meaningful display...
7423f116 337 // First of all we have to construct a human-readable date/time representation
9d567178 338
339 if($event->timestart + $event->timeduration < $now) {
7423f116 340 // It has expired, so we don't care about duration
341 $day = calendar_day_representation($event->timestart + $event->timeduration, $now);
342 $time = calendar_time_representation($event->timestart + $event->timeduration);
343
344 // This var always has the printable time representation
c635dcda 345 $eventtime = '<span class="dimmed_text"><a class="dimmed" href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).'">'.$day.'</a> ('.$time.')</span>';
f10f1b7b 346
7423f116 347 }
348 else if($event->timeduration) {
349 // It has a duration
9d567178 350 if($usermidnightstart == $usermidnightend) {
351 // But it's all on the same day
7423f116 352 $day = calendar_day_representation($event->timestart, $now);
353 $timestart = calendar_time_representation($event->timestart);
354 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
355
356 // Set printable representation
c635dcda 357 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
14fcda78 358 ' ('.$timestart.' -> '.$timeend.')';
7423f116 359 }
360 else {
361 // It spans two or more days
362 $daystart = calendar_day_representation($event->timestart, $now);
363 $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now);
364 $timestart = calendar_time_representation($event->timestart);
365 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
366
367 // Set printable representation
c635dcda 368 $eventtime = calendar_get_link_tag($daystart, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).
369 ' ('.$timestart.') -> '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
7423f116 370 ' ('.$timeend.')';
371 }
372 }
373 else {
374 // It's an "instantaneous" event
375 $day = calendar_day_representation($event->timestart, $now);
376 $time = calendar_time_representation($event->timestart);
377
378 // Set printable representation
c635dcda 379 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).' ('.$time.')';
7423f116 380 }
381
382 $outkey = count($output);
383
421b0337 384 $output[$outkey] = $event; // Grab the whole raw event by default
385
7423f116 386 // Now we know how to display the time, we have to see how to display the event
41d30a8e 387 if(!empty($event->modulename)) { // Activity event
7423f116 388
389 // The module name is set. I will assume that it has to be displayed, and
390 // also that it is an automatically-generated event. And of course that the
391 // three fields for get_coursemodule_from_instance are set correctly.
392
9958a08c 393 calendar_get_course_cached($coursecache, $event->courseid);
394
7423f116 395 $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance, $event->courseid);
396
9958a08c 397 if ($module === false) {
7423f116 398 // This shouldn't have happened. What to do now?
399 // Just ignore it
400 continue;
401 }
402
403 $modulename = get_string('modulename', $event->modulename);
404 $eventtype = get_string($event->eventtype, $event->modulename);
405 $icon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
9958a08c 406
552064d3 407 $output[$outkey]->icon = '<img height=16 width=16 src="'.$icon.'" alt="" title="'.$modulename.'" style="vertical-align: middle;" />';
9958a08c 408 $output[$outkey]->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
7423f116 409 $output[$outkey]->time = $eventtime;
9958a08c 410 $output[$outkey]->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>';
9958a08c 411 $output[$outkey]->cmid = $module->id;
9958a08c 412
413
414
41d30a8e 415 } else if($event->courseid == 1) { // Site event
552064d3 416 $output[$outkey]->icon = '<img height=16 width=16 src="'.$CFG->pixpath.'/c/site.gif" alt="" style="vertical-align: middle;" />';
7423f116 417 $output[$outkey]->time = $eventtime;
9958a08c 418
419
420
41d30a8e 421 } else if($event->courseid > 1 and !$event->groupid) { // Course event
7423f116 422 calendar_get_course_cached($coursecache, $event->courseid);
423
552064d3 424 $output[$outkey]->icon = '<img height=16 width=16 src="'.$CFG->pixpath.'/c/course.gif" alt="" style="vertical-align: middle;" />';
7423f116 425 $output[$outkey]->time = $eventtime;
9958a08c 426 $output[$outkey]->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>';
9958a08c 427
428
429
41d30a8e 430 } else if ($event->groupid) { // Group event
552064d3 431 $output[$outkey]->icon = '<img height=16 width=16 src="'.$CFG->pixpath.'/c/group.gif" alt="" style="vertical-align: middle;" />';
7423f116 432 $output[$outkey]->time = $eventtime;
9958a08c 433
434
435
41d30a8e 436 } else if($event->userid) { // User event
552064d3 437 $output[$outkey]->icon = '<img height=16 width=16 src="'.$CFG->pixpath.'/c/user.gif" alt="" style="vertical-align: middle;" />';
7423f116 438 $output[$outkey]->time = $eventtime;
7423f116 439 }
440 ++$processed;
441 }
442 }
443 return $output;
444}
445
8c165fe9 446function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
7423f116 447 $whereclause = '';
448 // Quick test
449 if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
450 return false;
451 }
7423f116 452 if(is_array($users) && !empty($users)) {
453 // Events from a number of users
454 if(!empty($whereclause)) $whereclause .= ' OR';
455 $whereclause .= ' userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0';
456 }
457 else if(is_numeric($users)) {
458 // Events from one user
459 if(!empty($whereclause)) $whereclause .= ' OR';
460 $whereclause .= ' userid = '.$users.' AND courseid = 0 AND groupid = 0';
461 }
462 else if($users === true) {
463 // Events from ALL users
464 if(!empty($whereclause)) $whereclause .= ' OR';
465 $whereclause .= ' userid != 0 AND courseid = 0 AND groupid = 0';
466 }
f52f7413 467 else if($users === false) {
468 // No user at all
469 if(!empty($whereclause)) {
470 $whereclause .= ' OR';
471 }
472 $whereclause .= ' 0';
473 }
7423f116 474 if(is_array($groups) && !empty($groups)) {
475 // Events from a number of groups
476 if(!empty($whereclause)) $whereclause .= ' OR';
477 $whereclause .= ' groupid IN ('.implode(',', $groups).')';
478 }
479 else if(is_numeric($groups)) {
480 // Events from one group
481 if(!empty($whereclause)) $whereclause .= ' OR ';
482 $whereclause .= ' groupid = '.$groups;
483 }
484 else if($groups === true) {
485 // Events from ALL groups
486 if(!empty($whereclause)) $whereclause .= ' OR ';
487 $whereclause .= ' groupid != 0';
488 }
f52f7413 489 if(is_array($courses)) {
490 // A number of courses (maybe none at all!)
491 if(!empty($courses)) {
492 if(!empty($whereclause)) {
493 $whereclause .= ' OR';
494 }
495 $whereclause .= ' groupid = 0 AND courseid IN ('.implode(',', $courses).')';
496 }
497 else {
498 // This means NO courses, not that we don't care!
499 if(!empty($whereclause)) {
500 $whereclause .= ' OR';
501 }
502 $whereclause .= ' 0';
503 }
7423f116 504 }
505 else if(is_numeric($courses)) {
506 // One course
507 if(!empty($whereclause)) $whereclause .= ' OR';
508 $whereclause .= ' groupid = 0 AND courseid = '.$courses;
509 }
510 else if($courses === true) {
511 // Events from ALL courses
512 if(!empty($whereclause)) $whereclause .= ' OR';
513 $whereclause .= ' groupid = 0 AND courseid != 0';
514 }
8c165fe9 515
516 if ($ignorehidden) {
517 if (!empty($whereclause)) $whereclause .= ' AND';
518 $whereclause .= ' visible = 1';
519 }
520
7423f116 521 if($withduration) {
522 $timeclause = 'timestart + timeduration >= '.$tstart.' AND timestart <= '.$tend;
523 }
524 else {
525 $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend;
526 }
527 if(!empty($whereclause)) {
528 // We have additional constraints
529 $whereclause = $timeclause.' AND ('.$whereclause.')';
530 }
531 else {
532 // Just basic time filtering
533 $whereclause = $timeclause;
534 }
f52f7413 535
8c54cec6 536 return $whereclause;
7423f116 537}
538
539function calendar_top_controls($type, $data) {
540 global $CFG;
541 $content = '';
542 if(!isset($data['d'])) {
543 $data['d'] = 1;
544 }
00341fea 545 $time = calendar_mktime_check($data['m'], $data['d'], $data['y']);
33683b36 546 $date = getdate($time);
7423f116 547 $data['m'] = $date['mon'];
548 $data['y'] = $date['year'];
7423f116 549
550 switch($type) {
551 case 'frontpage':
552 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
553 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
73341343 554 $nextlink = calendar_get_link_tag('&gt;&gt;', 'index.php?', 0, $nextmonth, $nextyear);
555 $prevlink = calendar_get_link_tag('&lt;&lt;', 'index.php?', 0, $prevmonth, $prevyear);
7423f116 556 $content .= '<table class="generaltable" style="width: 100%;"><tr>';
557 $content .= '<td style="text-align: left; width: 12%;">'.$prevlink."</td>\n";
61854a2f 558 $content .= '<td style="text-align: center;"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;', 1, $data['m'], $data['y']).'">'.strftime(get_string('strftimemonthyear'), $time)."</a></td>\n";
7423f116 559 $content .= '<td style="text-align: right; width: 12%;">'.$nextlink."</td>\n";
560 $content .= '</tr></table>';
561 break;
562 case 'course':
563 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
564 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
565 $nextlink = calendar_get_link_tag('&gt;&gt;', 'view.php?id='.$data['id'].'&amp;', 0, $nextmonth, $nextyear);
566 $prevlink = calendar_get_link_tag('&lt;&lt;', 'view.php?id='.$data['id'].'&amp;', 0, $prevmonth, $prevyear);
567 $content .= '<table class="generaltable" style="width: 100%;"><tr>';
568 $content .= '<td style="text-align: left; width: 12%;">'.$prevlink."</td>\n";
227bc46b 569 $content .= '<td style="text-align: center;"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;course='.$data['id'].'&amp;', 1, $data['m'], $data['y']).'">'.strftime(get_string('strftimemonthyear'), $time)."</a></td>\n";
7423f116 570 $content .= '<td style="text-align: right; width: 12%;">'.$nextlink."</td>\n";
571 $content .= '</tr></table>';
572 break;
573 case 'upcoming':
43c3ffbe 574 $content .= '<div style="text-align: center;"><a href="'.CALENDAR_URL.'view.php?view=upcoming">'.strftime(get_string('strftimemonthyear'), $time)."</a></div>\n";
7423f116 575 break;
576 case 'display':
aa870ca2 577 $content .= '<div style="text-align: center;"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;', 1, $data['m'], $data['y']).'">'.strftime(get_string('strftimemonthyear'), $time)."</a></div>\n";
7423f116 578 break;
579 case 'month':
580 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
581 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
f7656746 582 $prevdate = calendar_mktime_check($prevmonth, 1, $prevyear);
583 $nextdate = calendar_mktime_check($nextmonth, 1, $nextyear);
7423f116 584 $content .= "<table style='width: 100%;'><tr>\n";
aa870ca2 585 $content .= '<td style="text-align: left; width: 30%;"><a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $prevmonth, $prevyear).'">&lt;&lt; '.strftime(get_string('strftimemonthyear'), $prevdate)."</a></td>\n";
6e8e8ec6 586 $content .= '<td style="text-align: center"><strong>'.strftime(get_string('strftimemonthyear'), $time)."</strong></td>\n";
aa870ca2 587 $content .= '<td style="text-align: right; width: 30%;"><a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $nextmonth, $nextyear).'">'.strftime(get_string('strftimemonthyear'), $nextdate)." &gt;&gt;</a></td>\n";
7423f116 588 $content .= "</tr></table>\n";
589 break;
590 case 'day':
591 $data['d'] = $date['mday']; // Just for convenience
592 $dayname = calendar_wday_name($date['weekday']);
593 $prevdate = getdate($time - SECS_IN_DAY);
594 $nextdate = getdate($time + SECS_IN_DAY);
595 $prevname = calendar_wday_name($prevdate['weekday']);
596 $nextname = calendar_wday_name($nextdate['weekday']);
597 $content .= "<table style='width: 100%;'><tr>\n";
3696526b 598 $content .= '<td style="text-align: left; width: 20%;"><a href="'.calendar_get_link_href('view.php?view=day&amp;', $prevdate['mday'], $prevdate['mon'], $prevdate['year']).'">&lt;&lt; '.$prevname."</a></td>\n";
6e8e8ec6 599
600 // Get the format string
601 $text = get_string('strftimedaydate');
602 // Regexp hackery to make a link out of the month/year part
603 $text = ereg_replace('(%B.+%Y|%Y.+%B|%Y.+%m[^ ]+)', '<a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $data['m'], $data['y']).'">\\1</a>', $text);
604 // Replace with actual values and lose any day leading zero
605 $text = strftime($text, $time);
606 $text = str_replace(' 0', ' ', $text);
607 // Print the actual thing
608 $content .= '<td style="text-align: center"><strong>'.$text."</strong></td>\n";
609
3696526b 610 $content .= '<td style="text-align: right; width: 20%;"><a href="'.calendar_get_link_href('view.php?view=day&amp;', $nextdate['mday'], $nextdate['mon'], $nextdate['year']).'">'.$nextname." &gt;&gt;</a></td>\n";
7423f116 611 $content .= "</tr></table>\n";
612 break;
613 }
614 return $content;
615}
616
3cb9ee39 617function calendar_filter_controls($type, $vars = NULL, $course = NULL) {
43c3ffbe 618 global $CFG, $SESSION, $USER;
7423f116 619
48f508ab 620 $groupevents = true;
3cb9ee39 621 $getvars = '';
d715f7c4 622
34bf3ad4 623 switch($type) {
eb15f829 624 case 'event':
34bf3ad4 625 case 'upcoming':
34bf3ad4 626 case 'day':
c3f463ca 627 case 'month':
eb15f829 628 $getvars = '&amp;from='.$type;
34bf3ad4 629 break;
630 case 'course':
2eb68e6f 631 if (isset($_GET['id'])) {
632 $getvars = '&amp;from=course&amp;id='.$_GET['id'];
633 } else {
634 $getvars = '&amp;from=course';
635 }
3cb9ee39 636 if (isset($course->groupmode) and !$course->groupmode and $course->groupmodeforce) {
34bf3ad4 637 $groupevents = false;
638 }
639 break;
d715f7c4 640 }
34bf3ad4 641
3cb9ee39 642 if (!empty($vars)) {
34bf3ad4 643 $getvars .= '&amp;'.$vars;
48f508ab 644 }
7423f116 645
48f508ab 646 $content = '<table class="cal_controls" style="width: 98%;">';
7423f116 647
48f508ab 648 $content .= '<tr>';
649 if($SESSION->cal_show_global) {
2b7591cc 650 $content .= '<td class="cal_event_global" style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_hideglobal', 'calendar').'">'.get_string('globalevents', 'calendar').'</a></td>'."\n";
48f508ab 651 }
652 else {
2b7591cc 653 $content .= '<td style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_showglobal', 'calendar').'">'.get_string('globalevents', 'calendar').'</a></td>'."\n";
48f508ab 654 }
655 if($SESSION->cal_show_course) {
2b7591cc 656 $content .= '<td class="cal_event_course" style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.get_string('courseevents', 'calendar').'</a></td>'."\n";
7423f116 657 }
48f508ab 658 else {
2b7591cc 659 $content .= '<td style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_showcourse', 'calendar').'">'.get_string('courseevents', 'calendar').'</a></td>'."\n";
48f508ab 660 }
48f508ab 661
fd5594a0 662 if(!empty($USER) && !isguest()) {
43c3ffbe 663 $content .= "</tr>\n<tr>";
664
665 if($groupevents) {
666 // This course MIGHT have group events defined, so show the filter
667 if($SESSION->cal_show_groups) {
668 $content .= '<td class="cal_event_group" style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_hidegroups', 'calendar').'">'.get_string('groupevents', 'calendar').'</a></td>'."\n";
cd6469cb 669 } else {
43c3ffbe 670 $content .= '<td style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_showgroups', 'calendar').'">'.get_string('groupevents', 'calendar').'</a></td>'."\n";
671 }
cd6469cb 672 if ($SESSION->cal_show_user) {
43c3ffbe 673 $content .= '<td class="cal_event_user" style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('userevents', 'calendar').'</a></td>'."\n";
cd6469cb 674 } else {
43c3ffbe 675 $content .= '<td style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('userevents', 'calendar').'</a></td>'."\n";
676 }
cd6469cb 677
678 } else {
43c3ffbe 679 // This course CANNOT have group events, so lose the filter
cd6469cb 680 $content .= '<td style="width: 8px;"></td><td>&nbsp;</td>'."\n";
681
43c3ffbe 682 if($SESSION->cal_show_user) {
cd6469cb 683 $content .= '<td class="cal_event_user" style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('userevents', 'calendar').'</a></td>'."\n";
684 } else {
685 $content .= '<td style="width: 8px;"></td><td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('userevents', 'calendar').'</a></td>'."\n";
43c3ffbe 686 }
48f508ab 687 }
688 }
689 $content .= "</tr>\n</table>\n";
690
7423f116 691 return $content;
692}
693
694function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
695
0ef7c973 696 static $shortformat;
697 if(empty($shortformat)) {
e70fdac0 698 $shortformat = get_string('strftimedayshort');
0ef7c973 699 }
700
7423f116 701 if($now === false) {
702 $now = time();
703 }
704
705 // To have it in one place, if a change is needed
e70fdac0 706 $formal = userdate($tstamp, $shortformat);
7423f116 707
7b38bfa6 708 $datestamp = usergetdate($tstamp);
709 $datenow = usergetdate($now);
7423f116 710
711 if($usecommonwords == false) {
712 // We don't want words, just a date
713 return $formal;
714 }
7b38bfa6 715 else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
7423f116 716 // Today
717 return get_string('today', 'calendar');
718 }
7b38bfa6 719 else if(
720 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
721 ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1)
722 ) {
7423f116 723 // Yesterday
724 return get_string('yesterday', 'calendar');
725 }
7b38bfa6 726 else if(
727 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
728 ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1)
729 ) {
7423f116 730 // Tomorrow
731 return get_string('tomorrow', 'calendar');
732 }
733 else {
734 return $formal;
735 }
736}
737
738function calendar_time_representation($time) {
1b0ebe79 739 static $langtimeformat = NULL;
740 if($langtimeformat === NULL) {
741 $langtimeformat = get_string('strftimetime');
742 }
743 $timeformat = get_user_preferences('calendar_timeformat');
744 // The ? is needed because the preference might be present, but empty
745 return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat);
7423f116 746}
747
748function calendar_get_link_href($linkbase, $d, $m, $y) {
749 if(empty($linkbase)) return '';
750 $paramstr = '';
751 if(!empty($d)) $paramstr .= '&amp;cal_d='.$d;
752 if(!empty($m)) $paramstr .= '&amp;cal_m='.$m;
753 if(!empty($y)) $paramstr .= '&amp;cal_y='.$y;
754 if(!empty($paramstr)) $paramstr = substr($paramstr, 5);
755 return $linkbase.$paramstr;
756}
757
758function calendar_get_link_tag($text, $linkbase, $d, $m, $y) {
759 $href = calendar_get_link_href($linkbase, $d, $m, $y);
760 if(empty($href)) return $text;
761 return '<a href="'.$href.'">'.$text.'</a>';
762}
763
764function calendar_gmmktime_check($m, $d, $y, $default = false) {
765 if($default === false) $default = time();
766 if(!checkdate($m, $d, $y)) {
767 return $default;
768 }
769 else {
770 return gmmktime(0, 0, 0, $m, $d, $y);
771 }
772}
773
774function calendar_mktime_check($m, $d, $y, $default = false) {
775 if($default === false) $default = time();
776 if(!checkdate($m, $d, $y)) {
777 return $default;
778 }
779 else {
780 return mktime(0, 0, 0, $m, $d, $y);
781 }
782}
783
7423f116 784function calendar_wday_name($englishname) {
785 return get_string(strtolower($englishname), 'calendar');
786}
787
788function calendar_days_in_month($month, $year) {
7b38bfa6 789 return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
7423f116 790}
791
792function calendar_get_sideblock_upcoming($courses, $groups, $users, $daysinfuture, $maxevents) {
793 $events = calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents);
794
795 $content = '';
796 $lines = count($events);
396b61f0 797 if (!$lines) {
798 return $content;
799 }
7423f116 800
396b61f0 801 for ($i = 0; $i < $lines; ++$i) {
7423f116 802 $content .= '<div class="cal_event">'.$events[$i]->icon.' ';
43c3ffbe 803 if (!empty($events[$i]->referer)) {
7423f116 804 // That's an activity event, so let's provide the hyperlink
396b61f0 805 $content .= $events[$i]->referer;
806 } else {
807 $content .= $events[$i]->name;
7423f116 808 }
14fcda78 809 $events[$i]->time = str_replace('->', '<br />->', $events[$i]->time);
d13403fb 810 $content .= '</div><div class="cal_event_date" style="text-align:right;">'.$events[$i]->time.'</div>';
396b61f0 811 if ($i < $lines - 1) $content .= '<hr />';
7423f116 812 }
813
814 return $content;
7423f116 815}
816
817function calendar_add_month($month, $year) {
818 if($month == 12) {
819 return array(1, $year + 1);
820 }
821 else {
822 return array($month + 1, $year);
823 }
824}
825
826function calendar_sub_month($month, $year) {
827 if($month == 1) {
828 return array(12, $year - 1);
829 }
830 else {
831 return array($month - 1, $year);
832 }
833}
834
7b38bfa6 835function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday) {
7423f116 836 $eventsbyday = array();
837 $typesbyday = array();
838 $durationbyday = array();
839
840 if($events === false) {
841 return;
842 }
843
7423f116 844 foreach($events as $event) {
7423f116 845
7b38bfa6 846 $startdate = usergetdate($event->timestart);
847 $enddate = usergetdate($event->timestart + $event->timeduration);
7423f116 848
7b38bfa6 849 // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair
ef618501 850 if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
7b38bfa6 851 // Out of bounds
852 continue;
7423f116 853 }
854
7b38bfa6 855 $eventdaystart = intval($startdate['mday']);
7423f116 856
7b38bfa6 857 if($startdate['mon'] == $month && $startdate['year'] == $year) {
858 // Give the event to its day
859 $eventsbyday[$eventdaystart][] = $event->id;
7423f116 860
7b38bfa6 861 // Mark the day as having such an event
862 if($event->courseid == 1 && $event->groupid == 0) {
863 $typesbyday[$eventdaystart]['startglobal'] = true;
864 }
865 else if($event->courseid > 1 && $event->groupid == 0) {
866 $typesbyday[$eventdaystart]['startcourse'] = true;
867 }
868 else if($event->groupid) {
869 $typesbyday[$eventdaystart]['startgroup'] = true;
870 }
871 else if($event->userid) {
872 $typesbyday[$eventdaystart]['startuser'] = true;
873 }
874 }
7423f116 875
7b38bfa6 876 if($event->timeduration == 0) {
877 // Proceed with the next
878 continue;
879 }
7423f116 880
7b38bfa6 881 // The event starts on $month $year or before. So...
ef618501 882 $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0;
7b38bfa6 883
884 // Also, it ends on $month $year or later...
885 $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year);
886
887 // Mark all days between $lowerbound and $upperbound (inclusive) as duration
888 for($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
889 $durationbyday[$i][] = $event->id;
890 if($event->courseid == 1 && $event->groupid == 0) {
891 $typesbyday[$i]['durationglobal'] = true;
892 }
893 else if($event->courseid > 1 && $event->groupid == 0) {
894 $typesbyday[$i]['durationcourse'] = true;
895 }
896 else if($event->groupid) {
897 $typesbyday[$i]['durationgroup'] = true;
898 }
899 else if($event->userid) {
900 $typesbyday[$i]['durationuser'] = true;
7423f116 901 }
902 }
7b38bfa6 903
7423f116 904 }
905 return;
906}
907
908function calendar_get_module_cached(&$coursecache, $modulename, $instance, $courseid) {
909 $module = get_coursemodule_from_instance($modulename, $instance, $courseid);
910
911 if($module === false) return false;
912 if(!calendar_get_course_cached($coursecache, $courseid)) {
913 return false;
914 }
915 return $module;
916}
917
918function calendar_get_course_cached(&$coursecache, $courseid) {
919 if(!isset($coursecache[$courseid])) {
920 $coursecache[$courseid] = get_record('course', 'id', $courseid);
921 }
922 return $coursecache[$courseid];
923}
924
925function calendar_session_vars() {
926 global $SESSION, $USER;
927
928 if(!isset($SESSION->cal_course_referer)) {
929 $SESSION->cal_course_referer = 0;
930 }
931 if(!isset($SESSION->cal_show_global)) {
932 $SESSION->cal_show_global = true;
933 }
934 if(!isset($SESSION->cal_show_groups)) {
935 $SESSION->cal_show_groups = true;
936 }
937 if(!isset($SESSION->cal_show_course)) {
938 $SESSION->cal_show_course = true;
939 }
940 if(!isset($SESSION->cal_show_user)) {
89adb174 941 $SESSION->cal_show_user = true;
7423f116 942 }
43c3ffbe 943 if(empty($SESSION->cal_courses_shown)) {
944 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
945 }
89adb174 946 if(empty($SESSION->cal_users_shown)) {
947 // The empty() instead of !isset() here makes a whole world of difference,
948 // as it will automatically change to the user's id when the user first logs
949 // in. With !isset(), it would never do that.
950 $SESSION->cal_users_shown = isset($USER->id) ? $USER->id : false;
951 }
952 else if(is_numeric($SESSION->cal_users_shown) && !empty($USER->id) && $SESSION->cal_users_shown != $USER->id) {
953 // Follow the white rabbit, for example if a teacher logs in as a student
954 $SESSION->cal_users_shown = $USER->id;
955 }
7423f116 956}
957
958function calendar_overlib_html() {
959 global $CFG;
960
961 $html = '';
962 $html .= '<div id="overDiv" style="position: absolute; visibility: hidden; z-index:1000;"></div>';
963 $html .= '<script type="text/javascript" src="'.CALENDAR_URL.'overlib.cfg.php"></script>';
964
965 return $html;
966}
967
7423f116 968function calendar_set_referring_course($courseid) {
969 global $SESSION;
970 $SESSION->cal_course_referer = intval($courseid);
971}
972
43c3ffbe 973function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) {
7423f116 974 global $SESSION, $USER;
975
43c3ffbe 976 // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause
51f8a12f 977 // the code to function incorrectly UNLESS we convert it to an integer. One case where
978 // PHP's loose type system works against us.
43c3ffbe 979 if(is_string($SESSION->cal_courses_shown)) {
980 $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown);
51f8a12f 981 }
982
43c3ffbe 983 if($courseeventsfrom === NULL) {
984 $courseeventsfrom = $SESSION->cal_courses_shown;
7423f116 985 }
43c3ffbe 986 if($groupeventsfrom === NULL) {
987 $groupeventsfrom = $SESSION->cal_courses_shown;
7423f116 988 }
989
43c3ffbe 990 if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) {
991 if(is_int($courseeventsfrom)) {
992 $courses = array(1, $courseeventsfrom);
7423f116 993 }
43c3ffbe 994 else if(is_array($courseeventsfrom)) {
995 $courses = array_keys($courseeventsfrom);
7423f116 996 $courses[] = 1;
997 }
998 }
43c3ffbe 999 else if($SESSION->cal_show_course) {
1000 if(is_int($courseeventsfrom)) {
1001 $courses = array($courseeventsfrom);
7423f116 1002 }
43c3ffbe 1003 else if(is_array($courseeventsfrom)) {
1004 $courses = array_keys($courseeventsfrom);
7423f116 1005 }
9a2ba13e 1006 $courses = array_diff($courses, array(1));
7423f116 1007 }
1008 else if($SESSION->cal_show_global) {
1009 $courses = array(1);
1010 }
1011 else {
1012 $courses = false;
1013 }
1014
43c3ffbe 1015 if($SESSION->cal_show_user || $ignorefilters) {
89adb174 1016 // This doesn't work for arrays yet (maybe someday it will)
1017 $user = $SESSION->cal_users_shown;
7423f116 1018 }
1019 else {
1020 $user = false;
1021 }
43c3ffbe 1022 if($SESSION->cal_show_groups || $ignorefilters) {
1023 if(is_int($groupeventsfrom)) {
1024 $groupcourses = array($groupeventsfrom);
7423f116 1025 }
43c3ffbe 1026 else if(is_array($groupeventsfrom)) {
1027 $groupcourses = array_keys($groupeventsfrom);
1028 }
1029 $grouparray = array();
1030
1031 // We already have the courses to examine in $courses
1032 // For each course...
1033 foreach($groupcourses as $courseid) {
1034 // If the user is an editing teacher in there,
9c81fc35 1035 if(!empty($USER->id) && isteacheredit($courseid, $USER->id)) {
43c3ffbe 1036 // Show events from all groups
1037 if(($grouprecords = get_groups($courseid)) !== false) {
1038 $grouparray = array_merge($grouparray, array_keys($grouprecords));
7423f116 1039 }
1040 }
43c3ffbe 1041 // Otherwise show events from the group he is a member of
1042 else if(isset($USER->groupmember[$courseid])) {
1043 $grouparray[] = $USER->groupmember[$courseid];
6c9584d1 1044 }
7423f116 1045 }
43c3ffbe 1046 if(empty($grouparray)) {
1047 $group = false;
7423f116 1048 }
1049 else {
43c3ffbe 1050 $group = $grouparray;
7423f116 1051 }
1052 }
1053 else {
1054 $group = false;
1055 }
1056}
1057
1058function calendar_edit_event_allowed($event) {
1059 global $USER;
1060
f52f7413 1061 if(empty($USER) || isguest($USER->id)) {
1062 return false;
1063 }
1064
421b0337 1065 if (isadmin($USER->id)) return true; // Admins are allowed anything
7423f116 1066
421b0337 1067 if ($event->courseid > 1) {
7423f116 1068 // Course event, only editing teachers may... edit :P
1069 if(isteacheredit($event->courseid)) {
1070 return true;
1071 }
421b0337 1072
1073 } else if($event->courseid == 0 && $event->groupid != 0) {
7423f116 1074 // Group event
1075 $group = get_record('groups', 'id', $event->groupid);
1076 if($group === false) return false;
1077 if(isteacheredit($group->courseid)) {
1078 return true;
1079 }
421b0337 1080
1081 } else if($event->courseid == 0 && $event->groupid == 0 && $event->userid == $USER->id) {
7423f116 1082 // User event, owned by this user
1083 return true;
1084 }
1085
1086 return false;
1087}
1088
8c54cec6 1089function calendar_get_default_courses($ignoreref = false) {
9ff136e5 1090 global $USER, $CFG, $SESSION;
1091
8c54cec6 1092 if(!empty($SESSION->cal_course_referer) && !$ignoreref) {
9ff136e5 1093 return array($SESSION->cal_course_referer => 1);
1094 }
7423f116 1095
2ef75eee 1096 if(empty($USER)) {
1097 return array();
1098 }
1099
7423f116 1100 $courses = array();
9c81fc35 1101 if(!empty($USER->id) && isadmin($USER->id)) {
95a89225 1102 $courses = get_records_sql('SELECT id, 1 FROM '.$CFG->prefix.'course');
1103 return $courses;
1104 }
3696526b 1105 if(isset($USER->student) && is_array($USER->student)) {
7423f116 1106 $courses = $USER->student + $courses;
1107 }
3696526b 1108 if(isset($USER->teacher) && is_array($USER->teacher)) {
7423f116 1109 $courses = $USER->teacher + $courses;
1110 }
1111 return $courses;
1112}
1113
7423f116 1114function calendar_get_tz_offset() {
1115 global $USER, $CFG;
1116 static $tzfix;
1117
1118 // Caching
1119 if(isset($tzfix)) {
1120 return $tzfix;
1121 }
1122
1123 if(empty($USER)) {
1124 // Don't forget that there are users which have NOT logged in, even as guests
1125 $timezone = $CFG->timezone;
1126 }
1127 else {
1128 // If, on the other hand, we do have a user...
1129 $timezone = $USER->timezone;
1130 if(abs($timezone > 13)) {
1131 // But if the user has specified 'server default' time,
1132 // don't get the server's; get the Moodle $CFG setting
1133 // (Martin's help text on site cfg implies this)
1134 $timezone = $CFG->timezone;
1135 }
1136 }
1137
1138 if(abs($timezone) <= 13) {
1139 $tzfix = $timezone * 3600;
1140 }
1141 else {
3696526b 1142 $tzfix = date('Z');
7423f116 1143 }
1144
1145 return $tzfix;
1146}
1147
1148function calendar_preferences_array() {
1149 return array(
1150 'startwday' => get_string('pref_startwday', 'calendar'),
1151 'maxevents' => get_string('pref_maxevents', 'calendar'),
1152 'lookahead' => get_string('pref_lookahead', 'calendar'),
1b0ebe79 1153 'timeformat' => get_string('pref_timeformat', 'calendar'),
7423f116 1154 );
1155}
1156
1e1ff33b 1157function calendar_preferences_button() {
1158 global $CFG, $USER;
7423f116 1159
1160 // Guests have no preferences
f52f7413 1161 if (empty($USER->id) || isguest()) {
7423f116 1162 return '';
1163 }
1164
1e1ff33b 1165 return "<form target=\"$CFG->framename\" method=\"get\" ".
1166 " action=\"$CFG->wwwroot/calendar/preferences.php\">".
1167 "<input type=\"submit\" value=\"".get_string("preferences", "calendar")." ...\" /></form>";
7423f116 1168}
1169
1170if(!function_exists('array_diff_assoc')) {
1171 // PHP < 4.3.0
1172 function array_diff_assoc($source, $diff) {
1173 $res = $source;
1174 foreach ($diff as $key=>$data) {
1175 unset($res[$key]);
1176 }
1177 return $res;
1178 }
1179}
1180
1181?>