Refactor course deletion code to eliminate about 70 lines of duplication.
[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
bb4a2e85 41// These are read by the administration component to provide default values
42define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
43define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
44define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1);
45// This is a packed bitfield: day X is "weekend" if $field & (1 << X) is true
46// Default value = 65 = 64 + 1 = 2^6 + 2^0 = Saturday & Sunday
47define('CALENDAR_DEFAULT_WEEKEND', 65);
48
49// Fetch the correct values from admin settings/lang pack
50// If no such settings found, use the above defaults
51$firstday = isset($CFG->calendar_startwday) ? $CFG->calendar_startwday : get_string('firstdayofweek');
52if(!is_numeric($firstday)) {
53 define ('CALENDAR_STARTING_WEEKDAY', CALENDAR_DEFAULT_STARTING_WEEKDAY);
ed151929 54}
55else {
56 define ('CALENDAR_STARTING_WEEKDAY', intval($firstday) % 7);
57}
bb4a2e85 58define ('CALENDAR_UPCOMING_DAYS', isset($CFG->calendar_lookahead) ? intval($CFG->calendar_lookahead) : CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD);
59define ('CALENDAR_UPCOMING_MAXEVENTS', isset($CFG->calendar_maxevents) ? intval($CFG->calendar_maxevents) : CALENDAR_DEFAULT_UPCOMING_MAXEVENTS);
60define ('CALENDAR_WEEKEND', isset($CFG->calendar_weekend) ? intval($CFG->calendar_weekend) : CALENDAR_DEFAULT_WEEKEND);
7423f116 61define ('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
1b0ebe79 62define ('CALENDAR_TF_24', '%H:%M');
63define ('CALENDAR_TF_12', '%I:%M %p');
7423f116 64
f2bffd9e 65$CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
66
bd119567 67// Initialize the session variables here to be sure
68calendar_session_vars();
69
7423f116 70function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
71 global $CFG, $USER;
72
4e17c6f3 73 $display = &New stdClass;
7423f116 74 $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
75 $display->maxwday = $display->minwday + 6;
76
77 $content = '';
78
79 if(!empty($cal_month) && !empty($cal_year)) {
80 $thisdate = usergetdate(time()); // Date and time the user sees at his location
81 if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) {
82 // Navigated to this month
83 $date = $thisdate;
84 $display->thismonth = true;
85 }
86 else {
87 // Navigated to other month, let's do a nice trick and save us a lot of work...
88 if(!checkdate($cal_month, 1, $cal_year)) {
89 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
90 $display->thismonth = true;
91 }
92 else {
93 $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
94 $display->thismonth = false;
95 }
96 }
97 }
98 else {
99 $date = usergetdate(time()); // Date and time the user sees at his location
100 $display->thismonth = true;
101 }
102
103 // Fill in the variables we 're going to use, nice and tidy
104 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
105 $display->maxdays = calendar_days_in_month($m, $y);
106
107 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
108 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
109 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
110
69244b91 111 $startwday = dayofweek(1, $m, $y);
7423f116 112
113 // Align the starting weekday to fall in our display range
114 // This is simple, not foolproof.
115 if($startwday < $display->minwday) {
116 $startwday += 7;
117 }
118
c0b507d1 119 // TODO: THIS IS TEMPORARY CODE!
120 // [pj] I was just reading through this and realized that I when writing this code I was probably
121 // asking for trouble, as all these time manipulations seem to be unnecessary and a simple
122 // make_timestamp would accomplish the same thing. So here goes a test:
1747ee05 123 //$test_start = make_timestamp($y, $m, 1);
124 //$test_end = make_timestamp($y, $m, $display->maxdays, 23, 59, 59);
125 //if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) {
126 //notify('Failed assertion in calendar/lib.php line 126; display->tstart = '.$display->tstart.', dst_offset = '.dst_offset_on($display->tstart).', usertime = '.usertime($display->tstart).', make_t = '.$test_start);
127 //}
128 //if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) {
129 //notify('Failed assertion in calendar/lib.php line 130; display->tend = '.$display->tend.', dst_offset = '.dst_offset_on($display->tend).', usertime = '.usertime($display->tend).', make_t = '.$test_end);
130 //}
c0b507d1 131
132
7423f116 133 // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
d3555a2f 134 $whereclause = calendar_sql_where(
135 usertime($display->tstart) - dst_offset_on($display->tstart),
136 usertime($display->tend) - dst_offset_on($display->tend),
137 $users, $groups, $courses);
7423f116 138
139 if($whereclause === false) {
140 $events = array();
141 }
142 else {
a94329a8 143 $events = get_records_select('event', $whereclause, 'timestart');
7423f116 144 }
145
c635dcda 146 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
9064751b 147 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
c635dcda 148 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
149 // arguments to this function.
150
7bd1677c 151 $morehref = '';
152 if(!empty($courses)) {
e749554e 153 $courses = array_diff($courses, array(SITEID));
7bd1677c 154 if(count($courses) == 1) {
155 $morehref = '&amp;course='.reset($courses);
156 }
c635dcda 157 }
158
7423f116 159 // We want to have easy access by day, since the display is on a per-day basis.
160 // Arguments passed by reference.
7b38bfa6 161 //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
162 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday);
7423f116 163
7d50b363 164 $content .= '<table class="minicalendar">'; // Begin table
f136e4c5 165 $content .= '<tr class="weekdays">'; // Header row: day names
7423f116 166
167 // Print out the names of the weekdays
168 $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
169 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
170 // This uses the % operator to get the correct weekday no matter what shift we have
171 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
7d50b363 172 $content .= '<th>'.get_string($days[$i % 7], 'calendar').'</th>';
7423f116 173 }
174
f136e4c5 175 $content .= '</tr><tr>'; // End of day names; prepare for day numbers
7423f116 176
177 // For the table display. $week is the row; $dayweek is the column.
7423f116 178 $dayweek = $startwday;
179
180 // Paddding (the first week may have blank days in the beginning)
181 for($i = $display->minwday; $i < $startwday; ++$i) {
92668ad2 182 $content .= '<td>&nbsp;</td>'."\n";
7423f116 183 }
184
d56d4e23 185 $strftimetimedayshort = get_string('strftimedayshort');
186
7423f116 187 // Now display all the calendar
188 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
189 if($dayweek > $display->maxwday) {
190 // We need to change week (table row)
d56d4e23 191 $content .= '</tr><tr>';
7423f116 192 $dayweek = $display->minwday;
7423f116 193 }
92668ad2 194
7423f116 195 // Reset vars
7423f116 196 $cell = '';
bb4a2e85 197 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
7423f116 198 // Weekend. This is true no matter what the exact range is.
e2aa618b 199 $class = 'weekend day';
7423f116 200 }
201 else {
202 // Normal working day.
e2aa618b 203 $class = 'day';
7423f116 204 }
205
206 // Special visual fx if an event is defined
207 if(isset($eventsbyday[$day])) {
c635dcda 208 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $day, $m, $y);
f434283f 209
7423f116 210 // OverLib popup
211 $popupcontent = '';
212 foreach($eventsbyday[$day] as $eventid) {
41d30a8e 213 if (!isset($events[$eventid])) {
214 continue;
4e17c6f3 215 }
41d30a8e 216 $event = $events[$eventid];
41d30a8e 217 if(!empty($event->modulename)) {
218 $popupicon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
219 $popupalt = $event->modulename;
220
9064751b 221 } else if ($event->courseid == SITEID) { // Site event
41d30a8e 222 $popupicon = $CFG->pixpath.'/c/site.gif';
223 $popupalt = '';
c3d3b6d4 224 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
41d30a8e 225 $popupicon = $CFG->pixpath.'/c/course.gif';
c9b05b32 226 $popupalt = '';
41d30a8e 227 } else if ($event->groupid) { // Group event
228 $popupicon = $CFG->pixpath.'/c/group.gif';
229 $popupalt = '';
230 } else if ($event->userid) { // User event
231 $popupicon = $CFG->pixpath.'/c/user.gif';
232 $popupalt = '';
4e17c6f3 233 }
fb73f3b3 234 $popupcontent .= '<div><img height="16" width="16" src="'.$popupicon.'" style="vertical-align: middle; margin-right: 4px;" alt="'.$popupalt.'" /><a href="'.$dayhref.'">'.format_string($event->name,true).'</a></div>';
7423f116 235 }
236
d56d4e23 237 $popupcaption = get_string('eventsfor', 'calendar', userdate($events[$eventid]->timestart, $strftimetimedayshort));
9cb8879b 238 $popupcontent = str_replace("'", "\'", htmlspecialchars($popupcontent));
7423f116 239 $popup = 'onmouseover="return overlib(\''.$popupcontent.'\', CAPTION, \''.$popupcaption.'\');" onmouseout="return nd();"';
240
241 // Class and cell content
242 if(isset($typesbyday[$day]['startglobal'])) {
edbe6c1b 243 $class .= ' event_global';
7423f116 244 }
245 else if(isset($typesbyday[$day]['startcourse'])) {
edbe6c1b 246 $class .= ' event_course';
7423f116 247 }
248 else if(isset($typesbyday[$day]['startgroup'])) {
edbe6c1b 249 $class .= ' event_group';
7423f116 250 }
251 else if(isset($typesbyday[$day]['startuser'])) {
edbe6c1b 252 $class .= ' event_user';
7423f116 253 }
05fcc5fd 254 $cell = '<a href="'.$dayhref.'" '.$popup.'>'.$day.'</a>';
7423f116 255 }
256 else {
05fcc5fd 257 $cell = $day;
7423f116 258 }
259
260 if(isset($typesbyday[$day]['durationglobal'])) {
edbe6c1b 261 $class .= ' duration_global';
7423f116 262 }
263 else if(isset($typesbyday[$day]['durationcourse'])) {
edbe6c1b 264 $class .= ' duration_course';
7423f116 265 }
266 else if(isset($typesbyday[$day]['durationgroup'])) {
edbe6c1b 267 $class .= ' duration_group';
7423f116 268 }
269 else if(isset($typesbyday[$day]['durationuser'])) {
edbe6c1b 270 $class .= ' duration_user';
7423f116 271 }
272
273 // Special visual fx for today
274 if($display->thismonth && $day == $d) {
edbe6c1b 275 $class .= ' today';
7423f116 276 }
277
278 // Just display it
92668ad2 279 if(!empty($class)) {
d56d4e23 280 $class = ' class="'.$class.'"';
92668ad2 281 }
282 $content .= '<td'.$class.'>'.$cell."</td>\n";
7423f116 283 }
284
285 // Paddding (the last week may have blank days at the end)
286 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
92668ad2 287 $content .= '<td>&nbsp;</td>';
7423f116 288 }
289 $content .= '</tr>'; // Last row ends
290
f136e4c5 291 $content .= '</table>'; // Tabular display of days ends
7423f116 292
293 return $content;
294}
295
9958a08c 296function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
7423f116 297 global $CFG;
298
1f473774 299 $display = &new stdClass;
7423f116 300 $display->range = $daysinfuture; // How many days in the future we 'll look
301 $display->maxevents = $maxevents;
302
303 $output = array();
304
305 // Prepare "course caching", since it may save us a lot of queries
306 $coursecache = array();
307
308 $processed = 0;
309 $now = time(); // We 'll need this later
9d567178 310 $usermidnighttoday = usergetmidnight($now);
7423f116 311
9958a08c 312 if ($fromtime) {
313 $display->tstart = $fromtime;
314 } else {
9d567178 315 $display->tstart = $usermidnighttoday;
9958a08c 316 }
7423f116 317
1f473774 318 // This works correctly with respect to the user's DST, but it is accurate
319 // only because $fromtime is always the exact midnight of some day!
320 $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1;
7423f116 321
322 // Get the events matching our criteria
323 $whereclause = calendar_sql_where($display->tstart, $display->tend, $users, $groups, $courses);
9958a08c 324 if ($whereclause === false) {
7423f116 325 $events = false;
9958a08c 326 } else {
a94329a8 327 $events = get_records_select('event', $whereclause, 'timestart');
7423f116 328 }
329
c635dcda 330 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
9064751b 331 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
c635dcda 332 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
333 // arguments to this function.
334
7bd1677c 335 $morehref = '';
336 if(!empty($courses)) {
e749554e 337 $courses = array_diff($courses, array(SITEID));
7bd1677c 338 if(count($courses) == 1) {
339 $morehref = '&amp;course='.reset($courses);
340 }
c635dcda 341 }
342
7423f116 343 if($events !== false) {
fa22fd5f 344
7423f116 345 foreach($events as $event) {
9d567178 346
347 if($processed >= $display->maxevents) {
348 break;
349 }
7423f116 350
3c134875 351 $event->time = calendar_format_event_time($event, $now, $morehref);
352 $output[] = $event;
353 ++$processed;
354 }
355 }
356 return $output;
357}
7423f116 358
9df8ff44 359function calendar_add_event_metadata($event) {
360 global $CFG;
fb73f3b3 361
362 //Support multilang in event->name
363 $event->name = format_string($event->name,true);
364
3c134875 365 if(!empty($event->modulename)) { // Activity event
366 // The module name is set. I will assume that it has to be displayed, and
367 // also that it is an automatically-generated event. And of course that the
368 // fields for get_coursemodule_from_instance are set correctly.
369 $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance);
7423f116 370
3c134875 371 if ($module === false) {
372 return;
373 }
9958a08c 374
3c134875 375 $modulename = get_string('modulename', $event->modulename);
376 $eventtype = get_string($event->eventtype, $event->modulename);
377 $icon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
9958a08c 378
3c134875 379 $event->icon = '<img height="16" width="16" src="'.$icon.'" alt="" title="'.$modulename.'" style="vertical-align: middle;" />';
fb73f3b3 380 $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
3c134875 381 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$coursecache[$module->course]->fullname.'</a>';
382 $event->cmid = $module->id;
9958a08c 383
9958a08c 384
3c134875 385 } else if($event->courseid == SITEID) { // Site event
386 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/site.gif" alt="" style="vertical-align: middle;" />';
9958a08c 387
3c134875 388 } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
389 calendar_get_course_cached($coursecache, $event->courseid);
390 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/course.gif" alt="" style="vertical-align: middle;" />';
391 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>';
7423f116 392
3c134875 393 } else if ($event->groupid) { // Group event
394 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/group.gif" alt="" style="vertical-align: middle;" />';
9958a08c 395
3c134875 396 } else if($event->userid) { // User event
397 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/user.gif" alt="" style="vertical-align: middle;" />';
398 }
9958a08c 399
9df8ff44 400 return $event;
401}
402
403function calendar_print_event($event) {
404 global $CFG, $USER;
405
406 static $strftimetime;
407
408 $event = calendar_add_event_metadata($event);
f54a86af 409 echo '<table class="event" cellspacing="0">';
ed8ea92b 410 echo '<tr><td class="picture">';
3c134875 411 if (!empty($event->icon)) {
412 echo $event->icon;
413 } else {
414 print_spacer(16,16);
415 }
416 echo '</td>';
df349d1d 417 echo '<td class="topic">';
9958a08c 418
3c134875 419 if (!empty($event->referer)) {
e96ba29d 420 echo '<div class="referer">'.$event->referer.'</div>';
3c134875 421 } else {
ed8ea92b 422 echo '<div class="name">'.$event->name."</div>";
3c134875 423 }
424 if (!empty($event->courselink)) {
ed8ea92b 425 echo '<div class="course">'.$event->courselink.' </div>';
3c134875 426 }
427 if (!empty($event->time)) {
ed8ea92b 428 echo '<span class="date">'.$event->time.'</span>';
3c134875 429 } else {
ed8ea92b 430 echo '<span class="date">'.calendar_time_representation($event->timestart).'</span>';
3c134875 431 }
9958a08c 432
ed8ea92b 433 echo '</td></tr>';
434 echo '<tr><td class="side">&nbsp;</td>';
435 echo '<td class="description">';
3c134875 436 echo format_text($event->description, FORMAT_HTML);
437 if (calendar_edit_event_allowed($event)) {
ed8ea92b 438 echo '<div class="commands">';
3c134875 439 if (empty($event->cmid)) {
440 $editlink = CALENDAR_URL.'event.php?action=edit&amp;id='.$event->id;
441 $deletelink = CALENDAR_URL.'event.php?action=delete&amp;id='.$event->id;
442 } else {
443 $editlink = $CFG->wwwroot.'/course/mod.php?update='.$event->cmid.'&amp;return=true&amp;sesskey='.$USER->sesskey;
444 $deletelink = $CFG->wwwroot.'/course/mod.php?delete='.$event->cmid.'&amp;sesskey='.$USER->sesskey;;
7423f116 445 }
3c134875 446 echo ' <a href="'.$editlink.'"><img
447 src="'.$CFG->pixpath.'/t/edit.gif" alt="'.get_string('tt_editevent', 'calendar').'"
448 title="'.get_string('tt_editevent', 'calendar').'" /></a>';
449 echo ' <a href="'.$deletelink.'"><img
450 src="'.$CFG->pixpath.'/t/delete.gif" alt="'.get_string('tt_deleteevent', 'calendar').'"
451 title="'.get_string('tt_deleteevent', 'calendar').'" /></a>';
452 echo '</div>';
7423f116 453 }
3c134875 454 echo '</td></tr></table>';
455
7423f116 456}
457
8c165fe9 458function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
7423f116 459 $whereclause = '';
460 // Quick test
461 if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
462 return false;
463 }
482dbe0c 464
7423f116 465 if(is_array($users) && !empty($users)) {
466 // Events from a number of users
467 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 468 $whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)';
7423f116 469 }
470 else if(is_numeric($users)) {
471 // Events from one user
472 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 473 $whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)';
7423f116 474 }
475 else if($users === true) {
476 // Events from ALL users
477 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 478 $whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)';
7423f116 479 }
f52f7413 480 else if($users === false) {
6e957c41 481 // No user at all, do nothing
f52f7413 482 }
482dbe0c 483
7423f116 484 if(is_array($groups) && !empty($groups)) {
485 // Events from a number of groups
486 if(!empty($whereclause)) $whereclause .= ' OR';
487 $whereclause .= ' groupid IN ('.implode(',', $groups).')';
488 }
489 else if(is_numeric($groups)) {
490 // Events from one group
491 if(!empty($whereclause)) $whereclause .= ' OR ';
492 $whereclause .= ' groupid = '.$groups;
493 }
494 else if($groups === true) {
495 // Events from ALL groups
496 if(!empty($whereclause)) $whereclause .= ' OR ';
497 $whereclause .= ' groupid != 0';
498 }
482dbe0c 499 // boolean false (no groups at all): we don't need to do anything
500
f52f7413 501 if(is_array($courses)) {
502 // A number of courses (maybe none at all!)
503 if(!empty($courses)) {
504 if(!empty($whereclause)) {
505 $whereclause .= ' OR';
506 }
6e957c41 507 $whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))';
f52f7413 508 }
509 else {
510 // This means NO courses, not that we don't care!
09d36284 511 // No need to do anything
f52f7413 512 }
7423f116 513 }
514 else if(is_numeric($courses)) {
515 // One course
516 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 517 $whereclause .= ' (groupid = 0 AND courseid = '.$courses.')';
7423f116 518 }
519 else if($courses === true) {
520 // Events from ALL courses
521 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 522 $whereclause .= ' (groupid = 0 AND courseid != 0)';
7423f116 523 }
8c165fe9 524
482dbe0c 525 // Security check: if, by now, we have NOTHING in $whereclause, then it means
526 // that NO event-selecting clauses were defined. Thus, we won't be returning ANY
527 // events no matter what. Allowing the code to proceed might return a completely
528 // valid query with only time constraints, thus selecting ALL events in that time frame!
529 if(empty($whereclause)) {
530 return false;
531 }
532
7423f116 533 if($withduration) {
534 $timeclause = 'timestart + timeduration >= '.$tstart.' AND timestart <= '.$tend;
535 }
536 else {
537 $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend;
538 }
539 if(!empty($whereclause)) {
540 // We have additional constraints
541 $whereclause = $timeclause.' AND ('.$whereclause.')';
542 }
543 else {
544 // Just basic time filtering
545 $whereclause = $timeclause;
546 }
f52f7413 547
0ad072de 548 if ($ignorehidden) {
549 $whereclause .= ' AND visible = 1';
550 }
551
552 return $whereclause;
7423f116 553}
554
555function calendar_top_controls($type, $data) {
f2bffd9e 556 global $CFG, $CALENDARDAYS;
7423f116 557 $content = '';
558 if(!isset($data['d'])) {
559 $data['d'] = 1;
560 }
5147ad48 561
562 if(!checkdate($data['m'], $data['d'], $data['y'])) {
563 $time = time();
564 }
565 else {
566 $time = make_timestamp($data['y'], $data['m'], $data['d']);
567 }
568 $date = usergetdate($time);
569
7423f116 570 $data['m'] = $date['mon'];
571 $data['y'] = $date['year'];
7423f116 572
573 switch($type) {
574 case 'frontpage':
575 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
576 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
73341343 577 $nextlink = calendar_get_link_tag('&gt;&gt;', 'index.php?', 0, $nextmonth, $nextyear);
578 $prevlink = calendar_get_link_tag('&lt;&lt;', 'index.php?', 0, $prevmonth, $prevyear);
1f473774 579 $content .= '<table class="calendar-controls"><tr>';
580 $content .= '<td class="previous">'.$prevlink."</td>\n";
5147ad48 581 $content .= '<td class="current"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear')).'</a></td>';
1f473774 582 $content .= '<td class="next">'.$nextlink."</td>\n";
7423f116 583 $content .= '</tr></table>';
584 break;
585 case 'course':
586 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
587 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
588 $nextlink = calendar_get_link_tag('&gt;&gt;', 'view.php?id='.$data['id'].'&amp;', 0, $nextmonth, $nextyear);
589 $prevlink = calendar_get_link_tag('&lt;&lt;', 'view.php?id='.$data['id'].'&amp;', 0, $prevmonth, $prevyear);
1f473774 590 $content .= '<table class="calendar-controls"><tr>';
591 $content .= '<td class="previous">'.$prevlink."</td>\n";
5147ad48 592 $content .= '<td class="current"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;course='.$data['id'].'&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear')).'</a></td>';
1f473774 593 $content .= '<td class="next">'.$nextlink."</td>\n";
7423f116 594 $content .= '</tr></table>';
595 break;
596 case 'upcoming':
5147ad48 597 $content .= '<div style="text-align: center;"><a href="'.CALENDAR_URL.'view.php?view=upcoming">'.userdate($time, get_string('strftimemonthyear'))."</a></div>\n";
7423f116 598 break;
599 case 'display':
5147ad48 600 $content .= '<div style="text-align: center;"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear'))."</a></div>\n";
7423f116 601 break;
602 case 'month':
603 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
604 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
5147ad48 605 $prevdate = make_timestamp($prevyear, $prevmonth, 1);
606 $nextdate = make_timestamp($nextyear, $nextmonth, 1);
1f473774 607 $content .= '<table class="calendar-controls"><tr>';
5147ad48 608 $content .= '<td class="previous"><a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $prevmonth, $prevyear).'">&lt;&lt; '.userdate($prevdate, get_string('strftimemonthyear')).'</a></td>';
609 $content .= '<td class="current">'.userdate($time, get_string('strftimemonthyear'))."</td>\n";
610 $content .= '<td class="next"><a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $nextmonth, $nextyear).'">'.userdate($nextdate, get_string('strftimemonthyear'))." &gt;&gt;</a></td>\n";
7423f116 611 $content .= "</tr></table>\n";
612 break;
613 case 'day':
614 $data['d'] = $date['mday']; // Just for convenience
5147ad48 615 $prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1));
616 $nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1));
f2bffd9e 617 $prevname = calendar_wday_name($CALENDARDAYS[$prevdate['wday']]);
618 $nextname = calendar_wday_name($CALENDARDAYS[$nextdate['wday']]);
1f473774 619 $content .= '<table class="calendar-controls"><tr>';
620 $content .= '<td class="previous"><a href="'.calendar_get_link_href('view.php?view=day&amp;', $prevdate['mday'], $prevdate['mon'], $prevdate['year']).'">&lt;&lt; '.$prevname."</a></td>\n";
6e8e8ec6 621
622 // Get the format string
623 $text = get_string('strftimedaydate');
3477a900 624 /*
6e8e8ec6 625 // Regexp hackery to make a link out of the month/year part
626 $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);
5147ad48 627 $text = ereg_replace('(F.+Y|Y.+F|Y.+m[^ ]+)', '<a href="'.calendar_get_link_href('view.php?view=month&amp;', 1, $data['m'], $data['y']).'">\\1</a>', $text);
3477a900 628 */
6e8e8ec6 629 // Replace with actual values and lose any day leading zero
3477a900 630 $text = userdate($time, $text);
6e8e8ec6 631 // Print the actual thing
1f473774 632 $content .= '<td class="current">'.$text.'</td>';
6e8e8ec6 633
1f473774 634 $content .= '<td class="next"><a href="'.calendar_get_link_href('view.php?view=day&amp;', $nextdate['mday'], $nextdate['mon'], $nextdate['year']).'">'.$nextname." &gt;&gt;</a></td>\n";
635 $content .= '</tr></table>';
7423f116 636 break;
637 }
638 return $content;
639}
640
3cb9ee39 641function calendar_filter_controls($type, $vars = NULL, $course = NULL) {
43c3ffbe 642 global $CFG, $SESSION, $USER;
7423f116 643
48f508ab 644 $groupevents = true;
3cb9ee39 645 $getvars = '';
7f4d18fc 646
647 $id = optional_param( 'id',0,PARAM_INT );
d715f7c4 648
34bf3ad4 649 switch($type) {
eb15f829 650 case 'event':
34bf3ad4 651 case 'upcoming':
34bf3ad4 652 case 'day':
c3f463ca 653 case 'month':
eb15f829 654 $getvars = '&amp;from='.$type;
34bf3ad4 655 break;
656 case 'course':
fac01360 657 if ($id > 0) {
7f4d18fc 658 $getvars = '&amp;from=course&amp;id='.$id;
2eb68e6f 659 } else {
660 $getvars = '&amp;from=course';
661 }
5a74ffd3 662 if (isset($course->groupmode) and $course->groupmode == NOGROUPS and $course->groupmodeforce) {
34bf3ad4 663 $groupevents = false;
664 }
665 break;
d715f7c4 666 }
34bf3ad4 667
3cb9ee39 668 if (!empty($vars)) {
34bf3ad4 669 $getvars .= '&amp;'.$vars;
48f508ab 670 }
7423f116 671
2ad2cbc3 672 $content = '<table>';
7423f116 673
48f508ab 674 $content .= '<tr>';
675 if($SESSION->cal_show_global) {
edbe6c1b 676 $content .= '<td class="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 677 }
678 else {
2b7591cc 679 $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 680 }
681 if($SESSION->cal_show_course) {
edbe6c1b 682 $content .= '<td class="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 683 }
48f508ab 684 else {
2b7591cc 685 $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 686 }
48f508ab 687
1066e0dc 688 if(!empty($USER->id) && !isguest()) {
43c3ffbe 689 $content .= "</tr>\n<tr>";
690
691 if($groupevents) {
692 // This course MIGHT have group events defined, so show the filter
693 if($SESSION->cal_show_groups) {
edbe6c1b 694 $content .= '<td class="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 695 } else {
43c3ffbe 696 $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";
697 }
cd6469cb 698 if ($SESSION->cal_show_user) {
edbe6c1b 699 $content .= '<td class="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 700 } else {
43c3ffbe 701 $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";
702 }
cd6469cb 703
704 } else {
43c3ffbe 705 // This course CANNOT have group events, so lose the filter
cd6469cb 706 $content .= '<td style="width: 8px;"></td><td>&nbsp;</td>'."\n";
707
43c3ffbe 708 if($SESSION->cal_show_user) {
edbe6c1b 709 $content .= '<td class="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 710 } else {
711 $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 712 }
48f508ab 713 }
714 }
715 $content .= "</tr>\n</table>\n";
716
7423f116 717 return $content;
718}
719
720function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
721
0ef7c973 722 static $shortformat;
723 if(empty($shortformat)) {
e70fdac0 724 $shortformat = get_string('strftimedayshort');
0ef7c973 725 }
726
7423f116 727 if($now === false) {
728 $now = time();
729 }
730
731 // To have it in one place, if a change is needed
e70fdac0 732 $formal = userdate($tstamp, $shortformat);
7423f116 733
7b38bfa6 734 $datestamp = usergetdate($tstamp);
735 $datenow = usergetdate($now);
7423f116 736
737 if($usecommonwords == false) {
738 // We don't want words, just a date
739 return $formal;
740 }
7b38bfa6 741 else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
7423f116 742 // Today
743 return get_string('today', 'calendar');
744 }
7b38bfa6 745 else if(
746 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
747 ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1)
748 ) {
7423f116 749 // Yesterday
750 return get_string('yesterday', 'calendar');
751 }
7b38bfa6 752 else if(
753 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
754 ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1)
755 ) {
7423f116 756 // Tomorrow
757 return get_string('tomorrow', 'calendar');
758 }
759 else {
760 return $formal;
761 }
762}
763
764function calendar_time_representation($time) {
1b0ebe79 765 static $langtimeformat = NULL;
766 if($langtimeformat === NULL) {
767 $langtimeformat = get_string('strftimetime');
768 }
769 $timeformat = get_user_preferences('calendar_timeformat');
770 // The ? is needed because the preference might be present, but empty
771 return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat);
7423f116 772}
773
774function calendar_get_link_href($linkbase, $d, $m, $y) {
775 if(empty($linkbase)) return '';
776 $paramstr = '';
777 if(!empty($d)) $paramstr .= '&amp;cal_d='.$d;
778 if(!empty($m)) $paramstr .= '&amp;cal_m='.$m;
779 if(!empty($y)) $paramstr .= '&amp;cal_y='.$y;
780 if(!empty($paramstr)) $paramstr = substr($paramstr, 5);
781 return $linkbase.$paramstr;
782}
783
784function calendar_get_link_tag($text, $linkbase, $d, $m, $y) {
785 $href = calendar_get_link_href($linkbase, $d, $m, $y);
786 if(empty($href)) return $text;
787 return '<a href="'.$href.'">'.$text.'</a>';
788}
789
7423f116 790function calendar_wday_name($englishname) {
791 return get_string(strtolower($englishname), 'calendar');
792}
793
794function calendar_days_in_month($month, $year) {
7b38bfa6 795 return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
7423f116 796}
797
e749554e 798function calendar_get_sideblock_upcoming($events, $linkhref = NULL) {
7423f116 799 $content = '';
800 $lines = count($events);
396b61f0 801 if (!$lines) {
802 return $content;
803 }
7423f116 804
396b61f0 805 for ($i = 0; $i < $lines; ++$i) {
b0ac9180 806 if (!isset($events[$i]->time)) { // Just for robustness
807 continue;
808 }
9df8ff44 809 $events[$i] = calendar_add_event_metadata($events[$i]);
edbe6c1b 810 $content .= '<div class="event">'.$events[$i]->icon.' ';
43c3ffbe 811 if (!empty($events[$i]->referer)) {
7423f116 812 // That's an activity event, so let's provide the hyperlink
396b61f0 813 $content .= $events[$i]->referer;
814 } else {
e749554e 815 if(!empty($linkhref)) {
816 $ed = usergetdate($events[$i]->timestart);
817 $href = calendar_get_link_href(CALENDAR_URL.$linkhref, $ed['mday'], $ed['mon'], $ed['year']);
818 $content .= '<a href="'.$href.'">'.$events[$i]->name.'</a>';
819 }
820 else {
821 $content .= $events[$i]->name;
822 }
7423f116 823 }
9ecf051d 824 $events[$i]->time = str_replace('&raquo;', '<br />&raquo;', $events[$i]->time);
825 $content .= '<div class="date">'.$events[$i]->time.'</div></div>';
396b61f0 826 if ($i < $lines - 1) $content .= '<hr />';
7423f116 827 }
828
829 return $content;
7423f116 830}
831
832function calendar_add_month($month, $year) {
833 if($month == 12) {
834 return array(1, $year + 1);
835 }
836 else {
837 return array($month + 1, $year);
838 }
839}
840
841function calendar_sub_month($month, $year) {
842 if($month == 1) {
843 return array(12, $year - 1);
844 }
845 else {
846 return array($month - 1, $year);
847 }
848}
849
7b38bfa6 850function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday) {
7423f116 851 $eventsbyday = array();
852 $typesbyday = array();
853 $durationbyday = array();
854
855 if($events === false) {
856 return;
857 }
858
7423f116 859 foreach($events as $event) {
7423f116 860
7b38bfa6 861 $startdate = usergetdate($event->timestart);
862 $enddate = usergetdate($event->timestart + $event->timeduration);
7423f116 863
7b38bfa6 864 // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair
ef618501 865 if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
7b38bfa6 866 // Out of bounds
867 continue;
7423f116 868 }
869
7b38bfa6 870 $eventdaystart = intval($startdate['mday']);
7423f116 871
7b38bfa6 872 if($startdate['mon'] == $month && $startdate['year'] == $year) {
873 // Give the event to its day
874 $eventsbyday[$eventdaystart][] = $event->id;
7423f116 875
7b38bfa6 876 // Mark the day as having such an event
9064751b 877 if($event->courseid == SITEID && $event->groupid == 0) {
7b38bfa6 878 $typesbyday[$eventdaystart]['startglobal'] = true;
879 }
c3d3b6d4 880 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
7b38bfa6 881 $typesbyday[$eventdaystart]['startcourse'] = true;
882 }
883 else if($event->groupid) {
884 $typesbyday[$eventdaystart]['startgroup'] = true;
885 }
886 else if($event->userid) {
887 $typesbyday[$eventdaystart]['startuser'] = true;
888 }
889 }
7423f116 890
7b38bfa6 891 if($event->timeduration == 0) {
892 // Proceed with the next
893 continue;
894 }
7423f116 895
7b38bfa6 896 // The event starts on $month $year or before. So...
ef618501 897 $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0;
7b38bfa6 898
899 // Also, it ends on $month $year or later...
900 $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year);
901
902 // Mark all days between $lowerbound and $upperbound (inclusive) as duration
903 for($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
904 $durationbyday[$i][] = $event->id;
9064751b 905 if($event->courseid == SITEID && $event->groupid == 0) {
7b38bfa6 906 $typesbyday[$i]['durationglobal'] = true;
907 }
c3d3b6d4 908 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
7b38bfa6 909 $typesbyday[$i]['durationcourse'] = true;
910 }
911 else if($event->groupid) {
912 $typesbyday[$i]['durationgroup'] = true;
913 }
914 else if($event->userid) {
915 $typesbyday[$i]['durationuser'] = true;
7423f116 916 }
917 }
7b38bfa6 918
7423f116 919 }
920 return;
921}
922
b63c0ee5 923function calendar_get_module_cached(&$coursecache, $modulename, $instance) {
924 $module = get_coursemodule_from_instance($modulename, $instance);
7423f116 925
926 if($module === false) return false;
b63c0ee5 927 if(!calendar_get_course_cached($coursecache, $module->course)) {
7423f116 928 return false;
929 }
930 return $module;
931}
932
933function calendar_get_course_cached(&$coursecache, $courseid) {
934 if(!isset($coursecache[$courseid])) {
935 $coursecache[$courseid] = get_record('course', 'id', $courseid);
936 }
937 return $coursecache[$courseid];
938}
939
940function calendar_session_vars() {
941 global $SESSION, $USER;
942
1066e0dc 943 if(!empty($USER->id) && isset($USER->realuser) && !isset($SESSION->cal_loggedinas)) {
02ce3848 944 // We just logged in as someone else, update the filtering
945 unset($SESSION->cal_users_shown);
946 unset($SESSION->cal_courses_shown);
947 $SESSION->cal_loggedinas = true;
054193be 948 if(intval(get_user_preferences('calendar_persistflt', 0))) {
949 calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
950 }
02ce3848 951 }
1066e0dc 952 else if(!empty($USER->id) && !isset($USER->realuser) && isset($SESSION->cal_loggedinas)) {
02ce3848 953 // We just logged back to our real self, update again
954 unset($SESSION->cal_users_shown);
955 unset($SESSION->cal_courses_shown);
956 unset($SESSION->cal_loggedinas);
054193be 957 if(intval(get_user_preferences('calendar_persistflt', 0))) {
958 calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
959 }
02ce3848 960 }
961
7423f116 962 if(!isset($SESSION->cal_course_referer)) {
963 $SESSION->cal_course_referer = 0;
964 }
965 if(!isset($SESSION->cal_show_global)) {
966 $SESSION->cal_show_global = true;
967 }
968 if(!isset($SESSION->cal_show_groups)) {
969 $SESSION->cal_show_groups = true;
970 }
971 if(!isset($SESSION->cal_show_course)) {
972 $SESSION->cal_show_course = true;
973 }
974 if(!isset($SESSION->cal_show_user)) {
89adb174 975 $SESSION->cal_show_user = true;
7423f116 976 }
43c3ffbe 977 if(empty($SESSION->cal_courses_shown)) {
978 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
979 }
89adb174 980 if(empty($SESSION->cal_users_shown)) {
981 // The empty() instead of !isset() here makes a whole world of difference,
982 // as it will automatically change to the user's id when the user first logs
983 // in. With !isset(), it would never do that.
984 $SESSION->cal_users_shown = isset($USER->id) ? $USER->id : false;
985 }
986 else if(is_numeric($SESSION->cal_users_shown) && !empty($USER->id) && $SESSION->cal_users_shown != $USER->id) {
987 // Follow the white rabbit, for example if a teacher logs in as a student
988 $SESSION->cal_users_shown = $USER->id;
989 }
7423f116 990}
991
992function calendar_overlib_html() {
9c6bf80e 993 return '<div id="overDiv" style="position: absolute; visibility: hidden; z-index:1000;"></div>'
994 .'<script type="text/javascript" src="'.CALENDAR_URL.'overlib.cfg.php"></script>';
7423f116 995}
996
7423f116 997function calendar_set_referring_course($courseid) {
998 global $SESSION;
999 $SESSION->cal_course_referer = intval($courseid);
1000}
1001
43c3ffbe 1002function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) {
257e3f4c 1003 global $SESSION, $USER, $CFG;
1004
43c3ffbe 1005 // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause
51f8a12f 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.
43c3ffbe 1008 if(is_string($SESSION->cal_courses_shown)) {
1009 $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown);
51f8a12f 1010 }
1011
43c3ffbe 1012 if($courseeventsfrom === NULL) {
1013 $courseeventsfrom = $SESSION->cal_courses_shown;
7423f116 1014 }
43c3ffbe 1015 if($groupeventsfrom === NULL) {
1016 $groupeventsfrom = $SESSION->cal_courses_shown;
7423f116 1017 }
1018
43c3ffbe 1019 if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) {
1020 if(is_int($courseeventsfrom)) {
9064751b 1021 $courses = array(SITEID, $courseeventsfrom);
7423f116 1022 }
43c3ffbe 1023 else if(is_array($courseeventsfrom)) {
1024 $courses = array_keys($courseeventsfrom);
9064751b 1025 $courses[] = SITEID;
7423f116 1026 }
1027 }
43c3ffbe 1028 else if($SESSION->cal_show_course) {
1029 if(is_int($courseeventsfrom)) {
1030 $courses = array($courseeventsfrom);
7423f116 1031 }
43c3ffbe 1032 else if(is_array($courseeventsfrom)) {
1033 $courses = array_keys($courseeventsfrom);
7423f116 1034 }
e749554e 1035 $courses = array_diff($courses, array(SITEID));
7423f116 1036 }
1037 else if($SESSION->cal_show_global) {
e749554e 1038 $courses = array(SITEID);
7423f116 1039 }
1040 else {
1041 $courses = false;
1042 }
1043
43c3ffbe 1044 if($SESSION->cal_show_user || $ignorefilters) {
89adb174 1045 // This doesn't work for arrays yet (maybe someday it will)
1046 $user = $SESSION->cal_users_shown;
7423f116 1047 }
1048 else {
1049 $user = false;
1050 }
43c3ffbe 1051 if($SESSION->cal_show_groups || $ignorefilters) {
1052 if(is_int($groupeventsfrom)) {
1053 $groupcourses = array($groupeventsfrom);
7423f116 1054 }
43c3ffbe 1055 else if(is_array($groupeventsfrom)) {
1056 $groupcourses = array_keys($groupeventsfrom);
1057 }
257e3f4c 1058
1059 if(isadmin() && !empty($CFG->calendar_adminseesall)) {
1060 $group = true;
1061 }
1062 else {
1063 $grouparray = array();
69762501 1064 $groupmodes = NULL;
1065
257e3f4c 1066 // We already have the courses to examine in $courses
1067 // For each course...
1068 foreach($groupcourses as $courseid) {
69762501 1069
257e3f4c 1070 // If the user is an editing teacher in there,
1071 if(!empty($USER->id) && isteacheredit($courseid, $USER->id)) {
69762501 1072
1073 // The first time we get in here, retrieve all groupmodes at once
1074 if($groupmodes === NULL) {
3bb2ac06 1075 $groupmodes = get_records_list('course', 'id', implode(',', $groupcourses), '', 'id, groupmode, groupmodeforce');
69762501 1076 }
1077
1078 // If this course has groups, show events from all of them
5a74ffd3 1079 if(isset($groupmodes[$courseid]) && ($groupmodes[$courseid]->groupmode != NOGROUPS || !$groupmodes[$courseid]->groupmodeforce) && ($grouprecords = get_groups($courseid)) !== false) {
257e3f4c 1080 $grouparray = array_merge($grouparray, array_keys($grouprecords));
1081 }
1082 }
69762501 1083
1084 // Otherwise (not editing teacher) show events from the group he is a member of
257e3f4c 1085 else if(isset($USER->groupmember[$courseid])) {
fa22fd5f 1086 //changed to 2D array
1087 foreach ($USER->groupmember[$courseid] as $groupid){
1088 $grouparray[] = $groupid;
1089 }
7423f116 1090 }
1091 }
257e3f4c 1092 if(empty($grouparray)) {
1093 $group = false;
1094 }
1095 else {
1096 $group = $grouparray;
6c9584d1 1097 }
7423f116 1098 }
257e3f4c 1099
7423f116 1100 }
1101 else {
1102 $group = false;
1103 }
1104}
1105
1106function calendar_edit_event_allowed($event) {
1107 global $USER;
1108
1066e0dc 1109 if(empty($USER->id) || isguest($USER->id)) {
f52f7413 1110 return false;
1111 }
1112
421b0337 1113 if (isadmin($USER->id)) return true; // Admins are allowed anything
7423f116 1114
69244b91 1115 if ($event->courseid != 0 && isteacher($event->courseid)) {
1116 return true;
421b0337 1117 } else if($event->courseid == 0 && $event->groupid != 0) {
7423f116 1118 // Group event
1119 $group = get_record('groups', 'id', $event->groupid);
69244b91 1120 if($group === false) {
1121 return false;
1122 }
edf205a6 1123 return isteacheredit($group->courseid) || isteacher($group->courseid) && ismember($event->groupid);
421b0337 1124 } else if($event->courseid == 0 && $event->groupid == 0 && $event->userid == $USER->id) {
7423f116 1125 // User event, owned by this user
1126 return true;
1127 }
1128
1129 return false;
1130}
1131
8c54cec6 1132function calendar_get_default_courses($ignoreref = false) {
9ff136e5 1133 global $USER, $CFG, $SESSION;
1134
8c54cec6 1135 if(!empty($SESSION->cal_course_referer) && !$ignoreref) {
4c7d1137 1136 return array($SESSION->cal_course_referer => 1);
9ff136e5 1137 }
7423f116 1138
1066e0dc 1139 if(empty($USER->id)) {
2ef75eee 1140 return array();
1141 }
1142
7423f116 1143 $courses = array();
55d9c93a 1144 if(isadmin($USER->id)) {
86f092d2 1145 if(!empty($CFG->calendar_adminseesall)) {
1146 $courses = get_records_sql('SELECT id, 1 FROM '.$CFG->prefix.'course');
1147 return $courses;
1148 }
95a89225 1149 }
3696526b 1150 if(isset($USER->student) && is_array($USER->student)) {
d515b729 1151 $courses = $courses + $USER->student;
7423f116 1152 }
3696526b 1153 if(isset($USER->teacher) && is_array($USER->teacher)) {
d515b729 1154 $courses = $courses + $USER->teacher;
7423f116 1155 }
1156 return $courses;
1157}
1158
1e1ff33b 1159function calendar_preferences_button() {
1160 global $CFG, $USER;
7423f116 1161
1162 // Guests have no preferences
f52f7413 1163 if (empty($USER->id) || isguest()) {
7423f116 1164 return '';
1165 }
1166
1e1ff33b 1167 return "<form target=\"$CFG->framename\" method=\"get\" ".
1168 " action=\"$CFG->wwwroot/calendar/preferences.php\">".
1169 "<input type=\"submit\" value=\"".get_string("preferences", "calendar")." ...\" /></form>";
7423f116 1170}
1171
8f896582 1172function calendar_format_event_time($event, $now, $morehref, $usecommonwords = true) {
1173 $startdate = usergetdate($event->timestart);
1174 $enddate = usergetdate($event->timestart + $event->timeduration);
1175 $usermidnightstart = usergetmidnight($event->timestart);
1176
1177 if($event->timeduration) {
1178 // To avoid doing the math if one IF is enough :)
1179 $usermidnightend = usergetmidnight($event->timestart + $event->timeduration);
1180 }
1181 else {
1182 $usermidnightend = $usermidnightstart;
1183 }
1184
1185 // OK, now to get a meaningful display...
1186 // First of all we have to construct a human-readable date/time representation
1187
1188 if($event->timestart + $event->timeduration < $now) {
1189 // It has expired, so we don't care about duration
1190 $day = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords);
1191 $time = calendar_time_representation($event->timestart + $event->timeduration);
1192
1193 // This var always has the printable time representation
1194 $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>';
1195
1196 }
1197 else if($event->timeduration) {
1198 // It has a duration
1199 if($usermidnightstart == $usermidnightend) {
1200 // But it's all on the same day
1201 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1202 $timestart = calendar_time_representation($event->timestart);
1203 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
1204
1205 // Set printable representation
1206 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
7d50b363 1207 ' ('.$timestart.' <strong>&raquo;</strong> '.$timeend.')';
8f896582 1208 }
1209 else {
1210 // It spans two or more days
1211 $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords);
1212 $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords);
1213 $timestart = calendar_time_representation($event->timestart);
1214 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
1215
1216 // Set printable representation
1217 $eventtime = calendar_get_link_tag($daystart, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).
7d50b363 1218 ' ('.$timestart.') <strong>&raquo;</strong> '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
8f896582 1219 ' ('.$timeend.')';
1220 }
1221 }
1222 else {
1223 // It's an "instantaneous" event
1224 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1225 $time = calendar_time_representation($event->timestart);
1226
1227 // Set printable representation
1228 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).' ('.$time.')';
1229 }
09d36284 1230
8f896582 1231 return $eventtime;
1232}
054193be 1233
86f092d2 1234function calendar_print_month_selector($name, $selected) {
1235
1236 $months = array();
1237
1238 for ($i=1; $i<=12; $i++) {
1239 $months[$i] = userdate(gmmktime(12, 0, 0, $i, 1, 2000), '%B');
1240 }
1241
1242 choose_from_menu($months, $name, $selected, '');
1243}
1244
054193be 1245function calendar_get_filters_status() {
1246 global $SESSION;
1247
1248 $status = 0;
1249 if($SESSION->cal_show_global) {
1250 $status += 1;
1251 }
1252 if($SESSION->cal_show_course) {
1253 $status += 2;
1254 }
1255 if($SESSION->cal_show_groups) {
1256 $status += 4;
1257 }
1258 if($SESSION->cal_show_user) {
1259 $status += 8;
1260 }
1261 return $status;
1262}
1263
1264function calendar_set_filters_status($packed_bitfield) {
1265 global $SESSION, $USER;
1266
1267 if(!isset($USER) || empty($USER->id)) {
1268 return false;
1269 }
1270
1271 $SESSION->cal_show_global = ($packed_bitfield & 1);
1272 $SESSION->cal_show_course = ($packed_bitfield & 2);
1273 $SESSION->cal_show_groups = ($packed_bitfield & 4);
1274 $SESSION->cal_show_user = ($packed_bitfield & 8);
1275
1276 return true;
1277}
1278
7423f116 1279?>