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