Tidied up logs HTML a bit, to make it more readable.
[moodle-linuxchix.git] / calendar / event.php
blobfd2e1f24b5bfc9f0f8e298ad1985389c80608c58
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@uom.gr) //
13 // Jon Papaioannou (pj@uom.gr) //
14 // //
15 // Programming and development: //
16 // Jon Papaioannou (pj@uom.gr) //
17 // //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@uom.gr) //
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 if(isguest()) {
49 // Guests cannot do anything with events
50 redirect(CALENDAR_URL.'view.php?view=upcoming');
53 $action = required_param('action', PARAM_ALPHA);
54 $eventid = optional_param('id', 0, PARAM_INT);
55 $eventtype = optional_param('type', 'select', PARAM_ALPHA);
56 $urlcourse = optional_param('course', 0, PARAM_INT);
58 if(!$site = get_site()) {
59 redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
62 $strcalendar = get_string('calendar', 'calendar');
64 $now = usergetdate(time());
65 $nav = calendar_get_link_tag($strcalendar, CALENDAR_URL.'view.php?view=upcoming&amp;', $now['mday'], $now['mon'], $now['year']);
66 $day = intval($now['mday']);
67 $mon = intval($now['mon']);
68 $yr = intval($now['year']);
70 if ($usehtmleditor = can_use_richtext_editor()) {
71 $defaultformat = FORMAT_HTML;
72 } else {
73 $defaultformat = FORMAT_MOODLE;
76 // If a course has been supplied in the URL, change the filters to show that one
77 if($urlcourse > 0 && record_exists('course', 'id', $urlcourse)) {
78 if($urlcourse == SITEID) {
79 // If coming from the site page, show all courses
80 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
81 calendar_set_referring_course(0);
83 else {
84 // Otherwise show just this one
85 $SESSION->cal_courses_shown = $urlcourse;
86 calendar_set_referring_course($SESSION->cal_courses_shown);
90 switch($action) {
91 case 'delete':
92 $title = get_string('deleteevent', 'calendar');
93 $event = get_record('event', 'id', $eventid);
94 if($event === false) {
95 error('Invalid event');
97 if(!calendar_edit_event_allowed($event)) {
98 error('You are not authorized to do this');
100 break;
102 case 'edit':
103 $title = get_string('editevent', 'calendar');
104 $event = get_record('event', 'id', $eventid);
105 $repeats = optional_param('repeats', 0, PARAM_INT);
107 if($event === false) {
108 error('Invalid event');
110 if(!calendar_edit_event_allowed($event)) {
111 error('You are not authorized to do this');
114 if($form = data_submitted()) {
116 $form->name = strip_tags($form->name,'<lang>'); // Strip all tags, but <lang>
118 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
119 if($form->duration == 1) {
120 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
121 if($form->timeduration < 0) {
122 $form->timeduration = 0;
125 else if($form->duration == 2) {
126 $form->timeduration = $form->minutes * MINSECS;
128 else {
129 $form->timeduration = 0;
132 validate_form($form, $err);
134 if (count($err) == 0) {
136 if($event->repeatid && $repeats) {
137 // Update all
138 if($form->timestart >= $event->timestart) {
139 $timestartoffset = 'timestart + '.($form->timestart - $event->timestart);
141 else {
142 $timestartoffset = 'timestart - '.($event->timestart - $form->timestart);
145 execute_sql('UPDATE '.$CFG->prefix.'event SET '.
146 'name = '.$db->qstr($form->name).','.
147 'description = '.$db->qstr($form->description).','.
148 'timestart = '.$timestartoffset.','.
149 'timeduration = '.$form->timeduration.','.
150 'timemodified = '.time().' WHERE repeatid = '.$event->repeatid);
152 /// Log the event update.
153 $form->name = stripslashes($form->name); //To avoid double-slashes
154 add_to_log($form->courseid, 'calendar', 'edit all', 'event.php?action=edit&amp;id='.$form->id, $form->name);
157 else {
158 // Update this
159 $form->timemodified = time();
160 update_record('event', $form);
162 /// Log the event update.
163 $form->name = stripslashes($form->name); //To avoid double-slashes
164 add_to_log($form->courseid, 'calendar', 'edit', 'event.php?action=edit&amp;id='.$form->id, $form->name);
167 // OK, now redirect to day view
168 redirect(CALENDAR_URL.'view.php?view=day&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
170 else {
171 foreach ($err as $key => $value) {
172 $focus = 'form.'.$key;
176 break;
178 case 'new':
179 $title = get_string('newevent', 'calendar');
180 $form = data_submitted();
181 if(!empty($form) && $form->type == 'defined') {
183 $form->name = strip_tags($form->name, '<lang>'); // Strip all tags but <lang>
185 $form->timestart = make_timestamp($form->startyr, $form->startmon, $form->startday, $form->starthr, $form->startmin);
186 if($form->duration == 1) {
187 $form->timeduration = make_timestamp($form->endyr, $form->endmon, $form->endday, $form->endhr, $form->endmin) - $form->timestart;
188 if($form->timeduration < 0) {
189 $form->timeduration = 0;
192 else if ($form->duration == 2) {
193 $form->timeduration = $form->minutes * MINSECS;
195 else {
196 $form->timeduration = 0;
198 if(!calendar_add_event_allowed($form->courseid, $form->groupid, $form->userid)) {
199 error('You are not authorized to do this');
201 validate_form($form, $err);
202 if (count($err) == 0) {
203 $form->timemodified = time();
205 if ($form->repeat) {
206 $fetch = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event');
207 $form->repeatid = empty($fetch) ? 1 : $fetch->repeatid + 1;
210 /// Get the event id for the log record.
211 $eventid = insert_record('event', $form, true);
213 /// Log the event entry.
214 $form->name = stripslashes($form->name); //To avoid double-slashes
215 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, $form->name);
217 if ($form->repeat) {
218 for($i = 1; $i < $form->repeats; $i++) {
219 // [pj]
220 // This will not necessarily work correctly because of DST
221 $form->timestart += 604800; // add one week
222 /// Get the event id for the log record.
223 $eventid = insert_record('event', $form, true);
224 /// Log the event entry.
225 $form->name = stripslashes($form->name); //To avoid double-slashes
226 add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&amp;id='.$eventid, $form->name);
230 // OK, now redirect to day view
231 redirect(CALENDAR_URL.'view.php?view=day&cal_d='.$form->startday.'&cal_m='.$form->startmon.'&cal_y='.$form->startyr);
233 else {
234 foreach ($err as $key => $value) {
235 $focus = 'form.'.$key;
239 break;
241 if(empty($focus)) $focus = '';
243 // Let's see if we are supposed to provide a referring course link
244 // but NOT for the "main page" course
245 if($SESSION->cal_course_referer != SITEID &&
246 ($shortname = get_field('course', 'shortname', 'id', $SESSION->cal_course_referer)) !== false) {
247 // If we know about the referring course, show a return link
248 $nav = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$SESSION->cal_course_referer.'">'.$shortname.'</a> -> '.$nav;
251 if (!empty($SESSION->cal_course_referer)) {
252 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
253 $course = get_record('course', 'id', $SESSION->cal_course_referer);
254 } else {
255 $course = $site;
258 print_header($site->shortname.': '.$strcalendar.': '.$title, $strcalendar, $nav.' -> '.$title,
259 $focus, '', true, '', user_login_string($site));
261 echo calendar_overlib_html();
263 echo '<table id="calendar">';
264 echo '<tr><td class="maincalendar">';
266 switch($action) {
267 case 'delete':
268 $confirm = optional_param('confirm', 0, PARAM_INT);
269 $repeats = optional_param('repeats', 0, PARAM_INT);
270 if($confirm) {
271 // Kill it and redirect to day view
272 if(($event = get_record('event', 'id', $eventid)) !== false) {
274 if($event->repeatid && $repeats) {
275 delete_records('event', 'repeatid', $event->repeatid);
276 add_to_log($event->courseid, 'calendar', 'delete all', '', $event->name);
278 else {
279 delete_records('event', 'id', $eventid);
280 add_to_log($event->courseid, 'calendar', 'delete', '', $event->name);
284 redirect(CALENDAR_URL.'view.php?view=day&cal_d='.$_REQUEST['d'].'&cal_m='.$_REQUEST['m'].'&cal_y='.$_REQUEST['y']);
287 else {
288 $eventtime = usergetdate($event->timestart);
289 $m = $eventtime['mon'];
290 $d = $eventtime['mday'];
291 $y = $eventtime['year'];
293 if($event->repeatid) {
294 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
295 $repeatcount = $fetch->repeatcount;
297 else {
298 $repeatcount = 0;
301 // Display confirmation form
302 echo '<div class="header">'.get_string('deleteevent', 'calendar').': '.$event->name.'</div>';
303 echo '<h2>'.get_string('confirmeventdelete', 'calendar').'</h2>';
304 if($repeatcount > 1) {
305 echo '<p>'.get_string('youcandeleteallrepeats', 'calendar', $repeatcount).'</p>';
307 echo '<div class="eventlist">';
308 $event->time = calendar_format_event_time($event, time(), '', false);
309 calendar_print_event($event);
310 echo '</div>';
311 include('event_delete.html');
313 break;
315 case 'edit':
316 if(empty($form)) {
317 $form->name = $event->name;
318 $form->courseid = $event->courseid; // Not to update, but for date validation
319 $form->description = $event->description;
320 $form->timestart = $event->timestart;
321 $form->timeduration = $event->timeduration;
322 $form->id = $event->id;
323 $form->format = $defaultformat;
324 if($event->timeduration > HOURSECS) {
325 // More than one hour, so default to normal duration mode
326 $form->duration = 1;
327 $form->minutes = '';
329 else if($event->timeduration) {
330 // Up to one hour, "minutes" mode probably is better here
331 $form->duration = 2;
332 $form->minutes = $event->timeduration / MINSECS;
334 else {
335 // No duration
336 $form->duration = 0;
337 $form->minutes = '';
341 if (!empty($form->courseid)) {
342 // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
343 $course = get_record('course', 'id', $form->courseid);
344 } else {
345 $course = $site;
348 if($event->repeatid) {
349 $fetch = get_record_sql('SELECT 1, COUNT(id) AS repeatcount FROM '.$CFG->prefix.'event WHERE repeatid = '.$event->repeatid);
350 $repeatcount = $fetch->repeatcount;
352 else {
353 $repeatcount = 0;
356 echo '<div class="header">'.get_string('editevent', 'calendar').'</div>';
357 include('event_edit.html');
358 if ($usehtmleditor) {
359 use_html_editor("description");
361 break;
363 case 'new':
364 optional_variable($_GET['cal_y']);
365 optional_variable($_GET['cal_m']);
366 optional_variable($_GET['cal_d']);
367 optional_variable($form->timestart, -1);
369 if($_GET['cal_y'] && $_GET['cal_m'] && $_GET['cal_d'] && checkdate($_GET['cal_m'], $_GET['cal_d'], $_GET['cal_y'])) {
370 $form->timestart = make_timestamp($_GET['cal_y'], $_GET['cal_m'], $_GET['cal_d'], 0, 0, 0);
372 else if($_GET['cal_y'] && $_GET['cal_m'] && checkdate($_GET['cal_m'], 1, $_GET['cal_y'])) {
373 if($_GET['cal_y'] == $now['year'] && $_GET['cal_m'] == $now['mon']) {
374 $form->timestart = make_timestamp($_GET['cal_y'], $_GET['cal_m'], $now['mday'], 0, 0, 0);
376 else {
377 $form->timestart = make_timestamp($_GET['cal_y'], $_GET['cal_m'], 1, 0, 0, 0);
380 if($form->timestart < 0) {
381 $form->timestart = time();
384 calendar_get_allowed_types($allowed);
385 if(!$allowed->groups && !$allowed->courses && !$allowed->site) {
386 // Take the shortcut
387 $eventtype = 'user';
390 $header = '';
392 switch($eventtype) {
393 case 'user':
394 $form->name = '';
395 $form->description = '';
396 $form->courseid = 0;
397 $form->groupid = 0;
398 $form->userid = $USER->id;
399 $form->modulename = '';
400 $form->eventtype = '';
401 $form->instance = 0;
402 $form->timeduration = 0;
403 $form->duration = 0;
404 $form->repeat = 0;
405 $form->repeats = '';
406 $form->minutes = '';
407 $header = get_string('typeuser', 'calendar');
408 break;
409 case 'group':
410 $groupid = optional_param('groupid', 0, PARAM_INT);
411 if(!($group = get_record('groups', 'id', $groupid) )) {
412 calendar_get_allowed_types($allowed);
413 $eventtype = 'select';
415 else {
416 $form->name = '';
417 $form->description = '';
418 $form->courseid = $group->courseid;
419 $form->groupid = $group->id;
420 $form->userid = $USER->id;
421 $form->modulename = '';
422 $form->eventtype = '';
423 $form->instance = 0;
424 $form->timeduration = 0;
425 $form->duration = 0;
426 $form->repeat = 0;
427 $form->repeats = '';
428 $form->minutes = '';
429 $header = get_string('typegroup', 'calendar');
431 break;
432 case 'course':
433 $courseid = optional_param('courseid', 0, PARAM_INT);
434 if(!record_exists('course', 'id', $courseid)) {
435 calendar_get_allowed_types($allowed);
436 $eventtype = 'select';
438 else {
439 $form->name = '';
440 $form->description = '';
441 $form->courseid = $courseid;
442 $form->groupid = 0;
443 $form->userid = $USER->id;
444 $form->modulename = '';
445 $form->eventtype = '';
446 $form->instance = 0;
447 $form->timeduration = 0;
448 $form->duration = 0;
449 $form->repeat = 0;
450 $form->repeats = '';
451 $form->minutes = '';
452 $header = get_string('typecourse', 'calendar');
454 break;
455 case 'site':
456 $form->name = '';
457 $form->description = '';
458 $form->courseid = 1;
459 $form->groupid = 0;
460 $form->userid = $USER->id;
461 $form->modulename = '';
462 $form->eventtype = '';
463 $form->instance = 0;
464 $form->timeduration = 0;
465 $form->duration = 0;
466 $form->repeat = 0;
467 $form->repeats = '';
468 $form->minutes = '';
469 $header = get_string('typesite', 'calendar');
470 break;
471 case 'defined':
472 case 'select':
473 break;
474 default:
475 error('Unsupported event type');
478 $form->format = $defaultformat;
479 if(!empty($header)) {
480 $header = ' ('.$header.')';
483 echo '<div class="header">'.get_string('newevent', 'calendar').$header.'</div>';
485 if($eventtype == 'select') {
486 $defaultcourse = $SESSION->cal_course_referer;
487 if(isteacheredit($defaultcourse, $USER->id)) {
488 $defaultgroup = $groupid = 0;
490 else {
491 $defaultgroup = user_group($defaultcourse, $USER->id);
492 $groupid = optional_param('groupid', $defaultgroup->id, PARAM_INT);
494 $courseid = optional_param('courseid', $defaultcourse, PARAM_INT);
495 echo '<h2>'.get_string('eventkind', 'calendar').':</h2>';
496 echo '<div id="selecteventtype">';
497 include('event_select.html');
498 echo '</div>';
500 else {
501 include('event_new.html');
502 if ($usehtmleditor) {
503 use_html_editor("description");
507 break;
509 echo '</td>';
511 // START: Last column (3-month display)
513 $defaultcourses = calendar_get_default_courses();
514 calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);
515 list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
516 list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
518 echo '<td class="sidecalendar">';
519 echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';
520 echo '<div class="filters">';
521 echo calendar_filter_controls('event', 'action='.$action.'&amp;type='.$eventtype.'&amp;id='.$eventid);
522 echo '</div>';
524 echo '<div class="minicalendarblock">';
525 echo calendar_top_controls('display', array('m' => $prevmon, 'y' => $prevyr));
526 echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
527 echo '</div><div class="minicalendarblock">';
528 echo calendar_top_controls('display', array('m' => $mon, 'y' => $yr));
529 echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
530 echo '</div><div class="minicalendarblock">';
531 echo calendar_top_controls('display', array('m' => $nextmon, 'y' => $nextyr));
532 echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
533 echo '</div>';
535 echo '</td>';
536 echo '</tr></table>';
538 print_footer();
541 function validate_form(&$form, &$err) {
543 $form->name = trim($form->name);
544 $form->description = trim($form->description);
546 if(empty($form->name)) {
547 $err['name'] = get_string('errornoeventname', 'calendar');
549 if(empty($form->description)) {
550 $err['description'] = get_string('errornodescription', 'calendar');
552 if(!checkdate($form->startmon, $form->startday, $form->startyr)) {
553 $err['timestart'] = get_string('errorinvaliddate', 'calendar');
555 if($form->duration == 2 and !checkdate($form->endmon, $form->endday, $form->endyr)) {
556 $err['timeduration'] = get_string('errorinvaliddate', 'calendar');
558 if($form->duration == 2 and !($form->minutes > 0 and $form->minutes < 1000)) {
559 $err['minutes'] = get_string('errorinvalidminutes', 'calendar');
561 if (!empty($form->repeat) and !($form->repeats > 1 and $form->repeats < 100)) {
562 $err['repeats'] = get_string('errorinvalidrepeats', 'calendar');
564 if(!empty($form->courseid)) {
565 // Timestamps must be >= course startdate
566 $course = get_record('course', 'id', $form->courseid);
567 if($course === false) {
568 error('Event belongs to invalid course');
570 else if($form->timestart < $course->startdate) {
571 $err['timestart'] = get_string('errorbeforecoursestart', 'calendar');
576 function calendar_add_event_allowed($courseid, $groupid, $userid) {
577 global $USER;
579 if(isadmin()) {
580 return true;
582 else if($courseid == 0 && $groupid == 0 && $userid == $USER->id) {
583 return true;
585 else if($courseid == 0 && $groupid != 0) {
586 $group = get_record('groups', 'id', $groupid);
587 if($group === false) {
588 return false;
590 return isteacheredit($group->courseid) || isteacher($group->courseid) && ismember($groupid);
592 else if($courseid != 0 && isteacher($courseid)) {
593 return true;
596 return false;
599 function calendar_get_allowed_types(&$allowed) {
600 global $USER, $CFG, $SESSION;
602 $allowed->user = true; // User events always allowed
603 $allowed->groups = false; // This may change just below
604 $allowed->courses = false; // This may change just below
605 $allowed->site = isteacher(SITEID);
607 if(!empty($SESSION->cal_course_referer) && $SESSION->cal_course_referer != SITEID && isteacher($SESSION->cal_course_referer, $USER->id)) {
608 $course = get_record('course', 'id', $SESSION->cal_course_referer);
610 $allowed->courses = array($course->id => 1);
612 if($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
613 $allowed->groups = get_groups($SESSION->cal_course_referer);