fixing some problems with multichoice file link find and replace code for multichoice...
[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: //
bdcb26b7 12// Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13// Jon Papaioannou (pj@moodle.org) //
7423f116 14// //
15// Programming and development: //
bdcb26b7 16// Jon Papaioannou (pj@moodle.org) //
7423f116 17// //
18// For bugs, suggestions, etc contact: //
bdcb26b7 19// Jon Papaioannou (pj@moodle.org) //
7423f116 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
bf9ae76c 65define ('CALENDAR_MAXCOURSES', 3);
66
f2bffd9e 67$CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
68
37d87d11 69
bd119567 70
7423f116 71function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
72 global $CFG, $USER;
73
4e17c6f3 74 $display = &New stdClass;
7423f116 75 $display->minwday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
76 $display->maxwday = $display->minwday + 6;
77
78 $content = '';
79
80 if(!empty($cal_month) && !empty($cal_year)) {
81 $thisdate = usergetdate(time()); // Date and time the user sees at his location
82 if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) {
83 // Navigated to this month
84 $date = $thisdate;
85 $display->thismonth = true;
86 }
87 else {
88 // Navigated to other month, let's do a nice trick and save us a lot of work...
89 if(!checkdate($cal_month, 1, $cal_year)) {
90 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
91 $display->thismonth = true;
92 }
93 else {
94 $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
95 $display->thismonth = false;
96 }
97 }
98 }
99 else {
100 $date = usergetdate(time()); // Date and time the user sees at his location
101 $display->thismonth = true;
102 }
103
104 // Fill in the variables we 're going to use, nice and tidy
105 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
106 $display->maxdays = calendar_days_in_month($m, $y);
107
d6198903 108 if (get_user_timezone_offset() < 99) {
109 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
61240489 110 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
111 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
d6198903 112 } else {
113 // no timezone info specified
61240489 114 $display->tstart = mktime(0, 0, 0, $m, 1, $y);
115 $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
d6198903 116 }
7423f116 117
69244b91 118 $startwday = dayofweek(1, $m, $y);
7423f116 119
120 // Align the starting weekday to fall in our display range
121 // This is simple, not foolproof.
122 if($startwday < $display->minwday) {
123 $startwday += 7;
124 }
125
c0b507d1 126 // TODO: THIS IS TEMPORARY CODE!
127 // [pj] I was just reading through this and realized that I when writing this code I was probably
128 // asking for trouble, as all these time manipulations seem to be unnecessary and a simple
129 // make_timestamp would accomplish the same thing. So here goes a test:
1747ee05 130 //$test_start = make_timestamp($y, $m, 1);
131 //$test_end = make_timestamp($y, $m, $display->maxdays, 23, 59, 59);
132 //if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) {
133 //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);
134 //}
135 //if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) {
136 //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);
137 //}
c0b507d1 138
139
7423f116 140 // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
d3555a2f 141 $whereclause = calendar_sql_where(
142 usertime($display->tstart) - dst_offset_on($display->tstart),
143 usertime($display->tend) - dst_offset_on($display->tend),
144 $users, $groups, $courses);
7423f116 145
146 if($whereclause === false) {
147 $events = array();
148 }
149 else {
a94329a8 150 $events = get_records_select('event', $whereclause, 'timestart');
7423f116 151 }
152
b4892fa2 153 // Set event course class for course events
154 if (!empty($events)) {
155 foreach ($events as $event) {
156 if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
bf9ae76c 157 $event->class = 'event_course'.array_search($event->courseid, $courses) % CALENDAR_MAXCOURSES;
b4892fa2 158 }
159 }
160 }
161
c635dcda 162 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
9064751b 163 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
c635dcda 164 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
165 // arguments to this function.
166
7bd1677c 167 $morehref = '';
168 if(!empty($courses)) {
e749554e 169 $courses = array_diff($courses, array(SITEID));
7bd1677c 170 if(count($courses) == 1) {
171 $morehref = '&amp;course='.reset($courses);
172 }
c635dcda 173 }
174
7423f116 175 // We want to have easy access by day, since the display is on a per-day basis.
176 // Arguments passed by reference.
7b38bfa6 177 //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
7c50db30 178 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
7423f116 179
f20c4d02 180 //Accessibility: added summary and <abbr> elements.
181 ///global $CALENDARDAYS; appears to be broken.
182 $days_title = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
183
2a06efcc 184 $summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear')));
185 $summary = get_string('tabledata', 'access', $summary);
186 $content .= '<table class="minicalendar" summary="'.$summary.'">'; // Begin table
f136e4c5 187 $content .= '<tr class="weekdays">'; // Header row: day names
7423f116 188
189 // Print out the names of the weekdays
190 $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
191 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
192 // This uses the % operator to get the correct weekday no matter what shift we have
193 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
f20c4d02 194 $content .= '<th scope="col"><abbr title="'. get_string($days_title[$i % 7], 'calendar') .'">'.
195 get_string($days[$i % 7], 'calendar') ."</abbr></th>\n";
7423f116 196 }
197
f136e4c5 198 $content .= '</tr><tr>'; // End of day names; prepare for day numbers
7423f116 199
200 // For the table display. $week is the row; $dayweek is the column.
7423f116 201 $dayweek = $startwday;
202
203 // Paddding (the first week may have blank days in the beginning)
204 for($i = $display->minwday; $i < $startwday; ++$i) {
3bfd8bc8 205 $content .= '<td class="dayblank">&nbsp;</td>'."\n";
7423f116 206 }
207
208 // Now display all the calendar
209 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
210 if($dayweek > $display->maxwday) {
211 // We need to change week (table row)
d56d4e23 212 $content .= '</tr><tr>';
7423f116 213 $dayweek = $display->minwday;
7423f116 214 }
92668ad2 215
7423f116 216 // Reset vars
7423f116 217 $cell = '';
bb4a2e85 218 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
7423f116 219 // Weekend. This is true no matter what the exact range is.
e2aa618b 220 $class = 'weekend day';
7423f116 221 }
222 else {
223 // Normal working day.
e2aa618b 224 $class = 'day';
7423f116 225 }
226
227 // Special visual fx if an event is defined
228 if(isset($eventsbyday[$day])) {
c635dcda 229 $dayhref = calendar_get_link_href(CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $day, $m, $y);
f434283f 230
7423f116 231 // OverLib popup
232 $popupcontent = '';
233 foreach($eventsbyday[$day] as $eventid) {
41d30a8e 234 if (!isset($events[$eventid])) {
235 continue;
4e17c6f3 236 }
41d30a8e 237 $event = $events[$eventid];
41d30a8e 238 if(!empty($event->modulename)) {
239 $popupicon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
240 $popupalt = $event->modulename;
241
9064751b 242 } else if ($event->courseid == SITEID) { // Site event
41d30a8e 243 $popupicon = $CFG->pixpath.'/c/site.gif';
244 $popupalt = '';
c3d3b6d4 245 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
41d30a8e 246 $popupicon = $CFG->pixpath.'/c/course.gif';
c9b05b32 247 $popupalt = '';
41d30a8e 248 } else if ($event->groupid) { // Group event
249 $popupicon = $CFG->pixpath.'/c/group.gif';
250 $popupalt = '';
251 } else if ($event->userid) { // User event
252 $popupicon = $CFG->pixpath.'/c/user.gif';
253 $popupalt = '';
4e17c6f3 254 }
53d8fac0 255 $popupcontent .= '<div><img class="icon" src="'.$popupicon.'" alt="'.$popupalt.'" /><a href="'.$dayhref.'#event_'.$event->id.'">'.format_string($event->name, true).'</a></div>';
7423f116 256 }
b5c42e70 257
258 //Accessibility: functionality moved to calendar_get_popup.
259 if($display->thismonth && $day == $d) {
450a0a7d 260 $popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
b5c42e70 261 } else {
450a0a7d 262 $popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
b5c42e70 263 }
f20c4d02 264
7423f116 265 // Class and cell content
266 if(isset($typesbyday[$day]['startglobal'])) {
edbe6c1b 267 $class .= ' event_global';
7423f116 268 }
269 else if(isset($typesbyday[$day]['startcourse'])) {
edbe6c1b 270 $class .= ' event_course';
7423f116 271 }
272 else if(isset($typesbyday[$day]['startgroup'])) {
edbe6c1b 273 $class .= ' event_group';
7423f116 274 }
275 else if(isset($typesbyday[$day]['startuser'])) {
edbe6c1b 276 $class .= ' event_user';
7423f116 277 }
05fcc5fd 278 $cell = '<a href="'.$dayhref.'" '.$popup.'>'.$day.'</a>';
7423f116 279 }
280 else {
05fcc5fd 281 $cell = $day;
7423f116 282 }
283
284 if(isset($typesbyday[$day]['durationglobal'])) {
edbe6c1b 285 $class .= ' duration_global';
7423f116 286 }
287 else if(isset($typesbyday[$day]['durationcourse'])) {
edbe6c1b 288 $class .= ' duration_course';
7423f116 289 }
290 else if(isset($typesbyday[$day]['durationgroup'])) {
edbe6c1b 291 $class .= ' duration_group';
7423f116 292 }
293 else if(isset($typesbyday[$day]['durationuser'])) {
edbe6c1b 294 $class .= ' duration_user';
7423f116 295 }
296
b4892fa2 297 // If event has a class set then add it to the table day <td> tag
298 // Note: only one colour for minicalendar
299 if(isset($eventsbyday[$day])) {
300 foreach($eventsbyday[$day] as $eventid) {
301 if (!isset($events[$eventid])) {
302 continue;
303 }
304 $event = $events[$eventid];
305 if (!empty($event->class)) {
306 $class .= ' '.$event->class;
307 }
308 break;
309 }
310 }
311
7423f116 312 // Special visual fx for today
450a0a7d 313 //Accessibility: hidden text for today, and popup.
7423f116 314 if($display->thismonth && $day == $d) {
edbe6c1b 315 $class .= ' today';
b5c42e70 316 $today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort'));
317
318 if(! isset($eventsbyday[$day])) {
450a0a7d 319 $class .= ' eventnone';
320 $popup = calendar_get_popup(true, false);
321 $cell = '<a href="#" '.$popup.'>'.$day.'</a>';
b5c42e70 322 }
323 $cell = '<span class="accesshide">'.$today.' </span>'.$cell;
7423f116 324 }
325
326 // Just display it
92668ad2 327 if(!empty($class)) {
d56d4e23 328 $class = ' class="'.$class.'"';
92668ad2 329 }
330 $content .= '<td'.$class.'>'.$cell."</td>\n";
7423f116 331 }
332
333 // Paddding (the last week may have blank days at the end)
334 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
3bfd8bc8 335 $content .= '<td class="dayblank">&nbsp;</td>';
7423f116 336 }
337 $content .= '</tr>'; // Last row ends
338
f136e4c5 339 $content .= '</table>'; // Tabular display of days ends
7423f116 340
341 return $content;
342}
343
b5c42e70 344/**
345 * calendar_get_popup, called at multiple points in from calendar_get_mini.
346 * Copied and modified from calendar_get_mini.
347 * @uses OverLib popup.
348 * @param $is_today bool, false except when called on the current day.
349 * @param $event_timestart mixed, $events[$eventid]->timestart, OR false if there are no events.
350 * @param $popupcontent string.
351 * @return $popup string, contains onmousover and onmouseout events.
352 */
353function calendar_get_popup($is_today, $event_timestart, $popupcontent='') {
354 $popupcaption = '';
355 if($is_today) {
356 $popupcaption = get_string('today', 'calendar').' ';
357 }
358 if (false === $event_timestart) {
359 $popupcaption .= userdate(time(), get_string('strftimedayshort'));
360 $popupcontent = get_string('eventnone', 'calendar');
450a0a7d 361
b5c42e70 362 } else {
363 $popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort')));
364 }
365 $popupcontent = str_replace("'", "\'", htmlspecialchars($popupcontent));
d8947f95 366 $popupcaption = str_replace("'", "\'", htmlspecialchars($popupcaption));
b5c42e70 367 $popup = 'onmouseover="return overlib(\''.$popupcontent.'\', CAPTION, \''.$popupcaption.'\');" onmouseout="return nd();"';
450a0a7d 368 return $popup;
b5c42e70 369}
370
9958a08c 371function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
7423f116 372 global $CFG;
373
1f473774 374 $display = &new stdClass;
7423f116 375 $display->range = $daysinfuture; // How many days in the future we 'll look
376 $display->maxevents = $maxevents;
377
378 $output = array();
379
380 // Prepare "course caching", since it may save us a lot of queries
381 $coursecache = array();
382
383 $processed = 0;
384 $now = time(); // We 'll need this later
9d567178 385 $usermidnighttoday = usergetmidnight($now);
7423f116 386
9958a08c 387 if ($fromtime) {
388 $display->tstart = $fromtime;
389 } else {
9d567178 390 $display->tstart = $usermidnighttoday;
9958a08c 391 }
7423f116 392
1f473774 393 // This works correctly with respect to the user's DST, but it is accurate
394 // only because $fromtime is always the exact midnight of some day!
395 $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1;
7423f116 396
397 // Get the events matching our criteria
398 $whereclause = calendar_sql_where($display->tstart, $display->tend, $users, $groups, $courses);
9958a08c 399 if ($whereclause === false) {
7423f116 400 $events = false;
9958a08c 401 } else {
a94329a8 402 $events = get_records_select('event', $whereclause, 'timestart');
7423f116 403 }
404
c635dcda 405 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
9064751b 406 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
c635dcda 407 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
408 // arguments to this function.
409
7bd1677c 410 $morehref = '';
411 if(!empty($courses)) {
e749554e 412 $courses = array_diff($courses, array(SITEID));
7bd1677c 413 if(count($courses) == 1) {
414 $morehref = '&amp;course='.reset($courses);
415 }
c635dcda 416 }
417
7423f116 418 if($events !== false) {
fa22fd5f 419
7423f116 420 foreach($events as $event) {
9d567178 421
422 if($processed >= $display->maxevents) {
423 break;
424 }
7423f116 425
3c134875 426 $event->time = calendar_format_event_time($event, $now, $morehref);
427 $output[] = $event;
428 ++$processed;
429 }
430 }
431 return $output;
432}
7423f116 433
9df8ff44 434function calendar_add_event_metadata($event) {
435 global $CFG;
fb73f3b3 436
437 //Support multilang in event->name
438 $event->name = format_string($event->name,true);
439
3c134875 440 if(!empty($event->modulename)) { // Activity event
441 // The module name is set. I will assume that it has to be displayed, and
442 // also that it is an automatically-generated event. And of course that the
443 // fields for get_coursemodule_from_instance are set correctly.
444 $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance);
7423f116 445
3c134875 446 if ($module === false) {
447 return;
448 }
9958a08c 449
3c134875 450 $modulename = get_string('modulename', $event->modulename);
451 $eventtype = get_string($event->eventtype, $event->modulename);
452 $icon = $CFG->modpixpath.'/'.$event->modulename.'/icon.gif';
9958a08c 453
10daca92 454 $event->icon = '<img height="16" width="16" src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" style="vertical-align: middle;" />';
fb73f3b3 455 $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
3c134875 456 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$coursecache[$module->course]->fullname.'</a>';
457 $event->cmid = $module->id;
9958a08c 458
9958a08c 459
3c134875 460 } else if($event->courseid == SITEID) { // Site event
10daca92 461 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/site.gif" alt="'.get_string('globalevent', 'calendar').'" style="vertical-align: middle;" />';
3c49918a 462 $event->cssclass = 'event_global';
3c134875 463 } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
464 calendar_get_course_cached($coursecache, $event->courseid);
10daca92 465 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/course.gif" alt="'.get_string('courseevent', 'calendar').'" style="vertical-align: middle;" />';
3c134875 466 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>';
3c49918a 467 $event->cssclass = 'event_course';
3c134875 468 } else if ($event->groupid) { // Group event
10daca92 469 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/group.gif" alt="'.get_string('groupevent', 'calendar').'" style="vertical-align: middle;" />';
3c49918a 470 $event->cssclass = 'event_group';
3c134875 471 } else if($event->userid) { // User event
10daca92 472 $event->icon = '<img height="16" width="16" src="'.$CFG->pixpath.'/c/user.gif" alt="'.get_string('userevent', 'calendar').'" style="vertical-align: middle;" />';
3c49918a 473 $event->cssclass = 'event_user';
3c134875 474 }
9df8ff44 475 return $event;
476}
477
478function calendar_print_event($event) {
479 global $CFG, $USER;
480
481 static $strftimetime;
482
483 $event = calendar_add_event_metadata($event);
53d8fac0 484 echo '<a name="event_'.$event->id.'"></a><table class="event" cellspacing="0">';
ed8ea92b 485 echo '<tr><td class="picture">';
3c134875 486 if (!empty($event->icon)) {
487 echo $event->icon;
488 } else {
489 print_spacer(16,16);
490 }
491 echo '</td>';
df349d1d 492 echo '<td class="topic">';
9958a08c 493
3c134875 494 if (!empty($event->referer)) {
e96ba29d 495 echo '<div class="referer">'.$event->referer.'</div>';
3c134875 496 } else {
ed8ea92b 497 echo '<div class="name">'.$event->name."</div>";
3c134875 498 }
499 if (!empty($event->courselink)) {
ed8ea92b 500 echo '<div class="course">'.$event->courselink.' </div>';
3c134875 501 }
502 if (!empty($event->time)) {
ed8ea92b 503 echo '<span class="date">'.$event->time.'</span>';
3c134875 504 } else {
ed8ea92b 505 echo '<span class="date">'.calendar_time_representation($event->timestart).'</span>';
3c134875 506 }
9958a08c 507
ed8ea92b 508 echo '</td></tr>';
509 echo '<tr><td class="side">&nbsp;</td>';
b4892fa2 510
511 // If event has a class set then add it to the event <td> tag
512 $eventclass='';
513 if (!empty($event->class)) {
514 $eventclass = ' '.$event->class;
515 }
516
517 echo '<td class="description '.$event->cssclass.$eventclass.'">';
3c134875 518 echo format_text($event->description, FORMAT_HTML);
519 if (calendar_edit_event_allowed($event)) {
ed8ea92b 520 echo '<div class="commands">';
54052a7a 521 $calendarcourseid = '';
522 if (!empty($event->calendarcourseid)) {
523 $calendarcourseid = '&amp;course='.$event->calendarcourseid;
524 }
3c134875 525 if (empty($event->cmid)) {
54052a7a 526 $editlink = CALENDAR_URL.'event.php?action=edit&amp;id='.$event->id.$calendarcourseid;
527 $deletelink = CALENDAR_URL.'event.php?action=delete&amp;id='.$event->id.$calendarcourseid;
3c134875 528 } else {
529 $editlink = $CFG->wwwroot.'/course/mod.php?update='.$event->cmid.'&amp;return=true&amp;sesskey='.$USER->sesskey;
530 $deletelink = $CFG->wwwroot.'/course/mod.php?delete='.$event->cmid.'&amp;sesskey='.$USER->sesskey;;
7423f116 531 }
3c134875 532 echo ' <a href="'.$editlink.'"><img
533 src="'.$CFG->pixpath.'/t/edit.gif" alt="'.get_string('tt_editevent', 'calendar').'"
534 title="'.get_string('tt_editevent', 'calendar').'" /></a>';
535 echo ' <a href="'.$deletelink.'"><img
536 src="'.$CFG->pixpath.'/t/delete.gif" alt="'.get_string('tt_deleteevent', 'calendar').'"
537 title="'.get_string('tt_deleteevent', 'calendar').'" /></a>';
538 echo '</div>';
7423f116 539 }
3c134875 540 echo '</td></tr></table>';
541
7423f116 542}
543
8c165fe9 544function calendar_sql_where($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
7423f116 545 $whereclause = '';
546 // Quick test
547 if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
548 return false;
549 }
482dbe0c 550
7423f116 551 if(is_array($users) && !empty($users)) {
552 // Events from a number of users
553 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 554 $whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)';
7423f116 555 }
556 else if(is_numeric($users)) {
557 // Events from one user
558 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 559 $whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)';
7423f116 560 }
561 else if($users === true) {
562 // Events from ALL users
563 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 564 $whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)';
7423f116 565 }
f52f7413 566 else if($users === false) {
6e957c41 567 // No user at all, do nothing
f52f7413 568 }
482dbe0c 569
7423f116 570 if(is_array($groups) && !empty($groups)) {
571 // Events from a number of groups
572 if(!empty($whereclause)) $whereclause .= ' OR';
573 $whereclause .= ' groupid IN ('.implode(',', $groups).')';
574 }
575 else if(is_numeric($groups)) {
576 // Events from one group
577 if(!empty($whereclause)) $whereclause .= ' OR ';
578 $whereclause .= ' groupid = '.$groups;
579 }
580 else if($groups === true) {
581 // Events from ALL groups
582 if(!empty($whereclause)) $whereclause .= ' OR ';
583 $whereclause .= ' groupid != 0';
584 }
482dbe0c 585 // boolean false (no groups at all): we don't need to do anything
586
f52f7413 587 if(is_array($courses)) {
588 // A number of courses (maybe none at all!)
589 if(!empty($courses)) {
590 if(!empty($whereclause)) {
591 $whereclause .= ' OR';
592 }
6e957c41 593 $whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))';
f52f7413 594 }
595 else {
596 // This means NO courses, not that we don't care!
09d36284 597 // No need to do anything
f52f7413 598 }
7423f116 599 }
600 else if(is_numeric($courses)) {
601 // One course
602 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 603 $whereclause .= ' (groupid = 0 AND courseid = '.$courses.')';
7423f116 604 }
605 else if($courses === true) {
606 // Events from ALL courses
607 if(!empty($whereclause)) $whereclause .= ' OR';
6e957c41 608 $whereclause .= ' (groupid = 0 AND courseid != 0)';
7423f116 609 }
8c165fe9 610
482dbe0c 611 // Security check: if, by now, we have NOTHING in $whereclause, then it means
612 // that NO event-selecting clauses were defined. Thus, we won't be returning ANY
613 // events no matter what. Allowing the code to proceed might return a completely
614 // valid query with only time constraints, thus selecting ALL events in that time frame!
615 if(empty($whereclause)) {
616 return false;
617 }
618
7423f116 619 if($withduration) {
b4892fa2 620 $timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend;
7423f116 621 }
622 else {
623 $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend;
624 }
625 if(!empty($whereclause)) {
626 // We have additional constraints
627 $whereclause = $timeclause.' AND ('.$whereclause.')';
628 }
629 else {
630 // Just basic time filtering
631 $whereclause = $timeclause;
632 }
f52f7413 633
0ad072de 634 if ($ignorehidden) {
635 $whereclause .= ' AND visible = 1';
636 }
637
638 return $whereclause;
7423f116 639}
640
641function calendar_top_controls($type, $data) {
57100a90 642 global $CFG, $CALENDARDAYS, $THEME;
7423f116 643 $content = '';
644 if(!isset($data['d'])) {
645 $data['d'] = 1;
646 }
5147ad48 647
f21ed0f3 648 // Ensure course id passed if relevant
649 // Required due to changes in view/lib.php mainly (calendar_session_vars())
650 $courseid = '';
651 if (!empty($data['id'])) {
652 $courseid = '&amp;course='.$data['id'];
653 }
654
5147ad48 655 if(!checkdate($data['m'], $data['d'], $data['y'])) {
656 $time = time();
657 }
658 else {
659 $time = make_timestamp($data['y'], $data['m'], $data['d']);
660 }
661 $date = usergetdate($time);
662
7423f116 663 $data['m'] = $date['mon'];
664 $data['y'] = $date['year'];
7423f116 665
2a06efcc 666 //Accessibility: calendar block controls, replaced <table> with <div>.
a84dea2c 667 //$nexttext = link_arrow_right(get_string('monthnext', 'access'), $url='', $accesshide=true);
668 //$prevtext = link_arrow_left(get_string('monthprev', 'access'), $url='', $accesshide=true);
2a06efcc 669
7423f116 670 switch($type) {
671 case 'frontpage':
672 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
673 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
a84dea2c 674 $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'index.php?', 0, $nextmonth, $nextyear, $accesshide=true);
675 $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'index.php?', 0, $prevmonth, $prevyear, true);
676 $content .= "\n".'<div class="calendar-controls">'. $prevlink;
54052a7a 677 $content .= '<span class="hide"> | </span><span class="current"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month'.$courseid.'&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear')).'</a></span>';
a84dea2c 678 $content .= '<span class="hide"> | </span>'. $nextlink ."\n";
f2de62da 679 $content .= "<span class=\"clearer\"><!-- --></span></div>\n";
7423f116 680 break;
681 case 'course':
682 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
683 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
a84dea2c 684 $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'view.php?id='.$data['id'].'&amp;', 0, $nextmonth, $nextyear, $accesshide=true);
685 $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'view.php?id='.$data['id'].'&amp;', 0, $prevmonth, $prevyear, true);
686 $content .= "\n".'<div class="calendar-controls">'. $prevlink;
54052a7a 687 $content .= '<span class="hide"> | </span><span class="current"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month'.$courseid.'&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear')).'</a></span>';
a84dea2c 688 $content .= '<span class="hide"> | </span>'. $nextlink ."\n";
f2de62da 689 $content .= "<span class=\"clearer\"><!-- --></span></div>\n";
7423f116 690 break;
691 case 'upcoming':
f21ed0f3 692 $content .= '<div style="text-align: center;"><a href="'.CALENDAR_URL.'view.php?view=upcoming"'.$courseid.'>'.userdate($time, get_string('strftimemonthyear'))."</a></div>\n";
7423f116 693 break;
694 case 'display':
f21ed0f3 695 $content .= '<div style="text-align: center;"><a href="'.calendar_get_link_href(CALENDAR_URL.'view.php?view=month'.$courseid.'&amp;', 1, $data['m'], $data['y']).'">'.userdate($time, get_string('strftimemonthyear'))."</a></div>\n";
7423f116 696 break;
697 case 'month':
698 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
699 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
5147ad48 700 $prevdate = make_timestamp($prevyear, $prevmonth, 1);
701 $nextdate = make_timestamp($nextyear, $nextmonth, 1);
a84dea2c 702 $content .= "\n".'<div class="calendar-controls">';
f21ed0f3 703 $content .= calendar_get_link_previous(userdate($prevdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&amp;', 1, $prevmonth, $prevyear);
a7c385e6 704 $content .= '<span class="hide"> | </span><span class="current">'.userdate($time, get_string('strftimemonthyear'))."</span>\n";
f21ed0f3 705 $content .= '<span class="hide"> | </span>'.calendar_get_link_next(userdate($nextdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&amp;', 1, $nextmonth, $nextyear);
f2de62da 706 $content .= "<span class=\"clearer\"><!-- --></span></div>\n";
7423f116 707 break;
708 case 'day':
709 $data['d'] = $date['mday']; // Just for convenience
5147ad48 710 $prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1));
711 $nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1));
f2bffd9e 712 $prevname = calendar_wday_name($CALENDARDAYS[$prevdate['wday']]);
713 $nextname = calendar_wday_name($CALENDARDAYS[$nextdate['wday']]);
a84dea2c 714 $content .= "\n".'<div class="calendar-controls">';
f21ed0f3 715 $content .= calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&amp;', $prevdate['mday'], $prevdate['mon'], $prevdate['year']);
a84dea2c 716
6e8e8ec6 717 // Get the format string
718 $text = get_string('strftimedaydate');
3477a900 719 /*
6e8e8ec6 720 // Regexp hackery to make a link out of the month/year part
721 $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 722 $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 723 */
6e8e8ec6 724 // Replace with actual values and lose any day leading zero
3477a900 725 $text = userdate($time, $text);
6e8e8ec6 726 // Print the actual thing
a7c385e6 727 $content .= '<span class="hide"> | </span><span class="current">'.$text.'</span>';
6e8e8ec6 728
f21ed0f3 729 $content .= '<span class="hide"> | </span>'. calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&amp;', $nextdate['mday'], $nextdate['mon'], $nextdate['year']);
f2de62da 730 $content .= "<span class=\"clearer\"><!-- --></span></div>\n";
7423f116 731 break;
732 }
733 return $content;
734}
735
b4892fa2 736function calendar_filter_controls($type, $vars = NULL, $course = NULL, $courses = NULL) {
43c3ffbe 737 global $CFG, $SESSION, $USER;
7423f116 738
48f508ab 739 $groupevents = true;
3cb9ee39 740 $getvars = '';
7f4d18fc 741
742 $id = optional_param( 'id',0,PARAM_INT );
d715f7c4 743
34bf3ad4 744 switch($type) {
eb15f829 745 case 'event':
34bf3ad4 746 case 'upcoming':
34bf3ad4 747 case 'day':
c3f463ca 748 case 'month':
eb15f829 749 $getvars = '&amp;from='.$type;
34bf3ad4 750 break;
751 case 'course':
fac01360 752 if ($id > 0) {
7f4d18fc 753 $getvars = '&amp;from=course&amp;id='.$id;
2eb68e6f 754 } else {
755 $getvars = '&amp;from=course';
756 }
5a74ffd3 757 if (isset($course->groupmode) and $course->groupmode == NOGROUPS and $course->groupmodeforce) {
34bf3ad4 758 $groupevents = false;
759 }
760 break;
d715f7c4 761 }
34bf3ad4 762
3cb9ee39 763 if (!empty($vars)) {
34bf3ad4 764 $getvars .= '&amp;'.$vars;
48f508ab 765 }
7423f116 766
2ad2cbc3 767 $content = '<table>';
7423f116 768
48f508ab 769 $content .= '<tr>';
770 if($SESSION->cal_show_global) {
365cec3c 771 $content .= '<td class="eventskey event_global" style="width: 11px;"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hideglobal', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showglobal'.$getvars."'".'" /></td>';
b4892fa2 772 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_hideglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
48f508ab 773 }
774 else {
10b1d598 775 $content .= '<td style="width: 11px;"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showglobal', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showglobal'.$getvars."'".'" /></td>';
b4892fa2 776 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_showglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
48f508ab 777 }
b4892fa2 778 $tr = '';
48f508ab 779
1066e0dc 780 if(!empty($USER->id) && !isguest()) {
b4892fa2 781
10b1d598 782 $content .= $tr;
783 $tr = $tr ? '' : "</tr>\n<tr>";
784
785 if($groupevents) {
b4892fa2 786
43c3ffbe 787 // This course MIGHT have group events defined, so show the filter
788 if($SESSION->cal_show_groups) {
365cec3c 789 $content .= '<td class="eventskey event_group" style="width: 11px;"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hidegroups', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showgroups'.$getvars."'".'" /></td>';
b4892fa2 790 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_hidegroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
cd6469cb 791 } else {
10b1d598 792 $content .= '<td style="width: 11px;"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showgroups', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showgroups'.$getvars."'".'" /></td>';
b4892fa2 793 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_showgroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
43c3ffbe 794 }
cd6469cb 795 } else {
b4892fa2 796
43c3ffbe 797 // This course CANNOT have group events, so lose the filter
5c53988f 798 $content .= '<td style="width: 11px;"></td><td>&nbsp;</td>'."\n";
10b1d598 799 }
cd6469cb 800
10b1d598 801 $content .= $tr;
802 $tr = $tr ? '' : "</tr>\n<tr>";
b4892fa2 803
10b1d598 804 if ($SESSION->cal_show_user) {
365cec3c 805 $content .= '<td class="eventskey event_user" style="width: 11px;"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hideuser', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showuser'.$getvars."'".'" /></td>';
10b1d598 806 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
807 } else {
808 $content .= '<td style="width: 11px;"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.get_string('show').'" title="'.get_string('tt_showuser', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showuser'.$getvars."'".'" /></td>';
809 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
b4892fa2 810 }
811 }
812
813 // Remove global SITE ID from courses array as do not want to display this
814 if (!empty($courses)) {
815 $key = array_search(SITEID, $courses);
816 if ($key !== false) {
817 unset($courses[$key]);
818 }
819 }
820
821 if (empty($courses) || count($courses) == 1) {
822
823 // If not multiple courses then just display default single course colour highlighting
824 $content .= $tr;
825 $tr = $tr ? '' : "</tr>\n<tr>";
826
827 if($SESSION->cal_show_course) {
365cec3c 828 $content .= '<td class="eventskey event_course" style="width: 11px;"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hidecourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showcourses'.$getvars."'".'" /></td>';
b4892fa2 829 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
830 }
831 else {
10b1d598 832 $content .= '<td style="width: 11px;"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_showcourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showcourses'.$getvars."'".'" /></td>';
b4892fa2 833 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_showcourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
834 }
835 } else {
836
837 // Otherwise display list of course shortnames and relevant colours
838 // Get list of course shortnames (Limit to 12 for now - who would have more than that?)
839 $select = 'id in ('.implode(',', $courses).')';
840 $sort = 'id';
841 $fields = 'id, shortname';
842 $courseshortnames = get_records_select('course', $select, $sort, $fields, 0, 12);
843
bf9ae76c 844 for ($i = 0; $i < CALENDAR_MAXCOURSES; $i++) {
b4892fa2 845
846 // Concatenate shortnames if there are more than 3 courses
847 $strshortnames = '';
10b1d598 848 $n = 0;
bf9ae76c 849 for ($j = $i; $j < count($courses); $j += CALENDAR_MAXCOURSES) {
10b1d598 850 $strshortnames .= ', <a title="" href="'.$CFG->wwwroot.'/course/view.php?id='.$courses[$j].'">'.(!empty($courseshortnames[$courses[$j]]->shortname) ? $courseshortnames[$courses[$j]]->shortname : $courses[$j]).'</a>';
851 $n++;
b4892fa2 852 }
853
10b1d598 854 if ($n) {
b4892fa2 855
856 $content .= $tr;
857 $tr = $tr ? '' : "</tr>\n<tr>";
858
10b1d598 859 if ($n < 2) {
860 $strcourse = get_string('course', 'calendar');
861 } else {
862 $strcourse = get_string('courses', 'calendar');
863 }
864
b4892fa2 865 if($SESSION->cal_show_course) {
365cec3c 866 $content .= '<td class="eventskey event_course'.$i.'" style="width: 11px;"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_hidecourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showcourses'.$getvars."'".'" /></td>';
10b1d598 867 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.$strcourse.'</a>: '.substr($strshortnames, 2).'</td>'."\n";
b4892fa2 868 }
869 else {
10b1d598 870 $content .= '<td style="width: 11px;"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.get_string('hide').'" title="'.get_string('tt_showcourse', 'calendar').'" style="cursor:pointer" onclick="location.href='."'".CALENDAR_URL.'set.php?var=showcourses'.$getvars."'".'" /></td>';
871 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_showcourse', 'calendar').'">'.$strcourse.'</a>: '.substr($strshortnames, 2).'</td>'."\n";
b4892fa2 872 }
43c3ffbe 873 }
48f508ab 874 }
875 }
876 $content .= "</tr>\n</table>\n";
877
7423f116 878 return $content;
879}
880
881function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
882
0ef7c973 883 static $shortformat;
884 if(empty($shortformat)) {
e70fdac0 885 $shortformat = get_string('strftimedayshort');
0ef7c973 886 }
887
7423f116 888 if($now === false) {
889 $now = time();
890 }
891
892 // To have it in one place, if a change is needed
e70fdac0 893 $formal = userdate($tstamp, $shortformat);
7423f116 894
7b38bfa6 895 $datestamp = usergetdate($tstamp);
896 $datenow = usergetdate($now);
7423f116 897
898 if($usecommonwords == false) {
899 // We don't want words, just a date
900 return $formal;
901 }
7b38bfa6 902 else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
7423f116 903 // Today
904 return get_string('today', 'calendar');
905 }
7b38bfa6 906 else if(
907 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
908 ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1)
909 ) {
7423f116 910 // Yesterday
911 return get_string('yesterday', 'calendar');
912 }
7b38bfa6 913 else if(
914 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
915 ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1)
916 ) {
7423f116 917 // Tomorrow
918 return get_string('tomorrow', 'calendar');
919 }
920 else {
921 return $formal;
922 }
923}
924
925function calendar_time_representation($time) {
1b0ebe79 926 static $langtimeformat = NULL;
927 if($langtimeformat === NULL) {
928 $langtimeformat = get_string('strftimetime');
929 }
930 $timeformat = get_user_preferences('calendar_timeformat');
c7dd2550 931 if(empty($timeformat)){
932 $timeformat = get_config(NULL,'calendar_site_timeformat');
933 }
1b0ebe79 934 // The ? is needed because the preference might be present, but empty
935 return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat);
7423f116 936}
937
938function calendar_get_link_href($linkbase, $d, $m, $y) {
939 if(empty($linkbase)) return '';
940 $paramstr = '';
941 if(!empty($d)) $paramstr .= '&amp;cal_d='.$d;
942 if(!empty($m)) $paramstr .= '&amp;cal_m='.$m;
943 if(!empty($y)) $paramstr .= '&amp;cal_y='.$y;
944 if(!empty($paramstr)) $paramstr = substr($paramstr, 5);
945 return $linkbase.$paramstr;
946}
947
948function calendar_get_link_tag($text, $linkbase, $d, $m, $y) {
949 $href = calendar_get_link_href($linkbase, $d, $m, $y);
950 if(empty($href)) return $text;
951 return '<a href="'.$href.'">'.$text.'</a>';
952}
953
a84dea2c 954/**
955 * Build and return a previous month HTML link, with an arrow.
956 * @param string $text The text label.
957 * @param string $linkbase The URL stub.
958 * @param int $d $m $y Day of month, month and year numbers.
959 * @param bool $accesshide Default visible, or hide from all except screenreaders.
960 * @return string HTML string.
961 */
962function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide=false) {
963 $href = calendar_get_link_href($linkbase, $d, $m, $y);
964 if(empty($href)) return $text;
965 return link_arrow_left($text, $href, $accesshide, 'previous');
966}
967
968/**
969 * Build and return a next month HTML link, with an arrow.
970 * @param string $text The text label.
971 * @param string $linkbase The URL stub.
972 * @param int $d $m $y Day of month, month and year numbers.
973 * @param bool $accesshide Default visible, or hide from all except screenreaders.
974 * @return string HTML string.
975 */
976function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide=false) {
977 $href = calendar_get_link_href($linkbase, $d, $m, $y);
978 if(empty($href)) return $text;
979 return link_arrow_right($text, $href, $accesshide, 'next');
980}
981
7423f116 982function calendar_wday_name($englishname) {
983 return get_string(strtolower($englishname), 'calendar');
984}
985
986function calendar_days_in_month($month, $year) {
7b38bfa6 987 return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
7423f116 988}
989
e749554e 990function calendar_get_sideblock_upcoming($events, $linkhref = NULL) {
7423f116 991 $content = '';
992 $lines = count($events);
396b61f0 993 if (!$lines) {
994 return $content;
995 }
7423f116 996
396b61f0 997 for ($i = 0; $i < $lines; ++$i) {
b0ac9180 998 if (!isset($events[$i]->time)) { // Just for robustness
999 continue;
1000 }
9df8ff44 1001 $events[$i] = calendar_add_event_metadata($events[$i]);
ea21c1f4 1002 $content .= '<div class="event"><span class="icon c0">'.$events[$i]->icon.'</span> ';
43c3ffbe 1003 if (!empty($events[$i]->referer)) {
7423f116 1004 // That's an activity event, so let's provide the hyperlink
396b61f0 1005 $content .= $events[$i]->referer;
1006 } else {
e749554e 1007 if(!empty($linkhref)) {
1008 $ed = usergetdate($events[$i]->timestart);
1009 $href = calendar_get_link_href(CALENDAR_URL.$linkhref, $ed['mday'], $ed['mon'], $ed['year']);
53d8fac0 1010 $content .= '<a href="'.$href.'#event_'.$events[$i]->id.'">'.$events[$i]->name.'</a>';
e749554e 1011 }
1012 else {
1013 $content .= $events[$i]->name;
1014 }
7423f116 1015 }
9ecf051d 1016 $events[$i]->time = str_replace('&raquo;', '<br />&raquo;', $events[$i]->time);
1017 $content .= '<div class="date">'.$events[$i]->time.'</div></div>';
396b61f0 1018 if ($i < $lines - 1) $content .= '<hr />';
7423f116 1019 }
1020
1021 return $content;
7423f116 1022}
1023
1024function calendar_add_month($month, $year) {
1025 if($month == 12) {
1026 return array(1, $year + 1);
1027 }
1028 else {
1029 return array($month + 1, $year);
1030 }
1031}
1032
1033function calendar_sub_month($month, $year) {
1034 if($month == 1) {
1035 return array(12, $year - 1);
1036 }
1037 else {
1038 return array($month - 1, $year);
1039 }
1040}
1041
7c50db30 1042function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) {
7423f116 1043 $eventsbyday = array();
1044 $typesbyday = array();
1045 $durationbyday = array();
1046
1047 if($events === false) {
1048 return;
1049 }
1050
7423f116 1051 foreach($events as $event) {
7423f116 1052
7b38bfa6 1053 $startdate = usergetdate($event->timestart);
b4892fa2 1054 // Set end date = start date if no duration
1055 if ($event->timeduration) {
1056 $enddate = usergetdate($event->timestart + $event->timeduration - 1);
1057 } else {
1058 $enddate = $startdate;
1059 }
7423f116 1060
7b38bfa6 1061 // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair
ef618501 1062 if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
7b38bfa6 1063 // Out of bounds
1064 continue;
7423f116 1065 }
1066
7b38bfa6 1067 $eventdaystart = intval($startdate['mday']);
7423f116 1068
7b38bfa6 1069 if($startdate['mon'] == $month && $startdate['year'] == $year) {
1070 // Give the event to its day
1071 $eventsbyday[$eventdaystart][] = $event->id;
7423f116 1072
7b38bfa6 1073 // Mark the day as having such an event
9064751b 1074 if($event->courseid == SITEID && $event->groupid == 0) {
7b38bfa6 1075 $typesbyday[$eventdaystart]['startglobal'] = true;
b4892fa2 1076 // Set event class for global event
1077 $events[$event->id]->class = 'event_global';
7b38bfa6 1078 }
c3d3b6d4 1079 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
7b38bfa6 1080 $typesbyday[$eventdaystart]['startcourse'] = true;
b4892fa2 1081 // Set event class for course event
bf9ae76c 1082 $events[$event->id]->class = 'event_course'.array_search($event->courseid, $courses) % CALENDAR_MAXCOURSES;
7b38bfa6 1083 }
1084 else if($event->groupid) {
1085 $typesbyday[$eventdaystart]['startgroup'] = true;
b4892fa2 1086 // Set event class for group event
1087 $events[$event->id]->class = 'event_group';
7b38bfa6 1088 }
1089 else if($event->userid) {
1090 $typesbyday[$eventdaystart]['startuser'] = true;
b4892fa2 1091 // Set event class for user event
1092 $events[$event->id]->class = 'event_user';
7b38bfa6 1093 }
1094 }
7423f116 1095
7b38bfa6 1096 if($event->timeduration == 0) {
1097 // Proceed with the next
1098 continue;
1099 }
7423f116 1100
7b38bfa6 1101 // The event starts on $month $year or before. So...
ef618501 1102 $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0;
7b38bfa6 1103
1104 // Also, it ends on $month $year or later...
1105 $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year);
1106
1107 // Mark all days between $lowerbound and $upperbound (inclusive) as duration
1108 for($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
1109 $durationbyday[$i][] = $event->id;
9064751b 1110 if($event->courseid == SITEID && $event->groupid == 0) {
7b38bfa6 1111 $typesbyday[$i]['durationglobal'] = true;
1112 }
c3d3b6d4 1113 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
7b38bfa6 1114 $typesbyday[$i]['durationcourse'] = true;
1115 }
1116 else if($event->groupid) {
1117 $typesbyday[$i]['durationgroup'] = true;
1118 }
1119 else if($event->userid) {
1120 $typesbyday[$i]['durationuser'] = true;
7423f116 1121 }
1122 }
7b38bfa6 1123
7423f116 1124 }
1125 return;
1126}
1127
b63c0ee5 1128function calendar_get_module_cached(&$coursecache, $modulename, $instance) {
1129 $module = get_coursemodule_from_instance($modulename, $instance);
7423f116 1130
1131 if($module === false) return false;
b63c0ee5 1132 if(!calendar_get_course_cached($coursecache, $module->course)) {
7423f116 1133 return false;
1134 }
1135 return $module;
1136}
1137
1138function calendar_get_course_cached(&$coursecache, $courseid) {
1139 if(!isset($coursecache[$courseid])) {
1140 $coursecache[$courseid] = get_record('course', 'id', $courseid);
1141 }
1142 return $coursecache[$courseid];
1143}
1144
1145function calendar_session_vars() {
1146 global $SESSION, $USER;
1147
1066e0dc 1148 if(!empty($USER->id) && isset($USER->realuser) && !isset($SESSION->cal_loggedinas)) {
02ce3848 1149 // We just logged in as someone else, update the filtering
1150 unset($SESSION->cal_users_shown);
1151 unset($SESSION->cal_courses_shown);
1152 $SESSION->cal_loggedinas = true;
054193be 1153 if(intval(get_user_preferences('calendar_persistflt', 0))) {
1154 calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
1155 }
02ce3848 1156 }
1066e0dc 1157 else if(!empty($USER->id) && !isset($USER->realuser) && isset($SESSION->cal_loggedinas)) {
02ce3848 1158 // We just logged back to our real self, update again
1159 unset($SESSION->cal_users_shown);
1160 unset($SESSION->cal_courses_shown);
1161 unset($SESSION->cal_loggedinas);
054193be 1162 if(intval(get_user_preferences('calendar_persistflt', 0))) {
1163 calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
1164 }
02ce3848 1165 }
1166
7423f116 1167 if(!isset($SESSION->cal_course_referer)) {
1168 $SESSION->cal_course_referer = 0;
1169 }
1170 if(!isset($SESSION->cal_show_global)) {
1171 $SESSION->cal_show_global = true;
1172 }
1173 if(!isset($SESSION->cal_show_groups)) {
1174 $SESSION->cal_show_groups = true;
1175 }
1176 if(!isset($SESSION->cal_show_course)) {
1177 $SESSION->cal_show_course = true;
1178 }
1179 if(!isset($SESSION->cal_show_user)) {
89adb174 1180 $SESSION->cal_show_user = true;
7423f116 1181 }
f21ed0f3 1182 // if(empty($SESSION->cal_courses_shown)) {
43c3ffbe 1183 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
f21ed0f3 1184 //}
89adb174 1185 if(empty($SESSION->cal_users_shown)) {
1186 // The empty() instead of !isset() here makes a whole world of difference,
1187 // as it will automatically change to the user's id when the user first logs
1188 // in. With !isset(), it would never do that.
86a1ba04 1189 $SESSION->cal_users_shown = !empty($USER->id) ? $USER->id : false;
89adb174 1190 }
1191 else if(is_numeric($SESSION->cal_users_shown) && !empty($USER->id) && $SESSION->cal_users_shown != $USER->id) {
1192 // Follow the white rabbit, for example if a teacher logs in as a student
1193 $SESSION->cal_users_shown = $USER->id;
1194 }
7423f116 1195}
1196
1197function calendar_overlib_html() {
9c6bf80e 1198 return '<div id="overDiv" style="position: absolute; visibility: hidden; z-index:1000;"></div>'
1199 .'<script type="text/javascript" src="'.CALENDAR_URL.'overlib.cfg.php"></script>';
7423f116 1200}
1201
7423f116 1202function calendar_set_referring_course($courseid) {
1203 global $SESSION;
1204 $SESSION->cal_course_referer = intval($courseid);
1205}
1206
43c3ffbe 1207function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) {
257e3f4c 1208 global $SESSION, $USER, $CFG;
1209
43c3ffbe 1210 // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause
51f8a12f 1211 // the code to function incorrectly UNLESS we convert it to an integer. One case where
1212 // PHP's loose type system works against us.
43c3ffbe 1213 if(is_string($SESSION->cal_courses_shown)) {
1214 $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown);
51f8a12f 1215 }
1216
43c3ffbe 1217 if($courseeventsfrom === NULL) {
1218 $courseeventsfrom = $SESSION->cal_courses_shown;
7423f116 1219 }
43c3ffbe 1220 if($groupeventsfrom === NULL) {
1221 $groupeventsfrom = $SESSION->cal_courses_shown;
7423f116 1222 }
1223
43c3ffbe 1224 if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) {
1225 if(is_int($courseeventsfrom)) {
9064751b 1226 $courses = array(SITEID, $courseeventsfrom);
7423f116 1227 }
43c3ffbe 1228 else if(is_array($courseeventsfrom)) {
1229 $courses = array_keys($courseeventsfrom);
9064751b 1230 $courses[] = SITEID;
7423f116 1231 }
1232 }
43c3ffbe 1233 else if($SESSION->cal_show_course) {
1234 if(is_int($courseeventsfrom)) {
1235 $courses = array($courseeventsfrom);
7423f116 1236 }
43c3ffbe 1237 else if(is_array($courseeventsfrom)) {
1238 $courses = array_keys($courseeventsfrom);
7423f116 1239 }
e749554e 1240 $courses = array_diff($courses, array(SITEID));
7423f116 1241 }
1242 else if($SESSION->cal_show_global) {
e749554e 1243 $courses = array(SITEID);
7423f116 1244 }
1245 else {
1246 $courses = false;
1247 }
0e6a8f4b 1248 //BUG 6130 clean $courses array as SESSION has bad entries.
1249 // [pj] TODO: See if this has to do with my new change in get_default_courses and can be taken out
1384da1e 1250 if (is_array($courses)) {
1251 foreach ($courses as $index => $value) {
1252 if (empty($value)) unset($courses[$index]);
1253 }
7c50db30 1254
1255 // Sort courses for consistent colour highlighting
1256 // Effectively ignoring SITEID as setting as last course id
1257 $key = array_search(SITEID, $courses);
1258 if ($key !== false) {
1259 unset($courses[$key]);
1260 sort($courses);
1261 $courses[] = SITEID;
1262 } else {
1263 sort($courses);
1264 }
0e6a8f4b 1265 }
7423f116 1266
43c3ffbe 1267 if($SESSION->cal_show_user || $ignorefilters) {
89adb174 1268 // This doesn't work for arrays yet (maybe someday it will)
1269 $user = $SESSION->cal_users_shown;
7423f116 1270 }
1271 else {
1272 $user = false;
1273 }
43c3ffbe 1274 if($SESSION->cal_show_groups || $ignorefilters) {
1275 if(is_int($groupeventsfrom)) {
1276 $groupcourses = array($groupeventsfrom);
7423f116 1277 }
43c3ffbe 1278 else if(is_array($groupeventsfrom)) {
1279 $groupcourses = array_keys($groupeventsfrom);
1280 }
257e3f4c 1281
9c37662f 1282 // XXX TODO: not sure how to replace $CFG->calendar_adminseesall
1283 if(has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID)) && !empty($CFG->calendar_adminseesall)) {
257e3f4c 1284 $group = true;
1285 }
1286 else {
1287 $grouparray = array();
69762501 1288
257e3f4c 1289 // We already have the courses to examine in $courses
1290 // For each course...
37d87d11 1291
257e3f4c 1292 foreach($groupcourses as $courseid) {
69762501 1293
257e3f4c 1294 // If the user is an editing teacher in there,
cc398c72 1295 if(!empty($USER->id) && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $courseid))) {
69762501 1296 // If this course has groups, show events from all of them
b8227b88 1297 if(is_int($groupeventsfrom)) {
1298 $courserecord = get_record('course', 'id', $courseid);
1299 if ($courserecord->groupmode != NOGROUPS || !$courserecord->groupmodeforce) {
1300 $groupids[] = $courseid;
1301 }
1302 }
1303 else if(isset($SESSION->cal_courses_shown[$courseid]) && ($SESSION->cal_courses_shown[$courseid]->groupmode != NOGROUPS || !$SESSION->cal_courses_shown[$courseid]->groupmodeforce)) {
37d87d11 1304 $groupids[] = $courseid;
257e3f4c 1305 }
1306 }
69762501 1307
1308 // Otherwise (not editing teacher) show events from the group he is a member of
257e3f4c 1309 else if(isset($USER->groupmember[$courseid])) {
fa22fd5f 1310 //changed to 2D array
1311 foreach ($USER->groupmember[$courseid] as $groupid){
1312 $grouparray[] = $groupid;
1313 }
7423f116 1314 }
1315 }
37d87d11 1316
6b4aeb31 1317 if (!empty($groupids)) {
2524b0f2 1318 $sql = "SELECT id
1319 FROM {$CFG->prefix}groups
6b4aeb31 1320 WHERE courseid IN (".implode(',', $groupids).')';
1321
1322 if ($grouprecords= get_records_sql($sql)) {
1323 $grouparray = array_merge($grouparray, array_keys($grouprecords));
1324 }
1325 }
37d87d11 1326
257e3f4c 1327 if(empty($grouparray)) {
1328 $group = false;
1329 }
1330 else {
1331 $group = $grouparray;
6c9584d1 1332 }
7423f116 1333 }
257e3f4c 1334
7423f116 1335 }
1336 else {
1337 $group = false;
1338 }
1339}
1340
1341function calendar_edit_event_allowed($event) {
89491dbd 1342
7423f116 1343 global $USER;
1344
89491dbd 1345 // can not be using guest account
1346 if ($USER->username == "guest") {
1347 return false;
1348 }
cc398c72 1349
89491dbd 1350 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
1351 // if user has manageentries at site level, return true
28ee98c5 1352 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
89491dbd 1353 return true;
f52f7413 1354 }
c0a2c361 1355
1356 // if groupid is set, it's definitely a group event
1357 if ($event->groupid) {
3c49918a 1358 //TODO:check.
1359 if (! groups_group_exists($event->groupid)) {
69244b91 1360 return false;
89491dbd 1361 }
cc398c72 1362
89491dbd 1363 // this is ok because if you have this capability at course level, you should be able
1364 // to edit group calendar too
1365 // there is no need to check membership, because if you have this capability
1366 // you will have a role in this group context
c0a2c361 1367 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_GROUP, $event->groupid));
89491dbd 1368 } else if ($event->courseid) {
c0a2c361 1369 // if groupid is not set, but course is set,
1370 // it's definiely a course event
1371 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $event->courseid));
1372 } else if ($event->userid && $event->userid == $USER->id) {
1373 // if course is not set, but userid id set, it's a user event
1374 return (has_capability('moodle/calendar:manageownentries', $sitecontext));
1375 }
7423f116 1376 return false;
1377}
1378
8c54cec6 1379function calendar_get_default_courses($ignoreref = false) {
9ff136e5 1380 global $USER, $CFG, $SESSION;
1381
8c54cec6 1382 if(!empty($SESSION->cal_course_referer) && !$ignoreref) {
4c7d1137 1383 return array($SESSION->cal_course_referer => 1);
9ff136e5 1384 }
7423f116 1385
1066e0dc 1386 if(empty($USER->id)) {
2ef75eee 1387 return array();
1388 }
1389
7423f116 1390 $courses = array();
03bb25e1 1391 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
1392 if (!empty($CFG->calendar_adminseesall)) {
86f092d2 1393 $courses = get_records_sql('SELECT id, 1 FROM '.$CFG->prefix.'course');
1394 return $courses;
1395 }
95a89225 1396 }
03bb25e1 1397
daaf61f4 1398 if (isset($CFG->adminseesall)) {
e3fc108e 1399 $courses = get_my_courses($USER->id, null, null, $CFG->adminseesall);
daaf61f4 1400 }
1401 else {
e3fc108e 1402 $courses = get_my_courses($USER->id, null, null, false);
7423f116 1403 }
37d87d11 1404
1405 return $courses;
7423f116 1406}
1407
1e1ff33b 1408function calendar_preferences_button() {
1409 global $CFG, $USER;
7423f116 1410
1411 // Guests have no preferences
f52f7413 1412 if (empty($USER->id) || isguest()) {
7423f116 1413 return '';
1414 }
1415
fa738731 1416 return "<form $CFG->frametarget method=\"get\" ".
1e1ff33b 1417 " action=\"$CFG->wwwroot/calendar/preferences.php\">".
2aab6488 1418 "<div><input type=\"submit\" value=\"".get_string("preferences", "calendar")." ...\" /></div></form>";
7423f116 1419}
1420
b4892fa2 1421function calendar_format_event_time($event, $now, $morehref, $usecommonwords = true, $showtime=0) {
8f896582 1422 $startdate = usergetdate($event->timestart);
1423 $enddate = usergetdate($event->timestart + $event->timeduration);
1424 $usermidnightstart = usergetmidnight($event->timestart);
1425
1426 if($event->timeduration) {
1427 // To avoid doing the math if one IF is enough :)
1428 $usermidnightend = usergetmidnight($event->timestart + $event->timeduration);
1429 }
1430 else {
1431 $usermidnightend = $usermidnightstart;
1432 }
1433
1434 // OK, now to get a meaningful display...
1435 // First of all we have to construct a human-readable date/time representation
1436
b4892fa2 1437 if($event->timeduration) {
8f896582 1438 // It has a duration
b4892fa2 1439 if($usermidnightstart == $usermidnightend ||
1440 ($event->timestart == $usermidnightstart) && ($event->timeduration == 86400 || $event->timeduration == 86399) ||
1441 ($event->timestart + $event->timeduration <= $usermidnightstart + 86400)) {
8f896582 1442 // But it's all on the same day
8f896582 1443 $timestart = calendar_time_representation($event->timestart);
1444 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
b4892fa2 1445 $time = $timestart.' <strong>&raquo;</strong> '.$timeend;
1446
1447 if ($event->timestart == $usermidnightstart && ($event->timeduration == 86400 || $event->timeduration == 86399)) {
1448 $time = get_string('allday', 'calendar');
1449 }
8f896582 1450
1451 // Set printable representation
b4892fa2 1452 if (!$showtime) {
1453 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1454 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).', '.$time;
1455 } else {
1456 $eventtime = $time;
1457 }
1458 } else {
8f896582 1459 // It spans two or more days
b4892fa2 1460 $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords).', ';
1461 if ($showtime == $usermidnightstart) {
1462 $daystart = '';
1463 }
8f896582 1464 $timestart = calendar_time_representation($event->timestart);
b4892fa2 1465 $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords).', ';
1466 if ($showtime == $usermidnightend) {
1467 $dayend = '';
1468 }
8f896582 1469 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
1470
1471 // Set printable representation
b4892fa2 1472 if ($now >= $usermidnightstart && $now < ($usermidnightstart + 86400)) {
1473 $eventtime = $timestart.' <strong>&raquo;</strong> '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
1474 $timeend;
1475 } else {
1476 $eventtime = calendar_get_link_tag($daystart, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).
1477 $timestart.' <strong>&raquo;</strong> '.calendar_get_link_tag($dayend, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $enddate['mday'], $enddate['mon'], $enddate['year']).
1478 $timeend;
1479 }
8f896582 1480 }
b4892fa2 1481 } else {
1c4bedd7 1482 $time = ' ';
8f896582 1483
1484 // Set printable representation
b4892fa2 1485 if (!$showtime) {
1486 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1487 $eventtime = calendar_get_link_tag($day, CALENDAR_URL.'view.php?view=day'.$morehref.'&amp;', $startdate['mday'], $startdate['mon'], $startdate['year']).trim($time);
1488 } else {
1489 $eventtime = $time;
1490 }
1491 }
1492
1493 if($event->timestart + $event->timeduration < $now) {
1494 // It has expired
1495 $eventtime = '<span class="dimmed_text">'.str_replace(' href=', ' class="dimmed" href=', $eventtime).'</span>';
8f896582 1496 }
09d36284 1497
8f896582 1498 return $eventtime;
1499}
054193be 1500
86f092d2 1501function calendar_print_month_selector($name, $selected) {
1502
1503 $months = array();
1504
1505 for ($i=1; $i<=12; $i++) {
76ab1c33 1506 $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B');
86f092d2 1507 }
1508
1509 choose_from_menu($months, $name, $selected, '');
1510}
1511
054193be 1512function calendar_get_filters_status() {
1513 global $SESSION;
1514
1515 $status = 0;
1516 if($SESSION->cal_show_global) {
1517 $status += 1;
1518 }
1519 if($SESSION->cal_show_course) {
1520 $status += 2;
1521 }
1522 if($SESSION->cal_show_groups) {
1523 $status += 4;
1524 }
1525 if($SESSION->cal_show_user) {
1526 $status += 8;
1527 }
1528 return $status;
1529}
1530
1531function calendar_set_filters_status($packed_bitfield) {
1532 global $SESSION, $USER;
1533
1534 if(!isset($USER) || empty($USER->id)) {
1535 return false;
1536 }
1537
1538 $SESSION->cal_show_global = ($packed_bitfield & 1);
1539 $SESSION->cal_show_course = ($packed_bitfield & 2);
1540 $SESSION->cal_show_groups = ($packed_bitfield & 4);
1541 $SESSION->cal_show_user = ($packed_bitfield & 8);
1542
1543 return true;
1544}
1545
86ac8b24 1546function calendar_get_allowed_types(&$allowed) {
1547 global $USER, $CFG, $SESSION;
1548 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
1549 $allowed->user = has_capability('moodle/calendar:manageownentries', $sitecontext);
1550 $allowed->groups = false; // This may change just below
1551 $allowed->courses = false; // This may change just below
1552 $allowed->site = has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, SITEID));
1553
1554 if(!empty($SESSION->cal_course_referer) && $SESSION->cal_course_referer != SITEID && has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $SESSION->cal_course_referer))) {
1555 $course = get_record('course', 'id', $SESSION->cal_course_referer);
1556
1557 $allowed->courses = array($course->id => 1);
1558
1559 if($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
2c386f82 1560 $allowed->groups = (boolean)groups_get_all_groups($SESSION->cal_course_referer);
86ac8b24 1561 }
1562 }
1563}
1564
1565/**
1566 * see if user can add calendar entries at all
1567 * used to print the "New Event" button
1568 * @return bool
1569 */
1570function calendar_user_can_add_event() {
1571 calendar_get_allowed_types($allowed);
1572 return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->site);
1573}
d0a32914 1574?>