MDL-27854 qformat_xml fix import/export of essay questions to include the new options.
[moodle.git] / calendar / lib.php
blob92fd12c6bc563e9baaaad44ecfef82ff37933177
1 <?php
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@teikav.edu.gr) //
13 // Jon Papaioannou (pj@moodle.org) //
14 // //
15 // Programming and development: //
16 // Jon Papaioannou (pj@moodle.org) //
17 // //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@moodle.org) //
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 /////////////////////////////////////////////////////////////////////////////
41 // These are read by the administration component to provide default values
42 define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21);
43 define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10);
44 define('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
47 define('CALENDAR_DEFAULT_WEEKEND', 65);
48 define('CALENDAR_UPCOMING_DAYS', isset($CFG->calendar_lookahead) ? intval($CFG->calendar_lookahead) : CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD);
49 define('CALENDAR_UPCOMING_MAXEVENTS', isset($CFG->calendar_maxevents) ? intval($CFG->calendar_maxevents) : CALENDAR_DEFAULT_UPCOMING_MAXEVENTS);
50 define('CALENDAR_WEEKEND', isset($CFG->calendar_weekend) ? intval($CFG->calendar_weekend) : CALENDAR_DEFAULT_WEEKEND);
51 define('CALENDAR_URL', $CFG->wwwroot.'/calendar/');
52 define('CALENDAR_TF_24', '%H:%M');
53 define('CALENDAR_TF_12', '%I:%M %p');
55 /**
56 * CALENDAR_STARTING_WEEKDAY has since been deprecated please call calendar_get_starting_weekday() instead
57 * @deprecated
59 define('CALENDAR_STARTING_WEEKDAY', CALENDAR_DEFAULT_STARTING_WEEKDAY);
61 $CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
63 /**
64 * Gets the first day of the week
66 * Used to be define('CALENDAR_STARTING_WEEKDAY', blah);
68 * @return int
70 function calendar_get_starting_weekday() {
71 global $CFG;
73 if (isset($CFG->calendar_startwday)) {
74 $firstday = $CFG->calendar_startwday;
75 } else {
76 $firstday = get_string('firstdayofweek', 'langconfig');
79 if(!is_numeric($firstday)) {
80 return CALENDAR_DEFAULT_STARTING_WEEKDAY;
81 } else {
82 return intval($firstday) % 7;
86 /**
87 * Generates the HTML for a miniature calendar
89 * @global core_renderer $OUTPUT
90 * @param array $courses
91 * @param array $groups
92 * @param array $users
93 * @param int $cal_month
94 * @param int $cal_year
95 * @return string
97 function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) {
98 global $CFG, $USER, $OUTPUT;
100 $display = new stdClass;
101 $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
102 $display->maxwday = $display->minwday + 6;
104 $content = '';
106 if(!empty($cal_month) && !empty($cal_year)) {
107 $thisdate = usergetdate(time()); // Date and time the user sees at his location
108 if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) {
109 // Navigated to this month
110 $date = $thisdate;
111 $display->thismonth = true;
112 } else {
113 // Navigated to other month, let's do a nice trick and save us a lot of work...
114 if(!checkdate($cal_month, 1, $cal_year)) {
115 $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']);
116 $display->thismonth = true;
117 } else {
118 $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year);
119 $display->thismonth = false;
122 } else {
123 $date = usergetdate(time()); // Date and time the user sees at his location
124 $display->thismonth = true;
127 // Fill in the variables we 're going to use, nice and tidy
128 list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display
129 $display->maxdays = calendar_days_in_month($m, $y);
131 if (get_user_timezone_offset() < 99) {
132 // We 'll keep these values as GMT here, and offset them when the time comes to query the db
133 $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT
134 $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT
135 } else {
136 // no timezone info specified
137 $display->tstart = mktime(0, 0, 0, $m, 1, $y);
138 $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y);
141 $startwday = dayofweek(1, $m, $y);
143 // Align the starting weekday to fall in our display range
144 // This is simple, not foolproof.
145 if($startwday < $display->minwday) {
146 $startwday += 7;
149 // TODO: THIS IS TEMPORARY CODE!
150 // [pj] I was just reading through this and realized that I when writing this code I was probably
151 // asking for trouble, as all these time manipulations seem to be unnecessary and a simple
152 // make_timestamp would accomplish the same thing. So here goes a test:
153 //$test_start = make_timestamp($y, $m, 1);
154 //$test_end = make_timestamp($y, $m, $display->maxdays, 23, 59, 59);
155 //if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) {
156 //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);
158 //if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) {
159 //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);
163 // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ!
164 $events = calendar_get_events(
165 usertime($display->tstart) - dst_offset_on($display->tstart),
166 usertime($display->tend) - dst_offset_on($display->tend),
167 $users, $groups, $courses);
169 // Set event course class for course events
170 if (!empty($events)) {
171 foreach ($events as $eventid => $event) {
172 if (!empty($event->modulename)) {
173 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
174 if (!groups_course_module_visible($cm)) {
175 unset($events[$eventid]);
181 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
182 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
183 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
184 // arguments to this function.
186 $hrefparams = array();
187 if(!empty($courses)) {
188 $courses = array_diff($courses, array(SITEID));
189 if(count($courses) == 1) {
190 $hrefparams['course'] = reset($courses);
194 // We want to have easy access by day, since the display is on a per-day basis.
195 // Arguments passed by reference.
196 //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday);
197 calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses);
199 //Accessibility: added summary and <abbr> elements.
200 ///global $CALENDARDAYS; appears to be broken.
201 $days_title = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
203 $summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear')));
204 $summary = get_string('tabledata', 'access', $summary);
205 $content .= '<table class="minicalendar calendartable" summary="'.$summary.'">'; // Begin table
206 $content .= '<tr class="weekdays">'; // Header row: day names
208 // Print out the names of the weekdays
209 $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');
210 for($i = $display->minwday; $i <= $display->maxwday; ++$i) {
211 // This uses the % operator to get the correct weekday no matter what shift we have
212 // applied to the $display->minwday : $display->maxwday range from the default 0 : 6
213 $content .= '<th scope="col"><abbr title="'. get_string($days_title[$i % 7], 'calendar') .'">'.
214 get_string($days[$i % 7], 'calendar') ."</abbr></th>\n";
217 $content .= '</tr><tr>'; // End of day names; prepare for day numbers
219 // For the table display. $week is the row; $dayweek is the column.
220 $dayweek = $startwday;
222 // Paddding (the first week may have blank days in the beginning)
223 for($i = $display->minwday; $i < $startwday; ++$i) {
224 $content .= '<td class="dayblank">&nbsp;</td>'."\n";
227 // Now display all the calendar
228 for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) {
229 if($dayweek > $display->maxwday) {
230 // We need to change week (table row)
231 $content .= '</tr><tr>';
232 $dayweek = $display->minwday;
235 // Reset vars
236 $cell = '';
237 if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) {
238 // Weekend. This is true no matter what the exact range is.
239 $class = 'weekend day';
240 } else {
241 // Normal working day.
242 $class = 'day';
245 // Special visual fx if an event is defined
246 if(isset($eventsbyday[$day])) {
247 $class .= ' hasevent';
248 $hrefparams['view'] = 'day';
249 $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $hrefparams), $day, $m, $y);
251 $popupcontent = '';
252 foreach($eventsbyday[$day] as $eventid) {
253 if (!isset($events[$eventid])) {
254 continue;
256 $event = $events[$eventid];
257 $popupalt = '';
258 $component = 'moodle';
259 if(!empty($event->modulename)) {
260 $popupicon = 'icon';
261 $popupalt = $event->modulename;
262 $component = $event->modulename;
263 } else if ($event->courseid == SITEID) { // Site event
264 $popupicon = 'c/site';
265 } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
266 $popupicon = 'c/course';
267 } else if ($event->groupid) { // Group event
268 $popupicon = 'c/group';
269 } else if ($event->userid) { // User event
270 $popupicon = 'c/user';
273 $dayhref->set_anchor('event_'.$event->id);
275 $popupcontent .= html_writer::start_tag('div');
276 $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
277 $popupcontent .= html_writer::link($dayhref, format_string($event->name, true));
278 $popupcontent .= html_writer::end_tag('div');
281 //Accessibility: functionality moved to calendar_get_popup.
282 if($display->thismonth && $day == $d) {
283 $popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent);
284 } else {
285 $popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent);
288 // Class and cell content
289 if(isset($typesbyday[$day]['startglobal'])) {
290 $class .= ' calendar_event_global';
291 } else if(isset($typesbyday[$day]['startcourse'])) {
292 $class .= ' calendar_event_course';
293 } else if(isset($typesbyday[$day]['startgroup'])) {
294 $class .= ' calendar_event_group';
295 } else if(isset($typesbyday[$day]['startuser'])) {
296 $class .= ' calendar_event_user';
298 $cell = '<a href="'.(string)$dayhref.'" '.$popup.'>'.$day.'</a>';
299 } else {
300 $cell = $day;
303 $durationclass = false;
304 if (isset($typesbyday[$day]['durationglobal'])) {
305 $durationclass = ' duration_global';
306 } else if(isset($typesbyday[$day]['durationcourse'])) {
307 $durationclass = ' duration_course';
308 } else if(isset($typesbyday[$day]['durationgroup'])) {
309 $durationclass = ' duration_group';
310 } else if(isset($typesbyday[$day]['durationuser'])) {
311 $durationclass = ' duration_user';
313 if ($durationclass) {
314 $class .= ' duration '.$durationclass;
317 // If event has a class set then add it to the table day <td> tag
318 // Note: only one colour for minicalendar
319 if(isset($eventsbyday[$day])) {
320 foreach($eventsbyday[$day] as $eventid) {
321 if (!isset($events[$eventid])) {
322 continue;
324 $event = $events[$eventid];
325 if (!empty($event->class)) {
326 $class .= ' '.$event->class;
328 break;
332 // Special visual fx for today
333 //Accessibility: hidden text for today, and popup.
334 if($display->thismonth && $day == $d) {
335 $class .= ' today';
336 $today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort'));
338 if(! isset($eventsbyday[$day])) {
339 $class .= ' eventnone';
340 $popup = calendar_get_popup(true, false);
341 $cell = '<a href="#" '.$popup.'>'.$day.'</a>';
343 $cell = get_accesshide($today.' ').$cell;
346 // Just display it
347 if(!empty($class)) {
348 $class = ' class="'.$class.'"';
350 $content .= '<td'.$class.'>'.$cell."</td>\n";
353 // Paddding (the last week may have blank days at the end)
354 for($i = $dayweek; $i <= $display->maxwday; ++$i) {
355 $content .= '<td class="dayblank">&nbsp;</td>';
357 $content .= '</tr>'; // Last row ends
359 $content .= '</table>'; // Tabular display of days ends
361 return $content;
365 * calendar_get_popup, called at multiple points in from calendar_get_mini.
366 * Copied and modified from calendar_get_mini.
367 * @global moodle_page $PAGE
368 * @param $is_today bool, false except when called on the current day.
369 * @param $event_timestart mixed, $events[$eventid]->timestart, OR false if there are no events.
370 * @param $popupcontent string.
371 * @return $popup string, contains onmousover and onmouseout events.
373 function calendar_get_popup($is_today, $event_timestart, $popupcontent='') {
374 global $PAGE;
375 static $popupcount;
376 if ($popupcount === null) {
377 $popupcount = 1;
379 $popupcaption = '';
380 if($is_today) {
381 $popupcaption = get_string('today', 'calendar').' ';
383 if (false === $event_timestart) {
384 $popupcaption .= userdate(time(), get_string('strftimedayshort'));
385 $popupcontent = get_string('eventnone', 'calendar');
387 } else {
388 $popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort')));
390 $id = 'calendar_tooltip_'.$popupcount;
391 $PAGE->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.add_event', array(array('eventId'=>$id,'title'=>$popupcaption, 'content'=>$popupcontent)));
393 $popupcount++;
394 return 'id="'.$id.'"';
397 function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) {
398 global $CFG, $COURSE, $DB;
400 $display = new stdClass;
401 $display->range = $daysinfuture; // How many days in the future we 'll look
402 $display->maxevents = $maxevents;
404 $output = array();
406 // Prepare "course caching", since it may save us a lot of queries
407 $coursecache = array();
409 $processed = 0;
410 $now = time(); // We 'll need this later
411 $usermidnighttoday = usergetmidnight($now);
413 if ($fromtime) {
414 $display->tstart = $fromtime;
415 } else {
416 $display->tstart = $usermidnighttoday;
419 // This works correctly with respect to the user's DST, but it is accurate
420 // only because $fromtime is always the exact midnight of some day!
421 $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1;
423 // Get the events matching our criteria
424 $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses);
426 // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after
427 // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month
428 // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra
429 // arguments to this function.
431 $hrefparams = array();
432 if(!empty($courses)) {
433 $courses = array_diff($courses, array(SITEID));
434 if(count($courses) == 1) {
435 $hrefparams['course'] = reset($courses);
439 if ($events !== false) {
441 $modinfo =& get_fast_modinfo($COURSE);
443 foreach($events as $event) {
446 if (!empty($event->modulename)) {
447 if ($event->courseid == $COURSE->id) {
448 if (isset($modinfo->instances[$event->modulename][$event->instance])) {
449 $cm = $modinfo->instances[$event->modulename][$event->instance];
450 if (!$cm->uservisible) {
451 continue;
454 } else {
455 if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) {
456 continue;
458 if (!coursemodule_visible_for_user($cm)) {
459 continue;
462 if ($event->modulename == 'assignment'){
463 // create calendar_event to test edit_event capability
464 // this new event will also prevent double creation of calendar_event object
465 $checkevent = new calendar_event($event);
466 // TODO: rewrite this hack somehow
467 if (!calendar_edit_event_allowed($checkevent)){ // cannot manage entries, eg. student
468 if (!$assignment = $DB->get_record('assignment', array('id'=>$event->instance))) {
469 // print_error("invalidid", 'assignment');
470 continue;
472 // assign assignment to assignment object to use hidden_is_hidden method
473 require_once($CFG->dirroot.'/mod/assignment/lib.php');
475 if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) {
476 continue;
478 require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php');
480 $assignmentclass = 'assignment_'.$assignment->assignmenttype;
481 $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm);
483 if ($assignmentinstance->description_is_hidden()){//force not to show description before availability
484 $event->description = get_string('notavailableyet', 'assignment');
490 if ($processed >= $display->maxevents) {
491 break;
494 $event->time = calendar_format_event_time($event, $now, $hrefparams);
495 $output[] = $event;
496 ++$processed;
499 return $output;
502 function calendar_add_event_metadata($event) {
503 global $CFG, $OUTPUT;
505 //Support multilang in event->name
506 $event->name = format_string($event->name,true);
508 if(!empty($event->modulename)) { // Activity event
509 // The module name is set. I will assume that it has to be displayed, and
510 // also that it is an automatically-generated event. And of course that the
511 // fields for get_coursemodule_from_instance are set correctly.
512 $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance);
514 if ($module === false) {
515 return;
518 $modulename = get_string('modulename', $event->modulename);
519 if (get_string_manager()->string_exists($event->eventtype, $event->modulename)) {
520 // will be used as alt text if the event icon
521 $eventtype = get_string($event->eventtype, $event->modulename);
522 } else {
523 $eventtype = '';
525 $icon = $OUTPUT->pix_url('icon', $event->modulename) . '';
527 $event->icon = '<img height="16" width="16" src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" style="vertical-align: middle;" />';
528 $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>';
529 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$coursecache[$module->course]->fullname.'</a>';
530 $event->cmid = $module->id;
533 } else if($event->courseid == SITEID) { // Site event
534 $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/site') . '" alt="'.get_string('globalevent', 'calendar').'" style="vertical-align: middle;" />';
535 $event->cssclass = 'calendar_event_global';
536 } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event
537 calendar_get_course_cached($coursecache, $event->courseid);
538 $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/course') . '" alt="'.get_string('courseevent', 'calendar').'" style="vertical-align: middle;" />';
539 $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>';
540 $event->cssclass = 'calendar_event_course';
541 } else if ($event->groupid) { // Group event
542 $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/group') . '" alt="'.get_string('groupevent', 'calendar').'" style="vertical-align: middle;" />';
543 $event->cssclass = 'calendar_event_group';
544 } else if($event->userid) { // User event
545 $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/user') . '" alt="'.get_string('userevent', 'calendar').'" style="vertical-align: middle;" />';
546 $event->cssclass = 'calendar_event_user';
548 return $event;
552 * Prints a calendar event
554 * @deprecated 2.0
556 function calendar_print_event($event, $showactions=true) {
557 global $CFG, $USER, $OUTPUT, $PAGE;
558 debugging('calendar_print_event is deprecated please update your code', DEBUG_DEVELOPER);
559 $renderer = $PAGE->get_renderer('core_calendar');
560 if (!($event instanceof calendar_event)) {
561 $event = new calendar_event($event);
563 echo $renderer->event($event);
567 * Get calendar events
568 * @param int $tstart Start time of time range for events
569 * @param int $tend End time of time range for events
570 * @param array/int/boolean $users array of users, user id or boolean for all/no user events
571 * @param array/int/boolean $groups array of groups, group id or boolean for all/no group events
572 * @param array/int/boolean $courses array of courses, course id or boolean for all/no course events
573 * @param boolean $withduration whether only events starting within time range selected
574 * or events in progress/already started selected as well
575 * @param boolean $ignorehidden whether to select only visible events or all events
576 * @return array of selected events or an empty array if there aren't any (or there was an error)
578 function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) {
579 global $DB;
581 $whereclause = '';
582 // Quick test
583 if(is_bool($users) && is_bool($groups) && is_bool($courses)) {
584 return array();
587 if(is_array($users) && !empty($users)) {
588 // Events from a number of users
589 if(!empty($whereclause)) $whereclause .= ' OR';
590 $whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)';
591 } else if(is_numeric($users)) {
592 // Events from one user
593 if(!empty($whereclause)) $whereclause .= ' OR';
594 $whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)';
595 } else if($users === true) {
596 // Events from ALL users
597 if(!empty($whereclause)) $whereclause .= ' OR';
598 $whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)';
599 } else if($users === false) {
600 // No user at all, do nothing
603 if(is_array($groups) && !empty($groups)) {
604 // Events from a number of groups
605 if(!empty($whereclause)) $whereclause .= ' OR';
606 $whereclause .= ' groupid IN ('.implode(',', $groups).')';
607 } else if(is_numeric($groups)) {
608 // Events from one group
609 if(!empty($whereclause)) $whereclause .= ' OR ';
610 $whereclause .= ' groupid = '.$groups;
611 } else if($groups === true) {
612 // Events from ALL groups
613 if(!empty($whereclause)) $whereclause .= ' OR ';
614 $whereclause .= ' groupid != 0';
616 // boolean false (no groups at all): we don't need to do anything
618 if(is_array($courses) && !empty($courses)) {
619 if(!empty($whereclause)) {
620 $whereclause .= ' OR';
622 $whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))';
623 } else if(is_numeric($courses)) {
624 // One course
625 if(!empty($whereclause)) $whereclause .= ' OR';
626 $whereclause .= ' (groupid = 0 AND courseid = '.$courses.')';
627 } else if ($courses === true) {
628 // Events from ALL courses
629 if(!empty($whereclause)) $whereclause .= ' OR';
630 $whereclause .= ' (groupid = 0 AND courseid != 0)';
633 // Security check: if, by now, we have NOTHING in $whereclause, then it means
634 // that NO event-selecting clauses were defined. Thus, we won't be returning ANY
635 // events no matter what. Allowing the code to proceed might return a completely
636 // valid query with only time constraints, thus selecting ALL events in that time frame!
637 if(empty($whereclause)) {
638 return array();
641 if($withduration) {
642 $timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend;
644 else {
645 $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend;
647 if(!empty($whereclause)) {
648 // We have additional constraints
649 $whereclause = $timeclause.' AND ('.$whereclause.')';
651 else {
652 // Just basic time filtering
653 $whereclause = $timeclause;
656 if ($ignorehidden) {
657 $whereclause .= ' AND visible = 1';
660 $events = $DB->get_records_select('event', $whereclause, null, 'timestart');
661 if ($events === false) {
662 $events = array();
664 return $events;
667 function calendar_top_controls($type, $data) {
668 global $CFG, $CALENDARDAYS;
669 $content = '';
670 if(!isset($data['d'])) {
671 $data['d'] = 1;
674 // Ensure course id passed if relevant
675 // Required due to changes in view/lib.php mainly (calendar_session_vars())
676 $courseid = '';
677 if (!empty($data['id'])) {
678 $courseid = '&amp;course='.$data['id'];
681 if(!checkdate($data['m'], $data['d'], $data['y'])) {
682 $time = time();
684 else {
685 $time = make_timestamp($data['y'], $data['m'], $data['d']);
687 $date = usergetdate($time);
689 $data['m'] = $date['mon'];
690 $data['y'] = $date['year'];
692 //Accessibility: calendar block controls, replaced <table> with <div>.
693 //$nexttext = link_arrow_right(get_string('monthnext', 'access'), $url='', $accesshide=true);
694 //$prevtext = link_arrow_left(get_string('monthprev', 'access'), $url='', $accesshide=true);
696 switch($type) {
697 case 'frontpage':
698 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
699 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
700 $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'index.php?', 0, $nextmonth, $nextyear, $accesshide=true);
701 $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'index.php?', 0, $prevmonth, $prevyear, true);
703 $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']);
704 if (!empty($data['id'])) {
705 $calendarlink->param('course', $data['id']);
708 if (right_to_left()) {
709 $left = $nextlink;
710 $right = $prevlink;
711 } else {
712 $left = $prevlink;
713 $right = $nextlink;
716 $content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
717 $content .= $left.'<span class="hide"> | </span>';
718 $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current'));
719 $content .= '<span class="hide"> | </span>'. $right;
720 $content .= "<span class=\"clearer\"><!-- --></span>\n";
721 $content .= html_writer::end_tag('div');
723 break;
724 case 'course':
725 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
726 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
727 $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'view.php?id='.$data['id'].'&amp;', 0, $nextmonth, $nextyear, $accesshide=true);
728 $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'view.php?id='.$data['id'].'&amp;', 0, $prevmonth, $prevyear, true);
730 $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']);
731 if (!empty($data['id'])) {
732 $calendarlink->param('course', $data['id']);
735 if (right_to_left()) {
736 $left = $nextlink;
737 $right = $prevlink;
738 } else {
739 $left = $prevlink;
740 $right = $nextlink;
743 $content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
744 $content .= $left.'<span class="hide"> | </span>';
745 $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current'));
746 $content .= '<span class="hide"> | </span>'. $right;
747 $content .= "<span class=\"clearer\"><!-- --></span>";
748 $content .= html_writer::end_tag('div');
749 break;
750 case 'upcoming':
751 $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming')), 1, $data['m'], $data['y']);
752 if (!empty($data['id'])) {
753 $calendarlink->param('course', $data['id']);
755 $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
756 $content .= html_writer::tag('div', $calendarlink, array('class'=>'centered'));
757 break;
758 case 'display':
759 $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']);
760 if (!empty($data['id'])) {
761 $calendarlink->param('course', $data['id']);
763 $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')));
764 $content .= html_writer::tag('h3', $calendarlink);
765 break;
766 case 'month':
767 list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']);
768 list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']);
769 $prevdate = make_timestamp($prevyear, $prevmonth, 1);
770 $nextdate = make_timestamp($nextyear, $nextmonth, 1);
771 $prevlink = calendar_get_link_previous(userdate($prevdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&amp;', 1, $prevmonth, $prevyear);
772 $nextlink = calendar_get_link_next(userdate($nextdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&amp;', 1, $nextmonth, $nextyear);
774 if (right_to_left()) {
775 $left = $nextlink;
776 $right = $prevlink;
777 } else {
778 $left = $prevlink;
779 $right = $nextlink;
782 $content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
783 $content .= $left . '<span class="hide"> | </span><h1 class="current">'.userdate($time, get_string('strftimemonthyear'))."</h1>";
784 $content .= '<span class="hide"> | </span>' . $right;
785 $content .= '<span class="clearer"><!-- --></span>';
786 $content .= html_writer::end_tag('div')."\n";
787 break;
788 case 'day':
789 $data['d'] = $date['mday']; // Just for convenience
790 $prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1));
791 $nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1));
792 $prevname = calendar_wday_name($CALENDARDAYS[$prevdate['wday']]);
793 $nextname = calendar_wday_name($CALENDARDAYS[$nextdate['wday']]);
794 $prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&amp;', $prevdate['mday'], $prevdate['mon'], $prevdate['year']);
795 $nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&amp;', $nextdate['mday'], $nextdate['mon'], $nextdate['year']);
797 if (right_to_left()) {
798 $left = $nextlink;
799 $right = $prevlink;
800 } else {
801 $left = $prevlink;
802 $right = $nextlink;
805 $content .= html_writer::start_tag('div', array('class'=>'calendar-controls'));
806 $content .= $left;
807 $content .= '<span class="hide"> | </span><span class="current">'.userdate($time, get_string('strftimedaydate')).'</span>';
808 $content .= '<span class="hide"> | </span>'. $right;
809 $content .= "<span class=\"clearer\"><!-- --></span>";
810 $content .= html_writer::end_tag('div')."\n";
812 break;
814 return $content;
817 function calendar_filter_controls($type, $vars = NULL, $course = NULL, $courses = NULL) {
818 global $CFG, $SESSION, $USER, $OUTPUT;
820 $groupevents = true;
821 $getvars = '';
823 $id = optional_param( 'id',0,PARAM_INT );
825 switch($type) {
826 case 'event':
827 case 'upcoming':
828 case 'day':
829 case 'month':
830 $getvars = '&amp;from='.$type;
831 break;
832 case 'course':
833 if ($id > 0) {
834 $getvars = '&amp;from=course&amp;id='.$id;
835 } else {
836 $getvars = '&amp;from=course';
838 if (isset($course->groupmode) and $course->groupmode == NOGROUPS and $course->groupmodeforce) {
839 $groupevents = false;
841 break;
844 if (!empty($vars)) {
845 $getvars .= '&amp;'.$vars;
848 $content = '<table>';
850 $content .= '<tr>';
851 if($SESSION->cal_show_global) {
852 $content .= '<td class="eventskey calendar_event_global" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" 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>';
853 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_hideglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
854 } else {
855 $content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" 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>';
856 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showglobal'.$getvars.'" title="'.get_string('tt_showglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n";
858 if($SESSION->cal_show_course) {
859 $content .= '<td class="eventskey calendar_event_course" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" 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>';
860 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
861 } else {
862 $content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" 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>';
863 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showcourses'.$getvars.'" title="'.get_string('tt_showcourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n";
868 if (isloggedin() && !isguestuser()) {
869 $content .= "</tr>\n<tr>";
871 if($groupevents) {
872 // This course MIGHT have group events defined, so show the filter
873 if($SESSION->cal_show_groups) {
874 $content .= '<td class="eventskey calendar_event_group" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" 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>';
875 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_hidegroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
876 } else {
877 $content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" 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>';
878 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showgroups'.$getvars.'" title="'.get_string('tt_showgroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n";
880 } else {
881 // This course CANNOT have group events, so lose the filter
882 $content .= '<td style="width: 11px;"></td><td>&nbsp;</td>'."\n";
884 if($SESSION->cal_show_user) {
885 $content .= '<td class="eventskey calendar_event_user" style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/hide') . '" 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>';
886 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
887 } else {
888 $content .= '<td style="width: 11px;"><img src="'.$OUTPUT->pix_url('t/show') . '" 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>';
889 $content .= '<td><a href="'.CALENDAR_URL.'set.php?var=showuser'.$getvars.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n";
892 $content .= "</tr>\n</table>\n";
894 return $content;
897 function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) {
899 static $shortformat;
900 if(empty($shortformat)) {
901 $shortformat = get_string('strftimedayshort');
904 if($now === false) {
905 $now = time();
908 // To have it in one place, if a change is needed
909 $formal = userdate($tstamp, $shortformat);
911 $datestamp = usergetdate($tstamp);
912 $datenow = usergetdate($now);
914 if($usecommonwords == false) {
915 // We don't want words, just a date
916 return $formal;
918 else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) {
919 // Today
920 return get_string('today', 'calendar');
922 else if(
923 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) ||
924 ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1)
926 // Yesterday
927 return get_string('yesterday', 'calendar');
929 else if(
930 ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) ||
931 ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1)
933 // Tomorrow
934 return get_string('tomorrow', 'calendar');
936 else {
937 return $formal;
941 function calendar_time_representation($time) {
942 static $langtimeformat = NULL;
943 if($langtimeformat === NULL) {
944 $langtimeformat = get_string('strftimetime');
946 $timeformat = get_user_preferences('calendar_timeformat');
947 if(empty($timeformat)){
948 $timeformat = get_config(NULL,'calendar_site_timeformat');
950 // The ? is needed because the preference might be present, but empty
951 return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat);
955 * Adds day, month, year arguments to a URL and returns a moodle_url object.
957 * @param string|moodle_url $linkbase
958 * @param int $d
959 * @param int $m
960 * @param int $y
961 * @return moodle_url
963 function calendar_get_link_href($linkbase, $d, $m, $y) {
964 if (empty($linkbase)) {
965 return '';
967 if (!($linkbase instanceof moodle_url)) {
968 $linkbase = new moodle_url();
970 if (!empty($d)) {
971 $linkbase->param('cal_d', $d);
973 if (!empty($m)) {
974 $linkbase->param('cal_m', $m);
976 if (!empty($y)) {
977 $linkbase->param('cal_y', $y);
979 return $linkbase;
983 * This function has been deprecated as of Moodle 2.0... DO NOT USE!!!!!
985 * @deprecated
986 * @since 2.0
988 * @param string $text
989 * @param string|moodle_url $linkbase
990 * @param int|null $d
991 * @param int|null $m
992 * @param int|null $y
993 * @return string HTML link
995 function calendar_get_link_tag($text, $linkbase, $d, $m, $y) {
996 $url = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y);
997 if (empty($url)) {
998 return $text;
1000 return html_writer::link($url, $text);
1004 * Build and return a previous month HTML link, with an arrow.
1006 * @param string $text The text label.
1007 * @param string|moodle_url $linkbase The URL stub.
1008 * @param int $d $m $y Day of month, month and year numbers.
1009 * @param bool $accesshide Default visible, or hide from all except screenreaders.
1010 * @return string HTML string.
1012 function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide=false) {
1013 $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y);
1014 if (empty($href)) {
1015 return $text;
1017 return link_arrow_left($text, (string)$href, $accesshide, 'previous');
1021 * Build and return a next month HTML link, with an arrow.
1023 * @param string $text The text label.
1024 * @param string|moodle_url $linkbase The URL stub.
1025 * @param int $d $m $y Day of month, month and year numbers.
1026 * @param bool $accesshide Default visible, or hide from all except screenreaders.
1027 * @return string HTML string.
1029 function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide=false) {
1030 $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y);
1031 if (empty($href)) {
1032 return $text;
1034 return link_arrow_right($text, (string)$href, $accesshide, 'next');
1037 function calendar_wday_name($englishname) {
1038 return get_string(strtolower($englishname), 'calendar');
1041 function calendar_days_in_month($month, $year) {
1042 return intval(date('t', mktime(0, 0, 0, $month, 1, $year)));
1045 function calendar_get_block_upcoming($events, $linkhref = NULL) {
1046 $content = '';
1047 $lines = count($events);
1048 if (!$lines) {
1049 return $content;
1052 for ($i = 0; $i < $lines; ++$i) {
1053 if (!isset($events[$i]->time)) { // Just for robustness
1054 continue;
1056 $events[$i] = calendar_add_event_metadata($events[$i]);
1057 $content .= '<div class="event"><span class="icon c0">'.$events[$i]->icon.'</span> ';
1058 if (!empty($events[$i]->referer)) {
1059 // That's an activity event, so let's provide the hyperlink
1060 $content .= $events[$i]->referer;
1061 } else {
1062 if(!empty($linkhref)) {
1063 $ed = usergetdate($events[$i]->timestart);
1064 $href = calendar_get_link_href(new moodle_url(CALENDAR_URL.$linkhref), $ed['mday'], $ed['mon'], $ed['year']);
1065 $href->set_anchor('event_'.$events[$i]->id);
1066 $content .= html_writer::link($href, $events[$i]->name);
1068 else {
1069 $content .= $events[$i]->name;
1072 $events[$i]->time = str_replace('&raquo;', '<br />&raquo;', $events[$i]->time);
1073 $content .= '<div class="date">'.$events[$i]->time.'</div></div>';
1074 if ($i < $lines - 1) $content .= '<hr />';
1077 return $content;
1080 function calendar_add_month($month, $year) {
1081 if($month == 12) {
1082 return array(1, $year + 1);
1084 else {
1085 return array($month + 1, $year);
1089 function calendar_sub_month($month, $year) {
1090 if($month == 1) {
1091 return array(12, $year - 1);
1093 else {
1094 return array($month - 1, $year);
1098 function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) {
1099 $eventsbyday = array();
1100 $typesbyday = array();
1101 $durationbyday = array();
1103 if($events === false) {
1104 return;
1107 foreach($events as $event) {
1109 $startdate = usergetdate($event->timestart);
1110 // Set end date = start date if no duration
1111 if ($event->timeduration) {
1112 $enddate = usergetdate($event->timestart + $event->timeduration - 1);
1113 } else {
1114 $enddate = $startdate;
1117 // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair
1118 if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) {
1119 // Out of bounds
1120 continue;
1123 $eventdaystart = intval($startdate['mday']);
1125 if($startdate['mon'] == $month && $startdate['year'] == $year) {
1126 // Give the event to its day
1127 $eventsbyday[$eventdaystart][] = $event->id;
1129 // Mark the day as having such an event
1130 if($event->courseid == SITEID && $event->groupid == 0) {
1131 $typesbyday[$eventdaystart]['startglobal'] = true;
1132 // Set event class for global event
1133 $events[$event->id]->class = 'calendar_event_global';
1135 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
1136 $typesbyday[$eventdaystart]['startcourse'] = true;
1137 // Set event class for course event
1138 $events[$event->id]->class = 'calendar_event_course';
1140 else if($event->groupid) {
1141 $typesbyday[$eventdaystart]['startgroup'] = true;
1142 // Set event class for group event
1143 $events[$event->id]->class = 'calendar_event_group';
1145 else if($event->userid) {
1146 $typesbyday[$eventdaystart]['startuser'] = true;
1147 // Set event class for user event
1148 $events[$event->id]->class = 'calendar_event_user';
1152 if($event->timeduration == 0) {
1153 // Proceed with the next
1154 continue;
1157 // The event starts on $month $year or before. So...
1158 $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0;
1160 // Also, it ends on $month $year or later...
1161 $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year);
1163 // Mark all days between $lowerbound and $upperbound (inclusive) as duration
1164 for($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
1165 $durationbyday[$i][] = $event->id;
1166 if($event->courseid == SITEID && $event->groupid == 0) {
1167 $typesbyday[$i]['durationglobal'] = true;
1169 else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
1170 $typesbyday[$i]['durationcourse'] = true;
1172 else if($event->groupid) {
1173 $typesbyday[$i]['durationgroup'] = true;
1175 else if($event->userid) {
1176 $typesbyday[$i]['durationuser'] = true;
1181 return;
1184 function calendar_get_module_cached(&$coursecache, $modulename, $instance) {
1185 $module = get_coursemodule_from_instance($modulename, $instance);
1187 if($module === false) return false;
1188 if(!calendar_get_course_cached($coursecache, $module->course)) {
1189 return false;
1191 return $module;
1194 function calendar_get_course_cached(&$coursecache, $courseid) {
1195 global $COURSE, $DB;
1197 if (!isset($coursecache[$courseid])) {
1198 if ($courseid == $COURSE->id) {
1199 $coursecache[$courseid] = $COURSE;
1200 } else {
1201 $coursecache[$courseid] = $DB->get_record('course', array('id'=>$courseid));
1204 return $coursecache[$courseid];
1207 function calendar_session_vars($course=null) {
1208 global $SESSION, $USER;
1210 if(!isset($SESSION->cal_course_referer)) {
1211 $SESSION->cal_course_referer = 0;
1213 if(!isset($SESSION->cal_show_global)) {
1214 $SESSION->cal_show_global = true;
1216 if(!isset($SESSION->cal_show_groups)) {
1217 $SESSION->cal_show_groups = true;
1219 if(!isset($SESSION->cal_show_course)) {
1220 $SESSION->cal_show_course = true;
1222 if(!isset($SESSION->cal_show_user)) {
1223 $SESSION->cal_show_user = true;
1225 if ($course !== null) {
1226 // speedup hack for calendar related blocks
1227 if(isset($course->coursenode)) {
1228 // coursenode has been set up, which seems to break things further down the line.
1229 // Use a clone of $course with coursenode removed.
1230 $course = clone $course;
1231 unset($course->coursenode);
1233 $SESSION->cal_courses_shown = array($course->id => $course);
1234 } else {
1235 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
1237 if(empty($SESSION->cal_users_shown)) {
1238 // The empty() instead of !isset() here makes a whole world of difference,
1239 // as it will automatically change to the user's id when the user first logs
1240 // in. With !isset(), it would never do that.
1241 $SESSION->cal_users_shown = isloggedin() ? $USER->id : false;
1242 } else if(is_numeric($SESSION->cal_users_shown) && isloggedin() && $SESSION->cal_users_shown != $USER->id) {
1243 // Follow the white rabbit, for example if a teacher logs in as a student
1244 $SESSION->cal_users_shown = $USER->id;
1248 function calendar_set_referring_course($courseid) {
1249 global $SESSION;
1250 $SESSION->cal_course_referer = intval($courseid);
1253 function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) {
1254 global $SESSION, $USER, $CFG, $DB;
1256 // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause
1257 // the code to function incorrectly UNLESS we convert it to an integer. One case where
1258 // PHP's loose type system works against us.
1259 if(is_string($SESSION->cal_courses_shown)) {
1260 $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown);
1262 if($courseeventsfrom === NULL) {
1263 $courseeventsfrom = $SESSION->cal_courses_shown;
1266 // MDL-9059, $courseeventsfrom can be an int, or an array of ints, or an array of course objects
1267 // convert all to array of objects
1268 // we probably should do some clean up and make sure that session is set to use the proper form
1269 if (is_int($courseeventsfrom)) { // case of an int, e.g. calendar view page
1270 $c = array();
1271 $c[$courseeventsfrom] = $DB->get_record('course', array('id'=>$courseeventsfrom));
1272 $courseeventsfrom = $c;
1273 } else if (is_array($courseeventsfrom)) { // case of an array of ints, e.g. course home page
1274 foreach ($courseeventsfrom as $i=>$courseid) { // TODO: this seems wrong, the array is often constructed as [courseid] => 1 ???
1275 if (is_int($courseid)) {
1276 $courseeventsfrom[$i] = $DB->get_record('course', array('id'=>$courseid));
1281 if($groupeventsfrom === NULL) {
1282 $groupeventsfrom = $SESSION->cal_courses_shown;
1285 if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) {
1286 if(is_int($courseeventsfrom)) {
1287 $courses = array(SITEID, $courseeventsfrom);
1289 else if(is_array($courseeventsfrom)) {
1290 $courses = array_keys($courseeventsfrom);
1291 $courses[] = SITEID;
1294 else if($SESSION->cal_show_course) {
1295 if(is_int($courseeventsfrom)) {
1296 $courses = array($courseeventsfrom);
1298 else if(is_array($courseeventsfrom)) {
1299 $courses = array_keys($courseeventsfrom);
1301 $courses = array_diff($courses, array(SITEID));
1303 else if($SESSION->cal_show_global) {
1304 $courses = array(SITEID);
1306 else {
1307 $courses = false;
1309 //BUG 6130 clean $courses array as SESSION has bad entries.
1310 // [pj] TODO: See if this has to do with my new change in get_default_courses and can be taken out
1311 if (is_array($courses)) {
1312 foreach ($courses as $index => $value) {
1313 if (empty($value)) unset($courses[$index]);
1316 // Sort courses for consistent colour highlighting
1317 // Effectively ignoring SITEID as setting as last course id
1318 $key = array_search(SITEID, $courses);
1319 if ($key !== false) {
1320 unset($courses[$key]);
1321 sort($courses);
1322 $courses[] = SITEID;
1323 } else {
1324 sort($courses);
1328 if($SESSION->cal_show_user || $ignorefilters) {
1329 // This doesn't work for arrays yet (maybe someday it will)
1330 $user = $SESSION->cal_users_shown;
1332 else {
1333 $user = false;
1335 if($SESSION->cal_show_groups || $ignorefilters) {
1336 if(is_int($groupeventsfrom)) {
1337 $groupcourses = array($groupeventsfrom);
1339 else if(is_array($groupeventsfrom)) {
1340 $groupcourses = array_keys($groupeventsfrom);
1343 // XXX TODO: not sure how to replace $CFG->calendar_adminseesall
1344 if(has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) {
1345 $group = true;
1347 else {
1348 $grouparray = array();
1350 // We already have the courses to examine in $courses
1351 // For each course...
1353 foreach($groupcourses as $courseid) {
1355 if (!isset($courseeventsfrom[$courseid]->context)) { // SHOULD be set MDL-11221
1356 if (is_object($courseeventsfrom[$courseid])) {
1357 $courseeventsfrom[$courseid]->context = get_context_instance(CONTEXT_COURSE, $courseid);
1361 // If the user is an editing teacher in there,
1362 if (isloggedin() && isset($courseeventsfrom[$courseid]->context) && has_capability('moodle/calendar:manageentries', $courseeventsfrom[$courseid]->context)) {
1363 // If this course has groups, show events from all of them
1364 if(is_int($groupeventsfrom)) {
1365 if (is_object($courseeventsfrom[$courseid])) { // SHOULD be set MDL-11221
1366 $courserecord = $courseeventsfrom[$courseid];
1367 } else {
1368 $courserecord = $DB->get_record('course', array('id'=>$courseid));
1370 $courserecord = $DB->get_record('course', array('id'=>$courseid));
1371 if ($courserecord->groupmode != NOGROUPS || !$courserecord->groupmodeforce) {
1372 $groupids[] = $courseid;
1375 else if(isset($SESSION->cal_courses_shown[$courseid]) && ($SESSION->cal_courses_shown[$courseid]->groupmode != NOGROUPS || !$SESSION->cal_courses_shown[$courseid]->groupmodeforce)) {
1376 $groupids[] = $courseid;
1380 // Otherwise (not editing teacher) show events from the group he is a member of
1381 else if(isset($USER->groupmember[$courseid])) {
1382 //changed to 2D array
1383 foreach ($USER->groupmember[$courseid] as $groupid){
1384 $grouparray[] = $groupid;
1389 if (!empty($groupids)) {
1390 $sql = "SELECT *
1391 FROM {groups}
1392 WHERE courseid IN (".implode(',', $groupids).')';
1394 if ($grouprecords = $DB->get_records_sql($sql, null)) {
1395 foreach ($grouprecords as $grouprecord) {
1396 $grouparray[] = $grouprecord->id;
1401 if(empty($grouparray)) {
1402 $group = false;
1404 else {
1405 $group = $grouparray;
1410 else {
1411 $group = false;
1415 function calendar_edit_event_allowed($event) {
1416 global $USER, $DB;
1418 // Must be logged in
1419 if (!isloggedin()) {
1420 return false;
1423 // can not be using guest account
1424 if (isguestuser()) {
1425 return false;
1428 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
1429 // if user has manageentries at site level, return true
1430 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
1431 return true;
1434 // if groupid is set, it's definitely a group event
1435 if (!empty($event->groupid)) {
1436 // Allow users to add/edit group events if:
1437 // 1) They have manageentries (= entries for whole course)
1438 // 2) They have managegroupentries AND are in the group
1439 $group = $DB->get_record('groups', array('id'=>$event->groupid));
1440 return $group && (
1441 has_capability('moodle/calendar:manageentries', $event->context) ||
1442 (has_capability('moodle/calendar:managegroupentries', $event->context)
1443 && groups_is_member($event->groupid)));
1444 } else if (!empty($event->courseid)) {
1445 // if groupid is not set, but course is set,
1446 // it's definiely a course event
1447 return has_capability('moodle/calendar:manageentries', $event->context);
1448 } else if (!empty($event->userid) && $event->userid == $USER->id) {
1449 // if course is not set, but userid id set, it's a user event
1450 return (has_capability('moodle/calendar:manageownentries', $event->context));
1451 } else if (!empty($event->userid)) {
1452 return (has_capability('moodle/calendar:manageentries', $event->context));
1454 return false;
1457 function calendar_get_default_courses($ignoreref = false) {
1458 global $USER, $CFG, $SESSION, $DB;
1460 if(!empty($SESSION->cal_course_referer) && !$ignoreref) {
1461 return array($SESSION->cal_course_referer => 1);
1464 if (!isloggedin()) {
1465 return array();
1468 $courses = array();
1469 if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
1470 if (!empty($CFG->calendar_adminseesall)) {
1471 $courses = $DB->get_records_sql('SELECT id, 1 FROM {course}');
1472 return $courses;
1476 $courses = enrol_get_my_courses();
1478 return $courses;
1481 function calendar_preferences_button() {
1482 global $CFG, $USER;
1484 // Guests have no preferences
1485 if (!isloggedin() || isguestuser()) {
1486 return '';
1489 return "<form method=\"get\" ".
1490 " action=\"$CFG->wwwroot/calendar/preferences.php\">".
1491 "<div><input type=\"submit\" value=\"".get_string("preferences", "calendar")." ...\" /></div></form>";
1494 function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime=0) {
1495 $startdate = usergetdate($event->timestart);
1496 $enddate = usergetdate($event->timestart + $event->timeduration);
1497 $usermidnightstart = usergetmidnight($event->timestart);
1499 if($event->timeduration) {
1500 // To avoid doing the math if one IF is enough :)
1501 $usermidnightend = usergetmidnight($event->timestart + $event->timeduration);
1503 else {
1504 $usermidnightend = $usermidnightstart;
1507 if (empty($linkparams) || !is_array($linkparams)) {
1508 $linkparams = array();
1510 $linkparams['view'] = 'day';
1512 // OK, now to get a meaningful display...
1513 // First of all we have to construct a human-readable date/time representation
1515 if($event->timeduration) {
1516 // It has a duration
1517 if($usermidnightstart == $usermidnightend ||
1518 ($event->timestart == $usermidnightstart) && ($event->timeduration == 86400 || $event->timeduration == 86399) ||
1519 ($event->timestart + $event->timeduration <= $usermidnightstart + 86400)) {
1520 // But it's all on the same day
1521 $timestart = calendar_time_representation($event->timestart);
1522 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
1523 $time = $timestart.' <strong>&raquo;</strong> '.$timeend;
1525 if ($event->timestart == $usermidnightstart && ($event->timeduration == 86400 || $event->timeduration == 86399)) {
1526 $time = get_string('allday', 'calendar');
1529 // Set printable representation
1530 if (!$showtime) {
1531 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1532 $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']);
1533 $eventtime = html_writer::link($url, $day).', '.$time;
1534 } else {
1535 $eventtime = $time;
1537 } else {
1538 // It spans two or more days
1539 $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords).', ';
1540 if ($showtime == $usermidnightstart) {
1541 $daystart = '';
1543 $timestart = calendar_time_representation($event->timestart);
1544 $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords).', ';
1545 if ($showtime == $usermidnightend) {
1546 $dayend = '';
1548 $timeend = calendar_time_representation($event->timestart + $event->timeduration);
1550 // Set printable representation
1551 if ($now >= $usermidnightstart && $now < ($usermidnightstart + 86400)) {
1552 $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']);
1553 $eventtime = $timestart.' <strong>&raquo;</strong> '.html_writer::link($url, $dayend).$timeend;
1554 } else {
1555 $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']);
1556 $eventtime = html_writer::link($url, $daystart).$timestart.' <strong>&raquo;</strong> ';
1558 $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $startdate['mday'], $startdate['mon'], $startdate['year']);
1559 $eventtime .= html_writer::link($url, $dayend).$timeend;
1562 } else {
1563 $time = ' ';
1565 // Set printable representation
1566 if (!$showtime) {
1567 $day = calendar_day_representation($event->timestart, $now, $usecommonwords);
1568 $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $startdate['mday'], $startdate['mon'], $startdate['year']);
1569 $eventtime = html_writer::link($url, $day).trim($time);
1570 } else {
1571 $eventtime = $time;
1575 if($event->timestart + $event->timeduration < $now) {
1576 // It has expired
1577 $eventtime = '<span class="dimmed_text">'.str_replace(' href=', ' class="dimmed" href=', $eventtime).'</span>';
1580 return $eventtime;
1583 function calendar_print_month_selector($name, $selected) {
1584 $months = array();
1585 for ($i=1; $i<=12; $i++) {
1586 $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B');
1588 echo html_writer::select($months, $name, $selected, false);
1591 function calendar_get_filters_status() {
1592 global $SESSION;
1594 $status = 0;
1595 if($SESSION->cal_show_global) {
1596 $status += 1;
1598 if($SESSION->cal_show_course) {
1599 $status += 2;
1601 if($SESSION->cal_show_groups) {
1602 $status += 4;
1604 if($SESSION->cal_show_user) {
1605 $status += 8;
1607 return $status;
1610 function calendar_set_filters_status($packed_bitfield) {
1611 global $SESSION, $USER;
1613 if (!isloggedin()) {
1614 return false;
1617 $SESSION->cal_show_global = ($packed_bitfield & 1);
1618 $SESSION->cal_show_course = ($packed_bitfield & 2);
1619 $SESSION->cal_show_groups = ($packed_bitfield & 4);
1620 $SESSION->cal_show_user = ($packed_bitfield & 8);
1622 return true;
1625 function calendar_get_allowed_types(&$allowed) {
1626 global $USER, $CFG, $SESSION, $DB;
1627 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
1628 $allowed->user = has_capability('moodle/calendar:manageownentries', $sitecontext);
1629 $allowed->groups = false; // This may change just below
1630 $allowed->courses = false; // This may change just below
1631 $allowed->site = has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, SITEID));
1633 if(!empty($SESSION->cal_course_referer) && $SESSION->cal_course_referer != SITEID) {
1634 $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer));
1635 $coursecontext = get_context_instance(CONTEXT_COURSE, $SESSION->cal_course_referer);
1637 if(has_capability('moodle/calendar:manageentries', $coursecontext)) {
1638 $allowed->courses = array($course->id => 1);
1640 if($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
1641 $allowed->groups = groups_get_all_groups($SESSION->cal_course_referer);
1643 } else if(has_capability('moodle/calendar:managegroupentries', $coursecontext)) {
1644 if($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
1645 $allowed->groups = groups_get_all_groups($SESSION->cal_course_referer, $USER->id);
1652 * see if user can add calendar entries at all
1653 * used to print the "New Event" button
1654 * @return bool
1656 function calendar_user_can_add_event() {
1657 calendar_get_allowed_types($allowed);
1658 return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->site);
1662 * Check wether the current user is permitted to add events
1664 * @param object $event
1665 * @return bool
1667 function calendar_add_event_allowed($event) {
1668 global $USER, $DB;
1670 // can not be using guest account
1671 if (!isloggedin() or isguestuser()) {
1672 return false;
1675 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
1676 // if user has manageentries at site level, always return true
1677 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
1678 return true;
1681 switch ($event->eventtype) {
1682 case 'course':
1683 return has_capability('moodle/calendar:manageentries', $event->context);
1685 case 'group':
1686 // Allow users to add/edit group events if:
1687 // 1) They have manageentries (= entries for whole course)
1688 // 2) They have managegroupentries AND are in the group
1689 $group = $DB->get_record('groups', array('id'=>$event->groupid));
1690 return $group && (
1691 has_capability('moodle/calendar:manageentries', $event->context) ||
1692 (has_capability('moodle/calendar:managegroupentries', $event->context)
1693 && groups_is_member($event->groupid)));
1695 case 'user':
1696 if ($event->userid == $USER->id) {
1697 return (has_capability('moodle/calendar:manageownentries', $event->context));
1699 //there is no 'break;' intentionally
1701 case 'site':
1702 return has_capability('moodle/calendar:manageentries', $event->context);
1704 default:
1705 return has_capability('moodle/calendar:manageentries', $event->context);
1710 * A class to manage calendar events
1712 * This class provides the required functionality in order to manage calendar events.
1713 * It was introduced as part of Moodle 2.0 and was created in order to provide a
1714 * better framework for dealing with calendar events in particular regard to file
1715 * handling through the new file API
1717 * @property int $id The id within the event table
1718 * @property string $name The name of the event
1719 * @property string $description The description of the event
1720 * @property int $format The format of the description FORMAT_?
1721 * @property int $courseid The course the event is associated with (0 if none)
1722 * @property int $groupid The group the event is associated with (0 if none)
1723 * @property int $userid The user the event is associated with (0 if none)
1724 * @property int $repeatid If this is a repeated event this will be set to the
1725 * id of the original
1726 * @property string $modulename If added by a module this will be the module name
1727 * @property int $instance If added by a module this will be the module instance
1728 * @property string $eventtype The event type
1729 * @property int $timestart The start time as a timestamp
1730 * @property int $timeduration The duration of the event in seconds
1731 * @property int $visible 1 if the event is visible
1732 * @property int $uuid ?
1733 * @property int $sequence ?
1734 * @property int $timemodified The time last modified as a timestamp
1736 class calendar_event {
1739 * An object containing the event properties can be accessed via the
1740 * magic __get/set methods
1741 * @var array
1743 protected $properties = null;
1745 * The converted event discription with file paths resolved
1746 * This gets populated when someone requests description for the first time
1747 * @var string
1749 protected $_description = null;
1751 * The options to use with this description editor
1752 * @var array
1754 protected $editoroptions = array(
1755 'subdirs'=>false,
1756 'forcehttps'=>false,
1757 'maxfiles'=>-1,
1758 'maxbytes'=>null,
1759 'trusttext'=>false);
1761 * The context to use with the description editor
1762 * @var object
1764 protected $editorcontext = null;
1767 * Instantiates a new event and optionally populates its properties with the
1768 * data provided
1770 * @param stdClass $data Optional. An object containing the properties to for
1771 * an event
1773 public function __construct($data=null) {
1774 global $CFG, $USER;
1776 // First convert to object if it is not already (should either be object or assoc array)
1777 if (!is_object($data)) {
1778 $data = (object)$data;
1781 $this->editoroptions['maxbytes'] = $CFG->maxbytes;
1783 $data->eventrepeats = 0;
1785 if (empty($data->id)) {
1786 $data->id = null;
1789 // Default to a user event
1790 if (empty($data->eventtype)) {
1791 $data->eventtype = 'user';
1794 // Default to the current user
1795 if (empty($data->userid)) {
1796 $data->userid = $USER->id;
1799 if (!empty($data->timeduration) && is_array($data->timeduration)) {
1800 $data->timeduration = make_timestamp($data->timeduration['year'], $data->timeduration['month'], $data->timeduration['day'], $data->timeduration['hour'], $data->timeduration['minute']) - $data->timestart;
1802 if (!empty($data->description) && is_array($data->description)) {
1803 $data->format = $data->description['format'];
1804 $data->description = $data->description['text'];
1805 } else if (empty($data->description)) {
1806 $data->description = '';
1807 $data->format = editors_get_preferred_format();
1809 // Ensure form is defaulted correctly
1810 if (empty($data->format)) {
1811 $data->format = editors_get_preferred_format();
1814 if (empty($data->context)) {
1815 $data->context = $this->calculate_context($data);
1817 $this->properties = $data;
1821 * Magic property method
1823 * Attempts to call a set_$key method if one exists otherwise falls back
1824 * to simply set the property
1826 * @param string $key
1827 * @param mixed $value
1829 public function __set($key, $value) {
1830 if (method_exists($this, 'set_'.$key)) {
1831 $this->{'set_'.$key}($value);
1833 $this->properties->{$key} = $value;
1837 * Magic get method
1839 * Attempts to call a get_$key method to return the property and ralls over
1840 * to return the raw property
1842 * @param str $key
1843 * @return mixed
1845 public function __get($key) {
1846 if (method_exists($this, 'get_'.$key)) {
1847 return $this->{'get_'.$key}();
1849 if (!isset($this->properties->{$key})) {
1850 throw new coding_exception('Undefined property requested');
1852 return $this->properties->{$key};
1856 * Stupid PHP needs an isset magic method if you use the get magic method and
1857 * still want empty calls to work.... blah ~!
1859 * @param string $key
1860 * @return bool
1862 public function __isset($key) {
1863 return !empty($this->properties->{$key});
1867 * Calculate the context value needed for calendar_event.
1868 * Event's type can be determine by the available value store in $data
1869 * It is important to check for the existence of course/courseid to determine
1870 * the course event.
1871 * Default value is set to CONTEXT_USER
1873 * @return stdClass
1875 protected function calculate_context(stdClass $data) {
1876 global $USER;
1878 $context = null;
1879 if (isset($data->courseid) && $data->courseid > 0) {
1880 $context = get_context_instance(CONTEXT_COURSE, $data->courseid);
1881 } else if (isset($data->course) && $data->course > 0) {
1882 $context = get_context_instance(CONTEXT_COURSE, $data->course);
1883 } else if (isset($data->groupid) && $data->groupid > 0) {
1884 $group = $DB->get_record('groups', array('id'=>$data->groupid));
1885 $context = get_context_instance(CONTEXT_COURSE, $group->courseid);
1886 } else if (isset($data->userid) && $data->userid > 0 && $data->userid == $USER->id) {
1887 $context = get_context_instance(CONTEXT_USER);
1888 } else if (isset($data->userid) && $data->userid > 0 && $data->userid != $USER->id &&
1889 isset($data->instance) && $data->instance > 0) {
1890 $cm = get_coursemodule_from_id('', $data->instance, 0, false, MUST_EXIST);
1891 $context = get_context_instance(CONTEXT_COURSE, $cm->course);
1892 } else {
1893 $context = get_context_instance(CONTEXT_USER);
1896 return $context;
1900 * Returns an array of editoroptions for this event: Called by __get
1901 * Please use $blah = $event->editoroptions;
1902 * @return array
1904 protected function get_editoroptions() {
1905 return $this->editoroptions;
1909 * Returns an event description: Called by __get
1910 * Please use $blah = $event->description;
1912 * @return string
1914 protected function get_description() {
1915 global $CFG;
1917 require_once($CFG->libdir . '/filelib.php');
1919 if ($this->_description === null) {
1920 // Check if we have already resolved the context for this event
1921 if ($this->editorcontext === null) {
1922 // Switch on the event type to decide upon the appropriate context
1923 // to use for this event
1924 $this->editorcontext = $this->properties->context;
1925 if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course'
1926 && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') {
1927 return clean_text($this->properties->description, $this->properties->format);
1931 // Work out the item id for the editor, if this is a repeated event then the files will
1932 // be associated with the original
1933 if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
1934 $itemid = $this->properties->repeatid;
1935 } else {
1936 $itemid = $this->properties->id;
1939 // Convert file paths in the description so that things display correctly
1940 $this->_description = file_rewrite_pluginfile_urls($this->properties->description, 'pluginfile.php', $this->editorcontext->id, 'calendar', 'event_description', $itemid);
1941 // Clean the text so no nasties get through
1942 $this->_description = clean_text($this->_description, $this->properties->format);
1944 // Finally return the description
1945 return $this->_description;
1949 * Return the number of repeat events there are in this events series
1951 * @return int
1953 public function count_repeats() {
1954 global $DB;
1955 if (!empty($this->properties->repeatid)) {
1956 $this->properties->eventrepeats = $DB->count_records('event', array('repeatid'=>$this->properties->repeatid));
1957 // We don't want to count ourselves
1958 $this->properties->eventrepeats--;
1960 return $this->properties->eventrepeats;
1964 * Update or create an event within the database
1966 * Pass in a object containing the event properties and this function will
1967 * insert it into the database and deal with any associated files
1969 * @see add_event()
1970 * @see update_event()
1972 * @param stdClass $data
1973 * @param boolean $checkcapability if moodle should check calendar managing capability or not
1975 public function update($data, $checkcapability=true) {
1976 global $CFG, $DB, $USER;
1978 foreach ($data as $key=>$value) {
1979 $this->properties->$key = $value;
1982 $this->properties->timemodified = time();
1983 $usingeditor = (!empty($this->properties->description) && is_array($this->properties->description));
1985 if (empty($this->properties->id) || $this->properties->id < 1) {
1987 if ($checkcapability) {
1988 if (!calendar_add_event_allowed($this->properties)) {
1989 print_error('nopermissiontoupdatecalendar');
1993 if ($usingeditor) {
1994 switch ($this->properties->eventtype) {
1995 case 'user':
1996 $this->editorcontext = $this->properties->context;
1997 $this->properties->courseid = 0;
1998 $this->properties->groupid = 0;
1999 $this->properties->userid = $USER->id;
2000 break;
2001 case 'site':
2002 $this->editorcontext = $this->properties->context;
2003 $this->properties->courseid = SITEID;
2004 $this->properties->groupid = 0;
2005 $this->properties->userid = $USER->id;
2006 break;
2007 case 'course':
2008 $this->editorcontext = $this->properties->context;
2009 $this->properties->groupid = 0;
2010 $this->properties->userid = $USER->id;
2011 break;
2012 case 'group':
2013 $this->editorcontext = $this->properties->context;
2014 $this->properties->userid = $USER->id;
2015 break;
2016 default:
2017 // Ewww we should NEVER get here, but just incase we do lets
2018 // fail gracefully
2019 $usingeditor = false;
2020 break;
2023 $editor = $this->properties->description;
2024 $this->properties->format = $this->properties->description['format'];
2025 $this->properties->description = $this->properties->description['text'];
2028 // Insert the event into the database
2029 $this->properties->id = $DB->insert_record('event', $this->properties);
2031 if ($usingeditor) {
2032 $this->properties->description = file_save_draft_area_files(
2033 $editor['itemid'],
2034 $this->editorcontext->id,
2035 'calendar',
2036 'event_description',
2037 $this->properties->id,
2038 $this->editoroptions,
2039 $editor['text'],
2040 $this->editoroptions['forcehttps']);
2042 $DB->set_field('event', 'description', $this->properties->description, array('id'=>$this->properties->id));
2045 // Log the event entry.
2046 add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);
2048 $repeatedids = array();
2050 if (!empty($this->properties->repeat)) {
2051 $this->properties->repeatid = $this->properties->id;
2052 $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id'=>$this->properties->id));
2054 $eventcopy = clone($this->properties);
2055 unset($eventcopy->id);
2057 for($i = 1; $i < $eventcopy->repeats; $i++) {
2059 $eventcopy->timestart = ($eventcopy->timestart+WEEKSECS) + dst_offset_on($eventcopy->timestart) - dst_offset_on($eventcopy->timestart+WEEKSECS);
2061 // Get the event id for the log record.
2062 $eventcopyid = $DB->insert_record('event', $eventcopy);
2064 // If the context has been set delete all associated files
2065 if ($usingeditor) {
2066 $fs = get_file_storage();
2067 $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
2068 foreach ($files as $file) {
2069 $fs->create_file_from_storedfile(array('itemid'=>$eventcopyid), $file);
2073 $repeatedids[] = $eventcopyid;
2074 // Log the event entry.
2075 add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventcopyid, $eventcopy->name);
2079 // Hook for tracking added events
2080 self::calendar_event_hook('add_event', array($this->properties, $repeatedids));
2081 return true;
2082 } else {
2084 if ($checkcapability) {
2085 if(!calendar_edit_event_allowed($this->properties)) {
2086 print_error('nopermissiontoupdatecalendar');
2090 if ($usingeditor) {
2091 if ($this->editorcontext !== null) {
2092 $this->properties->description = file_save_draft_area_files(
2093 $this->properties->description['itemid'],
2094 $this->editorcontext->id,
2095 'calendar',
2096 'event_description',
2097 $this->properties->id,
2098 $this->editoroptions,
2099 $this->properties->description['text'],
2100 $this->editoroptions['forcehttps']);
2101 } else {
2102 $this->properties->format = $this->properties->description['format'];
2103 $this->properties->description = $this->properties->description['text'];
2107 $event = $DB->get_record('event', array('id'=>$this->properties->id));
2109 $updaterepeated = (!empty($this->properties->repeatid) && !empty($this->properties->repeateditall));
2111 if ($updaterepeated) {
2112 // Update all
2113 if ($this->properties->timestart != $event->timestart) {
2114 $timestartoffset = $this->properties->timestart - $event->timestart;
2115 $sql = "UPDATE {event}
2116 SET name = ?,
2117 description = ?,
2118 timestart = timestart + ?,
2119 timeduration = ?,
2120 timemodified = ?
2121 WHERE repeatid = ?";
2122 $params = array($this->properties->name, $this->properties->description, $timestartoffset, $this->properties->timeduration, time(), $event->repeatid);
2123 } else {
2124 $sql = "UPDATE {event} SET name = ?, description = ?, timeduration = ?, timemodified = ? WHERE repeatid = ?";
2125 $params = array($this->properties->name, $this->properties->description, $this->properties->timeduration, time(), $event->repeatid);
2127 $DB->execute($sql, $params);
2129 // Log the event update.
2130 add_to_log($this->properties->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);
2131 } else {
2132 $DB->update_record('event', $this->properties);
2133 $event = calendar_event::load($this->properties->id);
2134 $this->properties = $event->properties();
2135 add_to_log($this->properties->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id='.$this->properties->id, $this->properties->name);
2138 // Hook for tracking event updates
2139 self::calendar_event_hook('update_event', array($this->properties, $updaterepeated));
2140 return true;
2145 * Deletes an event and if selected an repeated events in the same series
2147 * This function deletes an event, any associated events if $deleterepeated=true,
2148 * and cleans up any files associated with the events.
2150 * @see delete_event()
2152 * @param bool $deleterepeated
2153 * @return bool
2155 public function delete($deleterepeated=false) {
2156 global $DB;
2158 // If $this->properties->id is not set then something is wrong
2159 if (empty($this->properties->id)) {
2160 debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER);
2161 return false;
2164 // Delete the event
2165 $DB->delete_records('event', array('id'=>$this->properties->id));
2167 // If the editor context hasn't already been set then set it now
2168 if ($this->editorcontext === null) {
2169 $this->editorcontext = $this->properties->context;
2172 // If the context has been set delete all associated files
2173 if ($this->editorcontext !== null) {
2174 $fs = get_file_storage();
2175 $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id);
2176 foreach ($files as $file) {
2177 $file->delete();
2181 // Fire the event deleted hook
2182 self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated));
2184 // If we need to delete repeated events then we will fetch them all and delete one by one
2185 if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
2186 // Get all records where the repeatid is the same as the event being removed
2187 $events = $DB->get_records('event', array('repeatid'=>$this->properties->repeatid));
2188 // For each of the returned events populate a calendar_event object and call delete
2189 // make sure the arg passed is false as we are already deleting all repeats
2190 foreach ($events as $event) {
2191 $event = new calendar_event($event);
2192 $event->delete(false);
2196 return true;
2200 * Fetch all event properties
2202 * This function returns all of the events properties as an object and optionally
2203 * can prepare an editor for the description field at the same time. This is
2204 * designed to work when the properties are going to be used to set the default
2205 * values of a moodle forms form.
2207 * @param bool $prepareeditor If set to true a editor is prepared for use with
2208 * the mforms editor element. (for description)
2209 * @return stdClass Object containing event properties
2211 public function properties($prepareeditor=false) {
2212 global $USER, $CFG, $DB;
2214 // First take a copy of the properties. We don't want to actually change the
2215 // properties or we'd forever be converting back and forwards between an
2216 // editor formatted description and not
2217 $properties = clone($this->properties);
2218 // Clean the description here
2219 $properties->description = clean_text($properties->description, $properties->format);
2221 // If set to true we need to prepare the properties for use with an editor
2222 // and prepare the file area
2223 if ($prepareeditor) {
2225 // We may or may not have a property id. If we do then we need to work
2226 // out the context so we can copy the existing files to the draft area
2227 if (!empty($properties->id)) {
2229 if ($properties->eventtype === 'site') {
2230 // Site context
2231 $this->editorcontext = $this->properties->context;
2232 } else if ($properties->eventtype === 'user') {
2233 // User context
2234 $this->editorcontext = $this->properties->context;
2235 } else if ($properties->eventtype === 'group' || $properties->eventtype === 'course') {
2236 // First check the course is valid
2237 $course = $DB->get_record('course', array('id'=>$properties->courseid));
2238 if (!$course) {
2239 print_error('invalidcourse');
2241 // Course context
2242 $this->editorcontext = $this->properties->context;
2243 // We have a course and are within the course context so we had
2244 // better use the courses max bytes value
2245 $this->editoroptions['maxbytes'] = $course->maxbytes;
2246 } else {
2247 // If we get here we have a custom event type as used by some
2248 // modules. In this case the event will have been added by
2249 // code and we won't need the editor
2250 $this->editoroptions['maxbytes'] = 0;
2251 $this->editoroptions['maxfiles'] = 0;
2254 if (empty($this->editorcontext) || empty($this->editorcontext->id)) {
2255 $contextid = false;
2256 } else {
2257 // Get the context id that is what we really want
2258 $contextid = $this->editorcontext->id;
2260 } else {
2262 // If we get here then this is a new event in which case we don't need a
2263 // context as there is no existing files to copy to the draft area.
2264 $contextid = null;
2267 // If the contextid === false we don't support files so no preparing
2268 // a draft area
2269 if ($contextid !== false) {
2270 // Just encase it has already been submitted
2271 $draftiddescription = file_get_submitted_draft_itemid('description');
2272 // Prepare the draft area, this copies existing files to the draft area as well
2273 $properties->description = file_prepare_draft_area($draftiddescription, $contextid, 'calendar', 'event_description', $properties->id, $this->editoroptions, $properties->description);
2274 } else {
2275 $draftiddescription = 0;
2278 // Structure the description field as the editor requires
2279 $properties->description = array('text'=>$properties->description, 'format'=>$properties->format, 'itemid'=>$draftiddescription);
2282 // Finally return the properties
2283 return $properties;
2287 * Toggles the visibility of an event
2289 * @param null|bool $force If it is left null the events visibility is flipped,
2290 * If it is false the event is made hidden, if it is true it
2291 * is made visible.
2293 public function toggle_visibility($force=null) {
2294 global $CFG, $DB;
2296 // Set visible to the default if it is not already set
2297 if (empty($this->properties->visible)) {
2298 $this->properties->visible = 1;
2301 if ($force === true || ($force !== false && $this->properties->visible == 0)) {
2302 // Make this event visible
2303 $this->properties->visible = 1;
2304 // Fire the hook
2305 self::calendar_event_hook('show_event', array($this->properties));
2306 } else {
2307 // Make this event hidden
2308 $this->properties->visible = 0;
2309 // Fire the hook
2310 self::calendar_event_hook('hide_event', array($this->properties));
2313 // Update the database to reflect this change
2314 return $DB->set_field('event', 'visible', $this->properties->visible, array('id'=>$this->properties->id));
2318 * Attempts to call the hook for the specified action should a calendar type
2319 * by set $CFG->calendar, and the appopriate function defined
2321 * @static
2322 * @staticvar bool $extcalendarinc Used to track the inclusion of the calendar lib
2323 * @param string $action One of `update_event`, `add_event`, `delete_event`, `show_event`, `hide_event`
2324 * @param array $args The args to pass to the hook, usually the event is the first element
2325 * @return bool
2327 public static function calendar_event_hook($action, array $args) {
2328 global $CFG;
2329 static $extcalendarinc;
2330 if ($extcalendarinc === null) {
2331 if (!empty($CFG->calendar) && file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) {
2332 include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php');
2333 $extcalendarinc = true;
2334 } else {
2335 $extcalendarinc = false;
2338 if($extcalendarinc === false) {
2339 return false;
2341 $hook = $CFG->calendar .'_'.$action;
2342 if (function_exists($hook)) {
2343 call_user_func_array($hook, $args);
2344 return true;
2346 return false;
2350 * Returns a calendar_event object when provided with an event id
2352 * This function makes use of MUST_EXIST, if the event id passed in is invalid
2353 * it will result in an exception being thrown
2355 * @param int|object $param
2356 * @return calendar_event|false
2358 public static function load($param) {
2359 global $DB;
2360 if (is_object($param)) {
2361 $event = new calendar_event($param);
2362 } else {
2363 $event = $DB->get_record('event', array('id'=>(int)$param), '*', MUST_EXIST);
2364 $event = new calendar_event($event);
2366 return $event;
2370 * Creates a new event and returns a calendar_event object
2372 * @param object|array $properties An object containing event properties
2373 * @return calendar_event|false The event object or false if it failed
2375 public static function create($properties) {
2376 if (is_array($properties)) {
2377 $properties = (object)$properties;
2379 if (!is_object($properties)) {
2380 throw new coding_exception('When creating an event properties should be either an object or an assoc array');
2382 $event = new calendar_event($properties);
2383 if ($event->update($properties)) {
2384 return $event;
2385 } else {
2386 return false;
2392 * Calendar information class
2394 * This class is used simply to organise the information pertaining to a calendar
2395 * and is used primarily to make information easily available.
2397 class calendar_information {
2399 * The day
2400 * @var int
2402 public $day;
2404 * The month
2405 * @var int
2407 public $month;
2409 * The year
2410 * @var int
2412 public $year;
2415 * A course id
2416 * @var int
2418 public $courseid = null;
2420 * An array of courses
2421 * @var array
2423 public $courses = array();
2425 * An array of groups
2426 * @var array
2428 public $groups = array();
2430 * An array of users
2431 * @var array
2433 public $users = array();
2436 * Creates a new instance
2438 * @param int $day
2439 * @param int $month
2440 * @param int $year
2442 public function __construct($day=0, $month=0, $year=0) {
2444 $date = usergetdate(time());
2446 if (empty($day)) {
2447 $day = $date['mday'];
2450 if (empty($month)) {
2451 $month = $date['mon'];
2454 if (empty($year)) {
2455 $year = $date['year'];
2458 $this->day = $day;
2459 $this->month = $month;
2460 $this->year = $year;
2464 * Ensures the date for the calendar is correct and either sets it to now
2465 * or throws a moodle_exception if not
2467 * @param bool $defaultonow
2468 * @return bool
2470 public function checkdate($defaultonow = true) {
2471 if (!checkdate($this->month, $this->day, $this->year)) {
2472 if ($defaultonow) {
2473 $now = usergetdate(time());
2474 $this->day = intval($now['mday']);
2475 $this->month = intval($now['mon']);
2476 $this->year = intval($now['year']);
2477 return true;
2478 } else {
2479 throw new moodle_exception('invaliddate');
2482 return true;
2485 * Gets todays timestamp for the calendar
2486 * @return int
2488 public function timestamp_today() {
2489 return make_timestamp($this->year, $this->month, $this->day);
2492 * Gets tomorrows timestamp for the calendar
2493 * @return int
2495 public function timestamp_tomorrow() {
2496 return make_timestamp($this->year, $this->month, $this->day+1);
2499 * Adds the pretend blocks for teh calendar
2501 * @param core_calendar_renderer $renderer
2502 * @param bool $showfilters
2503 * @param string|null $view
2505 public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) {
2506 if ($showfilters) {
2507 $filters = new block_contents();
2508 $filters->content = $renderer->fake_block_filters($this->courseid, $this->day, $this->month, $this->year, $view, $this->courses);
2509 $filters->footer = '';
2510 $filters->title = get_string('eventskey', 'calendar');
2511 $renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT);
2513 $block = new block_contents;
2514 $block->content = $renderer->fake_block_threemonths($this);
2515 $block->footer = '';
2516 $block->title = get_string('monthlyview', 'calendar');
2517 $renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT);