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