3 /////////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Calendar extension //
9 // Copyright (C) 2003-2004 Greek School Network www.sch.gr //
12 // Avgoustos Tsinakos (tsinakos@uom.gr) //
13 // Jon Papaioannou (pj@uom.gr) //
15 // Programming and development: //
16 // Jon Papaioannou (pj@uom.gr) //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@uom.gr) //
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. //
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. //
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: //
37 // http://www.gnu.org/copyleft/gpl.html //
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');
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&', $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
;
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);
84 // Otherwise show just this one
85 $SESSION->cal_courses_shown
= $urlcourse;
86 calendar_set_referring_course($SESSION->cal_courses_shown
);
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');
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
;
129 $form->timeduration
= 0;
132 validate_form($form, $err);
134 if (count($err) == 0) {
136 if($event->repeatid
&& $repeats) {
138 if($form->timestart
>= $event->timestart
) {
139 $timestartoffset = 'timestart + '.($form->timestart
- $event->timestart
);
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&id='.$form->id
, $form->name
);
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&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
);
171 foreach ($err as $key => $value) {
172 $focus = 'form.'.$key;
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
;
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();
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&id='.$eventid, $form->name
);
218 for($i = 1; $i < $form->repeats
; $i++
) {
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&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
);
234 foreach ($err as $key => $value) {
235 $focus = 'form.'.$key;
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
);
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">';
268 $confirm = optional_param('confirm', 0, PARAM_INT
);
269 $repeats = optional_param('repeats', 0, PARAM_INT
);
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
);
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']);
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
;
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);
311 include('event_delete.html');
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
329 else if($event->timeduration
) {
330 // Up to one hour, "minutes" mode probably is better here
332 $form->minutes
= $event->timeduration
/ MINSECS
;
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
);
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
;
356 echo '<div class="header">'.get_string('editevent', 'calendar').'</div>';
357 include('event_edit.html');
358 if ($usehtmleditor) {
359 use_html_editor("description");
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);
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
) {
395 $form->description
= '';
398 $form->userid
= $USER->id
;
399 $form->modulename
= '';
400 $form->eventtype
= '';
402 $form->timeduration
= 0;
407 $header = get_string('typeuser', 'calendar');
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';
417 $form->description
= '';
418 $form->courseid
= $group->courseid
;
419 $form->groupid
= $group->id
;
420 $form->userid
= $USER->id
;
421 $form->modulename
= '';
422 $form->eventtype
= '';
424 $form->timeduration
= 0;
429 $header = get_string('typegroup', 'calendar');
433 $courseid = optional_param('courseid', 0, PARAM_INT
);
434 if(!record_exists('course', 'id', $courseid)) {
435 calendar_get_allowed_types($allowed);
436 $eventtype = 'select';
440 $form->description
= '';
441 $form->courseid
= $courseid;
443 $form->userid
= $USER->id
;
444 $form->modulename
= '';
445 $form->eventtype
= '';
447 $form->timeduration
= 0;
452 $header = get_string('typecourse', 'calendar');
457 $form->description
= '';
460 $form->userid
= $USER->id
;
461 $form->modulename
= '';
462 $form->eventtype
= '';
464 $form->timeduration
= 0;
469 $header = get_string('typesite', 'calendar');
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;
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');
501 include('event_new.html');
502 if ($usehtmleditor) {
503 use_html_editor("description");
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.'&type='.$eventtype.'&id='.$eventid);
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);
536 echo '</tr></table>';
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) {
582 else if($courseid == 0 && $groupid == 0 && $userid == $USER->id
) {
585 else if($courseid == 0 && $groupid != 0) {
586 $group = get_record('groups', 'id', $groupid);
587 if($group === false) {
590 return isteacheredit($group->courseid
) ||
isteacher($group->courseid
) && ismember($groupid);
592 else if($courseid != 0 && isteacher($courseid)) {
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
);