Commit | Line | Data |
---|---|---|
93c91ee4 | 1 | <?php |
08b4a4e1 RW |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
7423f116 | 16 | |
08b4a4e1 RW |
17 | /** |
18 | * Calendar extension | |
19 | * | |
20 | * @package core_calendar | |
21 | * @copyright 2004 Greek School Network (http://www.sch.gr), Jon Papaioannou, | |
22 | * Avgoustos Tsinakos | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | /** | |
27 | * These are read by the administration component to provide default values | |
28 | */ | |
29 | ||
30 | /** | |
31 | * CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD - default value of upcoming event preference | |
32 | */ | |
bb4a2e85 | 33 | define('CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD', 21); |
08b4a4e1 RW |
34 | |
35 | /** | |
36 | * CALENDAR_DEFAULT_UPCOMING_MAXEVENTS - default value to display the maximum number of upcoming event | |
37 | */ | |
bb4a2e85 | 38 | define('CALENDAR_DEFAULT_UPCOMING_MAXEVENTS', 10); |
08b4a4e1 RW |
39 | |
40 | /** | |
41 | * CALENDAR_DEFAULT_STARTING_WEEKDAY - default value to display the starting weekday | |
42 | */ | |
43 | define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1); | |
44 | ||
bb4a2e85 | 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 | |
08b4a4e1 RW |
47 | |
48 | /** | |
49 | * CALENDAR_DEFAULT_WEEKEND - default value for weekend (Saturday & Sunday) | |
50 | */ | |
51 | define('CALENDAR_DEFAULT_WEEKEND', 65); | |
52 | ||
53 | /** | |
54 | * CALENDAR_URL - path to calendar's folder | |
55 | */ | |
76d9df3f | 56 | define('CALENDAR_URL', $CFG->wwwroot.'/calendar/'); |
08b4a4e1 RW |
57 | |
58 | /** | |
59 | * CALENDAR_TF_24 - Calendar time in 24 hours format | |
60 | */ | |
76d9df3f | 61 | define('CALENDAR_TF_24', '%H:%M'); |
08b4a4e1 RW |
62 | |
63 | /** | |
64 | * CALENDAR_TF_12 - Calendar time in 12 hours format | |
65 | */ | |
76d9df3f | 66 | define('CALENDAR_TF_12', '%I:%M %p'); |
bb4a2e85 | 67 | |
08b4a4e1 RW |
68 | /** |
69 | * CALENDAR_EVENT_GLOBAL - Global calendar event types | |
70 | */ | |
797cedc7 | 71 | define('CALENDAR_EVENT_GLOBAL', 1); |
08b4a4e1 RW |
72 | |
73 | /** | |
74 | * CALENDAR_EVENT_COURSE - Course calendar event types | |
75 | */ | |
797cedc7 | 76 | define('CALENDAR_EVENT_COURSE', 2); |
08b4a4e1 RW |
77 | |
78 | /** | |
79 | * CALENDAR_EVENT_GROUP - group calendar event types | |
80 | */ | |
797cedc7 | 81 | define('CALENDAR_EVENT_GROUP', 4); |
08b4a4e1 RW |
82 | |
83 | /** | |
84 | * CALENDAR_EVENT_USER - user calendar event types | |
85 | */ | |
797cedc7 SH |
86 | define('CALENDAR_EVENT_USER', 8); |
87 | ||
7423f116 | 88 | |
797cedc7 SH |
89 | /** |
90 | * Return the days of the week | |
91 | * | |
08b4a4e1 | 92 | * @return array array of days |
797cedc7 SH |
93 | */ |
94 | function calendar_get_days() { | |
95 | return array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'); | |
96 | } | |
f2bffd9e | 97 | |
76d9df3f SH |
98 | /** |
99 | * Gets the first day of the week | |
100 | * | |
101 | * Used to be define('CALENDAR_STARTING_WEEKDAY', blah); | |
102 | * | |
103 | * @return int | |
104 | */ | |
105 | function calendar_get_starting_weekday() { | |
106 | global $CFG; | |
37d87d11 | 107 | |
76d9df3f SH |
108 | if (isset($CFG->calendar_startwday)) { |
109 | $firstday = $CFG->calendar_startwday; | |
110 | } else { | |
0a933ae6 | 111 | $firstday = get_string('firstdayofweek', 'langconfig'); |
76d9df3f SH |
112 | } |
113 | ||
114 | if(!is_numeric($firstday)) { | |
115 | return CALENDAR_DEFAULT_STARTING_WEEKDAY; | |
116 | } else { | |
117 | return intval($firstday) % 7; | |
118 | } | |
119 | } | |
bd119567 | 120 | |
0f927f1e | 121 | /** |
14236cbd | 122 | * Generates the HTML for a miniature calendar |
0f927f1e | 123 | * |
08b4a4e1 RW |
124 | * @param array $courses list of course |
125 | * @param array $groups list of group | |
126 | * @param array $users user's info | |
127 | * @param int $cal_month calendar month in numeric, default is set to false | |
128 | * @param int $cal_year calendar month in numeric, default is set to false | |
129 | * @return string $content return html table for mini calendar | |
0f927f1e | 130 | */ |
7423f116 | 131 | function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_year = false) { |
6b608f8f | 132 | global $CFG, $USER, $OUTPUT; |
7423f116 | 133 | |
dd97c328 | 134 | $display = new stdClass; |
76d9df3f | 135 | $display->minwday = get_user_preferences('calendar_startwday', calendar_get_starting_weekday()); |
7423f116 | 136 | $display->maxwday = $display->minwday + 6; |
137 | ||
138 | $content = ''; | |
139 | ||
140 | if(!empty($cal_month) && !empty($cal_year)) { | |
141 | $thisdate = usergetdate(time()); // Date and time the user sees at his location | |
142 | if($cal_month == $thisdate['mon'] && $cal_year == $thisdate['year']) { | |
143 | // Navigated to this month | |
144 | $date = $thisdate; | |
145 | $display->thismonth = true; | |
36dc3b71 | 146 | } else { |
7423f116 | 147 | // Navigated to other month, let's do a nice trick and save us a lot of work... |
148 | if(!checkdate($cal_month, 1, $cal_year)) { | |
149 | $date = array('mday' => 1, 'mon' => $thisdate['mon'], 'year' => $thisdate['year']); | |
150 | $display->thismonth = true; | |
36dc3b71 | 151 | } else { |
7423f116 | 152 | $date = array('mday' => 1, 'mon' => $cal_month, 'year' => $cal_year); |
153 | $display->thismonth = false; | |
154 | } | |
155 | } | |
36dc3b71 | 156 | } else { |
7423f116 | 157 | $date = usergetdate(time()); // Date and time the user sees at his location |
158 | $display->thismonth = true; | |
159 | } | |
160 | ||
161 | // Fill in the variables we 're going to use, nice and tidy | |
162 | list($d, $m, $y) = array($date['mday'], $date['mon'], $date['year']); // This is what we want to display | |
163 | $display->maxdays = calendar_days_in_month($m, $y); | |
164 | ||
d6198903 | 165 | if (get_user_timezone_offset() < 99) { |
166 | // We 'll keep these values as GMT here, and offset them when the time comes to query the db | |
61240489 | 167 | $display->tstart = gmmktime(0, 0, 0, $m, 1, $y); // This is GMT |
168 | $display->tend = gmmktime(23, 59, 59, $m, $display->maxdays, $y); // GMT | |
d6198903 | 169 | } else { |
170 | // no timezone info specified | |
61240489 | 171 | $display->tstart = mktime(0, 0, 0, $m, 1, $y); |
172 | $display->tend = mktime(23, 59, 59, $m, $display->maxdays, $y); | |
d6198903 | 173 | } |
7423f116 | 174 | |
69244b91 | 175 | $startwday = dayofweek(1, $m, $y); |
7423f116 | 176 | |
177 | // Align the starting weekday to fall in our display range | |
178 | // This is simple, not foolproof. | |
179 | if($startwday < $display->minwday) { | |
180 | $startwday += 7; | |
181 | } | |
182 | ||
c0b507d1 | 183 | // TODO: THIS IS TEMPORARY CODE! |
184 | // [pj] I was just reading through this and realized that I when writing this code I was probably | |
185 | // asking for trouble, as all these time manipulations seem to be unnecessary and a simple | |
186 | // make_timestamp would accomplish the same thing. So here goes a test: | |
1747ee05 | 187 | //$test_start = make_timestamp($y, $m, 1); |
188 | //$test_end = make_timestamp($y, $m, $display->maxdays, 23, 59, 59); | |
189 | //if($test_start != usertime($display->tstart) - dst_offset_on($display->tstart)) { | |
190 | //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); | |
191 | //} | |
192 | //if($test_end != usertime($display->tend) - dst_offset_on($display->tend)) { | |
193 | //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); | |
194 | //} | |
c0b507d1 | 195 | |
196 | ||
7423f116 | 197 | // Get the events matching our criteria. Don't forget to offset the timestamps for the user's TZ! |
8263f802 | 198 | $events = calendar_get_events( |
d3555a2f | 199 | usertime($display->tstart) - dst_offset_on($display->tstart), |
200 | usertime($display->tend) - dst_offset_on($display->tend), | |
201 | $users, $groups, $courses); | |
7423f116 | 202 | |
b4892fa2 | 203 | // Set event course class for course events |
204 | if (!empty($events)) { | |
13534ef7 | 205 | foreach ($events as $eventid => $event) { |
13534ef7 ML |
206 | if (!empty($event->modulename)) { |
207 | $cm = get_coursemodule_from_instance($event->modulename, $event->instance); | |
208 | if (!groups_course_module_visible($cm)) { | |
209 | unset($events[$eventid]); | |
210 | } | |
211 | } | |
b4892fa2 | 212 | } |
213 | } | |
6619eba4 | 214 | |
c635dcda | 215 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 216 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 217 | // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra |
218 | // arguments to this function. | |
219 | ||
0f927f1e | 220 | $hrefparams = array(); |
7bd1677c | 221 | if(!empty($courses)) { |
e749554e | 222 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 223 | if(count($courses) == 1) { |
0f927f1e | 224 | $hrefparams['course'] = reset($courses); |
7bd1677c | 225 | } |
c635dcda | 226 | } |
227 | ||
7423f116 | 228 | // We want to have easy access by day, since the display is on a per-day basis. |
229 | // Arguments passed by reference. | |
7b38bfa6 | 230 | //calendar_events_by_day($events, $display->tstart, $eventsbyday, $durationbyday, $typesbyday); |
7c50db30 | 231 | calendar_events_by_day($events, $m, $y, $eventsbyday, $durationbyday, $typesbyday, $courses); |
7423f116 | 232 | |
f20c4d02 | 233 | //Accessibility: added summary and <abbr> elements. |
797cedc7 | 234 | $days_title = calendar_get_days(); |
f20c4d02 | 235 | |
2a06efcc | 236 | $summary = get_string('calendarheading', 'calendar', userdate(make_timestamp($y, $m), get_string('strftimemonthyear'))); |
90723839 | 237 | $content .= '<table class="minicalendar calendartable" summary="'.$summary.'">'; // Begin table |
f136e4c5 | 238 | $content .= '<tr class="weekdays">'; // Header row: day names |
7423f116 | 239 | |
240 | // Print out the names of the weekdays | |
241 | $days = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'); | |
242 | for($i = $display->minwday; $i <= $display->maxwday; ++$i) { | |
243 | // This uses the % operator to get the correct weekday no matter what shift we have | |
244 | // applied to the $display->minwday : $display->maxwday range from the default 0 : 6 | |
f20c4d02 | 245 | $content .= '<th scope="col"><abbr title="'. get_string($days_title[$i % 7], 'calendar') .'">'. |
246 | get_string($days[$i % 7], 'calendar') ."</abbr></th>\n"; | |
7423f116 | 247 | } |
248 | ||
f136e4c5 | 249 | $content .= '</tr><tr>'; // End of day names; prepare for day numbers |
7423f116 | 250 | |
251 | // For the table display. $week is the row; $dayweek is the column. | |
7423f116 | 252 | $dayweek = $startwday; |
253 | ||
254 | // Paddding (the first week may have blank days in the beginning) | |
255 | for($i = $display->minwday; $i < $startwday; ++$i) { | |
3bfd8bc8 | 256 | $content .= '<td class="dayblank"> </td>'."\n"; |
7423f116 | 257 | } |
258 | ||
797cedc7 SH |
259 | $weekend = CALENDAR_DEFAULT_WEEKEND; |
260 | if (isset($CFG->calendar_weekend)) { | |
261 | $weekend = intval($CFG->calendar_weekend); | |
262 | } | |
263 | ||
7423f116 | 264 | // Now display all the calendar |
265 | for($day = 1; $day <= $display->maxdays; ++$day, ++$dayweek) { | |
266 | if($dayweek > $display->maxwday) { | |
267 | // We need to change week (table row) | |
d56d4e23 | 268 | $content .= '</tr><tr>'; |
7423f116 | 269 | $dayweek = $display->minwday; |
7423f116 | 270 | } |
92668ad2 | 271 | |
7423f116 | 272 | // Reset vars |
7423f116 | 273 | $cell = ''; |
797cedc7 | 274 | if ($weekend & (1 << ($dayweek % 7))) { |
7423f116 | 275 | // Weekend. This is true no matter what the exact range is. |
e2aa618b | 276 | $class = 'weekend day'; |
90723839 | 277 | } else { |
7423f116 | 278 | // Normal working day. |
e2aa618b | 279 | $class = 'day'; |
7423f116 | 280 | } |
281 | ||
282 | // Special visual fx if an event is defined | |
283 | if(isset($eventsbyday[$day])) { | |
90723839 | 284 | $class .= ' hasevent'; |
0f927f1e SH |
285 | $hrefparams['view'] = 'day'; |
286 | $dayhref = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $hrefparams), $day, $m, $y); | |
f434283f | 287 | |
7423f116 | 288 | $popupcontent = ''; |
289 | foreach($eventsbyday[$day] as $eventid) { | |
41d30a8e | 290 | if (!isset($events[$eventid])) { |
291 | continue; | |
4e17c6f3 | 292 | } |
41d30a8e | 293 | $event = $events[$eventid]; |
0f927f1e SH |
294 | $popupalt = ''; |
295 | $component = 'moodle'; | |
41d30a8e | 296 | if(!empty($event->modulename)) { |
0f927f1e | 297 | $popupicon = 'icon'; |
41d30a8e | 298 | $popupalt = $event->modulename; |
0f927f1e | 299 | $component = $event->modulename; |
9064751b | 300 | } else if ($event->courseid == SITEID) { // Site event |
0f927f1e | 301 | $popupicon = 'c/site'; |
c3d3b6d4 | 302 | } else if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event |
0f927f1e | 303 | $popupicon = 'c/course'; |
41d30a8e | 304 | } else if ($event->groupid) { // Group event |
0f927f1e | 305 | $popupicon = 'c/group'; |
41d30a8e | 306 | } else if ($event->userid) { // User event |
0f927f1e | 307 | $popupicon = 'c/user'; |
4e17c6f3 | 308 | } |
1d5bd3d2 | 309 | |
0f927f1e | 310 | $dayhref->set_anchor('event_'.$event->id); |
1d5bd3d2 | 311 | |
0f927f1e | 312 | $popupcontent .= html_writer::start_tag('div'); |
adc3cfc7 | 313 | $popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component); |
0f927f1e SH |
314 | $popupcontent .= html_writer::link($dayhref, format_string($event->name, true)); |
315 | $popupcontent .= html_writer::end_tag('div'); | |
7423f116 | 316 | } |
e295df44 | 317 | |
b5c42e70 | 318 | //Accessibility: functionality moved to calendar_get_popup. |
319 | if($display->thismonth && $day == $d) { | |
450a0a7d | 320 | $popup = calendar_get_popup(true, $events[$eventid]->timestart, $popupcontent); |
b5c42e70 | 321 | } else { |
450a0a7d | 322 | $popup = calendar_get_popup(false, $events[$eventid]->timestart, $popupcontent); |
e295df44 | 323 | } |
f20c4d02 | 324 | |
7423f116 | 325 | // Class and cell content |
326 | if(isset($typesbyday[$day]['startglobal'])) { | |
90723839 SH |
327 | $class .= ' calendar_event_global'; |
328 | } else if(isset($typesbyday[$day]['startcourse'])) { | |
329 | $class .= ' calendar_event_course'; | |
330 | } else if(isset($typesbyday[$day]['startgroup'])) { | |
331 | $class .= ' calendar_event_group'; | |
332 | } else if(isset($typesbyday[$day]['startuser'])) { | |
333 | $class .= ' calendar_event_user'; | |
7423f116 | 334 | } |
0f927f1e | 335 | $cell = '<a href="'.(string)$dayhref.'" '.$popup.'>'.$day.'</a>'; |
90723839 | 336 | } else { |
05fcc5fd | 337 | $cell = $day; |
7423f116 | 338 | } |
339 | ||
90723839 SH |
340 | $durationclass = false; |
341 | if (isset($typesbyday[$day]['durationglobal'])) { | |
342 | $durationclass = ' duration_global'; | |
343 | } else if(isset($typesbyday[$day]['durationcourse'])) { | |
344 | $durationclass = ' duration_course'; | |
345 | } else if(isset($typesbyday[$day]['durationgroup'])) { | |
346 | $durationclass = ' duration_group'; | |
347 | } else if(isset($typesbyday[$day]['durationuser'])) { | |
348 | $durationclass = ' duration_user'; | |
7423f116 | 349 | } |
90723839 SH |
350 | if ($durationclass) { |
351 | $class .= ' duration '.$durationclass; | |
7423f116 | 352 | } |
353 | ||
b4892fa2 | 354 | // If event has a class set then add it to the table day <td> tag |
355 | // Note: only one colour for minicalendar | |
356 | if(isset($eventsbyday[$day])) { | |
357 | foreach($eventsbyday[$day] as $eventid) { | |
358 | if (!isset($events[$eventid])) { | |
359 | continue; | |
360 | } | |
361 | $event = $events[$eventid]; | |
362 | if (!empty($event->class)) { | |
363 | $class .= ' '.$event->class; | |
364 | } | |
365 | break; | |
366 | } | |
367 | } | |
368 | ||
7423f116 | 369 | // Special visual fx for today |
450a0a7d | 370 | //Accessibility: hidden text for today, and popup. |
7423f116 | 371 | if($display->thismonth && $day == $d) { |
edbe6c1b | 372 | $class .= ' today'; |
b5c42e70 | 373 | $today = get_string('today', 'calendar').' '.userdate(time(), get_string('strftimedayshort')); |
e295df44 | 374 | |
b5c42e70 | 375 | if(! isset($eventsbyday[$day])) { |
450a0a7d | 376 | $class .= ' eventnone'; |
377 | $popup = calendar_get_popup(true, false); | |
378 | $cell = '<a href="#" '.$popup.'>'.$day.'</a>'; | |
b5c42e70 | 379 | } |
f79f2494 | 380 | $cell = get_accesshide($today.' ').$cell; |
7423f116 | 381 | } |
382 | ||
383 | // Just display it | |
92668ad2 | 384 | if(!empty($class)) { |
d56d4e23 | 385 | $class = ' class="'.$class.'"'; |
92668ad2 | 386 | } |
387 | $content .= '<td'.$class.'>'.$cell."</td>\n"; | |
7423f116 | 388 | } |
389 | ||
390 | // Paddding (the last week may have blank days at the end) | |
391 | for($i = $dayweek; $i <= $display->maxwday; ++$i) { | |
3bfd8bc8 | 392 | $content .= '<td class="dayblank"> </td>'; |
7423f116 | 393 | } |
394 | $content .= '</tr>'; // Last row ends | |
395 | ||
f136e4c5 | 396 | $content .= '</table>'; // Tabular display of days ends |
7423f116 | 397 | |
398 | return $content; | |
399 | } | |
400 | ||
b5c42e70 | 401 | /** |
08b4a4e1 RW |
402 | * Gets the calendar popup |
403 | * | |
404 | * It called at multiple points in from calendar_get_mini. | |
405 | * Copied and modified from calendar_get_mini. | |
406 | * | |
407 | * @param bool $is_today false except when called on the current day. | |
408 | * @param mixed $event_timestart $events[$eventid]->timestart, OR false if there are no events. | |
409 | * @param string $popupcontent content for the popup window/layout | |
410 | * @return string of eventid for the calendar_tooltip popup window/layout | |
b5c42e70 | 411 | */ |
412 | function calendar_get_popup($is_today, $event_timestart, $popupcontent='') { | |
458eb0d1 | 413 | global $PAGE; |
414 | static $popupcount; | |
415 | if ($popupcount === null) { | |
416 | $popupcount = 1; | |
417 | } | |
b5c42e70 | 418 | $popupcaption = ''; |
419 | if($is_today) { | |
420 | $popupcaption = get_string('today', 'calendar').' '; | |
421 | } | |
422 | if (false === $event_timestart) { | |
423 | $popupcaption .= userdate(time(), get_string('strftimedayshort')); | |
424 | $popupcontent = get_string('eventnone', 'calendar'); | |
450a0a7d | 425 | |
b5c42e70 | 426 | } else { |
427 | $popupcaption .= get_string('eventsfor', 'calendar', userdate($event_timestart, get_string('strftimedayshort'))); | |
428 | } | |
458eb0d1 | 429 | $id = 'calendar_tooltip_'.$popupcount; |
33e48a1a | 430 | $PAGE->requires->yui_module('moodle-calendar-eventmanager', 'M.core_calendar.add_event', array(array('eventId'=>$id,'title'=>$popupcaption, 'content'=>$popupcontent))); |
bdbae764 | 431 | |
458eb0d1 | 432 | $popupcount++; |
433 | return 'id="'.$id.'"'; | |
b5c42e70 | 434 | } |
435 | ||
08b4a4e1 RW |
436 | /** |
437 | * Gets the calendar upcoming event | |
438 | * | |
439 | * @param array $courses array of courses | |
440 | * @param array|int|bool $groups array of groups, group id or boolean for all/no group events | |
441 | * @param array|int|bool $users array of users, user id or boolean for all/no user events | |
442 | * @param int $daysinfuture number of days in the future we 'll look | |
443 | * @param int $maxevents maximum number of events | |
444 | * @param int $fromtime start time | |
445 | * @return array $output array of upcoming events | |
446 | */ | |
9958a08c | 447 | function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxevents, $fromtime=0) { |
62d11d77 | 448 | global $CFG, $COURSE, $DB; |
7423f116 | 449 | |
dd97c328 | 450 | $display = new stdClass; |
7423f116 | 451 | $display->range = $daysinfuture; // How many days in the future we 'll look |
452 | $display->maxevents = $maxevents; | |
453 | ||
454 | $output = array(); | |
455 | ||
456 | // Prepare "course caching", since it may save us a lot of queries | |
457 | $coursecache = array(); | |
458 | ||
459 | $processed = 0; | |
460 | $now = time(); // We 'll need this later | |
9d567178 | 461 | $usermidnighttoday = usergetmidnight($now); |
7423f116 | 462 | |
9958a08c | 463 | if ($fromtime) { |
464 | $display->tstart = $fromtime; | |
465 | } else { | |
9d567178 | 466 | $display->tstart = $usermidnighttoday; |
9958a08c | 467 | } |
7423f116 | 468 | |
1f473774 | 469 | // This works correctly with respect to the user's DST, but it is accurate |
470 | // only because $fromtime is always the exact midnight of some day! | |
471 | $display->tend = usergetmidnight($display->tstart + DAYSECS * $display->range + 3 * HOURSECS) - 1; | |
7423f116 | 472 | |
473 | // Get the events matching our criteria | |
8263f802 | 474 | $events = calendar_get_events($display->tstart, $display->tend, $users, $groups, $courses); |
7423f116 | 475 | |
c635dcda | 476 | // This is either a genius idea or an idiot idea: in order to not complicate things, we use this rule: if, after |
9064751b | 477 | // possibly removing SITEID from $courses, there is only one course left, then clicking on a day in the month |
c635dcda | 478 | // will also set the $SESSION->cal_courses_shown variable to that one course. Otherwise, we 'd need to add extra |
479 | // arguments to this function. | |
480 | ||
0f927f1e | 481 | $hrefparams = array(); |
7bd1677c | 482 | if(!empty($courses)) { |
e749554e | 483 | $courses = array_diff($courses, array(SITEID)); |
7bd1677c | 484 | if(count($courses) == 1) { |
0f927f1e | 485 | $hrefparams['course'] = reset($courses); |
7bd1677c | 486 | } |
c635dcda | 487 | } |
488 | ||
dd97c328 | 489 | if ($events !== false) { |
490 | ||
f20edd52 | 491 | $modinfo = get_fast_modinfo($COURSE); |
fa22fd5f | 492 | |
7423f116 | 493 | foreach($events as $event) { |
dc6cb74e | 494 | |
dd97c328 | 495 | |
496 | if (!empty($event->modulename)) { | |
497 | if ($event->courseid == $COURSE->id) { | |
498 | if (isset($modinfo->instances[$event->modulename][$event->instance])) { | |
499 | $cm = $modinfo->instances[$event->modulename][$event->instance]; | |
500 | if (!$cm->uservisible) { | |
501 | continue; | |
502 | } | |
503 | } | |
504 | } else { | |
505 | if (!$cm = get_coursemodule_from_instance($event->modulename, $event->instance)) { | |
dc6cb74e | 506 | continue; |
507 | } | |
dd97c328 | 508 | if (!coursemodule_visible_for_user($cm)) { |
dc6cb74e | 509 | continue; |
510 | } | |
dd97c328 | 511 | } |
512 | if ($event->modulename == 'assignment'){ | |
f4700b91 RW |
513 | // create calendar_event to test edit_event capability |
514 | // this new event will also prevent double creation of calendar_event object | |
515 | $checkevent = new calendar_event($event); | |
dd97c328 | 516 | // TODO: rewrite this hack somehow |
f4700b91 | 517 | if (!calendar_edit_event_allowed($checkevent)){ // cannot manage entries, eg. student |
62d11d77 | 518 | if (!$assignment = $DB->get_record('assignment', array('id'=>$event->instance))) { |
f581f8d6 | 519 | // print_error("invalidid", 'assignment'); |
dd97c328 | 520 | continue; |
521 | } | |
522 | // assign assignment to assignment object to use hidden_is_hidden method | |
523 | require_once($CFG->dirroot.'/mod/assignment/lib.php'); | |
524 | ||
525 | if (!file_exists($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php')) { | |
526 | continue; | |
527 | } | |
528 | require_once ($CFG->dirroot.'/mod/assignment/type/'.$assignment->assignmenttype.'/assignment.class.php'); | |
529 | ||
530 | $assignmentclass = 'assignment_'.$assignment->assignmenttype; | |
531 | $assignmentinstance = new $assignmentclass($cm->id, $assignment, $cm); | |
532 | ||
533 | if ($assignmentinstance->description_is_hidden()){//force not to show description before availability | |
534 | $event->description = get_string('notavailableyet', 'assignment'); | |
535 | } | |
dc6cb74e | 536 | } |
537 | } | |
538 | } | |
9d567178 | 539 | |
dd97c328 | 540 | if ($processed >= $display->maxevents) { |
9d567178 | 541 | break; |
542 | } | |
7423f116 | 543 | |
0f927f1e | 544 | $event->time = calendar_format_event_time($event, $now, $hrefparams); |
3c134875 | 545 | $output[] = $event; |
546 | ++$processed; | |
547 | } | |
548 | } | |
549 | return $output; | |
550 | } | |
7423f116 | 551 | |
08b4a4e1 RW |
552 | /** |
553 | * Add calendar event metadata | |
554 | * | |
555 | * @param stdClass $event event info | |
556 | * @return stdClass $event metadata | |
557 | */ | |
9df8ff44 | 558 | function calendar_add_event_metadata($event) { |
6b608f8f | 559 | global $CFG, $OUTPUT; |
fb73f3b3 | 560 | |
e295df44 | 561 | //Support multilang in event->name |
fb73f3b3 | 562 | $event->name = format_string($event->name,true); |
e295df44 | 563 | |
3c134875 | 564 | if(!empty($event->modulename)) { // Activity event |
565 | // The module name is set. I will assume that it has to be displayed, and | |
566 | // also that it is an automatically-generated event. And of course that the | |
567 | // fields for get_coursemodule_from_instance are set correctly. | |
568 | $module = calendar_get_module_cached($coursecache, $event->modulename, $event->instance); | |
7423f116 | 569 | |
3c134875 | 570 | if ($module === false) { |
571 | return; | |
572 | } | |
9958a08c | 573 | |
3c134875 | 574 | $modulename = get_string('modulename', $event->modulename); |
a48bf079 DM |
575 | if (get_string_manager()->string_exists($event->eventtype, $event->modulename)) { |
576 | // will be used as alt text if the event icon | |
577 | $eventtype = get_string($event->eventtype, $event->modulename); | |
578 | } else { | |
579 | $eventtype = ''; | |
580 | } | |
b5d0cafc | 581 | $icon = $OUTPUT->pix_url('icon', $event->modulename) . ''; |
9958a08c | 582 | |
6ca657a7 | 583 | $context = context_course::instance($module->course); |
91d284c1 SH |
584 | $fullname = format_string($coursecache[$module->course]->fullname, true, array('context' => $context)); |
585 | ||
10daca92 | 586 | $event->icon = '<img height="16" width="16" src="'.$icon.'" alt="'.$eventtype.'" title="'.$modulename.'" style="vertical-align: middle;" />'; |
fb73f3b3 | 587 | $event->referer = '<a href="'.$CFG->wwwroot.'/mod/'.$event->modulename.'/view.php?id='.$module->id.'">'.$event->name.'</a>'; |
91d284c1 | 588 | $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$module->course.'">'.$fullname.'</a>'; |
3c134875 | 589 | $event->cmid = $module->id; |
9958a08c | 590 | |
9958a08c | 591 | |
3c134875 | 592 | } else if($event->courseid == SITEID) { // Site event |
b5d0cafc | 593 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/site') . '" alt="'.get_string('globalevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 594 | $event->cssclass = 'calendar_event_global'; |
3c134875 | 595 | } else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { // Course event |
596 | calendar_get_course_cached($coursecache, $event->courseid); | |
91d284c1 | 597 | |
6ca657a7 | 598 | $context = context_course::instance($event->courseid); |
91d284c1 SH |
599 | $fullname = format_string($coursecache[$event->courseid]->fullname, true, array('context' => $context)); |
600 | ||
b5d0cafc | 601 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/course') . '" alt="'.get_string('courseevent', 'calendar').'" style="vertical-align: middle;" />'; |
91d284c1 | 602 | $event->courselink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$event->courseid.'">'.$fullname.'</a>'; |
90723839 | 603 | $event->cssclass = 'calendar_event_course'; |
3c134875 | 604 | } else if ($event->groupid) { // Group event |
b5d0cafc | 605 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/group') . '" alt="'.get_string('groupevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 606 | $event->cssclass = 'calendar_event_group'; |
3c134875 | 607 | } else if($event->userid) { // User event |
b5d0cafc | 608 | $event->icon = '<img height="16" width="16" src="'.$OUTPUT->pix_url('c/user') . '" alt="'.get_string('userevent', 'calendar').'" style="vertical-align: middle;" />'; |
90723839 | 609 | $event->cssclass = 'calendar_event_user'; |
3c134875 | 610 | } |
9df8ff44 | 611 | return $event; |
e295df44 | 612 | } |
9df8ff44 | 613 | |
8263f802 | 614 | /** |
615 | * Get calendar events | |
08b4a4e1 | 616 | * |
8263f802 | 617 | * @param int $tstart Start time of time range for events |
08b4a4e1 RW |
618 | * @param int $tend End time of time range for events |
619 | * @param array|int|boolean $users array of users, user id or boolean for all/no user events | |
620 | * @param array|int|boolean $groups array of groups, group id or boolean for all/no group events | |
621 | * @param array|int|boolean $courses array of courses, course id or boolean for all/no course events | |
8263f802 | 622 | * @param boolean $withduration whether only events starting within time range selected |
623 | * or events in progress/already started selected as well | |
624 | * @param boolean $ignorehidden whether to select only visible events or all events | |
08b4a4e1 | 625 | * @return array $events of selected events or an empty array if there aren't any (or there was an error) |
8263f802 | 626 | */ |
627 | function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withduration=true, $ignorehidden=true) { | |
62d11d77 | 628 | global $DB; |
629 | ||
7423f116 | 630 | $whereclause = ''; |
631 | // Quick test | |
632 | if(is_bool($users) && is_bool($groups) && is_bool($courses)) { | |
8263f802 | 633 | return array(); |
7423f116 | 634 | } |
482dbe0c | 635 | |
7423f116 | 636 | if(is_array($users) && !empty($users)) { |
637 | // Events from a number of users | |
638 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 639 | $whereclause .= ' (userid IN ('.implode(',', $users).') AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 640 | } else if(is_numeric($users)) { |
7423f116 | 641 | // Events from one user |
642 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 643 | $whereclause .= ' (userid = '.$users.' AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 644 | } else if($users === true) { |
7423f116 | 645 | // Events from ALL users |
646 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 647 | $whereclause .= ' (userid != 0 AND courseid = 0 AND groupid = 0)'; |
36dc3b71 | 648 | } else if($users === false) { |
6e957c41 | 649 | // No user at all, do nothing |
f52f7413 | 650 | } |
482dbe0c | 651 | |
7423f116 | 652 | if(is_array($groups) && !empty($groups)) { |
653 | // Events from a number of groups | |
654 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
655 | $whereclause .= ' groupid IN ('.implode(',', $groups).')'; | |
36dc3b71 | 656 | } else if(is_numeric($groups)) { |
7423f116 | 657 | // Events from one group |
658 | if(!empty($whereclause)) $whereclause .= ' OR '; | |
659 | $whereclause .= ' groupid = '.$groups; | |
36dc3b71 | 660 | } else if($groups === true) { |
7423f116 | 661 | // Events from ALL groups |
662 | if(!empty($whereclause)) $whereclause .= ' OR '; | |
663 | $whereclause .= ' groupid != 0'; | |
664 | } | |
482dbe0c | 665 | // boolean false (no groups at all): we don't need to do anything |
666 | ||
36dc3b71 SH |
667 | if(is_array($courses) && !empty($courses)) { |
668 | if(!empty($whereclause)) { | |
669 | $whereclause .= ' OR'; | |
f52f7413 | 670 | } |
36dc3b71 SH |
671 | $whereclause .= ' (groupid = 0 AND courseid IN ('.implode(',', $courses).'))'; |
672 | } else if(is_numeric($courses)) { | |
7423f116 | 673 | // One course |
674 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 675 | $whereclause .= ' (groupid = 0 AND courseid = '.$courses.')'; |
36dc3b71 | 676 | } else if ($courses === true) { |
7423f116 | 677 | // Events from ALL courses |
678 | if(!empty($whereclause)) $whereclause .= ' OR'; | |
6e957c41 | 679 | $whereclause .= ' (groupid = 0 AND courseid != 0)'; |
7423f116 | 680 | } |
8c165fe9 | 681 | |
482dbe0c | 682 | // Security check: if, by now, we have NOTHING in $whereclause, then it means |
683 | // that NO event-selecting clauses were defined. Thus, we won't be returning ANY | |
684 | // events no matter what. Allowing the code to proceed might return a completely | |
685 | // valid query with only time constraints, thus selecting ALL events in that time frame! | |
686 | if(empty($whereclause)) { | |
8263f802 | 687 | return array(); |
482dbe0c | 688 | } |
689 | ||
7423f116 | 690 | if($withduration) { |
b4892fa2 | 691 | $timeclause = '(timestart >= '.$tstart.' OR timestart + timeduration > '.$tstart.') AND timestart <= '.$tend; |
7423f116 | 692 | } |
693 | else { | |
694 | $timeclause = 'timestart >= '.$tstart.' AND timestart <= '.$tend; | |
695 | } | |
696 | if(!empty($whereclause)) { | |
697 | // We have additional constraints | |
698 | $whereclause = $timeclause.' AND ('.$whereclause.')'; | |
699 | } | |
700 | else { | |
701 | // Just basic time filtering | |
702 | $whereclause = $timeclause; | |
703 | } | |
f52f7413 | 704 | |
0ad072de | 705 | if ($ignorehidden) { |
706 | $whereclause .= ' AND visible = 1'; | |
707 | } | |
708 | ||
62d11d77 | 709 | $events = $DB->get_records_select('event', $whereclause, null, 'timestart'); |
8263f802 | 710 | if ($events === false) { |
711 | $events = array(); | |
712 | } | |
713 | return $events; | |
7423f116 | 714 | } |
715 | ||
08b4a4e1 RW |
716 | /** |
717 | * Get control options for Calendar | |
718 | * | |
719 | * @param string $type of calendar | |
720 | * @param array $data calendar information | |
721 | * @return string $content return available control for the calender in html | |
722 | */ | |
7423f116 | 723 | function calendar_top_controls($type, $data) { |
797cedc7 | 724 | global $CFG; |
7423f116 | 725 | $content = ''; |
726 | if(!isset($data['d'])) { | |
727 | $data['d'] = 1; | |
728 | } | |
5147ad48 | 729 | |
f21ed0f3 | 730 | // Ensure course id passed if relevant |
731 | // Required due to changes in view/lib.php mainly (calendar_session_vars()) | |
732 | $courseid = ''; | |
733 | if (!empty($data['id'])) { | |
734 | $courseid = '&course='.$data['id']; | |
735 | } | |
736 | ||
5147ad48 | 737 | if(!checkdate($data['m'], $data['d'], $data['y'])) { |
738 | $time = time(); | |
739 | } | |
740 | else { | |
741 | $time = make_timestamp($data['y'], $data['m'], $data['d']); | |
742 | } | |
743 | $date = usergetdate($time); | |
e295df44 | 744 | |
7423f116 | 745 | $data['m'] = $date['mon']; |
746 | $data['y'] = $date['year']; | |
7423f116 | 747 | |
2a06efcc | 748 | //Accessibility: calendar block controls, replaced <table> with <div>. |
a84dea2c | 749 | //$nexttext = link_arrow_right(get_string('monthnext', 'access'), $url='', $accesshide=true); |
750 | //$prevtext = link_arrow_left(get_string('monthprev', 'access'), $url='', $accesshide=true); | |
2a06efcc | 751 | |
7423f116 | 752 | switch($type) { |
753 | case 'frontpage': | |
754 | list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); | |
755 | list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); | |
a84dea2c | 756 | $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'index.php?', 0, $nextmonth, $nextyear, $accesshide=true); |
757 | $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'index.php?', 0, $prevmonth, $prevyear, true); | |
ae53bd1d | 758 | |
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 | ||
43c90b9b | 764 | if (right_to_left()) { |
0f927f1e SH |
765 | $left = $nextlink; |
766 | $right = $prevlink; | |
43c90b9b | 767 | } else { |
0f927f1e SH |
768 | $left = $prevlink; |
769 | $right = $nextlink; | |
43c90b9b | 770 | } |
ae53bd1d | 771 | |
0f927f1e SH |
772 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); |
773 | $content .= $left.'<span class="hide"> | </span>'; | |
e71e340d | 774 | $content .= html_writer::tag('span', html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear')), array('title'=>get_string('monththis','calendar'))), array('class'=>'current')); |
0f927f1e | 775 | $content .= '<span class="hide"> | </span>'. $right; |
6694ff84 | 776 | $content .= "<span class=\"clearer\"><!-- --></span>\n"; |
0f927f1e SH |
777 | $content .= html_writer::end_tag('div'); |
778 | ||
779 | break; | |
7423f116 | 780 | case 'course': |
781 | list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); | |
782 | list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); | |
a84dea2c | 783 | $nextlink = calendar_get_link_next(get_string('monthnext', 'access'), 'view.php?id='.$data['id'].'&', 0, $nextmonth, $nextyear, $accesshide=true); |
784 | $prevlink = calendar_get_link_previous(get_string('monthprev', 'access'), 'view.php?id='.$data['id'].'&', 0, $prevmonth, $prevyear, true); | |
0f927f1e SH |
785 | |
786 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']); | |
787 | if (!empty($data['id'])) { | |
788 | $calendarlink->param('course', $data['id']); | |
789 | } | |
790 | ||
791 | if (right_to_left()) { | |
792 | $left = $nextlink; | |
793 | $right = $prevlink; | |
794 | } else { | |
795 | $left = $prevlink; | |
796 | $right = $nextlink; | |
797 | } | |
798 | ||
799 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); | |
800 | $content .= $left.'<span class="hide"> | </span>'; | |
e71e340d | 801 | $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 |
802 | $content .= '<span class="hide"> | </span>'. $right; |
803 | $content .= "<span class=\"clearer\"><!-- --></span>"; | |
804 | $content .= html_writer::end_tag('div'); | |
805 | break; | |
7423f116 | 806 | case 'upcoming': |
0f927f1e SH |
807 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming')), 1, $data['m'], $data['y']); |
808 | if (!empty($data['id'])) { | |
809 | $calendarlink->param('course', $data['id']); | |
810 | } | |
811 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
812 | $content .= html_writer::tag('div', $calendarlink, array('class'=>'centered')); | |
813 | break; | |
7423f116 | 814 | case 'display': |
0f927f1e SH |
815 | $calendarlink = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'month')), 1, $data['m'], $data['y']); |
816 | if (!empty($data['id'])) { | |
817 | $calendarlink->param('course', $data['id']); | |
818 | } | |
819 | $calendarlink = html_writer::link($calendarlink, userdate($time, get_string('strftimemonthyear'))); | |
820 | $content .= html_writer::tag('h3', $calendarlink); | |
821 | break; | |
7423f116 | 822 | case 'month': |
823 | list($prevmonth, $prevyear) = calendar_sub_month($data['m'], $data['y']); | |
824 | list($nextmonth, $nextyear) = calendar_add_month($data['m'], $data['y']); | |
5147ad48 | 825 | $prevdate = make_timestamp($prevyear, $prevmonth, 1); |
826 | $nextdate = make_timestamp($nextyear, $nextmonth, 1); | |
0f927f1e SH |
827 | $prevlink = calendar_get_link_previous(userdate($prevdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $prevmonth, $prevyear); |
828 | $nextlink = calendar_get_link_next(userdate($nextdate, get_string('strftimemonthyear')), 'view.php?view=month'.$courseid.'&', 1, $nextmonth, $nextyear); | |
829 | ||
830 | if (right_to_left()) { | |
831 | $left = $nextlink; | |
832 | $right = $prevlink; | |
833 | } else { | |
834 | $left = $prevlink; | |
835 | $right = $nextlink; | |
836 | } | |
837 | ||
838 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); | |
839 | $content .= $left . '<span class="hide"> | </span><h1 class="current">'.userdate($time, get_string('strftimemonthyear'))."</h1>"; | |
840 | $content .= '<span class="hide"> | </span>' . $right; | |
841 | $content .= '<span class="clearer"><!-- --></span>'; | |
842 | $content .= html_writer::end_tag('div')."\n"; | |
843 | break; | |
7423f116 | 844 | case 'day': |
797cedc7 | 845 | $days = calendar_get_days(); |
7423f116 | 846 | $data['d'] = $date['mday']; // Just for convenience |
5147ad48 | 847 | $prevdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] - 1)); |
848 | $nextdate = usergetdate(make_timestamp($data['y'], $data['m'], $data['d'] + 1)); | |
797cedc7 SH |
849 | $prevname = calendar_wday_name($days[$prevdate['wday']]); |
850 | $nextname = calendar_wday_name($days[$nextdate['wday']]); | |
0f927f1e SH |
851 | $prevlink = calendar_get_link_previous($prevname, 'view.php?view=day'.$courseid.'&', $prevdate['mday'], $prevdate['mon'], $prevdate['year']); |
852 | $nextlink = calendar_get_link_next($nextname, 'view.php?view=day'.$courseid.'&', $nextdate['mday'], $nextdate['mon'], $nextdate['year']); | |
853 | ||
854 | if (right_to_left()) { | |
855 | $left = $nextlink; | |
856 | $right = $prevlink; | |
857 | } else { | |
858 | $left = $prevlink; | |
859 | $right = $nextlink; | |
860 | } | |
861 | ||
862 | $content .= html_writer::start_tag('div', array('class'=>'calendar-controls')); | |
863 | $content .= $left; | |
864 | $content .= '<span class="hide"> | </span><span class="current">'.userdate($time, get_string('strftimedaydate')).'</span>'; | |
865 | $content .= '<span class="hide"> | </span>'. $right; | |
866 | $content .= "<span class=\"clearer\"><!-- --></span>"; | |
867 | $content .= html_writer::end_tag('div')."\n"; | |
868 | ||
869 | break; | |
7423f116 | 870 | } |
871 | return $content; | |
872 | } | |
873 | ||
08b4a4e1 RW |
874 | /** |
875 | * Get the controls filter for calendar. | |
876 | * | |
877 | * Filter is used to hide calendar info from the display page | |
878 | * | |
879 | * @param moodle_url $returnurl return-url for filter controls | |
880 | * @return string $content return filter controls in html | |
881 | */ | |
797cedc7 SH |
882 | function calendar_filter_controls(moodle_url $returnurl) { |
883 | global $CFG, $USER, $OUTPUT; | |
7423f116 | 884 | |
48f508ab | 885 | $groupevents = true; |
e295df44 | 886 | |
7f4d18fc | 887 | $id = optional_param( 'id',0,PARAM_INT ); |
d715f7c4 | 888 | |
eb59a448 | 889 | $seturl = new moodle_url('/calendar/set.php', array('return' => base64_encode($returnurl->out(false)), 'sesskey'=>sesskey())); |
7423f116 | 890 | |
2ad2cbc3 | 891 | $content = '<table>'; |
48f508ab | 892 | $content .= '<tr>'; |
797cedc7 SH |
893 | |
894 | $seturl->param('var', 'showglobal'); | |
895 | if (calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) { | |
896 | $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='."'".$seturl."'".'" /></td>'; | |
897 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hideglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n"; | |
fdbffa54 | 898 | } else { |
797cedc7 SH |
899 | $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='."'".$seturl."'".'" /></td>'; |
900 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showglobal', 'calendar').'">'.get_string('global', 'calendar').'</a></td>'."\n"; | |
48f508ab | 901 | } |
aa6c1ced | 902 | |
797cedc7 SH |
903 | $seturl->param('var', 'showcourses'); |
904 | if (calendar_show_event_type(CALENDAR_EVENT_COURSE)) { | |
905 | $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='."'".$seturl."'".'" /></td>'; | |
906 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hidecourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n"; | |
907 | } else { | |
908 | $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='."'".$seturl."'".'" /></td>'; | |
909 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showcourse', 'calendar').'">'.get_string('course', 'calendar').'</a></td>'."\n"; | |
e3bb6401 | 910 | } |
48f508ab | 911 | |
4f0c2d00 | 912 | if (isloggedin() && !isguestuser()) { |
e3bb6401 | 913 | $content .= "</tr>\n<tr>"; |
10b1d598 | 914 | |
797cedc7 | 915 | if ($groupevents) { |
43c3ffbe | 916 | // This course MIGHT have group events defined, so show the filter |
797cedc7 SH |
917 | $seturl->param('var', 'showgroups'); |
918 | if (calendar_show_event_type(CALENDAR_EVENT_GROUP)) { | |
919 | $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='."'".$seturl."'".'" /></td>'; | |
920 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hidegroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n"; | |
e3bb6401 | 921 | } else { |
797cedc7 SH |
922 | $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='."'".$seturl."'".'" /></td>'; |
923 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showgroups', 'calendar').'">'.get_string('group', 'calendar').'</a></td>'."\n"; | |
43c3ffbe | 924 | } |
e3bb6401 | 925 | } else { |
43c3ffbe | 926 | // This course CANNOT have group events, so lose the filter |
5c53988f | 927 | $content .= '<td style="width: 11px;"></td><td> </td>'."\n"; |
fdbffa54 | 928 | } |
797cedc7 SH |
929 | |
930 | $seturl->param('var', 'showuser'); | |
931 | if (calendar_show_event_type(CALENDAR_EVENT_USER)) { | |
932 | $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='."'".$seturl."'".'" /></td>'; | |
933 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_hideuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n"; | |
fdbffa54 | 934 | } else { |
797cedc7 SH |
935 | $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='."'".$seturl."'".'" /></td>'; |
936 | $content .= '<td><a href="'.$seturl.'" title="'.get_string('tt_showuser', 'calendar').'">'.get_string('user', 'calendar').'</a></td>'."\n"; | |
48f508ab | 937 | } |
938 | } | |
939 | $content .= "</tr>\n</table>\n"; | |
940 | ||
7423f116 | 941 | return $content; |
942 | } | |
943 | ||
08b4a4e1 RW |
944 | /** |
945 | * Return the representation day | |
946 | * | |
947 | * @param int $tstamp Timestamp in GMT | |
948 | * @param int $now current Unix timestamp | |
949 | * @param bool $usecommonwords | |
950 | * @return string the formatted date/time | |
951 | */ | |
7423f116 | 952 | function calendar_day_representation($tstamp, $now = false, $usecommonwords = true) { |
953 | ||
0ef7c973 | 954 | static $shortformat; |
955 | if(empty($shortformat)) { | |
e70fdac0 | 956 | $shortformat = get_string('strftimedayshort'); |
0ef7c973 | 957 | } |
958 | ||
7423f116 | 959 | if($now === false) { |
960 | $now = time(); | |
961 | } | |
962 | ||
963 | // To have it in one place, if a change is needed | |
e70fdac0 | 964 | $formal = userdate($tstamp, $shortformat); |
7423f116 | 965 | |
7b38bfa6 | 966 | $datestamp = usergetdate($tstamp); |
967 | $datenow = usergetdate($now); | |
7423f116 | 968 | |
969 | if($usecommonwords == false) { | |
970 | // We don't want words, just a date | |
971 | return $formal; | |
972 | } | |
7b38bfa6 | 973 | else if($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday']) { |
7423f116 | 974 | // Today |
975 | return get_string('today', 'calendar'); | |
976 | } | |
7b38bfa6 | 977 | else if( |
978 | ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] - 1 ) || | |
979 | ($datestamp['year'] == $datenow['year'] - 1 && $datestamp['mday'] == 31 && $datestamp['mon'] == 12 && $datenow['yday'] == 1) | |
980 | ) { | |
7423f116 | 981 | // Yesterday |
982 | return get_string('yesterday', 'calendar'); | |
983 | } | |
7b38bfa6 | 984 | else if( |
985 | ($datestamp['year'] == $datenow['year'] && $datestamp['yday'] == $datenow['yday'] + 1 ) || | |
986 | ($datestamp['year'] == $datenow['year'] + 1 && $datenow['mday'] == 31 && $datenow['mon'] == 12 && $datestamp['yday'] == 1) | |
987 | ) { | |
7423f116 | 988 | // Tomorrow |
989 | return get_string('tomorrow', 'calendar'); | |
990 | } | |
991 | else { | |
992 | return $formal; | |
993 | } | |
994 | } | |
995 | ||
08b4a4e1 RW |
996 | /** |
997 | * return the formatted representation time | |
998 | * | |
999 | * @param int $time the timestamp in UTC, as obtained from the database | |
1000 | * @return string the formatted date/time | |
1001 | */ | |
7423f116 | 1002 | function calendar_time_representation($time) { |
1b0ebe79 | 1003 | static $langtimeformat = NULL; |
1004 | if($langtimeformat === NULL) { | |
1005 | $langtimeformat = get_string('strftimetime'); | |
1006 | } | |
1007 | $timeformat = get_user_preferences('calendar_timeformat'); | |
c7dd2550 | 1008 | if(empty($timeformat)){ |
1009 | $timeformat = get_config(NULL,'calendar_site_timeformat'); | |
1010 | } | |
1b0ebe79 | 1011 | // The ? is needed because the preference might be present, but empty |
1012 | return userdate($time, empty($timeformat) ? $langtimeformat : $timeformat); | |
7423f116 | 1013 | } |
1014 | ||
e295df44 | 1015 | /** |
14236cbd SH |
1016 | * Adds day, month, year arguments to a URL and returns a moodle_url object. |
1017 | * | |
1018 | * @param string|moodle_url $linkbase | |
08b4a4e1 RW |
1019 | * @param int $d The number of the day. |
1020 | * @param int $m The number of the month. | |
1021 | * @param int $y The number of the year. | |
1022 | * @return moodle_url|null $linkbase | |
e295df44 | 1023 | */ |
7423f116 | 1024 | function calendar_get_link_href($linkbase, $d, $m, $y) { |
0f927f1e SH |
1025 | if (empty($linkbase)) { |
1026 | return ''; | |
1027 | } | |
1028 | if (!($linkbase instanceof moodle_url)) { | |
1029 | $linkbase = new moodle_url(); | |
1030 | } | |
1031 | if (!empty($d)) { | |
1032 | $linkbase->param('cal_d', $d); | |
1033 | } | |
1034 | if (!empty($m)) { | |
1035 | $linkbase->param('cal_m', $m); | |
1036 | } | |
1037 | if (!empty($y)) { | |
1038 | $linkbase->param('cal_y', $y); | |
1039 | } | |
1040 | return $linkbase; | |
7423f116 | 1041 | } |
1042 | ||
a84dea2c | 1043 | /** |
1044 | * Build and return a previous month HTML link, with an arrow. | |
14236cbd | 1045 | * |
a84dea2c | 1046 | * @param string $text The text label. |
14236cbd | 1047 | * @param string|moodle_url $linkbase The URL stub. |
08b4a4e1 RW |
1048 | * @param int $d The number of the date. |
1049 | * @param int $m The number of the month. | |
1050 | * @param int $y year The number of the year. | |
a84dea2c | 1051 | * @param bool $accesshide Default visible, or hide from all except screenreaders. |
1052 | * @return string HTML string. | |
1053 | */ | |
1054 | function calendar_get_link_previous($text, $linkbase, $d, $m, $y, $accesshide=false) { | |
0f927f1e SH |
1055 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y); |
1056 | if (empty($href)) { | |
1057 | return $text; | |
1058 | } | |
1059 | return link_arrow_left($text, (string)$href, $accesshide, 'previous'); | |
a84dea2c | 1060 | } |
1061 | ||
1062 | /** | |
1063 | * Build and return a next month HTML link, with an arrow. | |
1d5bd3d2 | 1064 | * |
a84dea2c | 1065 | * @param string $text The text label. |
14236cbd | 1066 | * @param string|moodle_url $linkbase The URL stub. |
08b4a4e1 RW |
1067 | * @param int $d the number of the Day |
1068 | * @param int $m The number of the month. | |
1069 | * @param int $y The number of the year. | |
a84dea2c | 1070 | * @param bool $accesshide Default visible, or hide from all except screenreaders. |
1071 | * @return string HTML string. | |
1072 | */ | |
1073 | function calendar_get_link_next($text, $linkbase, $d, $m, $y, $accesshide=false) { | |
0f927f1e SH |
1074 | $href = calendar_get_link_href(new moodle_url($linkbase), $d, $m, $y); |
1075 | if (empty($href)) { | |
1076 | return $text; | |
1077 | } | |
1078 | return link_arrow_right($text, (string)$href, $accesshide, 'next'); | |
a84dea2c | 1079 | } |
1080 | ||
08b4a4e1 RW |
1081 | /** |
1082 | * Return the name of the weekday | |
1083 | * | |
1084 | * @param string $englishname | |
1085 | * @return string of the weekeday | |
1086 | */ | |
7423f116 | 1087 | function calendar_wday_name($englishname) { |
1088 | return get_string(strtolower($englishname), 'calendar'); | |
1089 | } | |
1090 | ||
08b4a4e1 RW |
1091 | /** |
1092 | * Return the number of days in month | |
1093 | * | |
1094 | * @param int $month the number of the month. | |
1095 | * @param int $year the number of the year | |
1096 | * @return int | |
1097 | */ | |
7423f116 | 1098 | function calendar_days_in_month($month, $year) { |
7b38bfa6 | 1099 | return intval(date('t', mktime(0, 0, 0, $month, 1, $year))); |
7423f116 | 1100 | } |
1101 | ||
08b4a4e1 RW |
1102 | /** |
1103 | * Get the upcoming event block | |
1104 | * | |
1105 | * @param array $events list of events | |
1106 | * @param moodle_url|string $linkhref link to event referer | |
1107 | * @return string|null $content html block content | |
1108 | */ | |
6605ff8c | 1109 | function calendar_get_block_upcoming($events, $linkhref = NULL) { |
7423f116 | 1110 | $content = ''; |
1111 | $lines = count($events); | |
396b61f0 | 1112 | if (!$lines) { |
1113 | return $content; | |
1114 | } | |
7423f116 | 1115 | |
396b61f0 | 1116 | for ($i = 0; $i < $lines; ++$i) { |
b0ac9180 | 1117 | if (!isset($events[$i]->time)) { // Just for robustness |
1118 | continue; | |
1119 | } | |
9df8ff44 | 1120 | $events[$i] = calendar_add_event_metadata($events[$i]); |
ea21c1f4 | 1121 | $content .= '<div class="event"><span class="icon c0">'.$events[$i]->icon.'</span> '; |
43c3ffbe | 1122 | if (!empty($events[$i]->referer)) { |
7423f116 | 1123 | // That's an activity event, so let's provide the hyperlink |
396b61f0 | 1124 | $content .= $events[$i]->referer; |
1125 | } else { | |
e749554e | 1126 | if(!empty($linkhref)) { |
1127 | $ed = usergetdate($events[$i]->timestart); | |
0f927f1e SH |
1128 | $href = calendar_get_link_href(new moodle_url(CALENDAR_URL.$linkhref), $ed['mday'], $ed['mon'], $ed['year']); |
1129 | $href->set_anchor('event_'.$events[$i]->id); | |
1130 | $content .= html_writer::link($href, $events[$i]->name); | |
e749554e | 1131 | } |
1132 | else { | |
1133 | $content .= $events[$i]->name; | |
1134 | } | |
7423f116 | 1135 | } |
9ecf051d | 1136 | $events[$i]->time = str_replace('»', '<br />»', $events[$i]->time); |
1137 | $content .= '<div class="date">'.$events[$i]->time.'</div></div>'; | |
396b61f0 | 1138 | if ($i < $lines - 1) $content .= '<hr />'; |
7423f116 | 1139 | } |
1140 | ||
1141 | return $content; | |
7423f116 | 1142 | } |
1143 | ||
08b4a4e1 RW |
1144 | /** |
1145 | * Get the next following month | |
1146 | * | |
1147 | * If the current month is December, it will get the first month of the following year. | |
1148 | * | |
1149 | * | |
1150 | * @param int $month the number of the month. | |
1151 | * @param int $year the number of the year. | |
1152 | * @return array the following month | |
1153 | */ | |
7423f116 | 1154 | function calendar_add_month($month, $year) { |
1155 | if($month == 12) { | |
1156 | return array(1, $year + 1); | |
1157 | } | |
1158 | else { | |
1159 | return array($month + 1, $year); | |
1160 | } | |
1161 | } | |
1162 | ||
08b4a4e1 RW |
1163 | /** |
1164 | * Get the previous month | |
1165 | * | |
1166 | * If the current month is January, it will get the last month of the previous year. | |
1167 | * | |
1168 | * @param int $month the number of the month. | |
1169 | * @param int $year the number of the year. | |
1170 | * @return array previous month | |
1171 | */ | |
7423f116 | 1172 | function calendar_sub_month($month, $year) { |
1173 | if($month == 1) { | |
1174 | return array(12, $year - 1); | |
1175 | } | |
1176 | else { | |
1177 | return array($month - 1, $year); | |
1178 | } | |
1179 | } | |
1180 | ||
08b4a4e1 RW |
1181 | /** |
1182 | * Get per-day basis events | |
1183 | * | |
1184 | * @param array $events list of events | |
1185 | * @param int $month the number of the month | |
1186 | * @param int $year the number of the year | |
1187 | * @param array $eventsbyday event on specific day | |
1188 | * @param array $durationbyday duration of the event in days | |
1189 | * @param array $typesbyday event type (eg: global, course, user, or group) | |
1190 | * @param array $courses list of courses | |
1191 | * @return void | |
1192 | */ | |
7c50db30 | 1193 | function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses) { |
7423f116 | 1194 | $eventsbyday = array(); |
1195 | $typesbyday = array(); | |
1196 | $durationbyday = array(); | |
1197 | ||
1198 | if($events === false) { | |
1199 | return; | |
1200 | } | |
1201 | ||
7423f116 | 1202 | foreach($events as $event) { |
7423f116 | 1203 | |
7b38bfa6 | 1204 | $startdate = usergetdate($event->timestart); |
b4892fa2 | 1205 | // Set end date = start date if no duration |
1206 | if ($event->timeduration) { | |
1207 | $enddate = usergetdate($event->timestart + $event->timeduration - 1); | |
1208 | } else { | |
1209 | $enddate = $startdate; | |
1210 | } | |
7423f116 | 1211 | |
7b38bfa6 | 1212 | // Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair |
ef618501 | 1213 | if(!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && ($enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month)) { |
7b38bfa6 | 1214 | // Out of bounds |
1215 | continue; | |
7423f116 | 1216 | } |
1217 | ||
7b38bfa6 | 1218 | $eventdaystart = intval($startdate['mday']); |
7423f116 | 1219 | |
7b38bfa6 | 1220 | if($startdate['mon'] == $month && $startdate['year'] == $year) { |
1221 | // Give the event to its day | |
1222 | $eventsbyday[$eventdaystart][] = $event->id; | |
7423f116 | 1223 | |
7b38bfa6 | 1224 | // Mark the day as having such an event |
9064751b | 1225 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1226 | $typesbyday[$eventdaystart]['startglobal'] = true; |
b4892fa2 | 1227 | // Set event class for global event |
90723839 | 1228 | $events[$event->id]->class = 'calendar_event_global'; |
7b38bfa6 | 1229 | } |
c3d3b6d4 | 1230 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1231 | $typesbyday[$eventdaystart]['startcourse'] = true; |
b4892fa2 | 1232 | // Set event class for course event |
90723839 | 1233 | $events[$event->id]->class = 'calendar_event_course'; |
7b38bfa6 | 1234 | } |
1235 | else if($event->groupid) { | |
1236 | $typesbyday[$eventdaystart]['startgroup'] = true; | |
b4892fa2 | 1237 | // Set event class for group event |
90723839 | 1238 | $events[$event->id]->class = 'calendar_event_group'; |
7b38bfa6 | 1239 | } |
1240 | else if($event->userid) { | |
1241 | $typesbyday[$eventdaystart]['startuser'] = true; | |
b4892fa2 | 1242 | // Set event class for user event |
90723839 | 1243 | $events[$event->id]->class = 'calendar_event_user'; |
7b38bfa6 | 1244 | } |
1245 | } | |
7423f116 | 1246 | |
7b38bfa6 | 1247 | if($event->timeduration == 0) { |
1248 | // Proceed with the next | |
1249 | continue; | |
1250 | } | |
7423f116 | 1251 | |
7b38bfa6 | 1252 | // The event starts on $month $year or before. So... |
ef618501 | 1253 | $lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0; |
7b38bfa6 | 1254 | |
1255 | // Also, it ends on $month $year or later... | |
1256 | $upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year); | |
1257 | ||
1258 | // Mark all days between $lowerbound and $upperbound (inclusive) as duration | |
1259 | for($i = $lowerbound + 1; $i <= $upperbound; ++$i) { | |
1260 | $durationbyday[$i][] = $event->id; | |
9064751b | 1261 | if($event->courseid == SITEID && $event->groupid == 0) { |
7b38bfa6 | 1262 | $typesbyday[$i]['durationglobal'] = true; |
1263 | } | |
c3d3b6d4 | 1264 | else if($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) { |
7b38bfa6 | 1265 | $typesbyday[$i]['durationcourse'] = true; |
1266 | } | |
1267 | else if($event->groupid) { | |
1268 | $typesbyday[$i]['durationgroup'] = true; | |
1269 | } | |
1270 | else if($event->userid) { | |
1271 | $typesbyday[$i]['durationuser'] = true; | |
7423f116 | 1272 | } |
1273 | } | |
7b38bfa6 | 1274 | |
7423f116 | 1275 | } |
1276 | return; | |
1277 | } | |
1278 | ||
08b4a4e1 RW |
1279 | /** |
1280 | * Get current module cache | |
1281 | * | |
1282 | * @param array $coursecache list of course cache | |
1283 | * @param string $modulename name of the module | |
1284 | * @param int $instance module instance number | |
1285 | * @return stdClass|bool $module information | |
1286 | */ | |
b63c0ee5 | 1287 | function calendar_get_module_cached(&$coursecache, $modulename, $instance) { |
1288 | $module = get_coursemodule_from_instance($modulename, $instance); | |
7423f116 | 1289 | |
1290 | if($module === false) return false; | |
b63c0ee5 | 1291 | if(!calendar_get_course_cached($coursecache, $module->course)) { |
7423f116 | 1292 | return false; |
1293 | } | |
1294 | return $module; | |
1295 | } | |
1296 | ||
08b4a4e1 RW |
1297 | /** |
1298 | * Get current course cache | |
1299 | * | |
1300 | * @param array $coursecache list of course cache | |
1301 | * @param int $courseid id of the course | |
1302 | * @return stdClass $coursecache[$courseid] return the specific course cache | |
1303 | */ | |
7423f116 | 1304 | function calendar_get_course_cached(&$coursecache, $courseid) { |
62d11d77 | 1305 | global $COURSE, $DB; |
1306 | ||
b571c6b3 | 1307 | if (!isset($coursecache[$courseid])) { |
1308 | if ($courseid == $COURSE->id) { | |
1309 | $coursecache[$courseid] = $COURSE; | |
1310 | } else { | |
62d11d77 | 1311 | $coursecache[$courseid] = $DB->get_record('course', array('id'=>$courseid)); |
b571c6b3 | 1312 | } |
7423f116 | 1313 | } |
1314 | return $coursecache[$courseid]; | |
1315 | } | |
1316 | ||
797cedc7 SH |
1317 | /** |
1318 | * Returns the courses to load events for, the | |
1319 | * | |
797cedc7 | 1320 | * @param array $courseeventsfrom An array of courses to load calendar events for |
08b4a4e1 | 1321 | * @param bool $ignorefilters specify the use of filters, false is set as default |
797cedc7 SH |
1322 | * @return array An array of courses, groups, and user to load calendar events for based upon filters |
1323 | */ | |
1324 | function calendar_set_filters(array $courseeventsfrom, $ignorefilters = false) { | |
1325 | global $USER, $CFG, $DB; | |
7423f116 | 1326 | |
797cedc7 SH |
1327 | // For backwards compatability we have to check whether the courses array contains |
1328 | // just id's in which case we need to load course objects. | |
1329 | $coursestoload = array(); | |
1330 | foreach ($courseeventsfrom as $id => $something) { | |
1331 | if (!is_object($something)) { | |
1332 | $coursestoload[] = $id; | |
1333 | unset($courseeventsfrom[$id]); | |
753b799d | 1334 | } |
dd97c328 | 1335 | } |
797cedc7 SH |
1336 | if (!empty($coursestoload)) { |
1337 | // TODO remove this in 2.2 | |
1338 | debugging('calendar_set_filters now preferes an array of course objects with preloaded contexts', DEBUG_DEVELOPER); | |
1339 | $courseeventsfrom = array_merge($courseeventsfrom, $DB->get_records_list('course', 'id', $coursestoload)); | |
89adb174 | 1340 | } |
7423f116 | 1341 | |
797cedc7 SH |
1342 | $courses = array(); |
1343 | $user = false; | |
1344 | $group = false; | |
7423f116 | 1345 | |
7fd5786e HB |
1346 | // capabilities that allow seeing group events from all groups |
1347 | // TODO: rewrite so that moodle/calendar:manageentries is not necessary here | |
1348 | $allgroupscaps = array('moodle/site:accessallgroups', 'moodle/calendar:manageentries'); | |
1349 | ||
797cedc7 | 1350 | $isloggedin = isloggedin(); |
d12e3ff2 | 1351 | |
797cedc7 SH |
1352 | if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_COURSE)) { |
1353 | $courses = array_keys($courseeventsfrom); | |
7423f116 | 1354 | } |
797cedc7 SH |
1355 | if ($ignorefilters || calendar_show_event_type(CALENDAR_EVENT_GLOBAL)) { |
1356 | $courses[] = SITEID; | |
7423f116 | 1357 | } |
797cedc7 SH |
1358 | $courses = array_unique($courses); |
1359 | sort($courses); | |
7c50db30 | 1360 | |
797cedc7 | 1361 | if (!empty($courses) && in_array(SITEID, $courses)) { |
7c50db30 | 1362 | // Sort courses for consistent colour highlighting |
1363 | // Effectively ignoring SITEID as setting as last course id | |
1364 | $key = array_search(SITEID, $courses); | |
797cedc7 SH |
1365 | unset($courses[$key]); |
1366 | $courses[] = SITEID; | |
0e6a8f4b | 1367 | } |
7423f116 | 1368 | |
797cedc7 SH |
1369 | if ($ignorefilters || ($isloggedin && calendar_show_event_type(CALENDAR_EVENT_USER))) { |
1370 | $user = $USER->id; | |
7423f116 | 1371 | } |
257e3f4c | 1372 | |
797cedc7 SH |
1373 | if (!empty($courseeventsfrom) && (calendar_show_event_type(CALENDAR_EVENT_GROUP) || $ignorefilters)) { |
1374 | ||
7fd5786e HB |
1375 | if (count($courseeventsfrom)==1) { |
1376 | $course = reset($courseeventsfrom); | |
6ca657a7 | 1377 | if (has_any_capability($allgroupscaps, context_course::instance($course->id))) { |
7fd5786e HB |
1378 | $coursegroups = groups_get_all_groups($course->id, 0, 0, 'g.id'); |
1379 | $group = array_keys($coursegroups); | |
7423f116 | 1380 | } |
7fd5786e HB |
1381 | } |
1382 | if ($group === false) { | |
1383 | if (!empty($CFG->calendar_adminseesall) && has_any_capability($allgroupscaps, get_system_context())) { | |
1384 | $group = true; | |
1385 | } else if ($isloggedin) { | |
1386 | $groupids = array(); | |
c46da759 | 1387 | |
7fd5786e HB |
1388 | // We already have the courses to examine in $courses |
1389 | // For each course... | |
1390 | foreach ($courseeventsfrom as $courseid => $course) { | |
1391 | // If the user is an editing teacher in there, | |
1392 | if (!empty($USER->groupmember[$course->id])) { | |
1393 | // We've already cached the users groups for this course so we can just use that | |
1394 | $groupids = array_merge($groupids, $USER->groupmember[$course->id]); | |
1395 | } else if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) { | |
1396 | // If this course has groups, show events from all of those related to the current user | |
1397 | $coursegroups = groups_get_user_groups($course->id, $USER->id); | |
1398 | $groupids = array_merge($groupids, $coursegroups['0']); | |
1399 | } | |
1400 | } | |
1401 | if (!empty($groupids)) { | |
1402 | $group = $groupids; | |
1403 | } | |
6c9584d1 | 1404 | } |
7423f116 | 1405 | } |
7423f116 | 1406 | } |
797cedc7 SH |
1407 | if (empty($courses)) { |
1408 | $courses = false; | |
7423f116 | 1409 | } |
797cedc7 SH |
1410 | |
1411 | return array($courses, $group, $user); | |
7423f116 | 1412 | } |
1413 | ||
08b4a4e1 RW |
1414 | /** |
1415 | * Return the capability for editing calendar event | |
1416 | * | |
1417 | * @param calendar_event $event event object | |
1418 | * @return bool capability to edit event | |
1419 | */ | |
7423f116 | 1420 | function calendar_edit_event_allowed($event) { |
62d11d77 | 1421 | global $USER, $DB; |
7423f116 | 1422 | |
2ac8da76 | 1423 | // Must be logged in |
1424 | if (!isloggedin()) { | |
1425 | return false; | |
1426 | } | |
1427 | ||
89491dbd | 1428 | // can not be using guest account |
2396a414 | 1429 | if (isguestuser()) { |
e295df44 | 1430 | return false; |
89491dbd | 1431 | } |
e295df44 | 1432 | |
6ca657a7 | 1433 | $sitecontext = context_system::instance(); |
89491dbd | 1434 | // if user has manageentries at site level, return true |
28ee98c5 | 1435 | if (has_capability('moodle/calendar:manageentries', $sitecontext)) { |
89491dbd | 1436 | return true; |
f52f7413 | 1437 | } |
e295df44 | 1438 | |
c0a2c361 | 1439 | // if groupid is set, it's definitely a group event |
438e4add | 1440 | if (!empty($event->groupid)) { |
f63d2922 | 1441 | // Allow users to add/edit group events if: |
1442 | // 1) They have manageentries (= entries for whole course) | |
1443 | // 2) They have managegroupentries AND are in the group | |
62d11d77 | 1444 | $group = $DB->get_record('groups', array('id'=>$event->groupid)); |
f63d2922 | 1445 | return $group && ( |
f4700b91 RW |
1446 | has_capability('moodle/calendar:manageentries', $event->context) || |
1447 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
f63d2922 | 1448 | && groups_is_member($event->groupid))); |
438e4add | 1449 | } else if (!empty($event->courseid)) { |
c0a2c361 | 1450 | // if groupid is not set, but course is set, |
1451 | // it's definiely a course event | |
f4700b91 | 1452 | return has_capability('moodle/calendar:manageentries', $event->context); |
438e4add | 1453 | } else if (!empty($event->userid) && $event->userid == $USER->id) { |
c0a2c361 | 1454 | // if course is not set, but userid id set, it's a user event |
f4700b91 RW |
1455 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
1456 | } else if (!empty($event->userid)) { | |
1457 | return (has_capability('moodle/calendar:manageentries', $event->context)); | |
e295df44 | 1458 | } |
7423f116 | 1459 | return false; |
1460 | } | |
1461 | ||
797cedc7 SH |
1462 | /** |
1463 | * Returns the default courses to display on the calendar when there isn't a specific | |
1464 | * course to display. | |
1465 | * | |
08b4a4e1 | 1466 | * @return array $courses Array of courses to display |
797cedc7 SH |
1467 | */ |
1468 | function calendar_get_default_courses() { | |
1469 | global $CFG, $DB; | |
7423f116 | 1470 | |
4f0c2d00 | 1471 | if (!isloggedin()) { |
2ef75eee | 1472 | return array(); |
1473 | } | |
1474 | ||
7423f116 | 1475 | $courses = array(); |
d083084a | 1476 | if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', context_system::instance())) { |
797cedc7 | 1477 | list ($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx'); |
d083084a | 1478 | $sql = "SELECT c.* $select |
797cedc7 | 1479 | FROM {course} c |
d083084a ARN |
1480 | $join |
1481 | WHERE EXISTS (SELECT 1 FROM {event} e WHERE e.courseid = c.id) | |
1482 | "; | |
797cedc7 SH |
1483 | $courses = $DB->get_records_sql($sql, null, 0, 20); |
1484 | foreach ($courses as $course) { | |
d083084a | 1485 | context_helper::preload_from_record($course); |
86f092d2 | 1486 | } |
797cedc7 | 1487 | return $courses; |
95a89225 | 1488 | } |
e295df44 | 1489 | |
df997f84 | 1490 | $courses = enrol_get_my_courses(); |
37d87d11 | 1491 | |
1492 | return $courses; | |
7423f116 | 1493 | } |
1494 | ||
08b4a4e1 RW |
1495 | /** |
1496 | * Display calendar preference button | |
1497 | * | |
1498 | * @param stdClass $course course object | |
1499 | * @return string return preference button in html | |
1500 | */ | |
797cedc7 SH |
1501 | function calendar_preferences_button(stdClass $course) { |
1502 | global $OUTPUT; | |
7423f116 | 1503 | |
1504 | // Guests have no preferences | |
4f0c2d00 | 1505 | if (!isloggedin() || isguestuser()) { |
7423f116 | 1506 | return ''; |
1507 | } | |
1508 | ||
797cedc7 | 1509 | return $OUTPUT->single_button(new moodle_url('/calendar/preferences.php', array('course' => $course->id)), get_string("preferences", "calendar")); |
7423f116 | 1510 | } |
1511 | ||
08b4a4e1 RW |
1512 | /** |
1513 | * Get event format time | |
1514 | * | |
1515 | * @param calendar_event $event event object | |
1516 | * @param int $now current time in gmt | |
1517 | * @param array $linkparams list of params for event link | |
1518 | * @param bool $usecommonwords the words as formatted date/time. | |
1519 | * @param int $showtime determine the show time GMT timestamp | |
1520 | * @return string $eventtime link/string for event time | |
1521 | */ | |
0f927f1e | 1522 | function calendar_format_event_time($event, $now, $linkparams = null, $usecommonwords = true, $showtime=0) { |
8f896582 | 1523 | $startdate = usergetdate($event->timestart); |
1524 | $enddate = usergetdate($event->timestart + $event->timeduration); | |
1525 | $usermidnightstart = usergetmidnight($event->timestart); | |
1526 | ||
1527 | if($event->timeduration) { | |
1528 | // To avoid doing the math if one IF is enough :) | |
1529 | $usermidnightend = usergetmidnight($event->timestart + $event->timeduration); | |
1530 | } | |
1531 | else { | |
1532 | $usermidnightend = $usermidnightstart; | |
1533 | } | |
1534 | ||
0f927f1e SH |
1535 | if (empty($linkparams) || !is_array($linkparams)) { |
1536 | $linkparams = array(); | |
1537 | } | |
1538 | $linkparams['view'] = 'day'; | |
1539 | ||
8f896582 | 1540 | // OK, now to get a meaningful display... |
1541 | // First of all we have to construct a human-readable date/time representation | |
1542 | ||
b4892fa2 | 1543 | if($event->timeduration) { |
8f896582 | 1544 | // It has a duration |
b4892fa2 | 1545 | if($usermidnightstart == $usermidnightend || |
1546 | ($event->timestart == $usermidnightstart) && ($event->timeduration == 86400 || $event->timeduration == 86399) || | |
1547 | ($event->timestart + $event->timeduration <= $usermidnightstart + 86400)) { | |
8f896582 | 1548 | // But it's all on the same day |
8f896582 | 1549 | $timestart = calendar_time_representation($event->timestart); |
1550 | $timeend = calendar_time_representation($event->timestart + $event->timeduration); | |
b4892fa2 | 1551 | $time = $timestart.' <strong>»</strong> '.$timeend; |
1552 | ||
1553 | if ($event->timestart == $usermidnightstart && ($event->timeduration == 86400 || $event->timeduration == 86399)) { | |
1554 | $time = get_string('allday', 'calendar'); | |
1555 | } | |
8f896582 | 1556 | |
1557 | // Set printable representation | |
b4892fa2 | 1558 | if (!$showtime) { |
1559 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
0f927f1e SH |
1560 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']); |
1561 | $eventtime = html_writer::link($url, $day).', '.$time; | |
b4892fa2 | 1562 | } else { |
1563 | $eventtime = $time; | |
1564 | } | |
1565 | } else { | |
8f896582 | 1566 | // It spans two or more days |
b4892fa2 | 1567 | $daystart = calendar_day_representation($event->timestart, $now, $usecommonwords).', '; |
1568 | if ($showtime == $usermidnightstart) { | |
1569 | $daystart = ''; | |
1570 | } | |
8f896582 | 1571 | $timestart = calendar_time_representation($event->timestart); |
b4892fa2 | 1572 | $dayend = calendar_day_representation($event->timestart + $event->timeduration, $now, $usecommonwords).', '; |
1573 | if ($showtime == $usermidnightend) { | |
1574 | $dayend = ''; | |
1575 | } | |
8f896582 | 1576 | $timeend = calendar_time_representation($event->timestart + $event->timeduration); |
1577 | ||
1578 | // Set printable representation | |
b4892fa2 | 1579 | if ($now >= $usermidnightstart && $now < ($usermidnightstart + 86400)) { |
0f927f1e SH |
1580 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']); |
1581 | $eventtime = $timestart.' <strong>»</strong> '.html_writer::link($url, $dayend).$timeend; | |
b4892fa2 | 1582 | } else { |
0f927f1e SH |
1583 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $enddate['mday'], $enddate['mon'], $enddate['year']); |
1584 | $eventtime = html_writer::link($url, $daystart).$timestart.' <strong>»</strong> '; | |
1d5bd3d2 | 1585 | |
0f927f1e SH |
1586 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $startdate['mday'], $startdate['mon'], $startdate['year']); |
1587 | $eventtime .= html_writer::link($url, $dayend).$timeend; | |
b4892fa2 | 1588 | } |
8f896582 | 1589 | } |
b4892fa2 | 1590 | } else { |
2af268a1 | 1591 | $time = calendar_time_representation($event->timestart); |
8f896582 | 1592 | |
1593 | // Set printable representation | |
b4892fa2 | 1594 | if (!$showtime) { |
1595 | $day = calendar_day_representation($event->timestart, $now, $usecommonwords); | |
0f927f1e | 1596 | $url = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', $linkparams), $startdate['mday'], $startdate['mon'], $startdate['year']); |
2af268a1 | 1597 | $eventtime = html_writer::link($url, $day).', '.trim($time); |
b4892fa2 | 1598 | } else { |
1599 | $eventtime = $time; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | if($event->timestart + $event->timeduration < $now) { | |
1604 | // It has expired | |
1605 | $eventtime = '<span class="dimmed_text">'.str_replace(' href=', ' class="dimmed" href=', $eventtime).'</span>'; | |
8f896582 | 1606 | } |
09d36284 | 1607 | |
8f896582 | 1608 | return $eventtime; |
1609 | } | |
054193be | 1610 | |
08b4a4e1 RW |
1611 | /** |
1612 | * Display month selector options | |
1613 | * | |
1614 | * @param string $name for the select element | |
1615 | * @param string|array $selected options for select elements | |
1616 | */ | |
86f092d2 | 1617 | function calendar_print_month_selector($name, $selected) { |
86f092d2 | 1618 | $months = array(); |
86f092d2 | 1619 | for ($i=1; $i<=12; $i++) { |
76ab1c33 | 1620 | $months[$i] = userdate(gmmktime(12, 0, 0, $i, 15, 2000), '%B'); |
86f092d2 | 1621 | } |
5c576552 | 1622 | echo html_writer::label(get_string('months'), 'menu'. $name, false, array('class' => 'accesshide')); |
d776d59e | 1623 | echo html_writer::select($months, $name, $selected, false); |
86f092d2 | 1624 | } |
1625 | ||
797cedc7 SH |
1626 | /** |
1627 | * Checks to see if the requested type of event should be shown for the given user. | |
1628 | * | |
1629 | * @param CALENDAR_EVENT_GLOBAL|CALENDAR_EVENT_COURSE|CALENDAR_EVENT_GROUP|CALENDAR_EVENT_USER $type | |
1630 | * The type to check the display for (default is to display all) | |
1631 | * @param stdClass|int|null $user The user to check for - by default the current user | |
1632 | * @return bool True if the tyep should be displayed false otherwise | |
1633 | */ | |
1634 | function calendar_show_event_type($type, $user = null) { | |
1635 | $default = CALENDAR_EVENT_GLOBAL + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP + CALENDAR_EVENT_USER; | |
1636 | if (get_user_preferences('calendar_persistflt', 0, $user) === 0) { | |
1637 | global $SESSION; | |
1638 | if (!isset($SESSION->calendarshoweventtype)) { | |
1639 | $SESSION->calendarshoweventtype = $default; | |
1640 | } | |
1641 | return $SESSION->calendarshoweventtype & $type; | |
1642 | } else { | |
1643 | return get_user_preferences('calendar_savedflt', $default, $user) & $type; | |
054193be | 1644 | } |
797cedc7 SH |
1645 | } |
1646 | ||
1647 | /** | |
1648 | * Sets the display of the event type given $display. | |
08b4a4e1 | 1649 | * |
797cedc7 SH |
1650 | * If $display = true the event type will be shown. |
1651 | * If $display = false the event type will NOT be shown. | |
1652 | * If $display = null the current value will be toggled and saved. | |
1653 | * | |
08b4a4e1 RW |
1654 | * @param CALENDAR_EVENT_GLOBAL|CALENDAR_EVENT_COURSE|CALENDAR_EVENT_GROUP|CALENDAR_EVENT_USER $type object of CALENDAR_EVENT_XXX |
1655 | * @param bool $display option to display event type | |
1656 | * @param stdClass|int $user moodle user object or id, null means current user | |
797cedc7 SH |
1657 | */ |
1658 | function calendar_set_event_type_display($type, $display = null, $user = null) { | |
1659 | $persist = get_user_preferences('calendar_persistflt', 0, $user); | |
1660 | $default = CALENDAR_EVENT_GLOBAL + CALENDAR_EVENT_COURSE + CALENDAR_EVENT_GROUP + CALENDAR_EVENT_USER; | |
1661 | if ($persist === 0) { | |
1662 | global $SESSION; | |
1663 | if (!isset($SESSION->calendarshoweventtype)) { | |
1664 | $SESSION->calendarshoweventtype = $default; | |
1665 | } | |
1666 | $preference = $SESSION->calendarshoweventtype; | |
1667 | } else { | |
1668 | $preference = get_user_preferences('calendar_savedflt', $default, $user); | |
054193be | 1669 | } |
797cedc7 SH |
1670 | $current = $preference & $type; |
1671 | if ($display === null) { | |
1672 | $display = !$current; | |
054193be | 1673 | } |
797cedc7 SH |
1674 | if ($display && !$current) { |
1675 | $preference += $type; | |
1676 | } else if (!$display && $current) { | |
1677 | $preference -= $type; | |
054193be | 1678 | } |
797cedc7 SH |
1679 | if ($persist === 0) { |
1680 | $SESSION->calendarshoweventtype = $preference; | |
1681 | } else { | |
1682 | if ($preference == $default) { | |
1683 | unset_user_preference('calendar_savedflt', $user); | |
1684 | } else { | |
1685 | set_user_preference('calendar_savedflt', $preference, $user); | |
1686 | } | |
054193be | 1687 | } |
054193be | 1688 | } |
1689 | ||
08b4a4e1 RW |
1690 | /** |
1691 | * Get calendar's allowed types | |
1692 | * | |
1693 | * @param stdClass $allowed list of allowed edit for event type | |
1694 | * @param stdClass|int $course object of a course or course id | |
1695 | */ | |
797cedc7 SH |
1696 | function calendar_get_allowed_types(&$allowed, $course = null) { |
1697 | global $USER, $CFG, $DB; | |
b85b25eb | 1698 | $allowed = new stdClass(); |
797cedc7 | 1699 | $allowed->user = has_capability('moodle/calendar:manageownentries', get_system_context()); |
86ac8b24 | 1700 | $allowed->groups = false; // This may change just below |
1701 | $allowed->courses = false; // This may change just below | |
6ca657a7 | 1702 | $allowed->site = has_capability('moodle/calendar:manageentries', context_course::instance(SITEID)); |
86ac8b24 | 1703 | |
797cedc7 SH |
1704 | if (!empty($course)) { |
1705 | if (!is_object($course)) { | |
1706 | $course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST); | |
1707 | } | |
1708 | if ($course->id != SITEID) { | |
6ca657a7 | 1709 | $coursecontext = context_course::instance($course->id); |
3c0ea9b2 | 1710 | $allowed->user = has_capability('moodle/calendar:manageownentries', $coursecontext); |
86ac8b24 | 1711 | |
797cedc7 SH |
1712 | if (has_capability('moodle/calendar:manageentries', $coursecontext)) { |
1713 | $allowed->courses = array($course->id => 1); | |
aa6c1ced | 1714 | |
797cedc7 SH |
1715 | if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) { |
1716 | $allowed->groups = groups_get_all_groups($course->id); | |
1717 | } | |
1718 | } else if (has_capability('moodle/calendar:managegroupentries', $coursecontext)) { | |
1719 | if($course->groupmode != NOGROUPS || !$course->groupmodeforce) { | |
1720 | $allowed->groups = groups_get_all_groups($course->id); | |
1721 | } | |
f63d2922 | 1722 | } |
86ac8b24 | 1723 | } |
1724 | } | |
1725 | } | |
1726 | ||
1727 | /** | |
08b4a4e1 | 1728 | * See if user can add calendar entries at all |
86ac8b24 | 1729 | * used to print the "New Event" button |
08b4a4e1 RW |
1730 | * |
1731 | * @param stdClass $course object of a course or course id | |
1732 | * @return bool has the capability to add at least one event type | |
86ac8b24 | 1733 | */ |
797cedc7 SH |
1734 | function calendar_user_can_add_event($course) { |
1735 | if (!isloggedin() || isguestuser()) { | |
1736 | return false; | |
1737 | } | |
1738 | calendar_get_allowed_types($allowed, $course); | |
e295df44 | 1739 | return (bool)($allowed->user || $allowed->groups || $allowed->courses || $allowed->site); |
86ac8b24 | 1740 | } |
76d9df3f SH |
1741 | |
1742 | /** | |
1743 | * Check wether the current user is permitted to add events | |
1744 | * | |
08b4a4e1 RW |
1745 | * @param stdClass $event object of event |
1746 | * @return bool has the capability to add event | |
76d9df3f SH |
1747 | */ |
1748 | function calendar_add_event_allowed($event) { | |
1749 | global $USER, $DB; | |
1750 | ||
1751 | // can not be using guest account | |
4f0c2d00 | 1752 | if (!isloggedin() or isguestuser()) { |
76d9df3f SH |
1753 | return false; |
1754 | } | |
1755 | ||
6ca657a7 | 1756 | $sitecontext = context_system::instance(); |
76d9df3f SH |
1757 | // if user has manageentries at site level, always return true |
1758 | if (has_capability('moodle/calendar:manageentries', $sitecontext)) { | |
1759 | return true; | |
1760 | } | |
1761 | ||
1762 | switch ($event->eventtype) { | |
1763 | case 'course': | |
f4700b91 | 1764 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1765 | |
1766 | case 'group': | |
1767 | // Allow users to add/edit group events if: | |
1768 | // 1) They have manageentries (= entries for whole course) | |
1769 | // 2) They have managegroupentries AND are in the group | |
1770 | $group = $DB->get_record('groups', array('id'=>$event->groupid)); | |
1771 | return $group && ( | |
f4700b91 RW |
1772 | has_capability('moodle/calendar:manageentries', $event->context) || |
1773 | (has_capability('moodle/calendar:managegroupentries', $event->context) | |
76d9df3f SH |
1774 | && groups_is_member($event->groupid))); |
1775 | ||
1776 | case 'user': | |
1777 | if ($event->userid == $USER->id) { | |
f4700b91 | 1778 | return (has_capability('moodle/calendar:manageownentries', $event->context)); |
76d9df3f SH |
1779 | } |
1780 | //there is no 'break;' intentionally | |
1781 | ||
1782 | case 'site': | |
f4700b91 | 1783 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1784 | |
1785 | default: | |
f4700b91 | 1786 | return has_capability('moodle/calendar:manageentries', $event->context); |
76d9df3f SH |
1787 | } |
1788 | } | |
1789 | ||
1790 | /** | |
08b4a4e1 | 1791 | * Manage calendar events |
76d9df3f SH |
1792 | * |
1793 | * This class provides the required functionality in order to manage calendar events. | |
1794 | * It was introduced as part of Moodle 2.0 and was created in order to provide a | |
1795 | * better framework for dealing with calendar events in particular regard to file | |
1796 | * handling through the new file API | |
1797 | * | |
08b4a4e1 RW |
1798 | * @package core_calendar |
1799 | * @category calendar | |
1800 | * @copyright 2009 Sam Hemelryk | |
1801 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
1802 | * | |
76d9df3f SH |
1803 | * @property int $id The id within the event table |
1804 | * @property string $name The name of the event | |
1805 | * @property string $description The description of the event | |
1806 | * @property int $format The format of the description FORMAT_? | |
1807 | * @property int $courseid The course the event is associated with (0 if none) | |
1808 | * @property int $groupid The group the event is associated with (0 if none) | |
1809 | * @property int $userid The user the event is associated with (0 if none) | |
1810 | * @property int $repeatid If this is a repeated event this will be set to the | |
1811 | * id of the original | |
1812 | * @property string $modulename If added by a module this will be the module name | |
1813 | * @property int $instance If added by a module this will be the module instance | |
1814 | * @property string $eventtype The event type | |
1815 | * @property int $timestart The start time as a timestamp | |
1816 | * @property int $timeduration The duration of the event in seconds | |
1817 | * @property int $visible 1 if the event is visible | |
1818 | * @property int $uuid ? | |
1819 | * @property int $sequence ? | |
1820 | * @property int $timemodified The time last modified as a timestamp | |
1821 | */ | |
1822 | class calendar_event { | |
1823 | ||
08b4a4e1 | 1824 | /** @var array An object containing the event properties can be accessed via the magic __get/set methods */ |
76d9df3f | 1825 | protected $properties = null; |
08b4a4e1 | 1826 | |
76d9df3f | 1827 | /** |
08b4a4e1 | 1828 | * @var string The converted event discription with file paths resolved. This gets populated when someone requests description for the first time */ |
76d9df3f | 1829 | protected $_description = null; |
08b4a4e1 RW |
1830 | |
1831 | /** @var array The options to use with this description editor */ | |
76d9df3f SH |
1832 | protected $editoroptions = array( |
1833 | 'subdirs'=>false, | |
1834 | 'forcehttps'=>false, | |
2b35ed1a | 1835 | 'maxfiles'=>-1, |
76d9df3f SH |
1836 | 'maxbytes'=>null, |
1837 | 'trusttext'=>false); | |
08b4a4e1 RW |
1838 | |
1839 | /** @var object The context to use with the description editor */ | |
76d9df3f SH |
1840 | protected $editorcontext = null; |
1841 | ||
1842 | /** | |
1843 | * Instantiates a new event and optionally populates its properties with the | |
1844 | * data provided | |
1845 | * | |
1846 | * @param stdClass $data Optional. An object containing the properties to for | |
1847 | * an event | |
1848 | */ | |
1849 | public function __construct($data=null) { | |
c203a277 | 1850 | global $CFG, $USER; |
76d9df3f SH |
1851 | |
1852 | // First convert to object if it is not already (should either be object or assoc array) | |
1853 | if (!is_object($data)) { | |
1854 | $data = (object)$data; | |
1855 | } | |
1856 | ||
1857 | $this->editoroptions['maxbytes'] = $CFG->maxbytes; | |
1858 | ||
1859 | $data->eventrepeats = 0; | |
1860 | ||
1861 | if (empty($data->id)) { | |
1862 | $data->id = null; | |
1863 | } | |
1864 | ||
c203a277 SH |
1865 | // Default to a user event |
1866 | if (empty($data->eventtype)) { | |
1867 | $data->eventtype = 'user'; | |
1868 | } | |
1869 | ||
1870 | // Default to the current user | |
1871 | if (empty($data->userid)) { | |
1872 | $data->userid = $USER->id; | |
1873 | } | |
1874 | ||
76d9df3f SH |
1875 | if (!empty($data->timeduration) && is_array($data->timeduration)) { |
1876 | $data->timeduration = make_timestamp($data->timeduration['year'], $data->timeduration['month'], $data->timeduration['day'], $data->timeduration['hour'], $data->timeduration['minute']) - $data->timestart; | |
1877 | } | |
1878 | if (!empty($data->description) && is_array($data->description)) { | |
1879 | $data->format = $data->description['format']; | |
1880 | $data->description = $data->description['text']; | |
1881 | } else if (empty($data->description)) { | |
1882 | $data->description = ''; | |
20e5da7d | 1883 | $data->format = editors_get_preferred_format(); |
76d9df3f | 1884 | } |
c203a277 SH |
1885 | // Ensure form is defaulted correctly |
1886 | if (empty($data->format)) { | |
1887 | $data->format = editors_get_preferred_format(); | |
1888 | } | |
76d9df3f | 1889 | |
f4700b91 RW |
1890 | if (empty($data->context)) { |
1891 | $data->context = $this->calculate_context($data); | |
1892 | } | |
76d9df3f SH |
1893 | $this->properties = $data; |
1894 | } | |
1895 | ||
1896 | /** | |
1897 | * Magic property method | |
1898 | * | |
1899 | * Attempts to call a set_$key method if one exists otherwise falls back | |
1900 | * to simply set the property | |
1901 | * | |
08b4a4e1 RW |
1902 | * @param string $key property name |
1903 | * @param mixed $value value of the property | |
76d9df3f SH |
1904 | */ |
1905 | public function __set($key, $value) { | |
1906 | if (method_exists($this, 'set_'.$key)) { | |
1907 | $this->{'set_'.$key}($value); | |
1908 | } | |
1909 | $this->properties->{$key} = $value; | |
1910 | } | |
1911 | ||
1912 | /** | |
1913 | * Magic get method | |
1914 | * | |
1915 | * Attempts to call a get_$key method to return the property and ralls over | |
1916 | * to return the raw property | |
1917 | * | |
08b4a4e1 RW |
1918 | * @param string $key property name |
1919 | * @return mixed property value | |
76d9df3f SH |
1920 | */ |
1921 | public function __get($key) { | |
1922 | if (method_exists($this, 'get_'.$key)) { | |
1923 | return $this->{'get_'.$key}(); | |
1924 | } | |
c203a277 SH |
1925 | if (!isset($this->properties->{$key})) { |
1926 | throw new coding_exception('Undefined property requested'); | |
1927 | } | |
76d9df3f SH |
1928 | return $this->properties->{$key}; |
1929 | } | |
1930 | ||
1931 | /** | |
1932 | * Stupid PHP needs an isset magic method if you use the get magic method and | |
1933 | * still want empty calls to work.... blah ~! | |
1934 | * | |
08b4a4e1 RW |
1935 | * @param string $key $key property name |
1936 | * @return bool|mixed property value, false if property is not exist | |
76d9df3f SH |
1937 | */ |
1938 | public function __isset($key) { | |
1939 | return !empty($this->properties->{$key}); | |
1940 | } | |
1941 | ||
f4700b91 RW |
1942 | /** |
1943 | * Calculate the context value needed for calendar_event. | |
1944 | * Event's type can be determine by the available value store in $data | |
1945 | * It is important to check for the existence of course/courseid to determine | |
1946 | * the course event. | |
1947 | * Default value is set to CONTEXT_USER | |
1948 | * | |
08b4a4e1 RW |
1949 | * @param stdClass $data information about event |
1950 | * @return stdClass The context object. | |
f4700b91 RW |
1951 | */ |
1952 | protected function calculate_context(stdClass $data) { | |
025d82e0 | 1953 | global $USER, $DB; |
f4700b91 | 1954 | |
f4700b91 RW |
1955 | $context = null; |
1956 | if (isset($data->courseid) && $data->courseid > 0) { | |
6ca657a7 | 1957 | $context = context_course::instance($data->courseid); |
f4700b91 | 1958 | } else if (isset($data->course) && $data->course > 0) { |
6ca657a7 | 1959 | $context = context_course::instance($data->course); |
f4700b91 RW |
1960 | } else if (isset($data->groupid) && $data->groupid > 0) { |
1961 | $group = $DB->get_record('groups', array('id'=>$data->groupid)); | |
6ca657a7 | 1962 | $context = context_course::instance($group->courseid); |
f4700b91 | 1963 | } else if (isset($data->userid) && $data->userid > 0 && $data->userid == $USER->id) { |
6ca657a7 | 1964 | $context = context_user::instance($data->userid); |
f4700b91 RW |
1965 | } else if (isset($data->userid) && $data->userid > 0 && $data->userid != $USER->id && |
1966 | isset($data->instance) && $data->instance > 0) { | |
18d63ffb | 1967 | $cm = get_coursemodule_from_instance($data->modulename, $data->instance, 0, false, MUST_EXIST); |
6ca657a7 | 1968 | $context = context_course::instance($cm->course); |
f4700b91 | 1969 | } else { |
6ca657a7 | 1970 | $context = context_user::instance(); |
f4700b91 RW |
1971 | } |
1972 | ||
1973 | return $context; | |
1974 | } | |
1975 | ||
76d9df3f SH |
1976 | /** |
1977 | * Returns an array of editoroptions for this event: Called by __get | |
1978 | * Please use $blah = $event->editoroptions; | |
08b4a4e1 RW |
1979 | * |
1980 | * @return array event editor options | |
76d9df3f SH |
1981 | */ |
1982 | protected function get_editoroptions() { | |
1983 | return $this->editoroptions; | |
1984 | } | |
1985 | ||
1986 | /** | |
1987 | * Returns an event description: Called by __get | |
1988 | * Please use $blah = $event->description; | |
1989 | * | |
08b4a4e1 | 1990 | * @return string event description |
76d9df3f SH |
1991 | */ |
1992 | protected function get_description() { | |
f4700b91 | 1993 | global $CFG; |
99d19c13 PS |
1994 | |
1995 | require_once($CFG->libdir . '/filelib.php'); | |
1996 | ||
76d9df3f SH |
1997 | if ($this->_description === null) { |
1998 | // Check if we have already resolved the context for this event | |
1999 | if ($this->editorcontext === null) { | |
2000 | // Switch on the event type to decide upon the appropriate context | |
2001 | // to use for this event | |
f4700b91 RW |
2002 | $this->editorcontext = $this->properties->context; |
2003 | if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' | |
2004 | && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') { | |
2005 | return clean_text($this->properties->description, $this->properties->format); | |
76d9df3f SH |
2006 | } |
2007 | } | |
2008 | ||
2009 | // Work out the item id for the editor, if this is a repeated event then the files will | |
2010 | // be associated with the original | |
2011 | if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) { | |
2012 | $itemid = $this->properties->repeatid; | |
2013 | } else { | |
2014 | $itemid = $this->properties->id; | |
2015 | } | |
2016 | ||
2017 | // Convert file paths in the description so that things display correctly | |
64f93798 | 2018 | $this->_description = file_rewrite_pluginfile_urls($this->properties->description, 'pluginfile.php', $this->editorcontext->id, 'calendar', 'event_description', $itemid); |
76d9df3f SH |
2019 | // Clean the text so no nasties get through |
2020 | $this->_description = clean_text($this->_description, $this->properties->format); | |
2021 | } | |
2022 | // Finally return the description | |
2023 | return $this->_description; | |
2024 | } | |
2025 | ||
2026 | /** | |
2027 | * Return the number of repeat events there are in this events series | |
aa6c1ced | 2028 | * |
08b4a4e1 | 2029 | * @return int number of event repeated |
76d9df3f SH |
2030 | */ |
2031 | public function count_repeats() { | |
2032 | global $DB; | |
2033 | if (!empty($this->properties->repeatid)) { | |
2034 | $this->properties->eventrepeats = $DB->count_records('event', array('repeatid'=>$this->properties->repeatid)); | |
2035 | // We don't want to count ourselves | |
2036 | $this->properties->eventrepeats--; | |
2037 | } | |
2038 | return $this->properties->eventrepeats; | |
2039 | } | |
2040 | ||
2041 | /** | |
2042 | * Update or create an event within the database | |
2043 | * | |
2044 | * Pass in a object containing the event properties and this function will | |
2045 | * insert it into the database and deal with any associated files | |
2046 | * | |
2047 | * @see add_event() | |
2048 | * @see update_event() | |
2049 | * | |
08b4a4e1 RW |
2050 | * @param stdClass $data object of event |
2051 | * @param bool $checkcapability if moodle should check calendar managing capability or not | |
2052 | * @return bool event updated | |
76d9df3f | 2053 | */ |
1d5bd3d2 | 2054 | public function update($data, $checkcapability=true) { |
76d9df3f SH |
2055 | global $CFG, $DB, $USER; |
2056 | ||
438e4add SH |
2057 | foreach ($data as $key=>$value) { |
2058 | $this->properties->$key = $value; | |
2059 | } | |
2060 | ||
76d9df3f SH |
2061 | $this->properties->timemodified = time(); |
2062 | $usingeditor = (!empty($this->properties->description) && is_array($this->properties->description)); | |
2063 | ||
2064 | if (empty($this->properties->id) || $this->properties->id < 1) { | |
2065 | ||
1d5bd3d2 DC |
2066 | if ($checkcapability) { |
2067 | if (!calendar_add_event_allowed($this->properties)) { | |
2068 | print_error('nopermissiontoupdatecalendar'); | |
2069 | } | |
76d9df3f SH |
2070 | } |
2071 | ||
2072 | if ($usingeditor) { | |
2073 | switch ($this->properties->eventtype) { | |
2074 | case 'user': | |
f4700b91 | 2075 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2076 | $this->properties->courseid = 0; |
2077 | $this->properties->groupid = 0; | |
2078 | $this->properties->userid = $USER->id; | |
2079 | break; | |
2080 | case 'site': | |
f4700b91 | 2081 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2082 | $this->properties->courseid = SITEID; |
2083 | $this->properties->groupid = 0; | |
2084 | $this->properties->userid = $USER->id; | |
2085 | break; | |
2086 | case 'course': | |
f4700b91 | 2087 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2088 | $this->properties->groupid = 0; |
2089 | $this->properties->userid = $USER->id; | |
2090 | break; | |
2091 | case 'group': | |
f4700b91 | 2092 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2093 | $this->properties->userid = $USER->id; |
2094 | break; | |
2095 | default: | |
2096 | // Ewww we should NEVER get here, but just incase we do lets | |
2097 | // fail gracefully | |
2098 | $usingeditor = false; | |
2099 | break; | |
2100 | } | |
2101 | ||
2102 | $editor = $this->properties->description; | |
2103 | $this->properties->format = $this->properties->description['format']; | |
2104 | $this->properties->description = $this->properties->description['text']; | |
2105 | } | |
2106 | ||
2107 | // Insert the event into the database | |
2108 | $this->properties->id = $DB->insert_record('event', $this->properties); | |
2109 | ||
2110 | if ($usingeditor) { | |
2111 | $this->properties->description = file_save_draft_area_files( | |
2112 | $editor['itemid'], | |
2113 | $this->editorcontext->id, | |
64f93798 PS |
2114 | 'calendar', |
2115 | 'event_description', | |
76d9df3f SH |
2116 | $this->properties->id, |
2117 | $this->editoroptions, | |
2118 | $editor['text'], | |
2119 | $this->editoroptions['forcehttps']); | |
2120 | ||
2121 | $DB->set_field('event', 'description', $this->properties->description, array('id'=>$this->properties->id)); | |
2122 | } | |
aa6c1ced | 2123 | |
76d9df3f SH |
2124 | // Log the event entry. |
2125 | add_to_log($this->properties->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); | |
2126 | ||
2127 | $repeatedids = array(); | |
2128 | ||
2129 | if (!empty($this->properties->repeat)) { | |
2130 | $this->properties->repeatid = $this->properties->id; | |
2131 | $DB->set_field('event', 'repeatid', $this->properties->repeatid, array('id'=>$this->properties->id)); | |
2132 | ||
2133 | $eventcopy = clone($this->properties); | |
2134 | unset($eventcopy->id); | |
2135 | ||
2136 | for($i = 1; $i < $eventcopy->repeats; $i++) { | |
2137 | ||
2138 | $eventcopy->timestart = ($eventcopy->timestart+WEEKSECS) + dst_offset_on($eventcopy->timestart) - dst_offset_on($eventcopy->timestart+WEEKSECS); | |
2139 | ||
2140 | // Get the event id for the log record. | |
2141 | $eventcopyid = $DB->insert_record('event', $eventcopy); | |
2142 | ||
2143 | // If the context has been set delete all associated files | |
2144 | if ($usingeditor) { | |
2145 | $fs = get_file_storage(); | |
64f93798 | 2146 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2147 | foreach ($files as $file) { |
2148 | $fs->create_file_from_storedfile(array('itemid'=>$eventcopyid), $file); | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | $repeatedids[] = $eventcopyid; | |
2153 | // Log the event entry. | |
2154 | add_to_log($eventcopy->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventcopyid, $eventcopy->name); | |
2155 | } | |
2156 | } | |
2157 | ||
2158 | // Hook for tracking added events | |
2159 | self::calendar_event_hook('add_event', array($this->properties, $repeatedids)); | |
2160 | return true; | |
2161 | } else { | |
2162 | ||
1d5bd3d2 DC |
2163 | if ($checkcapability) { |
2164 | if(!calendar_edit_event_allowed($this->properties)) { | |
2165 | print_error('nopermissiontoupdatecalendar'); | |
2166 | } | |
76d9df3f SH |
2167 | } |
2168 | ||
2169 | if ($usingeditor) { | |
2170 | if ($this->editorcontext !== null) { | |
2171 | $this->properties->description = file_save_draft_area_files( | |
2172 | $this->properties->description['itemid'], | |
2173 | $this->editorcontext->id, | |
64f93798 PS |
2174 | 'calendar', |
2175 | 'event_description', | |
76d9df3f SH |
2176 | $this->properties->id, |
2177 | $this->editoroptions, | |
2178 | $this->properties->description['text'], | |
2179 | $this->editoroptions['forcehttps']); | |
2180 | } else { | |
2181 | $this->properties->format = $this->properties->description['format']; | |
2182 | $this->properties->description = $this->properties->description['text']; | |
2183 | } | |
2184 | } | |
2185 | ||
2186 | $event = $DB->get_record('event', array('id'=>$this->properties->id)); | |
2187 | ||
2188 | $updaterepeated = (!empty($this->properties->repeatid) && !empty($this->properties->repeateditall)); | |
2189 | ||
2190 | if ($updaterepeated) { | |
2191 | // Update all | |
2192 | if ($this->properties->timestart != $event->timestart) { | |
2193 | $timestartoffset = $this->properties->timestart - $event->timestart; | |
2194 | $sql = "UPDATE {event} | |
2195 | SET name = ?, | |
2196 | description = ?, | |
2197 | timestart = timestart + ?, | |
2198 | timeduration = ?, | |
2199 | timemodified = ? | |
2200 | WHERE repeatid = ?"; | |
2201 | $params = array($this->properties->name, $this->properties->description, $timestartoffset, $this->properties->timeduration, time(), $event->repeatid); | |
2202 | } else { | |
2203 | $sql = "UPDATE {event} SET name = ?, description = ?, timeduration = ?, timemodified = ? WHERE repeatid = ?"; | |
2204 | $params = array($this->properties->name, $this->properties->description, $this->properties->timeduration, time(), $event->repeatid); | |
2205 | } | |
2206 | $DB->execute($sql, $params); | |
2207 | ||
2208 | // Log the event update. | |
2209 | add_to_log($this->properties->courseid, 'calendar', 'edit all', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); | |
2210 | } else { | |
2211 | $DB->update_record('event', $this->properties); | |
ddaff608 SH |
2212 | $event = calendar_event::load($this->properties->id); |
2213 | $this->properties = $event->properties(); | |
76d9df3f SH |
2214 | add_to_log($this->properties->courseid, 'calendar', 'edit', 'event.php?action=edit&id='.$this->properties->id, $this->properties->name); |
2215 | } | |
2216 | ||
2217 | // Hook for tracking event updates | |
2218 | self::calendar_event_hook('update_event', array($this->properties, $updaterepeated)); | |
2219 | return true; | |
2220 | } | |
2221 | } | |
2222 | ||
2223 | /** | |
2224 | * Deletes an event and if selected an repeated events in the same series | |
2225 | * | |
2226 | * This function deletes an event, any associated events if $deleterepeated=true, | |
2227 | * and cleans up any files associated with the events. | |
2228 | * | |
2229 | * @see delete_event() | |
2230 | * | |
08b4a4e1 RW |
2231 | * @param bool $deleterepeated delete event repeatedly |
2232 | * @return bool succession of deleting event | |
76d9df3f SH |
2233 | */ |
2234 | public function delete($deleterepeated=false) { | |
f4700b91 | 2235 | global $DB; |
76d9df3f SH |
2236 | |
2237 | // If $this->properties->id is not set then something is wrong | |
2238 | if (empty($this->properties->id)) { | |
2239 | debugging('Attempting to delete an event before it has been loaded', DEBUG_DEVELOPER); | |
2240 | return false; | |
2241 | } | |
2242 | ||
2243 | // Delete the event | |
2244 | $DB->delete_records('event', array('id'=>$this->properties->id)); | |
2245 | ||
ff284fac AA |
2246 | // If we are deleting parent of a repeated event series, promote the next event in the series as parent |
2247 | if (($this->properties->id == $this->properties->repeatid) && !$deleterepeated) { | |
2248 | $newparent = $DB->get_field_sql("SELECT id from {event} where repeatid = ? order by id ASC", array($this->properties->id), IGNORE_MULTIPLE); | |
2249 | if (!empty($newparent)) { | |
2250 | $DB->execute("UPDATE {event} SET repeatid = ? WHERE repeatid = ?", array($newparent, $this->properties->id)); | |
5ff114ad AA |
2251 | // Get all records where the repeatid is the same as the event being removed |
2252 | $events = $DB->get_records('event', array('repeatid' => $newparent)); | |
2253 | // For each of the returned events trigger the event_update hook. | |
2254 | foreach ($events as $event) { | |
2255 | self::calendar_event_hook('update_event', array($event, false)); | |
2256 | } | |
ff284fac AA |
2257 | } |
2258 | } | |
2259 | ||
76d9df3f SH |
2260 | // If the editor context hasn't already been set then set it now |
2261 | if ($this->editorcontext === null) { | |
f4700b91 | 2262 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2263 | } |
2264 | ||
2265 | // If the context has been set delete all associated files | |
2266 | if ($this->editorcontext !== null) { | |
2267 | $fs = get_file_storage(); | |
64f93798 | 2268 | $files = $fs->get_area_files($this->editorcontext->id, 'calendar', 'event_description', $this->properties->id); |
76d9df3f SH |
2269 | foreach ($files as $file) { |
2270 | $file->delete(); | |
2271 | } | |
2272 | } | |
2273 | ||
2274 | // Fire the event deleted hook | |
2275 | self::calendar_event_hook('delete_event', array($this->properties->id, $deleterepeated)); | |
2276 | ||
2277 | // If we need to delete repeated events then we will fetch them all and delete one by one | |
2278 | if ($deleterepeated && !empty($this->properties->repeatid) && $this->properties->repeatid > 0) { | |
2279 | // Get all records where the repeatid is the same as the event being removed | |
2280 | $events = $DB->get_records('event', array('repeatid'=>$this->properties->repeatid)); | |
2281 | // For each of the returned events populate a calendar_event object and call delete | |
2282 | // make sure the arg passed is false as we are already deleting all repeats | |
2283 | foreach ($events as $event) { | |
2284 | $event = new calendar_event($event); | |
2285 | $event->delete(false); | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | return true; | |
2290 | } | |
2291 | ||
2292 | /** | |
2293 | * Fetch all event properties | |
2294 | * | |
2295 | * This function returns all of the events properties as an object and optionally | |
2296 | * can prepare an editor for the description field at the same time. This is | |
2297 | * designed to work when the properties are going to be used to set the default | |
2298 | * values of a moodle forms form. | |
2299 | * | |
2300 | * @param bool $prepareeditor If set to true a editor is prepared for use with | |
2301 | * the mforms editor element. (for description) | |
2302 | * @return stdClass Object containing event properties | |
2303 | */ | |
2304 | public function properties($prepareeditor=false) { | |
2305 | global $USER, $CFG, $DB; | |
2306 | ||
2307 | // First take a copy of the properties. We don't want to actually change the | |
2308 | // properties or we'd forever be converting back and forwards between an | |
2309 | // editor formatted description and not | |
2310 | $properties = clone($this->properties); | |
2311 | // Clean the description here | |
2312 | $properties->description = clean_text($properties->description, $properties->format); | |
2313 | ||
2314 | // If set to true we need to prepare the properties for use with an editor | |
2315 | // and prepare the file area | |
2316 | if ($prepareeditor) { | |
2317 | ||
2318 | // We may or may not have a property id. If we do then we need to work | |
2319 | // out the context so we can copy the existing files to the draft area | |
2320 | if (!empty($properties->id)) { | |
2321 | ||
2322 | if ($properties->eventtype === 'site') { | |
2323 | // Site context | |
f4700b91 | 2324 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2325 | } else if ($properties->eventtype === 'user') { |
2326 | // User context | |
f4700b91 | 2327 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2328 | } else if ($properties->eventtype === 'group' || $properties->eventtype === 'course') { |
2329 | // First check the course is valid | |
2330 | $course = $DB->get_record('course', array('id'=>$properties->courseid)); | |
2331 | if (!$course) { | |
2332 | print_error('invalidcourse'); | |
2333 | } | |
2334 | // Course context | |
f4700b91 | 2335 | $this->editorcontext = $this->properties->context; |
76d9df3f SH |
2336 | // We have a course and are within the course context so we had |
2337 | // better use the courses max bytes value | |
2338 | $this->editoroptions['maxbytes'] = $course->maxbytes; | |
2339 | } else { | |
2340 | // If we get here we have a custom event type as used by some | |
2341 | // modules. In this case the event will have been added by | |
2342 | // code and we won't need the editor | |
2343 | $this->editoroptions['maxbytes'] = 0; | |
2344 | $this->editoroptions['maxfiles'] = 0; | |
2345 | } | |
2346 | ||
2347 | if (empty($this->editorcontext) || empty($this->editorcontext->id)) { | |
2348 | $contextid = false; | |
2349 | } else { | |
2350 | // Get the context id that is what we really want | |
2351 | $contextid = $this->editorcontext->id; | |
2352 | } | |
2353 | } else { | |
2354 | ||
2355 | // If we get here then this is a new event in which case we don't need a | |
2356 | // context as there is no existing files to copy to the draft area. | |
2357 | $contextid = null; | |
2358 | } | |
2359 | ||
2360 | // If the contextid === false we don't support files so no preparing | |
2361 | // a draft area | |
2362 | if ($contextid !== false) { | |
2363 | // Just encase it has already been submitted | |
2364 | $draftiddescription = file_get_submitted_draft_itemid('description'); | |
2365 | // Prepare the draft area, this copies existing files to the draft area as well | |
64f93798 | 2366 | $properties->description = file_prepare_draft_area($draftiddescription, $contextid, 'calendar', 'event_description', $properties->id, $this->editoroptions, $properties->description); |
76d9df3f SH |
2367 | } else { |
2368 | $draftiddescription = 0; | |
2369 | } | |
aa6c1ced | 2370 | |
76d9df3f SH |
2371 | // Structure the description field as the editor requires |
2372 | $properties->description = array('text'=>$properties->description, 'format'=>$properties->format, 'itemid'=>$draftiddescription); | |
2373 | } | |
2374 | ||
2375 | // Finally return the properties | |
2376 | return $properties; | |
2377 | } | |
2378 | ||
2379 | /** | |
2380 | * Toggles the visibility of an event | |
2381 | * | |
2382 | * @param null|bool $force If it is left null the events visibility is flipped, | |
2383 | * If it is false the event is made hidden, if it is true it | |
2384 | * is made visible. | |
08b4a4e1 | 2385 | * @return bool if event is successfully updated, toggle will be visible |
76d9df3f SH |
2386 | */ |
2387 | public function toggle_visibility($force=null) { | |
2388 | global $CFG, $DB; | |
2389 | ||
2390 | // Set visible to the default if it is not already set | |
2391 | if (empty($this->properties->visible)) { | |
2392 | $this->properties->visible = 1; | |
2393 | } | |
2394 | ||
2395 | if ($force === true || ($force !== false && $this->properties->visible == 0)) { | |
2396 | // Make this event visible | |
2397 | $this->properties->visible = 1; | |
2398 | // Fire the hook | |
2399 | self::calendar_event_hook('show_event', array($this->properties)); | |
2400 | } else { | |
2401 | // Make this event hidden | |
2402 | $this->properties->visible = 0; | |
2403 | // Fire the hook | |
2404 | self::calendar_event_hook('hide_event', array($this->properties)); | |
2405 | } | |
2406 | ||
2407 | // Update the database to reflect this change | |
2408 | return $DB->set_field('event', 'visible', $this->properties->visible, array('id'=>$this->properties->id)); | |
2409 | } | |
2410 | ||
2411 | /** | |
2412 | * Attempts to call the hook for the specified action should a calendar type | |
2413 | * by set $CFG->calendar, and the appopriate function defined | |
2414 | * | |
76d9df3f SH |
2415 | * @param string $action One of `update_event`, `add_event`, `delete_event`, `show_event`, `hide_event` |
2416 | * @param array $args The args to pass to the hook, usually the event is the first element | |
08b4a4e1 | 2417 | * @return bool attempts to call event hook |
76d9df3f SH |
2418 | */ |
2419 | public static function calendar_event_hook($action, array $args) { | |
2420 | global $CFG; | |
2421 | static $extcalendarinc; | |
2422 | if ($extcalendarinc === null) { | |
2423 | if (!empty($CFG->calendar) && file_exists($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php')) { | |
2424 | include_once($CFG->dirroot .'/calendar/'. $CFG->calendar .'/lib.php'); | |
2425 | $extcalendarinc = true; | |
2426 | } else { | |
2427 | $extcalendarinc = false; | |
2428 | } | |
2429 | } | |
2430 | if($extcalendarinc === false) { | |
2431 | return false; | |
2432 | } | |
438e4add | 2433 | $hook = $CFG->calendar .'_'.$action; |
76d9df3f SH |
2434 | if (function_exists($hook)) { |
2435 | call_user_func_array($hook, $args); | |
2436 | return true; | |
2437 | } | |
2438 | return false; | |
2439 | } | |
2440 | ||
2441 | /** | |
2442 | * Returns a calendar_event object when provided with an event id | |
2443 | * | |
2444 | * This function makes use of MUST_EXIST, if the event id passed in is invalid | |
2445 | * it will result in an exception being thrown | |
2446 | * | |
08b4a4e1 RW |
2447 | * @param int|object $param event object or event id |
2448 | * @return calendar_event|false status for loading calendar_event | |
76d9df3f | 2449 | */ |
fd699564 | 2450 | public static function load($param) { |
76d9df3f | 2451 | global $DB; |
fd699564 SH |
2452 | if (is_object($param)) { |
2453 | $event = new calendar_event($param); | |
2454 | } else { | |
2455 | $event = $DB->get_record('event', array('id'=>(int)$param), '*', MUST_EXIST); | |
2456 | $event = new calendar_event($event); | |
2457 | } | |
76d9df3f SH |
2458 | return $event; |
2459 | } | |
2460 | ||
2461 | /** | |
2462 | * Creates a new event and returns a calendar_event object | |
2463 | * | |
2464 | * @param object|array $properties An object containing event properties | |
2465 | * @return calendar_event|false The event object or false if it failed | |
2466 | */ | |
2467 | public static function create($properties) { | |
2468 | if (is_array($properties)) { | |
2469 | $properties = (object)$properties; | |
2470 | } | |
2471 | if (!is_object($properties)) { | |
2472 | throw new coding_exception('When creating an event properties should be either an object or an assoc array'); | |
2473 | } | |
f4700b91 | 2474 | $event = new calendar_event($properties); |
76d9df3f SH |
2475 | if ($event->update($properties)) { |
2476 | return $event; | |
2477 | } else { | |
2478 | return false; | |
2479 | } | |
2480 | } | |
2481 | } | |
36dc3b71 SH |
2482 | |
2483 | /** | |
2484 | * Calendar information class | |
2485 | * | |
2486 | * This class is used simply to organise the information pertaining to a calendar | |
2487 | * and is used primarily to make information easily available. | |
08b4a4e1 RW |
2488 | * |
2489 | * @package core_calendar | |
2490 | * @category calendar | |
2491 | * @copyright 2010 Sam Hemelryk | |
2492 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36dc3b71 SH |
2493 | */ |
2494 | class calendar_information { | |
08b4a4e1 | 2495 | /** @var int The day */ |
36dc3b71 | 2496 | public $day; |
08b4a4e1 RW |
2497 | |
2498 | /** @var int The month */ | |
36dc3b71 | 2499 | public $month; |
08b4a4e1 RW |
2500 | |
2501 | /** @var int The year */ | |
36dc3b71 SH |
2502 | public $year; |
2503 | ||
08b4a4e1 | 2504 | /** @var int A course id */ |
36dc3b71 | 2505 | public $courseid = null; |
08b4a4e1 RW |
2506 | |
2507 | /** @var array An array of courses */ | |
36dc3b71 | 2508 | public $courses = array(); |
08b4a4e1 RW |
2509 | |
2510 | /** @var array An array of groups */ | |
36dc3b71 | 2511 | public $groups = array(); |
08b4a4e1 RW |
2512 | |
2513 | /** @var array An array of users */ | |
36dc3b71 SH |
2514 | public $users = array(); |
2515 | ||
2516 | /** | |
2517 | * Creates a new instance | |
2518 | * | |
08b4a4e1 RW |
2519 | * @param int $day the number of the day |
2520 | * @param int $month the number of the month | |
2521 | * @param int $year the number of the year | |
36dc3b71 | 2522 | */ |
d8d8bdd9 SH |
2523 | public function __construct($day=0, $month=0, $year=0) { |
2524 | ||
2525 | $date = usergetdate(time()); | |
2526 | ||
2527 | if (empty($day)) { | |
2528 | $day = $date['mday']; | |
2529 | } | |
2530 | ||
2531 | if (empty($month)) { | |
2532 | $month = $date['mon']; | |
2533 | } | |
2534 | ||
2535 | if (empty($year)) { | |
2536 | $year = $date['year']; | |
2537 | } | |
2538 | ||
36dc3b71 SH |
2539 | $this->day = $day; |
2540 | $this->month = $month; | |
2541 | $this->year = $year; | |
2542 | } | |
2543 | ||
797cedc7 | 2544 | /** |
08b4a4e1 | 2545 | * Initialize calendar information |
797cedc7 | 2546 | * |
08b4a4e1 | 2547 | * @param stdClass $course object |
797cedc7 | 2548 | * @param array $coursestoload An array of courses [$course->id => $course] |
08b4a4e1 | 2549 | * @param bool $ignorefilters options to use filter |
797cedc7 SH |
2550 | */ |
2551 | public function prepare_for_view(stdClass $course, array $coursestoload, $ignorefilters = false) { | |
2552 | $this->courseid = $course->id; | |
2553 | $this->course = $course; | |
2554 | list($courses, $group, $user) = calendar_set_filters($coursestoload, $ignorefilters); | |
2555 | $this->courses = $courses; | |
2556 | $this->groups = $group; | |
2557 | $this->users = $user; | |
2558 | } | |
2559 | ||
36dc3b71 SH |
2560 | /** |
2561 | * Ensures the date for the calendar is correct and either sets it to now | |
2562 | * or throws a moodle_exception if not | |
2563 | * | |
08b4a4e1 RW |
2564 | * @param bool $defaultonow use current time |
2565 | * @throws moodle_exception | |
2566 | * @return bool validation of checkdate | |
36dc3b71 SH |
2567 | */ |
2568 | public function checkdate($defaultonow = true) { | |
2569 | if (!checkdate($this->month, $this->day, $this->year)) { | |
2570 | if ($defaultonow) { | |
2571 | $now = usergetdate(time()); | |
2572 | $this->day = intval($now['mday']); | |
2573 | $this->month = intval($now['mon']); | |
2574 | $this->year = intval($now['year']); | |
2575 | return true; | |
2576 | } else { | |
2577 | throw new moodle_exception('invaliddate'); | |
2578 | } | |
2579 | } | |
2580 | return true; | |
2581 | } | |
2582 | /** | |
2583 | * Gets todays timestamp for the calendar | |
08b4a4e1 RW |
2584 | * |
2585 | * @return int today timestamp | |
36dc3b71 SH |
2586 | */ |
2587 | public function timestamp_today() { | |
2588 | return make_timestamp($this->year, $this->month, $this->day); | |
2589 | } | |
2590 | /** | |
2591 | * Gets tomorrows timestamp for the calendar | |
08b4a4e1 RW |
2592 | * |
2593 | * @return int tomorrow timestamp | |
36dc3b71 SH |
2594 | */ |
2595 | public function timestamp_tomorrow() { | |
2596 | return make_timestamp($this->year, $this->month, $this->day+1); | |
2597 | } | |
2598 | /** | |
2599 | * Adds the pretend blocks for teh calendar | |
2600 | * | |
2601 | * @param core_calendar_renderer $renderer | |
08b4a4e1 RW |
2602 | * @param bool $showfilters display filters, false is set as default |
2603 | * @param string|null $view preference view options (eg: day, month, upcoming) | |
36dc3b71 SH |
2604 | */ |
2605 | public function add_sidecalendar_blocks(core_calendar_renderer $renderer, $showfilters=false, $view=null) { | |
2606 | if ($showfilters) { | |
2607 | $filters = new block_contents(); | |
2608 | $filters->content = $renderer->fake_block_filters($this->courseid, $this->day, $this->month, $this->year, $view, $this->courses); | |
2609 | $filters->footer = ''; | |
2610 | $filters->title = get_string('eventskey', 'calendar'); | |
2611 | $renderer->add_pretend_calendar_block($filters, BLOCK_POS_RIGHT); | |
2612 | } | |
2613 | $block = new block_contents; | |
2614 | $block->content = $renderer->fake_block_threemonths($this); | |
2615 | $block->footer = ''; | |
2616 | $block->title = get_string('monthlyview', 'calendar'); | |
2617 | $renderer->add_pretend_calendar_block($block, BLOCK_POS_RIGHT); | |
2618 | } | |
1d5bd3d2 | 2619 | } |