MDL-12531, make the new member value available to all the affected plugins, thanks...
[moodle-linuxchix.git] / calendar / event.php
blobac650ac8106c13fbbe753e27f0c7c861e7a58d01
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 $navlinks = array();
74 $calendar_navlink = array('name' => $strcalendar,
75 'link' =>calendar_get_link_href(CALENDAR_URL.'view.php?view=upcoming&amp;course='.$urlcourse.'&amp;',
76 $now['mday'], $now['mon'], $now['year']),
77 'type' => 'misc');
79 $day = intval($now['mday']);
80 $mon = intval($now['mon']);
81 $yr = intval($now['year']);
83 if ($usehtmleditor = can_use_richtext_editor()) {
84 $defaultformat = FORMAT_HTML;
85 } else {
86 $defaultformat = FORMAT_MOODLE;
89 // If a course has been supplied in the URL, change the filters to show that one
90 if($urlcourse > 0 && record_exists('course', 'id', $urlcourse)) {
91 require_login($urlcourse, false);
93 if($urlcourse == SITEID) {
94 // If coming from the site page, show all courses
95 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
96 calendar_set_referring_course(0);
98 else {
99 // Otherwise show just this one
100 $SESSION->cal_courses_shown = $urlcourse;
101 calendar_set_referring_course($SESSION->cal_courses_shown);
105 switch($action) {
106 case 'delete':
107 $title = get_string('deleteevent', 'calendar');
108 $event = get_record('event', 'id', $eventid);
109 if($event === false) {
110 error('Invalid event');
112 if(!calendar_edit_event_allowed($event)) {
113 error('You are not authorized to do this');
115 break;
117 case 'edit':
118 $title = get_string('editevent', 'calendar');
119 $event = get_record('event', 'id', $eventid);
120 $repeats = optional_param('repeats', 0, PARAM_INT);
122 if($event === false) {
123 error('Invalid event');
125 if(!calendar_edit_event_allowed($event)) {
126 error('You are not authorized to do this');
129 if($form = data_submitted()) {
131 $form->name = clean_param(strip_tags($form->name,'<lang><span>'), PARAM_CLEAN);
133 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
134 if($form->duration == 1) {
135 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
136 if($form->timeduration < 0) {
137 $form->timeduration = 0;
140 else if($form->duration == 2) {
141 $form->timeduration = $form->minutes * MINSECS;
143 else {
144 $form->timeduration = 0;
147 validate_form($form, $err);
149 if (count($err) == 0) {
151 if($event->repeatid && $repeats) {
152 // Update all
153 if($form->timestart >= $event->timestart) {
154 $timestartoffset = 'timestart + '.($form->timestart - $event->timestart);
156 else {
157 $timestartoffset = 'timestart - '.($event->timestart - $form->timestart);
160 execute_sql('UPDATE '.$CFG->prefix.'event SET '.
161 'name = '.$db->qstr($form->name).','.
162 'description = '.$db->qstr($form->description).','.
163 'timestart = '.$timestartoffset.','.
164 'timeduration = '.$form->timeduration.','.
165 'timemodified = '.time().' WHERE repeatid = '.$event->repeatid);
167 /// Log the event update.
168 $form->name = stripslashes($form->name); //To avoid double-slashes
169 add_to_log($form->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id='.$form->id, $form->name);
172 else {
173 // Update this
174 $form->timemodified = time();
175 update_record('event', $form);
177 /// Log the event update.
178 $form->name = stripslashes($form->name); //To avoid double-slashes
179 add_to_log($form->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id='.$form->id, $form->name);
182 // OK, now redirect to day view
183 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
185 else {
186 foreach ($err as $key => $value) {
187 $focus = 'form.'.$key;
191 break;
193 case 'new':
194 $title = get_string('newevent', 'calendar');
195 $form = data_submitted();
196 if(!empty($form) && !empty($form->name)) {
198 $form->name = clean_text(strip_tags($form->name, '<lang><span>'));
200 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
201 if($form->duration == 1) {
202 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
203 if($form->timeduration < 0) {
204 $form->timeduration = 0;
207 else if ($form->duration == 2) {
208 $form->timeduration = $form->minutes * MINSECS;
210 else {
211 $form->timeduration = 0;
213 if(!calendar_add_event_allowed($form)) {
214 error('You are not authorized to do this');
216 validate_form($form, $err);
217 if (count($err) == 0) {
218 $form->timemodified = time();
220 /// Get the event id for the log record.
221 $eventid = insert_record('event', $form, true);
223 /// Use the event id as the repeatid to link repeat entries together
224 if ($form->repeat) {
225 $form->repeatid = $form->id = $eventid;
226 update_record('event', $form); // update the row, to set its repeatid
229 /// Log the event entry.
230 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, stripslashes($form->name));
232 if ($form->repeat) {
233 for($i = 1; $i < $form->repeats; $i++) {
234 // What's the DST offset for the previous repeat?
235 $dst_offset_prev = dst_offset_on($form->timestart);
237 $form->timestart += WEEKSECS;
239 // If the offset has changed in the meantime, update this repeat accordingly
240 $form->timestart += $dst_offset_prev - dst_offset_on($form->timestart);
242 /// Get the event id for the log record.
243 $eventid = insert_record('event', $form, true);
245 /// Log the event entry.
246 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, stripslashes($form->name));
249 // OK, now redirect to day view
250 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
252 else {
253 foreach ($err as $key => $value) {
254 $focus = 'form.'.$key;
258 break;
259 default: // no action
260 $title='';
261 break;
265 if (!empty($SESSION->cal_course_referer)) {
266 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
267 $course = get_record('course', 'id', $SESSION->cal_course_referer);
268 } else {
269 $course = $site;
271 require_login($course, false);
273 $navlinks[] = $calendar_navlink;
274 $navlinks[] = array('name' => $title, 'link' => null, 'type' => 'misc');
275 $navigation = build_navigation($navlinks);
276 print_header($site->shortname.': '.$strcalendar.': '.$title, $strcalendar, $navigation,
277 'eventform.name', '', true, '', user_login_string($site));
279 echo calendar_overlib_html();
281 echo '<table id="calendar">';
282 echo '<tr><td class="maincalendar">';
284 switch($action) {
285 case 'delete':
286 $confirm = optional_param('confirm', 0, PARAM_INT);
287 $repeats = optional_param('repeats', 0, PARAM_INT);
288 if($confirm) {
289 // Kill it and redirect to day view
290 if(($event = get_record('event', 'id', $eventid)) !== false) {
292 if($event->repeatid && $repeats) {
293 delete_records('event', 'repeatid', $event->repeatid);
294 add_to_log($event->courseid, 'calendar', 'delete all', '', $event->name);
296 else {
297 delete_records('event', 'id', $eventid);
298 add_to_log($event->courseid, 'calendar', 'delete', '', $event->name);
302 redirect(CALENDAR_URL.'view.php?view=day&amp;course='.$urlcourse.'&cal_d='.$_REQUEST['d'].'&cal_m='.$_REQUEST['m'].'&cal_y='.$_REQUEST['y']);
305 else {
306 $eventtime = usergetdate($event->timestart);
307 $m = $eventtime['mon'];
308 $d = $eventtime['mday'];
309 $y = $eventtime['year'];
311 if($event->repeatid) {
312 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
313 $repeatcount = $fetch->repeatcount;
315 else {
316 $repeatcount = 0;
319 // Display confirmation form
320 echo '<div class="header">'.get_string('deleteevent', 'calendar').': '.$event->name.'</div>';
321 echo '<h2>'.get_string('confirmeventdelete', 'calendar').'</h2>';
322 if($repeatcount > 1) {
323 echo '<p>'.get_string('youcandeleteallrepeats', 'calendar', $repeatcount).'</p>';
325 echo '<div class="eventlist">';
326 $event->time = calendar_format_event_time($event, time(), '', false);
327 calendar_print_event($event);
328 echo '</div>';
329 include('event_delete.html');
331 break;
333 case 'edit':
334 if(empty($form)) {
335 $form->name = $event->name;
336 $form->courseid = $event->courseid; // Not to update, but for date validation
337 $form->description = $event->description;
338 $form->timestart = $event->timestart;
339 $form->timeduration = $event->timeduration;
340 $form->id = $event->id;
341 $form->format = $defaultformat;
342 if($event->timeduration > HOURSECS) {
343 // More than one hour, so default to normal duration mode
344 $form->duration = 1;
345 $form->minutes = '';
347 else if($event->timeduration) {
348 // Up to one hour, "minutes" mode probably is better here
349 $form->duration = 2;
350 $form->minutes = $event->timeduration / MINSECS;
352 else {
353 // No duration
354 $form->duration = 0;
355 $form->minutes = '';
359 if (!empty($form->courseid)) {
360 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
361 $course = get_record('course', 'id', $form->courseid);
362 } else {
363 $course = $site;
366 if($event->repeatid) {
367 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
368 $repeatcount = $fetch->repeatcount;
370 else {
371 $repeatcount = 0;
374 echo '<div class="header">'.get_string('editevent', 'calendar').'</div>';
375 include('event_edit.html');
376 if ($usehtmleditor) {
377 use_html_editor("description");
379 break;
381 case 'new':
382 if($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) {
383 $form->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0);
385 else if($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) {
386 if($cal_y == $now['year'] && $cal_m == $now['mon']) {
387 $form->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0);
389 else {
390 $form->timestart = make_timestamp($cal_y, $cal_m, 1, 0, 0, 0);
393 if(!isset($form->timestart) or $form->timestart < 0) {
394 $form->timestart = time();
397 calendar_get_allowed_types($allowed);
398 if(!$allowed->groups && !$allowed->courses && !$allowed->site) {
399 // Take the shortcut
400 $eventtype = 'user';
403 $header = '';
405 switch($eventtype) {
406 case 'user':
407 $form->name = '';
408 $form->description = '';
409 $form->courseid = 0;
410 $form->groupid = 0;
411 $form->userid = $USER->id;
412 $form->modulename = '';
413 $form->eventtype = '';
414 $form->instance = 0;
415 $form->timeduration = 0;
416 $form->duration = 0;
417 $form->repeat = 0;
418 $form->repeats = '';
419 $form->minutes = '';
420 $form->type = 'user';
421 $header = get_string('typeuser', 'calendar');
422 break;
423 case 'group':
424 $groupid = optional_param('groupid', 0, PARAM_INT);
425 if (! ($group = groups_get_group($groupid))) { //TODO:check.
426 calendar_get_allowed_types($allowed);
427 $eventtype = 'select';
429 else {
430 $form->name = '';
431 $form->description = '';
432 $form->courseid = $group->courseid;
433 $form->groupid = $group->id;
434 $form->userid = $USER->id;
435 $form->modulename = '';
436 $form->eventtype = '';
437 $form->instance = 0;
438 $form->timeduration = 0;
439 $form->duration = 0;
440 $form->repeat = 0;
441 $form->repeats = '';
442 $form->minutes = '';
443 $form->type = 'group';
444 $header = get_string('typegroup', 'calendar');
446 break;
447 case 'course':
448 $courseid = optional_param('courseid', 0, PARAM_INT);
449 if(!record_exists('course', 'id', $courseid)) {
450 calendar_get_allowed_types($allowed);
451 $eventtype = 'select';
453 else {
454 $form->name = '';
455 $form->description = '';
456 $form->courseid = $courseid;
457 $form->groupid = 0;
458 $form->userid = $USER->id;
459 $form->modulename = '';
460 $form->eventtype = '';
461 $form->instance = 0;
462 $form->timeduration = 0;
463 $form->duration = 0;
464 $form->repeat = 0;
465 $form->repeats = '';
466 $form->minutes = '';
467 $form->type = 'course';
468 $header = get_string('typecourse', 'calendar');
470 break;
471 case 'site':
472 $form->name = '';
473 $form->description = '';
474 $form->courseid = SITEID;
475 $form->groupid = 0;
476 $form->userid = $USER->id;
477 $form->modulename = '';
478 $form->eventtype = '';
479 $form->instance = 0;
480 $form->timeduration = 0;
481 $form->duration = 0;
482 $form->repeat = 0;
483 $form->repeats = '';
484 $form->minutes = '';
485 $form->type = 'site';
486 $header = get_string('typesite', 'calendar');
487 break;
488 case 'select':
489 break;
490 default:
491 error('Unsupported event type');
494 $form->format = $defaultformat;
495 if(!empty($header)) {
496 $header = ' ('.$header.')';
499 echo '<div class="header">'.get_string('newevent', 'calendar').$header.'</div>';
501 if($eventtype == 'select') {
502 $courseid = optional_param('courseid', $SESSION->cal_course_referer, PARAM_INT);
503 if ($courseid == 0) { // workaround by Dan for bug #6130
504 $courseid = SITEID;
506 if (!$course = get_record('course', 'id', $courseid)) {
507 error('Incorrect course ID');
510 $groupid = groups_get_course_group($course);
512 echo '<h2>'.get_string('eventkind', 'calendar').':</h2>';
513 echo '<div id="selecteventtype">';
514 include('event_select.html');
515 echo '</div>';
517 else {
518 include('event_new.html');
519 if ($usehtmleditor) {
520 use_html_editor("description");
524 break;
526 echo '</td>';
528 // START: Last column (3-month display)
530 $defaultcourses = calendar_get_default_courses();
531 //calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
533 // when adding an event you can not be a guest, so I think it's reasonalbe to ignore defaultcourses
534 // MDL-10353
535 calendar_set_filters($courses, $groups, $users);
536 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
537 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
539 echo '<td class="sidecalendar">';
540 echo '<div class="sideblock">';
541 echo '<div class="header">'.get_string('eventskey', 'calendar').'</div>';
542 echo '<div class="filters">';
543 echo calendar_filter_controls('event', 'action='.$action.'&amp;type='.$eventtype.'&amp;id='.$eventid);
544 echo '</div>';
545 echo '</div>';
547 echo '<div class="sideblock">';
548 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
549 echo '<div class="minicalendarblock minicalendartop">';
550 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $prevmon, 'y' => $prevyr));
551 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
552 echo '</div><div class="minicalendarblock">';
553 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $mon, 'y' => $yr));
554 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
555 echo '</div><div class="minicalendarblock">';
556 echo calendar_top_controls('display', array('id' => $urlcourse, 'm' => $nextmon, 'y' => $nextyr));
557 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
558 echo '</div>';
559 echo '</div>';
561 echo '</td>';
562 echo '</tr></table>';
564 print_footer();
567 function validate_form(&$form, &$err) {
569 $form->name = trim($form->name);
570 $form->description = trim($form->description);
572 if(empty($form->name)) {
573 $err['name'] = get_string('errornoeventname', 'calendar');
575 /* Allow events without a description
576 if(empty($form->description)) {
577 $err['description'] = get_string('errornodescription', 'calendar');
580 if(!checkdate($form->startmon, $form->startday, $form->startyr)) {
581 $err['timestart'] = get_string('errorinvaliddate', 'calendar');
583 if($form->duration == 2 and !checkdate($form->endmon, $form->endday, $form->endyr)) {
584 $err['timeduration'] = get_string('errorinvaliddate', 'calendar');
586 if($form->duration == 2 and !($form->minutes > 0 and $form->minutes < 1000)) {
587 $err['minutes'] = get_string('errorinvalidminutes', 'calendar');
589 if (!empty($form->repeat) and !($form->repeats > 1 and $form->repeats < 100)) {
590 $err['repeats'] = get_string('errorinvalidrepeats', 'calendar');
592 if(!empty($form->courseid)) {
593 // Timestamps must be >= course startdate
594 $course = get_record('course', 'id', $form->courseid);
595 if($course === false) {
596 error('Event belongs to invalid course');
598 else if($form->timestart < $course->startdate) {
599 $err['timestart'] = get_string('errorbeforecoursestart', 'calendar');
604 function calendar_add_event_allowed($event) {
605 global $USER;
607 // can not be using guest account
608 if (empty($USER->id) or $USER->username == 'guest') {
609 return false;
612 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
613 // if user has manageentries at site level, always return true
614 if (has_capability('moodle/calendar:manageentries', $sitecontext)) {
615 return true;
618 switch ($event->type) {
619 case 'course':
620 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $event->courseid));
622 case 'group':
623 // Allow users to add/edit group events if:
624 // 1) They have manageentries (= entries for whole course)
625 // 2) They have managegroupentries AND are in the group
626 $group = get_record('groups', 'id', $event->groupid);
627 return $group && (
628 has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, $group->courseid)) ||
629 (has_capability('moodle/calendar:managegroupentries', get_context_instance(CONTEXT_COURSE, $group->courseid))
630 && groups_is_member($event->groupid)));
632 case 'user':
633 if ($event->userid == $USER->id) {
634 return (has_capability('moodle/calendar:manageownentries', $sitecontext));
636 //there is no 'break;' intentionally
638 case 'site':
639 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE, SITEID));
641 default:
642 return false;