3 /////////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Calendar extension //
9 // Copyright (C) 2003-2004 Greek School Network www.sch.gr //
12 // Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13 // Jon Papaioannou (pj@moodle.org) //
15 // Programming and development: //
16 // Jon Papaioannou (pj@moodle.org) //
18 // For bugs, suggestions, etc contact: //
19 // Jon Papaioannou (pj@moodle.org) //
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');
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');
57 // Guests cannot do anything with events
58 redirect(CALENDAR_URL
.'view.php?view=upcoming&course='.$urlcourse);
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&course='.$urlcourse.'&', $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
;
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);
94 // Otherwise show just this one
95 $SESSION->cal_courses_shown
= $urlcourse;
96 calendar_set_referring_course($SESSION->cal_courses_shown
);
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');
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
;
139 $form->timeduration
= 0;
142 validate_form($form, $err);
144 if (count($err) == 0) {
146 if($event->repeatid
&& $repeats) {
148 if($form->timestart
>= $event->timestart
) {
149 $timestartoffset = 'timestart + '.($form->timestart
- $event->timestart
);
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&id='.$form->id
, $form->name
);
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&id='.$form->id
, $form->name
);
177 // OK, now redirect to day view
178 redirect(CALENDAR_URL
.'view.php?view=day&course='.$urlcourse.'&cal_d='.$form->startday
.'&cal_m='.$form->startmon
.'&cal_y='.$form->startyr
);
181 foreach ($err as $key => $value) {
182 $focus = 'form.'.$key;
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
;
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();
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&id='.$eventid, stripslashes($form->name
));
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&id='.$eventid, stripslashes($form->name
));
243 // OK, now redirect to day view
244 redirect(CALENDAR_URL
.'view.php?view=day&course='.$urlcourse.'&cal_d='.$form->startday
.'&cal_m='.$form->startmon
.'&cal_y='.$form->startyr
);
247 foreach ($err as $key => $value) {
248 $focus = 'form.'.$key;
253 default: // no action
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
);
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">';
284 $confirm = optional_param('confirm', 0, PARAM_INT
);
285 $repeats = optional_param('repeats', 0, PARAM_INT
);
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
);
295 delete_records('event', 'id', $eventid);
296 add_to_log($event->courseid
, 'calendar', 'delete', '', $event->name
);
300 redirect(CALENDAR_URL
.'view.php?view=day&course='.$urlcourse.'&cal_d='.$_REQUEST['d'].'&cal_m='.$_REQUEST['m'].'&cal_y='.$_REQUEST['y']);
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
;
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);
327 include('event_delete.html');
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
345 else if($event->timeduration
) {
346 // Up to one hour, "minutes" mode probably is better here
348 $form->minutes
= $event->timeduration
/ MINSECS
;
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
);
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
;
372 echo '<div class="header">'.get_string('editevent', 'calendar').'</div>';
373 include('event_edit.html');
374 if ($usehtmleditor) {
375 use_html_editor("description");
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);
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
) {
406 $form->description
= '';
409 $form->userid
= $USER->id
;
410 $form->modulename
= '';
411 $form->eventtype
= '';
413 $form->timeduration
= 0;
418 $form->type
= 'user';
419 $header = get_string('typeuser', 'calendar');
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';
429 $form->description
= '';
430 $form->courseid
= $group->courseid
;
431 $form->groupid
= $group->id
;
432 $form->userid
= $USER->id
;
433 $form->modulename
= '';
434 $form->eventtype
= '';
436 $form->timeduration
= 0;
441 $form->type
= 'group';
442 $header = get_string('typegroup', 'calendar');
446 $courseid = optional_param('courseid', 0, PARAM_INT
);
447 if(!record_exists('course', 'id', $courseid)) {
448 calendar_get_allowed_types($allowed);
449 $eventtype = 'select';
453 $form->description
= '';
454 $form->courseid
= $courseid;
456 $form->userid
= $USER->id
;
457 $form->modulename
= '';
458 $form->eventtype
= '';
460 $form->timeduration
= 0;
465 $form->type
= 'course';
466 $header = get_string('typecourse', 'calendar');
471 $form->description
= '';
472 $form->courseid
= SITEID
;
474 $form->userid
= $USER->id
;
475 $form->modulename
= '';
476 $form->eventtype
= '';
478 $form->timeduration
= 0;
483 $form->type
= 'site';
484 $header = get_string('typesite', 'calendar');
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
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);
514 echo '<h2>'.get_string('eventkind', 'calendar').':</h2>';
515 echo '<div id="selecteventtype">';
516 include('event_select.html');
520 include('event_new.html');
521 if ($usehtmleditor) {
522 use_html_editor("description");
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.'&type='.$eventtype.'&id='.$eventid);
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);
560 echo '</tr></table>';
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) {
605 // can not be using guest account
606 if (empty($USER->id
) or $USER->username
== 'guest') {
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)) {
616 switch ($event->type
) {
618 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE
, $event->courseid
));
621 if (! groups_group_exists($event->groupid
)) { //TODO:check.
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
));
631 if ($event->userid
== $USER->id
) {
632 return (has_capability('moodle/calendar:manageownentries', $sitecontext));
634 //there is no 'break;' intentionally
637 return has_capability('moodle/calendar:manageentries', get_context_instance(CONTEXT_COURSE
, SITEID
));