Commit | Line | Data |
---|---|---|
93c91ee4 | 1 | <?php |
7423f116 | 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 |
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); | |
76d9df3f SH |
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'); | |
bb4a2e85 | 54 | |
76d9df3f SH |
55 | /** |
56 | * CALENDAR_STARTING_WEEKDAY has since been deprecated please call calendar_get_starting_weekday() instead | |
57 | * @deprecated | |
58 | */ | |
59 | define('CALENDAR_STARTING_WEEKDAY', CALENDAR_DEFAULT_STARTING_WEEKDAY); | |
7423f116 | 60 | |
f2bffd9e | 61 | $CALENDARDAYS = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday'); |
62 | ||
76d9df3f SH |
63 | /** |
64 | * Gets the first day of the week | |
65 | * | |
66 | * Used to be define('CALENDAR_STARTING_WEEKDAY', blah); | |
67 | * | |
68 | * @return int | |
69 | */ | |
70 | function calendar_get_starting_weekday() { | |
71 | global $CFG; | |
37d87d11 | 72 | |
76d9df3f SH |
73 | if (isset($CFG->calendar_startwday)) { |
74 | $firstday = $CFG->calendar_startwday; | |
75 | } else { | |
0a933ae6 | 76 | $firstday = get_string('firstdayofweek', 'langconfig'); |
76d9df3f SH |
77 | } |
78 | ||
79 | if(!is_numeric($firstday)) { | |
80 | return CALENDAR_DEFAULT_STARTING_WEEKDAY; | |
81 | } else { | |
82 | return intval($firstday) % 7; | |
83 | } | |
84 | } | |
bd119567 | 85 | |
0f927f1e | 86 | /** |
14236cbd | 87 | * Generates the HTML for a miniature calendar |
0f927f1e SH |
88 | * |
89 | * @global core_renderer $OUTPUT | |
14236cbd SH |
90 | * @param array $courses |
91 | * @param array $groups | |
92 | * @param array $users | |
93 | * @param int $cal_month | |
94 | * @param int $cal_year | |
0f927f1e SH |
95 | * @return string |
96 | */ | |
7423f116 | 97 | function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) { |
6b608f8f | 98 | global $CFG, $USER, $OUTPUT; |
7423f116 | 99 | |
dd97c328 | 100 | $display = new stdClass; |
76d9df3f | 101 | $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday()); |
7423f116 | 102 | $display->maxwday = $display->minwday + 6; |
103 | ||
104 | $content = ''; | |
105 | ||
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; | |
36dc3b71 | 112 | } else { |
7423f116 | 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; | |
36dc3b71 | 117 | } else { |
7423f116 | 118 | $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year); |
119 | $display->thismonth = false; | |
120 | } | |
121 | } | |
36dc3b71 | 122 | } else { |
7423f116 | 123 | $date = usergetdate(time()); // Date and time the user sees at his location |
124 | $display->thismonth = true; | |
125 | } | |
126 | ||
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); | |
130 | ||
d6198903 | 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 | |
61240489 | 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 | |
d6198903 | 135 | } else { |
136 | // no timezone info specified | |
61240489 | 137 | $display->tstart = mktime(0, 0, 0, $m, 1, $y); |
138 | $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y); | |
d6198903 | 139 | } |
7423f116 | 140 | |
69244b91 | 141 | $startwday = dayofweek(1, $m, $y); |
7423f116 | 142 | |
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; | |
147 | } | |
148 | ||
c0b507d1 | 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: | |
1747ee05 | 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); | |
157 | //} | |
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); | |
160 | //} | |
c0b507d1 | 161 | |
162 | ||
7423f116 | 163 | // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ! |
8263f802 | 164 | $events = calendar_get_events( |
d3555a2f | 165 | usertime($display->tstart) - dst_offset_on($display->tstart), |
166 | usertime($display->tend) - dst_offset_on($display->tend), | |
167 | $users, $groups, $courses); | |
7423f116 | 168 | |
b4892fa2 | 169 | // Set event course class for course events |
170 | if (!empty($events)) { | |
13534ef7 | 171 | foreach ($events as $eventid => $event) { |
13534ef7 ML |
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]); | |
176 | } | |
177 | } | |
b4892fa2 | 178 | } |
179 | } | |
6619eba4 | 180 | |
c635dcda | 181 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 182 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 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. | |
185 | ||
0f927f1e | 186 | $hrefparams = array(); |
7bd1677c | 187 | if(!empty($courses)) { |
e749554e | 188 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 189 | if(count($courses) == 1) { |
0f927f1e | 190 | $hrefparams['course'] = reset($courses); |
7bd1677c | 191 | } |
c635dcda | 192 | } |
193 | ||
7423f116 | 194 | // We want to have easy access by day, since the display is on a per-day basis. |
195 | // Arguments passed by reference. | |
7b38bfa6 | 196 | //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday); |
7c50db30 | 197 | calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses); |
7423f116 | 198 | |
f20c4d02 | 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'); | |
202 | ||
2a06efcc | 203 | $summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear'))); |
204 | $summary = get_string('tabledata', 'access', $summary); | |
90723839 | 205 | $content .= '<table class="minicalendar calendartable" summary="'.$summary.'">'; // Begin table |
f136e4c5 | 206 | $content .= '<tr class="weekdays">'; // Header row: day names |
7423f116 | 207 | |
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 | |
f20c4d02 | 213 | $content .= '<th scope="col"><abbr title="'. get_string($days_title[$i % 7], 'calendar') .'">'. |
214 | get_string($days[$i % 7], 'calendar') ."</abbr></th>\n"; | |
7423f116 | 215 | } |
216 | ||
f136e4c5 | 217 | $content .= '</tr><tr>'; // End of day names; prepare for day numbers |
7423f116 | 218 | |
219 | // For the table display. $week is the row; $dayweek is the column. | |
7423f116 | 220 | $dayweek = $startwday; |
221 | ||
222 | // Paddding (the first week may have blank days in the beginning) | |
223 | for($i = $display->minwday; $i < $startwday; ++$i) { | |
3bfd8bc8 | 224 | $content .= '<td class="dayblank"> </td>'."\n"; |
7423f116 | 225 | } |
226 | ||
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) | |
d56d4e23 | 231 | $content .= '</tr><tr>'; |
7423f116 | 232 | $dayweek = $display->minwday; |
7423f116 | 233 | } |
92668ad2 | 234 | |
7423f116 | 235 | // Reset vars |
7423f116 | 236 | $cell = ''; |
bb4a2e85 | 237 | if(CALENDAR_WEEKEND & (1 << ($dayweek % 7))) { |
7423f116 | 238 | // Weekend. This is true no matter what the exact range is. |
e2aa618b | 239 | $class = 'weekend day'; |
90723839 | 240 | } else { |
7423f116 | 241 | // Normal working day. |
e2aa618b | 242 | $class = 'day'; |
7423f116 | 243 | } |
244 | ||
245 | // Special visual fx if an event is defined | |
246 | if(isset($eventsbyday[$day])) { | |
90723839 | 247 | $class .= ' hasevent'; |
0f927f1e SH |
248 | $hrefparams['view'] = 'day'; |
249 | $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $hrefparams), $day, $m, $y); | |
f434283f | 250 | |
7423f116 | 251 | $popupcontent = ''; |
252 | foreach($eventsbyday[$day] as $eventid) { | |
41d30a8e | 253 | if (!isset($events[$eventid])) { |
254 | continue; | |
4e17c6f3 | 255 | } |
41d30a8e | 256 | $event = $events[$eventid]; |
0f927f1e SH |
257 | $popupalt = ''; |
258 | $component = 'moodle'; | |
41d30a8e | 259 | if(!empty($event->modulename)) { |
0f927f1e | 260 | $popupicon = 'icon'; |
41d30a8e | 261 | $popupalt = $event->modulename; |
0f927f1e | 262 | $component = $event->modulename; |
9064751b | 263 | } else if ($event->courseid == SITEID) { // Site event |
0f927f1e | 264 | $popupicon = 'c/site'; |
c3d3b6d4 | 265 | } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event |
0f927f1e | 266 | $popupicon = 'c/course'; |
41d30a8e | 267 | } else if ($event->groupid) { // Group event |
0f927f1e | 268 | $popupicon = 'c/group'; |
41d30a8e | 269 | } else if ($event->userid) { // User event |
0f927f1e | 270 | $popupicon = 'c/user'; |
4e17c6f3 | 271 | } |
1d5bd3d2 | 272 | |
0f927f1e | 273 | $dayhref->set_anchor('event_'.$event->id); |
1d5bd3d2 | 274 | |
0f927f1e | 275 | $popupcontent .= html_writer::start_tag('div'); |
adc3cfc7 | 276 | $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component); |
0f927f1e SH |
277 | $popupcontent .= html_writer::link($dayhref, format_string($event->name, true)); |
278 | $popupcontent .= html_writer::end_tag('div'); | |
7423f116 | 279 | } |
e295df44 | 280 | |
b5c42e70 | 281 | //Accessibility: functionality moved to calendar_get_popup. |
282 | if($display->thismonth && $day == $d) { | |
450a0a7d | 283 | $popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent); |
b5c42e70 | 284 | } else { |
450a0a7d | 285 | $popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent); |
e295df44 | 286 | } |
f20c4d02 | 287 | |
7423f116 | 288 | // Class and cell content |
289 | if(isset($typesbyday[$day]['startglobal'])) { | |
90723839 SH |
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'; | |
7423f116 | 297 | } |
0f927f1e | 298 | $cell = '<a href="'.(string)$dayhref.'" '.$popup.'>'.$day.'</a>'; |
90723839 | 299 | } else { |
05fcc5fd | 300 | $cell = $day; |
7423f116 | 301 | } |
302 | ||
90723839 SH |
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'; | |
7423f116 | 312 | } |
90723839 SH |
313 | if ($durationclass) { |
314 | $class .= ' duration '.$durationclass; | |
7423f116 | 315 | } |
316 | ||
b4892fa2 | 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; | |
323 | } | |
324 | $event = $events[$eventid]; | |
325 | if (!empty($event->class)) { | |
326 | $class .= ' '.$event->class; | |
327 | } | |
328 | break; | |
329 | } | |
330 | } | |
331 | ||
7423f116 | 332 | // Special visual fx for today |
450a0a7d | 333 | //Accessibility: hidden text for today, and popup. |
7423f116 | 334 | if($display->thismonth && $day == $d) { |
edbe6c1b | 335 | $class .= ' today'; |
b5c42e70 | 336 | $today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort')); |
e295df44 | 337 | |
b5c42e70 | 338 | if(! isset($eventsbyday[$day])) { |
450a0a7d | 339 | $class .= ' eventnone'; |
340 | $popup = calendar_get_popup(true, false); | |
341 | $cell = '<a href="#" '.$popup.'>'.$day.'</a>'; | |
b5c42e70 | 342 | } |
f79f2494 | 343 | $cell = get_accesshide($today.' ').$cell; |
7423f116 | 344 | } |
345 | ||
346 | // Just display it | |
92668ad2 | 347 | if(!empty($class)) { |
d56d4e23 | 348 | $class = ' class="'.$class.'"'; |
92668ad2 | 349 | } |
350 | $content .= '<td'.$class.'>'.$cell."</td>\n"; | |
7423f116 | 351 | } |
352 | ||
353 | // Paddding (the last week may have blank days at the end) | |
354 | for($i = $dayweek; $i <= $display->maxwday; ++$i) { | |
3bfd8bc8 | 355 | $content .= '<td class="dayblank"> </td>'; |
7423f116 | 356 | } |
357 | $content .= '</tr>'; // Last row ends | |
358 | ||
f136e4c5 | 359 | $content .= '</table>'; // Tabular display of days ends |
7423f116 | 360 | |
361 | return $content; | |
362 | } | |
363 | ||
b5c42e70 | 364 | /** |
365 | * calendar_get_popup, called at multiple points in from calendar_get_mini. | |
366 | * Copied and modified from calendar_get_mini. | |
33e48a1a | 367 | * @global moodle_page $PAGE |
b5c42e70 | 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. | |
e295df44 | 371 | * @return $popup string, contains onmousover and onmouseout events. |
b5c42e70 | 372 | */ |
373 | function calendar_get_popup($is_today, $event_timestart, $popupcontent='') { | |
458eb0d1 | 374 | global $PAGE; |
375 | static $popupcount; | |
376 | if ($popupcount === null) { | |
377 | $popupcount = 1; | |
378 | } | |
b5c42e70 | 379 | $popupcaption = ''; |
380 | if($is_today) { | |
381 | $popupcaption = get_string('today', 'calendar').' '; | |
382 | } | |
383 | if (false === $event_timestart) { | |
384 | $popupcaption .= userdate(time(), get_string('strftimedayshort')); | |
385 | $popupcontent = get_string('eventnone', 'calendar'); | |
450a0a7d | 386 | |
b5c42e70 | 387 | } else { |
388 | $popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort'))); | |
389 | } | |
458eb0d1 | 390 | $id = 'calendar_tooltip_'.$popupcount; |
33e48a1a | 391 | $PAGE->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.add_event', array(array('eventId'=>$id,'title'=>$popupcaption, 'content'=>$popupcontent))); |
bdbae764 | 392 | |
458eb0d1 | 393 | $popupcount++; |
394 | return 'id="'.$id.'"'; | |
b5c42e70 | 395 | } |
396 | ||
9958a08c | 397 | function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) { |
62d11d77 | 398 | global $CFG, $COURSE, $DB; |
7423f116 | 399 | |
dd97c328 | 400 | $display = new stdClass; |
7423f116 | 401 | $display->range = $daysinfuture; // How many days in the future we 'll look |
402 | $display->maxevents = $maxevents; | |
403 | ||
404 | $output = array(); | |
405 | ||
406 | // Prepare "course caching", since it may save us a lot of queries | |
407 | $coursecache = array(); | |
408 | ||
409 | $processed = 0; | |
410 | $now = time(); // We 'll need this later | |
9d567178 | 411 | $usermidnighttoday = usergetmidnight($now); |
7423f116 | 412 | |
9958a08c | 413 | if ($fromtime) { |
414 | $display->tstart = $fromtime; | |
415 | } else { | |
9d567178 | 416 | $display->tstart = $usermidnighttoday; |
9958a08c | 417 | } |
7423f116 | 418 | |
1f473774 | 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; | |
7423f116 | 422 | |
423 | // Get the events matching our criteria | |
8263f802 | 424 | $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); |
7423f116 | 425 | |
c635dcda | 426 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 427 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 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. | |
430 | ||
0f927f1e | 431 | $hrefparams = array(); |
7bd1677c | 432 | if(!empty($courses)) { |
e749554e | 433 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 434 | if(count($courses) == 1) { |
0f927f1e | 435 | $hrefparams['course'] = reset($courses); |
7bd1677c | 436 | } |
c635dcda | 437 | } |
438 | ||
dd97c328 | 439 | if ($events !== false) { |
440 | ||
441 | $modinfo =& get_fast_modinfo($COURSE); | |
fa22fd5f | 442 | |
7423f116 | 443 | foreach($events as $event) { |
dc6cb74e | 444 | |
dd97c328 | 445 | |
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; | |
452 | } | |
453 | } | |
454 | } else { | |
455 | if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) { | |
dc6cb74e | 456 | continue; |
457 | } | |
dd97c328 | 458 | if (!coursemodule_visible_for_user($cm)) { |
dc6cb74e | 459 | continue; |
460 | } | |
dd97c328 | 461 | } |
462 | if ($event->modulename == 'assignment'){ | |
f4700b91 RW |
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); | |
dd97c328 | 466 | // TODO: rewrite this hack somehow |
f4700b91 | 467 | if (!calendar_edit_event_allowed($checkevent)){ // cannot manage entries, eg. student |
62d11d77 | 468 | if (!$assignment = $DB->get_record('assignment', array('id'=>$event->instance))) { |
f581f8d6 | 469 | // print_error("invalidid", 'assignment'); |
dd97c328 | 470 | continue; |
471 | } | |
472 | // assign assignment to assignment object to use hidden_is_hidden method | |
473 | require_once($CFG->dirroot.'/mod/assignment/lib.php'); | |
474 | ||
475 | if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) { | |
476 | continue; | |
477 | } | |
478 | require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); | |
479 | ||
480 | $assignmentclass = 'assignment_'.$assignment->assignmenttype; | |
481 | $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm); | |
482 | ||
483 | if ($assignmentinstance->description_is_hidden()){//force not to show description before availability | |
484 | $event->description = get_string('notavailableyet', 'assignment'); | |
485 | } | |
dc6cb74e | 486 | } |
487 | } | |
488 | } | |
9d567178 | 489 | |
dd97c328 | 490 | if ($processed >= $display->maxevents) { |
9d567178 | 491 | break; |
492 | } | |
7423f116 | 493 | |
0f927f1e | 494 | $event->time = calendar_format_event_time($event, $now, $hrefparams); |
3c134875 | 495 | $output[] = $event; |
496 | ++$processed; | |
497 | } | |
498 | } | |
499 | return $output; | |
500 | } | |
7423f116 | 501 | |
9df8ff44 | 502 | function calendar_add_event_metadata($event) { |
6b608f8f | 503 | global $CFG, $OUTPUT; |
fb73f3b3 | 504 | |
e295df44 | 505 | //Support multilang in event->name |
fb73f3b3 | 506 | $event->name = format_string($event->name,true); |
e295df44 | 507 | |
3c134875 | 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); | |
7423f116 | 513 | |
3c134875 | 514 | if ($module === false) { |
515 | return; | |
516 | } | |
9958a08c | 517 | |
3c134875 | 518 | $modulename = get_string('modulename', $event->modulename); |
a48bf079 DM |
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 = ''; | |
524 | } | |
b5d0cafc | 525 | $icon = $OUTPUT->pix_url('icon', $event->modulename) . ''; |
9958a08c | 526 | |
10daca92 | 527 | $event->icon = '<img height="16" width="16" src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" style="vertical-align: middle;" />'; |
fb73f3b3 | 528 | $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>'; |
3c134875 | 529 | $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$coursecache[$module->course]->fullname.'</a>'; |
530 | $event->cmid = $module->id; | |
9958a08c | 531 | |
9958a08c | 532 | |
3c134875 | 533 | } else if($event->courseid == SITEID) { // Site event |
b5d0cafc | 534 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/site') . '" alt="'.get_string('globalevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 535 | $event->cssclass = 'calendar_event_global'; |
3c134875 | 536 | } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event |
537 | calendar_get_course_cached($coursecache, $event->courseid); | |
b5d0cafc | 538 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/course') . '" alt="'.get_string('courseevent', 'calendar').'" style="vertical-align: middle;" />'; |
3c134875 | 539 | $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$coursecache[$event->courseid]->fullname.'</a>'; |
90723839 | 540 | $event->cssclass = 'calendar_event_course'; |
3c134875 | 541 | } else if ($event->groupid) { // Group event |
b5d0cafc | 542 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/group') . '" alt="'.get_string('groupevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 543 | $event->cssclass = 'calendar_event_group'; |
3c134875 | 544 | } else if($event->userid) { // User event |
b5d0cafc | 545 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/user') . '" alt="'.get_string('userevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 546 | $event->cssclass = 'calendar_event_user'; |
3c134875 | 547 | } |
9df8ff44 | 548 | return $event; |
e295df44 | 549 | } |
9df8ff44 | 550 | |
36dc3b71 SH |
551 | /** |
552 | * Prints a calendar event | |
553 | * | |
554 | * @deprecated 2.0 | |
555 | */ | |
76d9df3f | 556 | function calendar_print_event($event, $showactions=true) { |
ae53bd1d | 557 | global $CFG, $USER, $OUTPUT, $PAGE; |
36dc3b71 SH |
558 | debugging('calendar_print_event is deprecated please update your code', DEBUG_DEVELOPER); |
559 | $renderer = $PAGE->get_renderer('core_calendar'); | |
8bdc9cac SH |
560 | if (!($event instanceof calendar_event)) { |
561 | $event = new calendar_event($event); | |
562 | } | |
36dc3b71 | 563 | echo $renderer->event($event); |
7423f116 | 564 | } |
565 | ||
8263f802 | 566 | /** |
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) | |
577 | */ | |
578 | function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) { | |
62d11d77 | 579 | global $DB; |
580 | ||
7423f116 | 581 | $whereclause = ''; |
582 | // Quick test | |
583 | if(is_bool($users) && is_bool($groups) && is_bool($courses)) { | |
8263f802 | 584 | return array(); |
7423f116 | 585 | } |
482dbe0c | 586 | |
7423f116 | 587 | if(is_array($users) && !empty($users)) { |
588 | // Events from a number of users | |
589 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 590 | $whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 591 | } else if(is_numeric($users)) { |
7423f116 | 592 | // Events from one user |
593 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 594 | $whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 595 | } else if($users === true) { |
7423f116 | 596 | // Events from ALL users |
597 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 598 | $whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 599 | } else if($users === false) { |
6e957c41 | 600 | // No user at all, do nothing |
f52f7413 | 601 | } |
482dbe0c | 602 | |
7423f116 | 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).')'; | |
36dc3b71 | 607 | } else if(is_numeric($groups)) { |
7423f116 | 608 | // Events from one group |
609 | if(!empty($whereclause)) $whereclause .= ' OR '; | |
610 | $whereclause .= ' groupid = '.$groups; | |
36dc3b71 | 611 | } else if($groups === true) { |
7423f116 | 612 | // Events from ALL groups |
613 | if(!empty($whereclause)) $whereclause .= ' OR '; | |
614 | $whereclause .= ' groupid != 0'; | |
615 | } | |
482dbe0c | 616 | // boolean false (no groups at all): we don't need to do anything |
617 | ||
36dc3b71 SH |
618 | if(is_array($courses) && !empty($courses)) { |
619 | if(!empty($whereclause)) { | |
620 | $whereclause .= ' OR'; | |
f52f7413 | 621 | } |
36dc3b71 SH |
622 | $whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))'; |
623 | } else if(is_numeric($courses)) { | |
7423f116 | 624 | // One course |
625 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 626 | $whereclause .= ' (groupid = 0 AND courseid = '.$courses.')'; |
36dc3b71 | 627 | } else if ($courses === true) { |
7423f116 | 628 | // Events from ALL courses |
629 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 630 | $whereclause .= ' (groupid = 0 AND courseid != 0)'; |
7423f116 | 631 | } |
8c165fe9 | 632 | |
482dbe0c | 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)) { | |
8263f802 | 638 | return array(); |
482dbe0c | 639 | } |
640 | ||
7423f116 | 641 | if($withduration) { |
b4892fa2 | 642 | $timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend; |
7423f116 | 643 | } |
644 | else { | |
645 | $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend; | |
646 | } | |
647 | if(!empty($whereclause)) { | |
648 | // We have additional constraints | |
649 | $whereclause = $timeclause.' AND ('.$whereclause.')'; | |
650 | } | |
651 | else { | |
652 | // Just basic time filtering | |
653 | $whereclause = $timeclause; | |
654 | } | |
f52f7413 | 655 | |
0ad072de | 656 | if ($ignorehidden) { |
657 | $whereclause .= ' AND visible = 1'; | |
658 | } | |
659 | ||
62d11d77 | 660 | $events = $DB->get_records_select('event', $whereclause, null, 'timestart'); |
8263f802 | 661 | if ($events === false) { |
662 | $events = array(); | |
663 | } | |
664 | return $events; | |
7423f116 | 665 | } |
666 | ||
667 | function calendar_top_controls($type, $data) { | |
78946b9b | 668 | global $CFG, $CALENDARDAYS; |
7423f116 | 669 | $content = ''; |
670 | if(!isset($data['d'])) { | |
671 | $data['d'] = 1; | |
672 | } | |
5147ad48 | 673 | |
f21ed0f3 | 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 = '&course='.$data['id']; | |
679 | } | |
680 | ||
5147ad48 | 681 | if(!checkdate($data['m'], $data['d'], $data['y'])) { |
682 | $time = time(); | |
683 | } | |
684 | else { | |
685 | $time = make_timestamp($data['y'], $data['m'], $data['d']); | |
686 | } | |
687 | $date = usergetdate($time); | |
e295df44 | 688 | |
7423f116 | 689 | $data['m'] = $date['mon']; |
690 | $data['y'] = $date['year']; | |
7423f116 | 691 | |
2a06efcc | 692 | //Accessibility: calendar block controls, replaced <table> with <div>. |
a84dea2c | 693 | //$nexttext = link_arrow_right(get_string('monthnext', 'access'), $url='', $accesshide=true); |
694 | //$prevtext = link_arrow_left(get_string('monthprev', 'access'), $url='', $accesshide=true); | |
2a06efcc | 695 | |
7423f116 | 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']); | |
a84dea2c | 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); | |
ae53bd1d | 702 | |
0f927f1e SH |
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']); | |
706 | } | |
707 | ||
43c90b9b | 708 | if (right_to_left()) { |
0f927f1e SH |
709 | $left = $nextlink; |
710 | $right = $prevlink; | |
43c90b9b | 711 | } else { |
0f927f1e SH |
712 | $left = $prevlink; |
713 | $right = $nextlink; | |
43c90b9b | 714 | } |
ae53bd1d | 715 | |
0f927f1e SH |
716 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
717 | $content .= $left.'<span class="hide"> | </span>'; | |
e71e340d | 718 | $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current')); |
0f927f1e | 719 | $content .= '<span class="hide"> | </span>'. $right; |
6694ff84 | 720 | $content .= "<span class=\"clearer\"><!-- --></span>\n"; |
0f927f1e SH |
721 | $content .= html_writer::end_tag('div'); |
722 | ||
723 | break; | |
7423f116 | 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']); | |
a84dea2c | 727 | $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'view.php?id='.$data['id'].'&', 0, $nextmonth, $nextyear, $accesshide=true); |
728 | $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'view.php?id='.$data['id'].'&', 0, $prevmonth, $prevyear, true); | |
0f927f1e SH |
729 | |
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']); | |
733 | } | |
734 | ||
735 | if (right_to_left()) { | |
736 | $left = $nextlink; | |
737 | $right = $prevlink; | |
738 | } else { | |
739 | $left = $prevlink; | |
740 | $right = $nextlink; | |
741 | } | |
742 | ||
743 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); | |
744 | $content .= $left.'<span class="hide"> | </span>'; | |
e71e340d | 745 | $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current')); |
0f927f1e SH |
746 | $content .= '<span class="hide"> | </span>'. $right; |
747 | $content .= "<span class=\"clearer\"><!-- --></span>"; | |
748 | $content .= html_writer::end_tag('div'); | |
749 | break; | |
7423f116 | 750 | case 'upcoming': |
0f927f1e SH |
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']); | |
754 | } | |
755 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
756 | $content .= html_writer::tag('div', $calendarlink, array('class'=>'centered')); | |
757 | break; | |
7423f116 | 758 | case 'display': |
0f927f1e SH |
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']); | |
762 | } | |
763 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
764 | $content .= html_writer::tag('h3', $calendarlink); | |
765 | break; | |
7423f116 | 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']); | |
5147ad48 | 769 | $prevdate = make_timestamp($prevyear, $prevmonth, 1); |
770 | $nextdate = make_timestamp($nextyear, $nextmonth, 1); | |
0f927f1e SH |
771 | $prevlink = calendar_get_link_previous(userdate($prevdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $prevmonth, $prevyear); |
772 | $nextlink = calendar_get_link_next(userdate($nextdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $nextmonth, $nextyear); | |
773 | ||
774 | if (right_to_left()) { | |
775 | $left = $nextlink; | |
776 | $right = $prevlink; | |
777 | } else { | |
778 | $left = $prevlink; | |
779 | $right = $nextlink; | |
780 | } | |
781 | ||
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; | |
7423f116 | 788 | case 'day': |
789 | $data['d'] = $date['mday']; // Just for convenience | |
5147ad48 | 790 | $prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1)); |
791 | $nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1)); | |
f2bffd9e | 792 | $prevname = calendar_wday_name($CALENDARDAYS[$prevdate['wday']]); |
793 | $nextname = calendar_wday_name($CALENDARDAYS[$nextdate['wday']]); | |
0f927f1e SH |
794 | $prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&', $prevdate['mday'], $prevdate['mon'], $prevdate['year']); |
795 | $nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&', $nextdate['mday'], $nextdate['mon'], $nextdate['year']); | |
796 | ||
797 | if (right_to_left()) { | |
798 | $left = $nextlink; | |
799 | $right = $prevlink; | |
800 | } else { | |
801 | $left = $prevlink; | |
802 | $right = $nextlink; | |
803 | } | |
804 | ||
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"; | |
811 | ||
812 | break; | |
7423f116 | 813 | } |
814 | return $content; | |
815 | } | |
816 | ||
b4892fa2 | 817 | function calendar_filter_controls($type, $vars = NULL, $course = NULL, $courses = NULL) { |
6b608f8f | 818 | global $CFG, $SESSION, $USER, $OUTPUT; |
7423f116 | 819 | |
48f508ab | 820 | $groupevents = true; |
3cb9ee39 | 821 | $getvars = ''; |
e295df44 | 822 | |
7f4d18fc | 823 | $id = optional_param( 'id',0,PARAM_INT ); |
d715f7c4 | 824 | |
34bf3ad4 | 825 | switch($type) { |
eb15f829 | 826 | case 'event': |
34bf3ad4 | 827 | case 'upcoming': |
34bf3ad4 | 828 | case 'day': |
c3f463ca | 829 | case 'month': |
eb15f829 | 830 | $getvars = '&from='.$type; |
34bf3ad4 | 831 | break; |
832 | case 'course': | |
fac01360 | 833 | if ($id > 0) { |
7f4d18fc | 834 | $getvars = '&from=course&id='.$id; |
2eb68e6f | 835 | } else { |
836 | $getvars = '&from=course'; | |
837 | } | |
5a74ffd3 | 838 | if (isset($course->groupmode) and $course->groupmode == NOGROUPS and $course->groupmodeforce) { |
34bf3ad4 | 839 | $groupevents = false; |
840 | } | |
841 | break; | |
d715f7c4 | 842 | } |
34bf3ad4 | 843 | |
3cb9ee39 | 844 | if (!empty($vars)) { |
34bf3ad4 | 845 | $getvars .= '&'.$vars; |
48f508ab | 846 | } |
7423f116 | 847 | |
2ad2cbc3 | 848 | $content = '<table>'; |
7423f116 | 849 | |
48f508ab | 850 | $content .= '<tr>'; |
851 | if($SESSION->cal_show_global) { | |
90723839 | 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>'; |
b4892fa2 | 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"; |
fdbffa54 | 854 | } else { |
b5d0cafc | 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>'; |
b4892fa2 | 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"; |
48f508ab | 857 | } |
e3bb6401 | 858 | if($SESSION->cal_show_course) { |
90723839 | 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>'; |
fdbffa54 | 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 { | |
b5d0cafc | 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>'; |
fdbffa54 | 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"; |
aa6c1ced | 864 | |
e3bb6401 | 865 | } |
48f508ab | 866 | |
b4892fa2 | 867 | |
4f0c2d00 | 868 | if (isloggedin() && !isguestuser()) { |
e3bb6401 | 869 | $content .= "</tr>\n<tr>"; |
10b1d598 | 870 | |
871 | if($groupevents) { | |
43c3ffbe | 872 | // This course MIGHT have group events defined, so show the filter |
873 | if($SESSION->cal_show_groups) { | |
90723839 | 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>'; |
fdbffa54 | 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"; |
e3bb6401 | 876 | } else { |
b5d0cafc | 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>'; |
fdbffa54 | 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"; |
43c3ffbe | 879 | } |
e3bb6401 | 880 | } else { |
43c3ffbe | 881 | // This course CANNOT have group events, so lose the filter |
5c53988f | 882 | $content .= '<td style="width: 11px;"></td><td> </td>'."\n"; |
fdbffa54 | 883 | } |
884 | if($SESSION->cal_show_user) { | |
90723839 | 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>'; |
fdbffa54 | 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 { | |
b5d0cafc | 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>'; |
fdbffa54 | 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"; |
48f508ab | 890 | } |
891 | } | |
892 | $content .= "</tr>\n</table>\n"; | |
893 | ||
7423f116 | 894 | return $content; |
895 | } | |
896 | ||
897 | function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) { | |
898 | ||
0ef7c973 | 899 | static $shortformat; |
900 | if(empty($shortformat)) { | |
e70fdac0 | 901 | $shortformat = get_string('strftimedayshort'); |
0ef7c973 | 902 | } |
903 | ||
7423f116 | 904 | if($now === false) { |
905 | $now = time(); | |
906 | } | |
907 | ||
908 | // To have it in one place, if a change is needed | |
e70fdac0 | 909 | $formal = userdate($tstamp, $shortformat); |
7423f116 | 910 | |
7b38bfa6 | 911 | $datestamp = usergetdate($tstamp); |
912 | $datenow = usergetdate($now); | |
7423f116 | 913 | |
914 | if($usecommonwords == false) { | |
915 | // We don't want words, just a date | |
916 | return $formal; | |
917 | } | |
7b38bfa6 | 918 | else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) { |
7423f116 | 919 | // Today |
920 | return get_string('today', 'calendar'); | |
921 | } | |
7b38bfa6 | 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) | |
925 | ) { | |
7423f116 | 926 | // Yesterday |
927 | return get_string('yesterday', 'calendar'); | |
928 | } | |
7b38bfa6 | 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) | |
932 | ) { | |
7423f116 | 933 | // Tomorrow |
934 | return get_string('tomorrow', 'calendar'); | |
935 | } | |
936 | else { | |
937 | return $formal; | |
938 | } | |
939 | } | |
940 | ||
941 | function calendar_time_representation($time) { | |
1b0ebe79 | 942 | static $langtimeformat = NULL; |
943 | if($langtimeformat === NULL) { | |
944 | $langtimeformat = get_string('strftimetime'); | |
945 | } | |
946 | $timeformat = get_user_preferences('calendar_timeformat'); | |
c7dd2550 | 947 | if(empty($timeformat)){ |
948 | $timeformat = get_config(NULL,'calendar_site_timeformat'); | |
949 | } | |
1b0ebe79 | 950 | // The ? is needed because the preference might be present, but empty |
951 | return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat); | |
7423f116 | 952 | } |
953 | ||
e295df44 | 954 | /** |
14236cbd SH |
955 | * Adds day, month, year arguments to a URL and returns a moodle_url object. |
956 | * | |
957 | * @param string|moodle_url $linkbase | |
958 | * @param int $d | |
959 | * @param int $m | |
960 | * @param int $y | |
1d5bd3d2 | 961 | * @return moodle_url |
e295df44 | 962 | */ |
7423f116 | 963 | function calendar_get_link_href($linkbase, $d, $m, $y) { |
0f927f1e SH |
964 | if (empty($linkbase)) { |
965 | return ''; | |
966 | } | |
967 | if (!($linkbase instanceof moodle_url)) { | |
968 | $linkbase = new moodle_url(); | |
969 | } | |
970 | if (!empty($d)) { | |
971 | $linkbase->param('cal_d', $d); | |
972 | } | |
973 | if (!empty($m)) { | |
974 | $linkbase->param('cal_m', $m); | |
975 | } | |
976 | if (!empty($y)) { | |
977 | $linkbase->param('cal_y', $y); | |
978 | } | |
979 | return $linkbase; | |
7423f116 | 980 | } |
981 | ||
e295df44 | 982 | /** |
0f927f1e SH |
983 | * This function has been deprecated as of Moodle 2.0... DO NOT USE!!!!! |
984 | * | |
985 | * @deprecated | |
986 | * @since 2.0 | |
987 | * | |
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 | |
e295df44 | 994 | */ |
7423f116 | 995 | function calendar_get_link_tag($text, $linkbase, $d, $m, $y) { |
0f927f1e SH |
996 | $url = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y); |
997 | if (empty($url)) { | |
998 | return $text; | |
999 | } | |
1000 | return html_writer::link($url, $text); | |
7423f116 | 1001 | } |
1002 | ||
a84dea2c | 1003 | /** |
1004 | * Build and return a previous month HTML link, with an arrow. | |
14236cbd | 1005 | * |
a84dea2c | 1006 | * @param string $text The text label. |
14236cbd | 1007 | * @param string|moodle_url $linkbase The URL stub. |
a84dea2c | 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. | |
1011 | */ | |
1012 | function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide=false) { | |
0f927f1e SH |
1013 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y); |
1014 | if (empty($href)) { | |
1015 | return $text; | |
1016 | } | |
1017 | return link_arrow_left($text, (string)$href, $accesshide, 'previous'); | |
a84dea2c | 1018 | } |
1019 | ||
1020 | /** | |
1021 | * Build and return a next month HTML link, with an arrow. | |
1d5bd3d2 | 1022 | * |
a84dea2c | 1023 | * @param string $text The text label. |
14236cbd | 1024 | * @param string|moodle_url $linkbase The URL stub. |
a84dea2c | 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. | |
1028 | */ | |
1029 | function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide=false) { | |
0f927f1e SH |
1030 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y); |
1031 | if (empty($href)) { | |
1032 | return $text; | |
1033 | } | |
1034 | return link_arrow_right($text, (string)$href, $accesshide, 'next'); | |
a84dea2c | 1035 | } |
1036 | ||
7423f116 | 1037 | function calendar_wday_name($englishname) { |
1038 | return get_string(strtolower($englishname), 'calendar'); | |
1039 | } | |
1040 | ||
1041 | function calendar_days_in_month($month, $year) { | |
7b38bfa6 | 1042 | return intval(date('t', mktime(0, 0, 0, $month, 1, $year))); |
7423f116 | 1043 | } |
1044 | ||
6605ff8c | 1045 | function calendar_get_block_upcoming($events, $linkhref = NULL) { |
7423f116 | 1046 | $content = ''; |
1047 | $lines = count($events); | |
396b61f0 | 1048 | if (!$lines) { |
1049 | return $content; | |
1050 | } | |
7423f116 | 1051 | |
396b61f0 | 1052 | for ($i = 0; $i < $lines; ++$i) { |
b0ac9180 | 1053 | if (!isset($events[$i]->time)) { // Just for robustness |
1054 | continue; | |
1055 | } | |
9df8ff44 | 1056 | $events[$i] = calendar_add_event_metadata($events[$i]); |
ea21c1f4 | 1057 | $content .= '<div class="event"><span class="icon c0">'.$events[$i]->icon.'</span> '; |
43c3ffbe | 1058 | if (!empty($events[$i]->referer)) { |
7423f116 | 1059 | // That's an activity event, so let's provide the hyperlink |
396b61f0 | 1060 | $content .= $events[$i]->referer; |
1061 | } else { | |
e749554e | 1062 | if(!empty($linkhref)) { |
1063 | $ed = usergetdate($events[$i]->timestart); | |
0f927f1e SH |
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); | |
e749554e | 1067 | } |
1068 | else { | |
1069 | $content .= $events[$i]->name; | |
1070 | } | |
7423f116 | 1071 | } |
9ecf051d | 1072 | $events[$i]->time = str_replace('»', '<br />»', $events[$i]->time); |
1073 | $content .= '<div class="date">'.$events[$i]->time.'</div></div>'; | |
396b61f0 | 1074 | if ($i < $lines - 1) $content .= '<hr />'; |
7423f116 | 1075 | } |
1076 | ||
1077 | return $content; | |
7423f116 | 1078 | } |
1079 | ||
1080 | function calendar_add_month($month, $year) { | |
1081 | if($month == 12) { | |
1082 | return array(1, $year + 1); | |
1083 | } | |
1084 | else { | |
1085 | return array($month + 1, $year); | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | function calendar_sub_month($month, $year) { | |
1090 | if($month == 1) { | |
1091 | return array(12, $year - 1); | |
1092 | } | |
1093 | else { | |
1094 | return array($month - 1, $year); | |
1095 | } | |
1096 | } | |
1097 | ||
7c50db30 | 1098 | function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) { |
7423f116 | 1099 | $eventsbyday = array(); |
1100 | $typesbyday = array(); | |
1101 | $durationbyday = array(); | |
1102 | ||
1103 | if($events === false) { | |
1104 | return; | |
1105 | } | |
1106 | ||
7423f116 | 1107 | foreach($events as $event) { |
7423f116 | 1108 | |
7b38bfa6 | 1109 | $startdate = usergetdate($event->timestart); |
b4892fa2 | 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; | |
1115 | } | |
7423f116 | 1116 | |
7b38bfa6 | 1117 | // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair |
ef618501 | 1118 | if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) { |
7b38bfa6 | 1119 | // Out of bounds |
1120 | continue; | |
7423f116 | 1121 | } |
1122 | ||
7b38bfa6 | 1123 | $eventdaystart = intval($startdate['mday']); |
7423f116 | 1124 | |
7b38bfa6 | 1125 | if($startdate['mon'] == $month && $startdate['year'] == $year) { |
1126 | // Give the event to its day | |
1127 | $eventsbyday[$eventdaystart][] = $event->id; | |
7423f116 | 1128 | |
7b38bfa6 | 1129 | // Mark the day as having such an event |
9064751b | 1130 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1131 | $typesbyday[$eventdaystart]['startglobal'] = true; |
b4892fa2 | 1132 | // Set event class for global event |
90723839 | 1133 | $events[$event->id]->class = 'calendar_event_global'; |
7b38bfa6 | 1134 | } |
c3d3b6d4 | 1135 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1136 | $typesbyday[$eventdaystart]['startcourse'] = true; |
b4892fa2 | 1137 | // Set event class for course event |
90723839 | 1138 | $events[$event->id]->class = 'calendar_event_course'; |
7b38bfa6 | 1139 | } |
1140 | else if($event->groupid) { | |
1141 | $typesbyday[$eventdaystart]['startgroup'] = true; | |
b4892fa2 | 1142 | // Set event class for group event |
90723839 | 1143 | $events[$event->id]->class = 'calendar_event_group'; |
7b38bfa6 | 1144 | } |
1145 | else if($event->userid) { | |
1146 | $typesbyday[$eventdaystart]['startuser'] = true; | |
b4892fa2 | 1147 | // Set event class for user event |
90723839 | 1148 | $events[$event->id]->class = 'calendar_event_user'; |
7b38bfa6 | 1149 | } |
1150 | } | |
7423f116 | 1151 | |
7b38bfa6 | 1152 | if($event->timeduration == 0) { |
1153 | // Proceed with the next | |
1154 | continue; | |
1155 | } | |
7423f116 | 1156 | |
7b38bfa6 | 1157 | // The event starts on $month $year or before. So... |
ef618501 | 1158 | $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0; |
7b38bfa6 | 1159 | |
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); | |
1162 | ||
1163 | // Mark all days between $lowerbound and $upperbound (inclusive) as duration | |
1164 | for($i = $lowerbound + 1; $i <= $upperbound; ++$i) { | |
1165 | $durationbyday[$i][] = $event->id; | |
9064751b | 1166 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1167 | $typesbyday[$i]['durationglobal'] = true; |
1168 | } | |
c3d3b6d4 | 1169 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1170 | $typesbyday[$i]['durationcourse'] = true; |
1171 | } | |
1172 | else if($event->groupid) { | |
1173 | $typesbyday[$i]['durationgroup'] = true; | |
1174 | } | |
1175 | else if($event->userid) { | |
1176 | $typesbyday[$i]['durationuser'] = true; | |
7423f116 | 1177 | } |
1178 | } | |
7b38bfa6 | 1179 | |
7423f116 | 1180 | } |
1181 | return; | |
1182 | } | |
1183 | ||
b63c0ee5 | 1184 | function calendar_get_module_cached(&$coursecache, $modulename, $instance) { |
1185 | $module = get_coursemodule_from_instance($modulename, $instance); | |
7423f116 | 1186 | |
1187 | if($module === false) return false; | |
b63c0ee5 | 1188 | if(!calendar_get_course_cached($coursecache, $module->course)) { |
7423f116 | 1189 | return false; |
1190 | } | |
1191 | return $module; | |
1192 | } | |
1193 | ||
1194 | function calendar_get_course_cached(&$coursecache, $courseid) { | |
62d11d77 | 1195 | global $COURSE, $DB; |
1196 | ||
b571c6b3 | 1197 | if (!isset($coursecache[$courseid])) { |
1198 | if ($courseid == $COURSE->id) { | |
1199 | $coursecache[$courseid] = $COURSE; | |
1200 | } else { | |
62d11d77 | 1201 | $coursecache[$courseid] = $DB->get_record('course', array('id'=>$courseid)); |
b571c6b3 | 1202 | } |
7423f116 | 1203 | } |
1204 | return $coursecache[$courseid]; | |
1205 | } | |
1206 | ||
dd97c328 | 1207 | function calendar_session_vars($course=null) { |
7423f116 | 1208 | global $SESSION, $USER; |
1209 | ||
1210 | if(!isset($SESSION->cal_course_referer)) { | |
1211 | $SESSION->cal_course_referer = 0; | |
1212 | } | |
1213 | if(!isset($SESSION->cal_show_global)) { | |
1214 | $SESSION->cal_show_global = true; | |
1215 | } | |
1216 | if(!isset($SESSION->cal_show_groups)) { | |
1217 | $SESSION->cal_show_groups = true; | |
1218 | } | |
1219 | if(!isset($SESSION->cal_show_course)) { | |
1220 | $SESSION->cal_show_course = true; | |
1221 | } | |
1222 | if(!isset($SESSION->cal_show_user)) { | |
89adb174 | 1223 | $SESSION->cal_show_user = true; |
7423f116 | 1224 | } |
76d9df3f | 1225 | if ($course !== null) { |
dd97c328 | 1226 | // speedup hack for calendar related blocks |
753b799d SH |
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); | |
1232 | } | |
dd97c328 | 1233 | $SESSION->cal_courses_shown = array($course->id => $course); |
aa6c1ced | 1234 | } else { |
43c3ffbe | 1235 | $SESSION->cal_courses_shown = calendar_get_default_courses(true); |
dd97c328 | 1236 | } |
89adb174 | 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. | |
4f0c2d00 PS |
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) { | |
89adb174 | 1243 | // Follow the white rabbit, for example if a teacher logs in as a student |
1244 | $SESSION->cal_users_shown = $USER->id; | |
1245 | } | |
7423f116 | 1246 | } |
1247 | ||
7423f116 | 1248 | function calendar_set_referring_course($courseid) { |
1249 | global $SESSION; | |
1250 | $SESSION->cal_course_referer = intval($courseid); | |
1251 | } | |
1252 | ||
43c3ffbe | 1253 | function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NULL, $groupeventsfrom = NULL, $ignorefilters = false) { |
62d11d77 | 1254 | global $SESSION, $USER, $CFG, $DB; |
257e3f4c | 1255 | |
43c3ffbe | 1256 | // Insidious bug-wannabe: setting $SESSION->cal_courses_shown to $course->id would cause |
51f8a12f | 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. | |
43c3ffbe | 1259 | if(is_string($SESSION->cal_courses_shown)) { |
1260 | $SESSION->cal_courses_shown = intval($SESSION->cal_courses_shown); | |
51f8a12f | 1261 | } |
43c3ffbe | 1262 | if($courseeventsfrom === NULL) { |
aa6c1ced | 1263 | $courseeventsfrom = $SESSION->cal_courses_shown; |
d12e3ff2 | 1264 | } |
aa6c1ced | 1265 | |
d12e3ff2 | 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(); | |
62d11d77 | 1271 | $c[$courseeventsfrom] = $DB->get_record('course', array('id'=>$courseeventsfrom)); |
d12e3ff2 | 1272 | $courseeventsfrom = $c; |
1273 | } else if (is_array($courseeventsfrom)) { // case of an array of ints, e.g. course home page | |
dd97c328 | 1274 | foreach ($courseeventsfrom as $i=>$courseid) { // TODO: this seems wrong, the array is often constructed as [courseid] => 1 ??? |
d12e3ff2 | 1275 | if (is_int($courseid)) { |
62d11d77 | 1276 | $courseeventsfrom[$i] = $DB->get_record('course', array('id'=>$courseid)); |
aa6c1ced PS |
1277 | } |
1278 | } | |
7423f116 | 1279 | } |
d12e3ff2 | 1280 | |
43c3ffbe | 1281 | if($groupeventsfrom === NULL) { |
1282 | $groupeventsfrom = $SESSION->cal_courses_shown; | |
7423f116 | 1283 | } |
1284 | ||
43c3ffbe | 1285 | if(($SESSION->cal_show_course && $SESSION->cal_show_global) || $ignorefilters) { |
1286 | if(is_int($courseeventsfrom)) { | |
9064751b | 1287 | $courses = array(SITEID, $courseeventsfrom); |
7423f116 | 1288 | } |
43c3ffbe | 1289 | else if(is_array($courseeventsfrom)) { |
1290 | $courses = array_keys($courseeventsfrom); | |
9064751b | 1291 | $courses[] = SITEID; |
7423f116 | 1292 | } |
1293 | } | |
43c3ffbe | 1294 | else if($SESSION->cal_show_course) { |
1295 | if(is_int($courseeventsfrom)) { | |
1296 | $courses = array($courseeventsfrom); | |
7423f116 | 1297 | } |
43c3ffbe | 1298 | else if(is_array($courseeventsfrom)) { |
1299 | $courses = array_keys($courseeventsfrom); | |
7423f116 | 1300 | } |
e749554e | 1301 | $courses = array_diff($courses, array(SITEID)); |
7423f116 | 1302 | } |
1303 | else if($SESSION->cal_show_global) { | |
e749554e | 1304 | $courses = array(SITEID); |
7423f116 | 1305 | } |
1306 | else { | |
1307 | $courses = false; | |
1308 | } | |
e295df44 | 1309 | //BUG 6130 clean $courses array as SESSION has bad entries. |
0e6a8f4b | 1310 | // [pj] TODO: See if this has to do with my new change in get_default_courses and can be taken out |
1384da1e | 1311 | if (is_array($courses)) { |
1312 | foreach ($courses as $index => $value) { | |
1313 | if (empty($value)) unset($courses[$index]); | |
1314 | } | |
7c50db30 | 1315 | |
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); | |
1325 | } | |
0e6a8f4b | 1326 | } |
7423f116 | 1327 | |
43c3ffbe | 1328 | if($SESSION->cal_show_user || $ignorefilters) { |
89adb174 | 1329 | // This doesn't work for arrays yet (maybe someday it will) |
1330 | $user = $SESSION->cal_users_shown; | |
7423f116 | 1331 | } |
1332 | else { | |
1333 | $user = false; | |
1334 | } | |
43c3ffbe | 1335 | if($SESSION->cal_show_groups || $ignorefilters) { |
1336 | if(is_int($groupeventsfrom)) { | |
1337 | $groupcourses = array($groupeventsfrom); | |
7423f116 | 1338 | } |
43c3ffbe | 1339 | else if(is_array($groupeventsfrom)) { |
1340 | $groupcourses = array_keys($groupeventsfrom); | |
1341 | } | |
257e3f4c | 1342 | |
9c37662f | 1343 | // XXX TODO: not sure how to replace $CFG->calendar_adminseesall |
ef35441d | 1344 | if(has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM)) && !empty($CFG->calendar_adminseesall)) { |
257e3f4c | 1345 | $group = true; |
1346 | } | |
1347 | else { | |
1348 | $grouparray = array(); | |
69762501 | 1349 | |
257e3f4c | 1350 | // We already have the courses to examine in $courses |
1351 | // For each course... | |
37d87d11 | 1352 | |
257e3f4c | 1353 | foreach($groupcourses as $courseid) { |
69762501 | 1354 | |
37875ac5 | 1355 | if (!isset($courseeventsfrom[$courseid]->context)) { // SHOULD be set MDL-11221 |
8397874d | 1356 | if (is_object($courseeventsfrom[$courseid])) { |
1357 | $courseeventsfrom[$courseid]->context = get_context_instance(CONTEXT_COURSE, $courseid); | |
1358 | } | |
37875ac5 | 1359 | } |
1360 | ||
257e3f4c | 1361 | // If the user is an editing teacher in there, |
4f0c2d00 | 1362 | if (isloggedin() && isset($courseeventsfrom[$courseid]->context) && has_capability('moodle/calendar:manageentries', $courseeventsfrom[$courseid]->context)) { |
69762501 | 1363 | // If this course has groups, show events from all of them |
b8227b88 | 1364 | if(is_int($groupeventsfrom)) { |
37875ac5 | 1365 | if (is_object($courseeventsfrom[$courseid])) { // SHOULD be set MDL-11221 |
1366 | $courserecord = $courseeventsfrom[$courseid]; | |
1367 | } else { | |
62d11d77 | 1368 | $courserecord = $DB->get_record('course', array('id'=>$courseid)); |
aa6c1ced | 1369 | } |
62d11d77 | 1370 | $courserecord = $DB->get_record('course', array('id'=>$courseid)); |
b8227b88 | 1371 | if ($courserecord->groupmode != NOGROUPS || !$courserecord->groupmodeforce) { |
1372 | $groupids[] = $courseid; | |
1373 | } | |
1374 | } | |
1375 | else if(isset($SESSION->cal_courses_shown[$courseid]) && ($SESSION->cal_courses_shown[$courseid]->groupmode != NOGROUPS || !$SESSION->cal_courses_shown[$courseid]->groupmodeforce)) { | |
37d87d11 | 1376 | $groupids[] = $courseid; |
257e3f4c | 1377 | } |
1378 | } | |
69762501 | 1379 | |
1380 | // Otherwise (not editing teacher) show events from the group he is a member of | |
257e3f4c | 1381 | else if(isset($USER->groupmember[$courseid])) { |
fa22fd5f | 1382 | //changed to 2D array |
1383 | foreach ($USER->groupmember[$courseid] as $groupid){ | |
1384 | $grouparray[] = $groupid; | |
1385 | } | |
7423f116 | 1386 | } |
1387 | } | |
e295df44 | 1388 | |
6b4aeb31 | 1389 | if (!empty($groupids)) { |
f6ee5e04 | 1390 | $sql = "SELECT * |
62d11d77 | 1391 | FROM {groups} |
6b4aeb31 | 1392 | WHERE courseid IN (".implode(',', $groupids).')'; |
e295df44 | 1393 | |
62d11d77 | 1394 | if ($grouprecords = $DB->get_records_sql($sql, null)) { |
f6ee5e04 | 1395 | foreach ($grouprecords as $grouprecord) { |
1396 | $grouparray[] = $grouprecord->id; | |
1397 | } | |
6b4aeb31 | 1398 | } |
1399 | } | |
e295df44 | 1400 | |
257e3f4c | 1401 | if(empty($grouparray)) { |
1402 | $group = false; | |
1403 | } | |
1404 | else { | |
1405 | $group = $grouparray; | |
6c9584d1 | 1406 | } |
7423f116 | 1407 | } |
e295df44 | 1408 | |
7423f116 | 1409 | } |
1410 | else { | |
1411 | $group = false; | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | function calendar_edit_event_allowed($event) { | |
62d11d77 | 1416 | global $USER, $DB; |
7423f116 | 1417 | |
2ac8da76 | 1418 | // Must be logged in |
1419 | if (!isloggedin()) { | |
1420 | return false; | |
1421 | } | |
1422 | ||
89491dbd | 1423 | // can not be using guest account |
2396a414 | 1424 | if (isguestuser()) { |
e295df44 | 1425 | return false; |
89491dbd | 1426 | } |
e295df44 | 1427 | |
ef35441d | 1428 | $sitecontext = get_context_instance(CONTEXT_SYSTEM); |
89491dbd | 1429 | // if user has manageentries at site level, return true |
28ee98c5 | 1430 | if (has_capability('moodle/calendar:manageentries', $sitecontext)) { |
89491dbd | 1431 | return true; |
f52f7413 | 1432 | } |
e295df44 | 1433 | |
c0a2c361 | 1434 | // if groupid is set, it's definitely a group event |
438e4add | 1435 | if (!empty($event->groupid)) { |
f63d2922 | 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 | |
62d11d77 | 1439 | $group = $DB->get_record('groups', array('id'=>$event->groupid)); |
f63d2922 | 1440 | return $group && ( |
f4700b91 RW |
1441 | has_capability('moodle/calendar:manageentries', $event->context) || |
1442 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
f63d2922 | 1443 | && groups_is_member($event->groupid))); |
438e4add | 1444 | } else if (!empty($event->courseid)) { |
c0a2c361 | 1445 | // if groupid is not set, but course is set, |
1446 | // it's definiely a course event | |
f4700b91 | 1447 | return has_capability('moodle/calendar:manageentries', $event->context); |
438e4add | 1448 | } else if (!empty($event->userid) && $event->userid == $USER->id) { |
c0a2c361 | 1449 | // if course is not set, but userid id set, it's a user event |
f4700b91 RW |
1450 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
1451 | } else if (!empty($event->userid)) { | |
1452 | return (has_capability('moodle/calendar:manageentries', $event->context)); | |
e295df44 | 1453 | } |
7423f116 | 1454 | return false; |
1455 | } | |
1456 | ||
8c54cec6 | 1457 | function calendar_get_default_courses($ignoreref = false) { |
62d11d77 | 1458 | global $USER, $CFG, $SESSION, $DB; |
9ff136e5 | 1459 | |
8c54cec6 | 1460 | if(!empty($SESSION->cal_course_referer) && !$ignoreref) { |
4c7d1137 | 1461 | return array($SESSION->cal_course_referer => 1); |
9ff136e5 | 1462 | } |
7423f116 | 1463 | |
4f0c2d00 | 1464 | if (!isloggedin()) { |
2ef75eee | 1465 | return array(); |
1466 | } | |
1467 | ||
7423f116 | 1468 | $courses = array(); |
d9d16e56 | 1469 | if (has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_SYSTEM))) { |
03bb25e1 | 1470 | if (!empty($CFG->calendar_adminseesall)) { |
62d11d77 | 1471 | $courses = $DB->get_records_sql('SELECT id, 1 FROM {course}'); |
86f092d2 | 1472 | return $courses; |
1473 | } | |
95a89225 | 1474 | } |
e295df44 | 1475 | |
df997f84 | 1476 | $courses = enrol_get_my_courses(); |
37d87d11 | 1477 | |
1478 | return $courses; | |
7423f116 | 1479 | } |
1480 | ||
1e1ff33b | 1481 | function calendar_preferences_button() { |
1482 | global $CFG, $USER; | |
7423f116 | 1483 | |
1484 | // Guests have no preferences | |
4f0c2d00 | 1485 | if (!isloggedin() || isguestuser()) { |
7423f116 | 1486 | return ''; |
1487 | } | |
1488 | ||
4aea3cc7 | 1489 | return "<form method=\"get\" ". |
1e1ff33b | 1490 | " action=\"$CFG->wwwroot/calendar/preferences.php\">". |
2aab6488 | 1491 | "<div><input type=\"submit\" value=\"".get_string("preferences", "calendar")." ...\" /></div></form>"; |
7423f116 | 1492 | } |
1493 | ||
0f927f1e | 1494 | function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime=0) { |
8f896582 | 1495 | $startdate = usergetdate($event->timestart); |
1496 | $enddate = usergetdate($event->timestart + $event->timeduration); | |
1497 | $usermidnightstart = usergetmidnight($event->timestart); | |
1498 | ||
1499 | if($event->timeduration) { | |
1500 | // To avoid doing the math if one IF is enough :) | |
1501 | $usermidnightend = usergetmidnight($event->timestart + $event->timeduration); | |
1502 | } | |
1503 | else { | |
1504 | $usermidnightend = $usermidnightstart; | |
1505 | } | |
1506 | ||
0f927f1e SH |
1507 | if (empty($linkparams) || !is_array($linkparams)) { |
1508 | $linkparams = array(); | |
1509 | } | |
1510 | $linkparams['view'] = 'day'; | |
1511 | ||
8f896582 | 1512 | // OK, now to get a meaningful display... |
1513 | // First of all we have to construct a human-readable date/time representation | |
1514 | ||
b4892fa2 | 1515 | if($event->timeduration) { |
8f896582 | 1516 | // It has a duration |
b4892fa2 | 1517 | if($usermidnightstart == $usermidnightend || |
1518 | ($event->timestart == $usermidnightstart) && ($event->timeduration == 86400 || $event->timeduration == 86399) || | |
1519 | ($event->timestart + $event->timeduration <= $usermidnightstart + 86400)) { | |
8f896582 | 1520 | // But it's all on the same day |
8f896582 | 1521 | $timestart = calendar_time_representation($event->timestart); |
1522 | $timeend = calendar_time_representation($event->timestart + $event->timeduration); | |
b4892fa2 | 1523 | $time = $timestart.' <strong>»</strong> '.$timeend; |
1524 | ||
1525 | if ($event->timestart == $usermidnightstart && ($event->timeduration == 86400 || $event->timeduration == 86399)) { | |
1526 | $time = get_string('allday', 'calendar'); | |
1527 | } | |
8f896582 | 1528 | |
1529 | // Set printable representation | |
b4892fa2 | 1530 | if (!$showtime) { |
1531 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
0f927f1e SH |
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; | |
b4892fa2 | 1534 | } else { |
1535 | $eventtime = $time; | |
1536 | } | |
1537 | } else { | |
8f896582 | 1538 | // It spans two or more days |
b4892fa2 | 1539 | $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords).', '; |
1540 | if ($showtime == $usermidnightstart) { | |
1541 | $daystart = ''; | |
1542 | } | |
8f896582 | 1543 | $timestart = calendar_time_representation($event->timestart); |
b4892fa2 | 1544 | $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords).', '; |
1545 | if ($showtime == $usermidnightend) { | |
1546 | $dayend = ''; | |
1547 | } | |
8f896582 | 1548 | $timeend = calendar_time_representation($event->timestart + $event->timeduration); |
1549 | ||
1550 | // Set printable representation | |
b4892fa2 | 1551 | if ($now >= $usermidnightstart && $now < ($usermidnightstart + 86400)) { |
0f927f1e SH |
1552 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']); |
1553 | $eventtime = $timestart.' <strong>»</strong> '.html_writer::link($url, $dayend).$timeend; | |
b4892fa2 | 1554 | } else { |
0f927f1e SH |
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>»</strong> '; | |
1d5bd3d2 | 1557 | |
0f927f1e SH |
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; | |
b4892fa2 | 1560 | } |
8f896582 | 1561 | } |
b4892fa2 | 1562 | } else { |
1c4bedd7 | 1563 | $time = ' '; |
8f896582 | 1564 | |
1565 | // Set printable representation | |
b4892fa2 | 1566 | if (!$showtime) { |
1567 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
0f927f1e SH |
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); | |
b4892fa2 | 1570 | } else { |
1571 | $eventtime = $time; | |
1572 | } | |
1573 | } | |
1574 | ||
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>'; | |
8f896582 | 1578 | } |
09d36284 | 1579 | |
8f896582 | 1580 | return $eventtime; |
1581 | } | |
054193be | 1582 | |
86f092d2 | 1583 | function calendar_print_month_selector($name, $selected) { |
86f092d2 | 1584 | $months = array(); |
86f092d2 | 1585 | for ($i=1; $i<=12; $i++) { |
76ab1c33 | 1586 | $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B'); |
86f092d2 | 1587 | } |
d776d59e | 1588 | echo html_writer::select($months, $name, $selected, false); |
86f092d2 | 1589 | } |
1590 | ||
054193be | 1591 | function calendar_get_filters_status() { |
1592 | global $SESSION; | |
1593 | ||
1594 | $status = 0; | |
1595 | if($SESSION->cal_show_global) { | |
1596 | $status += 1; | |
1597 | } | |
1598 | if($SESSION->cal_show_course) { | |
1599 | $status += 2; | |
1600 | } | |
1601 | if($SESSION->cal_show_groups) { | |
1602 | $status += 4; | |
1603 | } | |
1604 | if($SESSION->cal_show_user) { | |
1605 | $status += 8; | |
1606 | } | |
1607 | return $status; | |
1608 | } | |
1609 | ||
1610 | function calendar_set_filters_status($packed_bitfield) { | |
1611 | global $SESSION, $USER; | |
1612 | ||
4f0c2d00 | 1613 | if (!isloggedin()) { |
054193be | 1614 | return false; |
1615 | } | |
1616 | ||
1617 | $SESSION->cal_show_global = ($packed_bitfield & 1); | |
1618 | $SESSION->cal_show_course = ($packed_bitfield & 2); | |
e295df44 | 1619 | $SESSION->cal_show_groups = ($packed_bitfield & 4); |
054193be | 1620 | $SESSION->cal_show_user = ($packed_bitfield & 8); |
1621 | ||
1622 | return true; | |
1623 | } | |
1624 | ||
86ac8b24 | 1625 | function calendar_get_allowed_types(&$allowed) { |
62d11d77 | 1626 | global $USER, $CFG, $SESSION, $DB; |
86ac8b24 | 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)); | |
1632 | ||
f63d2922 | 1633 | if(!empty($SESSION->cal_course_referer) && $SESSION->cal_course_referer != SITEID) { |
62d11d77 | 1634 | $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer)); |
f63d2922 | 1635 | $coursecontext = get_context_instance(CONTEXT_COURSE, $SESSION->cal_course_referer); |
86ac8b24 | 1636 | |
f63d2922 | 1637 | if(has_capability('moodle/calendar:manageentries', $coursecontext)) { |
1638 | $allowed->courses = array($course->id => 1); | |
aa6c1ced | 1639 | |
f63d2922 | 1640 | if($course->groupmode != NOGROUPS || !$course->groupmodeforce) { |
1641 | $allowed->groups = groups_get_all_groups($SESSION->cal_course_referer); | |
1642 | } | |
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); | |
1646 | } | |
86ac8b24 | 1647 | } |
1648 | } | |
1649 | } | |
1650 | ||
1651 | /** | |
1652 | * see if user can add calendar entries at all | |
1653 | * used to print the "New Event" button | |
1654 | * @return bool | |
1655 | */ | |
1656 | function calendar_user_can_add_event() { | |
1657 | calendar_get_allowed_types($allowed); | |
e295df44 | 1658 | return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->site); |
86ac8b24 | 1659 | } |
76d9df3f SH |
1660 | |
1661 | /** | |
1662 | * Check wether the current user is permitted to add events | |
1663 | * | |
1664 | * @param object $event | |
1665 | * @return bool | |
1666 | */ | |
1667 | function calendar_add_event_allowed($event) { | |
1668 | global $USER, $DB; | |
1669 | ||
1670 | // can not be using guest account | |
4f0c2d00 | 1671 | if (!isloggedin() or isguestuser()) { |
76d9df3f SH |
1672 | return false; |
1673 | } | |
1674 | ||
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; | |
1679 | } | |
1680 | ||
1681 | switch ($event->eventtype) { | |
1682 | case 'course': | |
f4700b91 | 1683 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1684 | |
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 && ( | |
f4700b91 RW |
1691 | has_capability('moodle/calendar:manageentries', $event->context) || |
1692 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
76d9df3f SH |
1693 | && groups_is_member($event->groupid))); |
1694 | ||
1695 | case 'user': | |
1696 | if ($event->userid == $USER->id) { | |
f4700b91 | 1697 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
76d9df3f SH |
1698 | } |
1699 | //there is no 'break;' intentionally | |
1700 | ||
1701 | case 'site': | |
f4700b91 | 1702 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1703 | |
1704 | default: | |
f4700b91 | 1705 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1706 | } |
1707 | } | |
1708 | ||
1709 | /** | |
1710 | * A class to manage calendar events | |
1711 | * | |
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 | |
1716 | * | |
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 | |
1735 | */ | |
1736 | class calendar_event { | |
1737 | ||
1738 | /** | |
1739 | * An object containing the event properties can be accessed via the | |
1740 | * magic __get/set methods | |
1741 | * @var array | |
1742 | */ | |
1743 | protected $properties = null; | |
1744 | /** | |
1745 | * The converted event discription with file paths resolved | |
1746 | * This gets populated when someone requests description for the first time | |
1747 | * @var string | |
1748 | */ | |
1749 | protected $_description = null; | |
76d9df3f SH |
1750 | /** |
1751 | * The options to use with this description editor | |
1752 | * @var array | |
1753 | */ | |
1754 | protected $editoroptions = array( | |
1755 | 'subdirs'=>false, | |
1756 | 'forcehttps'=>false, | |
2b35ed1a | 1757 | 'maxfiles'=>-1, |
76d9df3f SH |
1758 | 'maxbytes'=>null, |
1759 | 'trusttext'=>false); | |
1760 | /** | |
1761 | * The context to use with the description editor | |
1762 | * @var object | |
1763 | */ | |
1764 | protected $editorcontext = null; | |
1765 | ||
1766 | /** | |
1767 | * Instantiates a new event and optionally populates its properties with the | |
1768 | * data provided | |
1769 | * | |
1770 | * @param stdClass $data Optional. An object containing the properties to for | |
1771 | * an event | |
1772 | */ | |
1773 | public function __construct($data=null) { | |
c203a277 | 1774 | global $CFG, $USER; |
76d9df3f SH |
1775 | |
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; | |
1779 | } | |
1780 | ||
1781 | $this->editoroptions['maxbytes'] = $CFG->maxbytes; | |
1782 | ||
1783 | $data->eventrepeats = 0; | |
1784 | ||
1785 | if (empty($data->id)) { | |
1786 | $data->id = null; | |
1787 | } | |
1788 | ||
c203a277 SH |
1789 | // Default to a user event |
1790 | if (empty($data->eventtype)) { | |
1791 | $data->eventtype = 'user'; | |
1792 | } | |
1793 | ||
1794 | // Default to the current user | |
1795 | if (empty($data->userid)) { | |
1796 | $data->userid = $USER->id; | |
1797 | } | |
1798 | ||
76d9df3f SH |
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; | |
1801 | } | |
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 = ''; | |
20e5da7d | 1807 | $data->format = editors_get_preferred_format(); |
76d9df3f | 1808 | } |
c203a277 SH |
1809 | // Ensure form is defaulted correctly |
1810 | if (empty($data->format)) { | |
1811 | $data->format = editors_get_preferred_format(); | |
1812 | } | |
76d9df3f | 1813 | |
f4700b91 RW |
1814 | if (empty($data->context)) { |
1815 | $data->context = $this->calculate_context($data); | |
1816 | } | |
76d9df3f SH |
1817 | $this->properties = $data; |
1818 | } | |
1819 | ||
1820 | /** | |
1821 | * Magic property method | |
1822 | * | |
1823 | * Attempts to call a set_$key method if one exists otherwise falls back | |
1824 | * to simply set the property | |
1825 | * | |
1826 | * @param string $key | |
1827 | * @param mixed $value | |
1828 | */ | |
1829 | public function __set($key, $value) { | |
1830 | if (method_exists($this, 'set_'.$key)) { | |
1831 | $this->{'set_'.$key}($value); | |
1832 | } | |
1833 | $this->properties->{$key} = $value; | |
1834 | } | |
1835 | ||
1836 | /** | |
1837 | * Magic get method | |
1838 | * | |
1839 | * Attempts to call a get_$key method to return the property and ralls over | |
1840 | * to return the raw property | |
1841 | * | |
1842 | * @param str $key | |
1843 | * @return mixed | |
1844 | */ | |
1845 | public function __get($key) { | |
1846 | if (method_exists($this, 'get_'.$key)) { | |
1847 | return $this->{'get_'.$key}(); | |
1848 | } | |
c203a277 SH |
1849 | if (!isset($this->properties->{$key})) { |
1850 | throw new coding_exception('Undefined property requested'); | |
1851 | } | |
76d9df3f SH |
1852 | return $this->properties->{$key}; |
1853 | } | |
1854 | ||
1855 | /** | |
1856 | * Stupid PHP needs an isset magic method if you use the get magic method and | |
1857 | * still want empty calls to work.... blah ~! | |
1858 | * | |
1859 | * @param string $key | |
1860 | * @return bool | |
1861 | */ | |
1862 | public function __isset($key) { | |
1863 | return !empty($this->properties->{$key}); | |
1864 | } | |
1865 | ||
f4700b91 RW |
1866 | /** |
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 | |
1872 | * | |
1873 | * @return stdClass | |
1874 | */ | |
1875 | protected function calculate_context(stdClass $data) { | |
1876 | global $USER; | |
1877 | ||
f4700b91 RW |
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) { | |
18d63ffb | 1890 | $cm = get_coursemodule_from_instance($data->modulename, $data->instance, 0, false, MUST_EXIST); |
f4700b91 RW |
1891 | $context = get_context_instance(CONTEXT_COURSE, $cm->course); |
1892 | } else { | |
1893 | $context = get_context_instance(CONTEXT_USER); | |
1894 | } | |
1895 | ||
1896 | return $context; | |
1897 | } | |
1898 | ||
76d9df3f SH |
1899 | /** |
1900 | * Returns an array of editoroptions for this event: Called by __get | |
1901 | * Please use $blah = $event->editoroptions; | |
1902 | * @return array | |
1903 | */ | |
1904 | protected function get_editoroptions() { | |
1905 | return $this->editoroptions; | |
1906 | } | |
1907 | ||
1908 | /** | |
1909 | * Returns an event description: Called by __get | |
1910 | * Please use $blah = $event->description; | |
1911 | * | |
1912 | * @return string | |
1913 | */ | |
1914 | protected function get_description() { | |
f4700b91 | 1915 | global $CFG; |
99d19c13 PS |
1916 | |
1917 | require_once($CFG->libdir . '/filelib.php'); | |
1918 | ||
76d9df3f SH |
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 | |
f4700b91 RW |
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); | |
76d9df3f SH |
1928 | } |
1929 | } | |
1930 | ||
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; | |
1937 | } | |
1938 | ||
1939 | // Convert file paths in the description so that things display correctly | |
64f93798 | 1940 | $this->_description = file_rewrite_pluginfile_urls($this->properties->description, 'pluginfile.php', $this->editorcontext->id, 'calendar', 'event_description', $itemid); |
76d9df3f SH |
1941 | // Clean the text so no nasties get through |
1942 | $this->_description = clean_text($this->_description, $this->properties->format); | |
1943 | } | |
1944 | // Finally return the description | |
1945 | return $this->_description; | |
1946 | } | |
1947 | ||
1948 | /** | |
1949 | * Return the number of repeat events there are in this events series | |
aa6c1ced | 1950 | * |
76d9df3f SH |
1951 | * @return int |
1952 | */ | |
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--; | |
1959 | } | |
1960 | return $this->properties->eventrepeats; | |
1961 | } | |
1962 | ||
1963 | /** | |
1964 | * Update or create an event within the database | |
1965 | * | |
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 | |
1968 | * | |
1969 | * @see add_event() | |
1970 | * @see update_event() | |
1971 | * | |
1972 | * @param stdClass $data | |
1d5bd3d2 | 1973 | * @param boolean $checkcapability if moodle should check calendar managing capability or not |
76d9df3f | 1974 | */ |
1d5bd3d2 | 1975 | public function update($data, $checkcapability=true) { |
76d9df3f SH |
1976 | global $CFG, $DB, $USER; |
1977 | ||
438e4add SH |
1978 | foreach ($data as $key=>$value) { |
1979 | $this->properties->$key = $value; | |
1980 | } | |
1981 | ||
76d9df3f SH |
1982 | $this->properties->timemodified = time(); |
1983 | $usingeditor = (!empty($this->properties->description) && is_array($this->properties->description)); | |
1984 | ||
1985 | if (empty($this->properties->id) || $this->properties->id < 1) { | |
1986 | ||
1d5bd3d2 DC |
1987 | if ($checkcapability) { |
1988 | if (!calendar_add_event_allowed($this->properties)) { | |
1989 | print_error('nopermissiontoupdatecalendar'); | |
1990 | } | |
76d9df3f SH |
1991 | } |
1992 | ||
1993 | if ($usingeditor) { | |
1994 | switch ($this->properties->eventtype) { | |
1995 | case 'user': | |
f4700b91 | 1996 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
1997 | $this->properties->courseid = 0; |
1998 | $this->properties->groupid = 0; | |
1999 | $this->properties->userid = $USER->id; | |
2000 | break; | |
2001 | case 'site': | |
f4700b91 | 2002 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2003 | $this->properties->courseid = SITEID; |
2004 | $this->properties->groupid = 0; | |
2005 | $this->properties->userid = $USER->id; | |
2006 | break; | |
2007 | case 'course': | |
f4700b91 | 2008 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2009 | $this->properties->groupid = 0; |
2010 | $this->properties->userid = $USER->id; | |
2011 | break; | |
2012 | case 'group': | |
f4700b91 | 2013 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
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; | |
2021 | } | |
2022 | ||
2023 | $editor = $this->properties->description; | |
2024 | $this->properties->format = $this->properties->description['format']; | |
2025 | $this->properties->description = $this->properties->description['text']; | |
2026 | } | |
2027 | ||
2028 | // Insert the event into the database | |
2029 | $this->properties->id = $DB->insert_record('event', $this->properties); | |
2030 | ||
2031 | if ($usingeditor) { | |
2032 | $this->properties->description = file_save_draft_area_files( | |
2033 | $editor['itemid'], | |
2034 | $this->editorcontext->id, | |
64f93798 PS |
2035 | 'calendar', |
2036 | 'event_description', | |
76d9df3f SH |
2037 | $this->properties->id, |
2038 | $this->editoroptions, | |
2039 | $editor['text'], | |
2040 | $this->editoroptions['forcehttps']); | |
2041 | ||
2042 | $DB->set_field('event', 'description', $this->properties->description, array('id'=>$this->properties->id)); | |
2043 | } | |
aa6c1ced | 2044 | |
76d9df3f SH |
2045 | // Log the event entry. |
2046 | add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); | |
2047 | ||
2048 | $repeatedids = array(); | |
2049 | ||
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)); | |
2053 | ||
2054 | $eventcopy = clone($this->properties); | |
2055 | unset($eventcopy->id); | |
2056 | ||
2057 | for($i = 1; $i < $eventcopy->repeats; $i++) { | |
2058 | ||
2059 | $eventcopy->timestart = ($eventcopy->timestart+WEEKSECS) + dst_offset_on($eventcopy->timestart) - dst_offset_on($eventcopy->timestart+WEEKSECS); | |
2060 | ||
2061 | // Get the event id for the log record. | |
2062 | $eventcopyid = $DB->insert_record('event', $eventcopy); | |
2063 | ||
2064 | // If the context has been set delete all associated files | |
2065 | if ($usingeditor) { | |
2066 | $fs = get_file_storage(); | |
64f93798 | 2067 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2068 | foreach ($files as $file) { |
2069 | $fs->create_file_from_storedfile(array('itemid'=>$eventcopyid), $file); | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | $repeatedids[] = $eventcopyid; | |
2074 | // Log the event entry. | |
2075 | add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventcopyid, $eventcopy->name); | |
2076 | } | |
2077 | } | |
2078 | ||
2079 | // Hook for tracking added events | |
2080 | self::calendar_event_hook('add_event', array($this->properties, $repeatedids)); | |
2081 | return true; | |
2082 | } else { | |
2083 | ||
1d5bd3d2 DC |
2084 | if ($checkcapability) { |
2085 | if(!calendar_edit_event_allowed($this->properties)) { | |
2086 | print_error('nopermissiontoupdatecalendar'); | |
2087 | } | |
76d9df3f SH |
2088 | } |
2089 | ||
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, | |
64f93798 PS |
2095 | 'calendar', |
2096 | 'event_description', | |
76d9df3f SH |
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']; | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | $event = $DB->get_record('event', array('id'=>$this->properties->id)); | |
2108 | ||
2109 | $updaterepeated = (!empty($this->properties->repeatid) && !empty($this->properties->repeateditall)); | |
2110 | ||
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); | |
2126 | } | |
2127 | $DB->execute($sql, $params); | |
2128 | ||
2129 | // Log the event update. | |
2130 | add_to_log($this->properties->courseid, 'calendar', 'edit all', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); | |
2131 | } else { | |
2132 | $DB->update_record('event', $this->properties); | |
ddaff608 SH |
2133 | $event = calendar_event::load($this->properties->id); |
2134 | $this->properties = $event->properties(); | |
76d9df3f SH |
2135 | add_to_log($this->properties->courseid, 'calendar', 'edit', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); |
2136 | } | |
2137 | ||
2138 | // Hook for tracking event updates | |
2139 | self::calendar_event_hook('update_event', array($this->properties, $updaterepeated)); | |
2140 | return true; | |
2141 | } | |
2142 | } | |
2143 | ||
2144 | /** | |
2145 | * Deletes an event and if selected an repeated events in the same series | |
2146 | * | |
2147 | * This function deletes an event, any associated events if $deleterepeated=true, | |
2148 | * and cleans up any files associated with the events. | |
2149 | * | |
2150 | * @see delete_event() | |
2151 | * | |
2152 | * @param bool $deleterepeated | |
2153 | * @return bool | |
2154 | */ | |
2155 | public function delete($deleterepeated=false) { | |
f4700b91 | 2156 | global $DB; |
76d9df3f SH |
2157 | |
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; | |
2162 | } | |
2163 | ||
2164 | // Delete the event | |
2165 | $DB->delete_records('event', array('id'=>$this->properties->id)); | |
2166 | ||
2167 | // If the editor context hasn't already been set then set it now | |
2168 | if ($this->editorcontext === null) { | |
f4700b91 | 2169 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2170 | } |
2171 | ||
2172 | // If the context has been set delete all associated files | |
2173 | if ($this->editorcontext !== null) { | |
2174 | $fs = get_file_storage(); | |
64f93798 | 2175 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2176 | foreach ($files as $file) { |
2177 | $file->delete(); | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | // Fire the event deleted hook | |
2182 | self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated)); | |
2183 | ||
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); | |
2193 | } | |
2194 | } | |
2195 | ||
2196 | return true; | |
2197 | } | |
2198 | ||
2199 | /** | |
2200 | * Fetch all event properties | |
2201 | * | |
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. | |
2206 | * | |
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 | |
2210 | */ | |
2211 | public function properties($prepareeditor=false) { | |
2212 | global $USER, $CFG, $DB; | |
2213 | ||
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); | |
2220 | ||
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) { | |
2224 | ||
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)) { | |
2228 | ||
2229 | if ($properties->eventtype === 'site') { | |
2230 | // Site context | |
f4700b91 | 2231 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2232 | } else if ($properties->eventtype === 'user') { |
2233 | // User context | |
f4700b91 | 2234 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
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'); | |
2240 | } | |
2241 | // Course context | |
f4700b91 | 2242 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
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; | |
2252 | } | |
2253 | ||
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; | |
2259 | } | |
2260 | } else { | |
2261 | ||
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; | |
2265 | } | |
2266 | ||
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 | |
64f93798 | 2273 | $properties->description = file_prepare_draft_area($draftiddescription, $contextid, 'calendar', 'event_description', $properties->id, $this->editoroptions, $properties->description); |
76d9df3f SH |
2274 | } else { |
2275 | $draftiddescription = 0; | |
2276 | } | |
aa6c1ced | 2277 | |
76d9df3f SH |
2278 | // Structure the description field as the editor requires |
2279 | $properties->description = array('text'=>$properties->description, 'format'=>$properties->format, 'itemid'=>$draftiddescription); | |
2280 | } | |
2281 | ||
2282 | // Finally return the properties | |
2283 | return $properties; | |
2284 | } | |
2285 | ||
2286 | /** | |
2287 | * Toggles the visibility of an event | |
2288 | * | |
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. | |
2292 | */ | |
2293 | public function toggle_visibility($force=null) { | |
2294 | global $CFG, $DB; | |
2295 | ||
2296 | // Set visible to the default if it is not already set | |
2297 | if (empty($this->properties->visible)) { | |
2298 | $this->properties->visible = 1; | |
2299 | } | |
2300 | ||
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)); | |
2311 | } | |
2312 | ||
2313 | // Update the database to reflect this change | |
2314 | return $DB->set_field('event', 'visible', $this->properties->visible, array('id'=>$this->properties->id)); | |
2315 | } | |
2316 | ||
2317 | /** | |
2318 | * Attempts to call the hook for the specified action should a calendar type | |
2319 | * by set $CFG->calendar, and the appopriate function defined | |
2320 | * | |
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 | |
2326 | */ | |
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; | |
2336 | } | |
2337 | } | |
2338 | if($extcalendarinc === false) { | |
2339 | return false; | |
2340 | } | |
438e4add | 2341 | $hook = $CFG->calendar .'_'.$action; |
76d9df3f SH |
2342 | if (function_exists($hook)) { |
2343 | call_user_func_array($hook, $args); | |
2344 | return true; | |
2345 | } | |
2346 | return false; | |
2347 | } | |
2348 | ||
2349 | /** | |
2350 | * Returns a calendar_event object when provided with an event id | |
2351 | * | |
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 | |
2354 | * | |
fd699564 | 2355 | * @param int|object $param |
76d9df3f SH |
2356 | * @return calendar_event|false |
2357 | */ | |
fd699564 | 2358 | public static function load($param) { |
76d9df3f | 2359 | global $DB; |
fd699564 SH |
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); | |
2365 | } | |
76d9df3f SH |
2366 | return $event; |
2367 | } | |
2368 | ||
2369 | /** | |
2370 | * Creates a new event and returns a calendar_event object | |
2371 | * | |
2372 | * @param object|array $properties An object containing event properties | |
2373 | * @return calendar_event|false The event object or false if it failed | |
2374 | */ | |
2375 | public static function create($properties) { | |
2376 | if (is_array($properties)) { | |
2377 | $properties = (object)$properties; | |
2378 | } | |
2379 | if (!is_object($properties)) { | |
2380 | throw new coding_exception('When creating an event properties should be either an object or an assoc array'); | |
2381 | } | |
f4700b91 | 2382 | $event = new calendar_event($properties); |
76d9df3f SH |
2383 | if ($event->update($properties)) { |
2384 | return $event; | |
2385 | } else { | |
2386 | return false; | |
2387 | } | |
2388 | } | |
2389 | } | |
36dc3b71 SH |
2390 | |
2391 | /** | |
2392 | * Calendar information class | |
2393 | * | |
2394 | * This class is used simply to organise the information pertaining to a calendar | |
2395 | * and is used primarily to make information easily available. | |
2396 | */ | |
2397 | class calendar_information { | |
2398 | /** | |
2399 | * The day | |
2400 | * @var int | |
2401 | */ | |
2402 | public $day; | |
2403 | /** | |
2404 | * The month | |
2405 | * @var int | |
2406 | */ | |
2407 | public $month; | |
2408 | /** | |
2409 | * The year | |
2410 | * @var int | |
2411 | */ | |
2412 | public $year; | |
2413 | ||
2414 | /** | |
2415 | * A course id | |
2416 | * @var int | |
2417 | */ | |
2418 | public $courseid = null; | |
2419 | /** | |
2420 | * An array of courses | |
2421 | * @var array | |
2422 | */ | |
2423 | public $courses = array(); | |
2424 | /** | |
2425 | * An array of groups | |
2426 | * @var array | |
2427 | */ | |
2428 | public $groups = array(); | |
2429 | /** | |
2430 | * An array of users | |
2431 | * @var array | |
2432 | */ | |
2433 | public $users = array(); | |
2434 | ||
2435 | /** | |
2436 | * Creates a new instance | |
2437 | * | |
2438 | * @param int $day | |
2439 | * @param int $month | |
2440 | * @param int $year | |
2441 | */ | |
d8d8bdd9 SH |
2442 | public function __construct($day=0, $month=0, $year=0) { |
2443 | ||
2444 | $date = usergetdate(time()); | |
2445 | ||
2446 | if (empty($day)) { | |
2447 | $day = $date['mday']; | |
2448 | } | |
2449 | ||
2450 | if (empty($month)) { | |
2451 | $month = $date['mon']; | |
2452 | } | |
2453 | ||
2454 | if (empty($year)) { | |
2455 | $year = $date['year']; | |
2456 | } | |
2457 | ||
36dc3b71 SH |
2458 | $this->day = $day; |
2459 | $this->month = $month; | |
2460 | $this->year = $year; | |
2461 | } | |
2462 | ||
2463 | /** | |
2464 | * Ensures the date for the calendar is correct and either sets it to now | |
2465 | * or throws a moodle_exception if not | |
2466 | * | |
2467 | * @param bool $defaultonow | |
2468 | * @return bool | |
2469 | */ | |
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'); | |
2480 | } | |
2481 | } | |
2482 | return true; | |
2483 | } | |
2484 | /** | |
2485 | * Gets todays timestamp for the calendar | |
2486 | * @return int | |
2487 | */ | |
2488 | public function timestamp_today() { | |
2489 | return make_timestamp($this->year, $this->month, $this->day); | |
2490 | } | |
2491 | /** | |
2492 | * Gets tomorrows timestamp for the calendar | |
2493 | * @return int | |
2494 | */ | |
2495 | public function timestamp_tomorrow() { | |
2496 | return make_timestamp($this->year, $this->month, $this->day+1); | |
2497 | } | |
2498 | /** | |
2499 | * Adds the pretend blocks for teh calendar | |
2500 | * | |
2501 | * @param core_calendar_renderer $renderer | |
2502 | * @param bool $showfilters | |
2503 | * @param string|null $view | |
2504 | */ | |
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); | |
2512 | } | |
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); | |
2518 | } | |
1d5bd3d2 | 2519 | } |