MDL-9137 Fixing errors in the overview report
[moodle-pu.git] / calendar / event.php
bloba00e7ceb9f0edf55441be3e03e8cf1274966113e
1 <?php // $Id$
3 /////////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Calendar extension //
8 // //
9 // Copyright (C) 2003-2004 Greek School Network www.sch.gr //
10 // //
11 // Designed by: //
12 // Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13 // Jon Papaioannou (pj@moodle.org) //
14 // //
15 // Programming and development: //
16 // Jon Papaioannou (pj@moodle.org) //
17 // //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@moodle.org) //
20 // //
21 // The current module was developed at the University of Macedonia //
22 // (www.uom.gr) under the funding of the Greek School Network (www.sch.gr) //
23 // The aim of this project is to provide additional and improved //
24 // functionality to the Asynchronous Distance Education service that the //
25 // Greek School Network deploys. //
26 // //
27 // This program is free software; you can redistribute it and/or modify //
28 // it under the terms of the GNU General Public License as published by //
29 // the Free Software Foundation; either version 2 of the License, or //
30 // (at your option) any later version. //
31 // //
32 // This program is distributed in the hope that it will be useful, //
33 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
34 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
35 // GNU General Public License for more details: //
36 // //
37 // http://www.gnu.org/copyleft/gpl.html //
38 // //
39 /////////////////////////////////////////////////////////////////////////////
41 require_once('../config.php');
42 require_once($CFG->dirroot.'/calendar/lib.php');
43 require_once($CFG->dirroot.'/course/lib.php');
44 require_once($CFG->dirroot.'/mod/forum/lib.php');
46 require_login();
48 $action = required_param('action', PARAM_ALPHA);
49 $eventid = optional_param('id', 0, PARAM_INT);
50 $eventtype = optional_param('type', 'select', PARAM_ALPHA);
51 $urlcourse = optional_param('course', 0, PARAM_INT);
52 $cal_y = optional_param('cal_y');
53 $cal_m = optional_param('cal_m');
54 $cal_d = optional_param('cal_d');
56 if(isguest()) {
57 // Guests cannot do anything with events
58 redirect(CALENDAR_URL.'view.php?view=upcoming&amp;course='.$urlcourse);
61 $focus = '';
63 if(!$site = get_site()) {
64 redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
67 $strcalendar = get_string('calendar', 'calendar');
69 // Initialize the session variables
70 calendar_session_vars();
72 $now = usergetdate(time());
73 $nav = calendar_get_link_tag($strcalendar, CALENDAR_URL.'view.php?view=upcoming&amp;course='.$urlcourse.'&amp;', $now['mday'], $now['mon'], $now['year']);
74 $day = intval($now['mday']);
75 $mon = intval($now['mon']);
76 $yr = intval($now['year']);
78 if ($usehtmleditor = can_use_richtext_editor()) {
79 $defaultformat = FORMAT_HTML;
80 } else {
81 $defaultformat = FORMAT_MOODLE;
84 // If a course has been supplied in the URL, change the filters to show that one
85 if($urlcourse > 0 && record_exists('course', 'id', $urlcourse)) {
86 require_login($urlcourse, false);
88 if($urlcourse == SITEID) {
89 // If coming from the site page, show all courses
90 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
91 calendar_set_referring_course(0);
93 else {
94 // Otherwise show just this one
95 $SESSION->cal_courses_shown = $urlcourse;
96 calendar_set_referring_course($SESSION->cal_courses_shown);
100 switch($action) {
101 case 'delete':
102 $title = get_string('deleteevent', 'calendar');
103 $event = get_record('event', 'id', $eventid);
104 if($event === false) {
105 error('Invalid event');
107 if(!calendar_edit_event_allowed($event)) {
108 error('You are not authorized to do this');
110 break;
112 case 'edit':
113 $title = get_string('editevent', 'calendar');
114 $event = get_record('event', 'id', $eventid);
115 $repeats = optional_param('repeats', 0, PARAM_INT);
117 if($event === false) {
118 error('Invalid event');
120 if(!calendar_edit_event_allowed($event)) {
121 error('You are not authorized to do this');
124 if($form = data_submitted()) {
126 $form->name = clean_param(strip_tags($form->name,'<lang><span>'), PARAM_CLEAN);
128 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
129 if($form->duration == 1) {
130 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
131 if($form->timeduration < 0) {
132 $form->timeduration = 0;
135 else if($form->duration == 2) {
136 $form->timeduration = $form->minutes * MINSECS;
138 else {
139 $form->timeduration = 0;
142 validate_form($form, $err);
144 if (count($err) == 0) {
146 if($event->repeatid && $repeats) {
147 // Update all
148 if($form->timestart >= $event->timestart) {
149 $timestartoffset = 'timestart + '.($form->timestart - $event->timestart);
151 else {
152 $timestartoffset = 'timestart - '.($event->timestart - $form->timestart);
155 execute_sql('UPDATE '.$CFG->prefix.'event SET '.
156 'name = '.$db->qstr($form->name).','.
157 'description = '.$db->qstr($form->description).','.
158 'timestart = '.$timestartoffset.','.
159 'timeduration = '.$form->timeduration.','.
160 'timemodified = '.time().' WHERE repeatid = '.$event->repeatid);
162 /// Log the event update.
163 $form->name = stripslashes($form->name); //To avoid double-slashes
164 add_to_log($form->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id='.$form->id, $form->name);
167 else {
168 // Update this
169 $form->timemodified = time();
170 update_record('event', $form);
172 /// Log the event update.
173 $form->name = stripslashes($form->name); //To avoid double-slashes
174 add_to_log($form->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id='.$form->id, $form->name);
177 // OK, now redirect to day view
178 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
180 else {
181 foreach ($err as $key => $value) {
182 $focus = 'form.'.$key;
186 break;
188 case 'new':
189 $title = get_string('newevent', 'calendar');
190 $form = data_submitted();
191 if(!empty($form) && !empty($form->name)) {
193 $form->name = clean_text(strip_tags($form->name, '<lang><span>'));
195 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
196 if($form->duration == 1) {
197 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
198 if($form->timeduration < 0) {
199 $form->timeduration = 0;
202 else if ($form->duration == 2) {
203 $form->timeduration = $form->minutes * MINSECS;
205 else {
206 $form->timeduration = 0;
208 if(!calendar_add_event_allowed($form)) {
209 error('You are not authorized to do this');
211 validate_form($form, $err);
212 if (count($err) == 0) {
213 $form->timemodified = time();
215 if ($form->repeat) {
216 $fetch = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
217 $form->repeatid = empty($fetch) ? 1 : $fetch->repeatid + 1;
220 /// Get the event id for the log record.
221 $eventid = insert_record('event', $form, true);
223 /// Log the event entry.
224 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, stripslashes($form->name));
226 if ($form->repeat) {
227 for($i = 1; $i < $form->repeats; $i++) {
228 // What's the DST offset for the previous repeat?
229 $dst_offset_prev = dst_offset_on($form->timestart);
231 $form->timestart += WEEKSECS;
233 // If the offset has changed in the meantime, update this repeat accordingly
234 $form->timestart += $dst_offset_prev - dst_offset_on($form->timestart);
236 /// Get the event id for the log record.
237 $eventid = insert_record('event', $form, true);
239 /// Log the event entry.
240 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, stripslashes($form->name));
243 // OK, now redirect to day view
244 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
246 else {
247 foreach ($err as $key => $value) {
248 $focus = 'form.'.$key;
252 break;
253 default: // no action
254 $title='';
255 break;
258 // Let's see if we are supposed to provide a referring course link
259 // but NOT for the "main page" course
260 if($SESSION->cal_course_referer != SITEID &&
261 ($shortname = get_field('course', 'shortname', 'id', $SESSION->cal_course_referer)) !== false) {
262 // If we know about the referring course, show a return link
263 $nav = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$SESSION->cal_course_referer.'">'.$shortname.'</a> -> '.$nav;
266 if (!empty($SESSION->cal_course_referer)) {
267 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
268 $course = get_record('course', 'id', $SESSION->cal_course_referer);
269 } else {
270 $course = $site;
272 require_login($course, false);
274 print_header($site->shortname.': '.$strcalendar.': '.$title, $strcalendar, $nav.' -> '.$title,
275 'eventform.name', '', true, '', user_login_string($site));
277 echo calendar_overlib_html();
279 echo '<table id="calendar">';
280 echo '<tr><td class="maincalendar">';
282 switch($action) {
283 case 'delete':
284 $confirm = optional_param('confirm', 0, PARAM_INT);
285 $repeats = optional_param('repeats', 0, PARAM_INT);
286 if($confirm) {
287 // Kill it and redirect to day view
288 if(($event = get_record('event', 'id', $eventid)) !== false) {
290 if($event->repeatid && $repeats) {
291 delete_records('event', 'repeatid', $event->repeatid);
292 add_to_log($event->courseid, 'calendar', 'delete all', '', $event->name);
294 else {
295 delete_records('event', 'id', $eventid);
296 add_to_log($event->courseid, 'calendar', 'delete', '', $event->name);
300 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$_REQUEST['d'].'&cal_m='.$_REQUEST['m'].'&cal_y='.$_REQUEST['y']);
303 else {
304 $eventtime = usergetdate($event->timestart);
305 $m = $eventtime['mon'];
306 $d = $eventtime['mday'];
307 $y = $eventtime['year'];
309 if($event->repeatid) {
310 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
311 $repeatcount = $fetch->repeatcount;
313 else {
314 $repeatcount = 0;
317 // Display confirmation form
318 echo '<div class="header">'.get_string('deleteevent', 'calendar').': '.$event->name.'</div>';
319 echo '<h2>'.get_string('confirmeventdelete', 'calendar').'</h2>';
320 if($repeatcount > 1) {
321 echo '<p>'.get_string('youcandeleteallrepeats', 'calendar', $repeatcount).'</p>';
323 echo '<div class="eventlist">';
324 $event->time = calendar_format_event_time($event, time(), '', false);
325 calendar_print_event($event);
326 echo '</div>';
327 include('event_delete.html');
329 break;
331 case 'edit':
332 if(empty($form)) {
333 $form->name = $event->name;
334 $form->courseid = $event->courseid; // Not to update, but for date validation
335 $form->description = $event->description;
336 $form->timestart = $event->timestart;
337 $form->timeduration = $event->timeduration;
338 $form->id = $event->id;
339 $form->format = $defaultformat;
340 if($event->timeduration > HOURSECS) {
341 // More than one hour, so default to normal duration mode
342 $form->duration = 1;
343 $form->minutes = '';
345 else if($event->timeduration) {
346 // Up to one hour, "minutes" mode probably is better here
347 $form->duration = 2;
348 $form->minutes = $event->timeduration / MINSECS;
350 else {
351 // No duration
352 $form->duration = 0;
353 $form->minutes = '';
357 if (!empty($form->courseid)) {
358 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
359 $course = get_record('course', 'id', $form->courseid);
360 } else {
361 $course = $site;
364 if($event->repeatid) {
365 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
366 $repeatcount = $fetch->repeatcount;
368 else {
369 $repeatcount = 0;
372 echo '<div class="header">'.get_string('editevent', 'calendar').'</div>';
373 include('event_edit.html');
374 if ($usehtmleditor) {
375 use_html_editor("description");
377 break;
379 case 'new':
380 if($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) {
381 $form->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0);
383 else if($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) {
384 if($cal_y == $now['year'] && $cal_m == $now['mon']) {
385 $form->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0);
387 else {
388 $form->timestart = make_timestamp($cal_y, $cal_m, 1, 0, 0, 0);
391 if(!isset($form->timestart) or $form->timestart < 0) {
392 $form->timestart = time();
395 calendar_get_allowed_types($allowed);
396 if(!$allowed->groups && !$allowed->courses && !$allowed->site) {
397 // Take the shortcut
398 $eventtype = 'user';
401 $header = '';
403 switch($eventtype) {
404 case 'user':
405 $form->name = '';
406 $form->description = '';
407 $form->courseid = 0;
408 $form->groupid = 0;
409 $form->userid = $USER->id;
410 $form->modulename = '';
411 $form->eventtype = '';
412 $form->instance = 0;
413 $form->timeduration = 0;
414 $form->duration = 0;
415 $form->repeat = 0;
416 $form->repeats = '';
417 $form->minutes = '';
418 $form->type = 'user';
419 $header = get_string('typeuser', 'calendar');
420 break;
421 case 'group':
422 $groupid = optional_param('groupid', 0, PARAM_INT);
423 if (! ($group = groups_get_group($groupid))) { //TODO:check.
424 calendar_get_allowed_types($allowed);
425 $eventtype = 'select';
427 else {
428 $form->name = '';
429 $form->description = '';
430 $form->courseid = $group->courseid;
431 $form->groupid = $group->id;
432 $form->userid = $USER->id;
433 $form->modulename = '';
434 $form->eventtype = '';
435 $form->instance = 0;
436 $form->timeduration = 0;
437 $form->duration = 0;
438 $form->repeat = 0;
439 $form->repeats = '';
440 $form->minutes = '';
441 $form->type = 'group';
442 $header = get_string('typegroup', 'calendar');
444 break;
445 case 'course':
446 $courseid = optional_param('courseid', 0, PARAM_INT);
447 if(!record_exists('course', 'id', $courseid)) {
448 calendar_get_allowed_types($allowed);
449 $eventtype = 'select';
451 else {
452 $form->name = '';
453 $form->description = '';
454 $form->courseid = $courseid;
455 $form->groupid = 0;
456 $form->userid = $USER->id;
457 $form->modulename = '';
458 $form->eventtype = '';
459 $form->instance = 0;
460 $form->timeduration = 0;
461 $form->duration = 0;
462 $form->repeat = 0;
463 $form->repeats = '';
464 $form->minutes = '';
465 $form->type = 'course';
466 $header = get_string('typecourse', 'calendar');
468 break;
469 case 'site':
470 $form->name = '';
471 $form->description = '';
472 $form->courseid = SITEID;
473 $form->groupid = 0;
474 $form->userid = $USER->id;
475 $form->modulename = '';
476 $form->eventtype = '';
477 $form->instance = 0;
478 $form->timeduration = 0;
479 $form->duration = 0;
480 $form->repeat = 0;
481 $form->repeats = '';
482 $form->minutes = '';
483 $form->type = 'site';
484 $header = get_string('typesite', 'calendar');
485 break;
486 case 'select':
487 break;
488 default:
489 error('Unsupported event type');
492 $form->format = $defaultformat;
493 if(!empty($header)) {
494 $header = ' ('.$header.')';
497 echo '<div class="header">'.get_string('newevent', 'calendar').$header.'</div>';
499 if($eventtype == 'select') {
500 $courseid = optional_param('courseid', $SESSION->cal_course_referer, PARAM_INT);
501 if ($courseid == 0) { // workaround by Dan for bug #6130
502 $courseid = SITEID;
504 if (!$course = get_record('course', 'id', $courseid)) {
505 error('Incorrect course ID');
507 if ($groupmode = groupmode($course)) { // Groups are being used
508 $changegroup = optional_param('group', -1, PARAM_INT);
509 $groupid = get_and_set_current_group($course, $groupmode, $changegroup);
510 } else {
511 $groupid = 0;
514 echo '<h2>'.get_string('eventkind', 'calendar').':</h2>';
515 echo '<div id="selecteventtype">';
516 include('event_select.html');
517 echo '</div>';
519 else {
520 include('event_new.html');
521 if ($usehtmleditor) {
522 use_html_editor("description");
526 break;
528 echo '</td>';
530 // START: Last column (3-month display)
532 $defaultcourses = calendar_get_default_courses();
533 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
534 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
535 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
537 echo '<td class="sidecalendar">';
538 echo '<div class="sideblock">';
539 echo '<div class="header">'.get_string('eventskey', 'calendar').'</div>';
540 echo '<div class="filters">';
541 echo calendar_filter_controls('event', 'action='.$action.'&amp;type='.$eventtype.'&amp;id='.$eventid);
542 echo '</div>';
543 echo '</div>';
545 echo '<div class="sideblock">';
546 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
547 echo '<div class="minicalendarblock minicalendartop">';
548 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $prevmon, 'y' => $prevyr));
549 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
550 echo '</div><div class="minicalendarblock">';
551 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $mon, 'y' => $yr));
552 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
553 echo '</div><div class="minicalendarblock">';
554 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $nextmon, 'y' => $nextyr));
555 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
556 echo '</div>';
557 echo '</div>';
559 echo '</td>';
560 echo '</tr></table>';
562 print_footer();
565 function validate_form(&$form, &$err) {
567 $form->name = trim($form->name);
568 $form->description = trim($form->description);
570 if(empty($form->name)) {
571 $err['name'] = get_string('errornoeventname', 'calendar');
573 /* Allow events without a description
574 if(empty($form->description)) {
575 $err['description'] = get_string('errornodescription', 'calendar');
578 if(!checkdate($form->startmon, $form->startday, $form->startyr)) {
579 $err['timestart'] = get_string('errorinvaliddate', 'calendar');
581 if($form->duration == 2 and !checkdate($form->endmon, $form->endday, $form->endyr)) {
582 $err['timeduration'] = get_string('errorinvaliddate', 'calendar');
584 if($form->duration == 2 and !($form->minutes > 0 and $form->minutes < 1000)) {
585 $err['minutes'] = get_string('errorinvalidminutes', 'calendar');
587 if (!empty($form->repeat) and !($form->repeats > 1 and $form->repeats < 100)) {
588 $err['repeats'] = get_string('errorinvalidrepeats', 'calendar');
590 if(!empty($form->courseid)) {
591 // Timestamps must be >= course startdate
592 $course = get_record('course', 'id', $form->courseid);
593 if($course === false) {
594 error('Event belongs to invalid course');
596 else if($form->timestart < $course->startdate) {
597 $err['timestart'] = get_string('errorbeforecoursestart', 'calendar');
602 function calendar_add_event_allowed($event) {
603 global $USER;
605 // can not be using guest account
606 if (empty($USER->id) or $USER->username == 'guest') {
607 return false;
610 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
611 // if user has manageentries at site level, always return true
612 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
613 return true;
616 switch ($event->type) {
617 case 'course':
618 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $event->courseid));
620 case 'group':
621 if (! groups_group_exists($event->groupid)) { //TODO:check.
622 return false;
624 // this is ok because if you have this capability at course level, you should be able
625 // to edit group calendar too
626 // there is no need to check membership, because if you have this capability
627 // you will have a role in this group context
628 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_GROUP, $event->groupid));
630 case 'user':
631 if ($event->userid == $USER->id) {
632 return (has_capability('moodle/calendar:manageownentries', $sitecontext));
634 //there is no 'break;' intentionally
636 case 'site':
637 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, SITEID));
639 default:
640 return false;