add page heading
[moodle.git] / calendar / event.php
1 <?php
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 /**
42  * This file is part of the Calendar section Moodle
43  *
44  * @copyright 2003-2004 Jon Papaioannou (pj@moodle.org)
45  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
46  * @package calendar
47  */
49 require_once('../config.php');
50 require_once($CFG->dirroot.'/calendar/event_form.php');
51 require_once($CFG->dirroot.'/calendar/lib.php');
52 require_once($CFG->dirroot.'/course/lib.php');
54 require_login();
56 $action = optional_param('action', 'new', PARAM_ALPHA);
57 $eventid = optional_param('id', 0, PARAM_INT);
58 $courseid = optional_param('courseid', 0, PARAM_INT);
59 $cal_y = optional_param('cal_y', 0, PARAM_INT);
60 $cal_m = optional_param('cal_m', 0, PARAM_INT);
61 $cal_d = optional_param('cal_d', 0, PARAM_INT);
63 if ($courseid === 0) {
64     $courseid = optional_param('course', 0, PARAM_INT);
65 }
67 $url = new moodle_url('/calendar/event.php', array('action'=>$action));
68 if ($eventid !== 0) $url->param('id', $eventid);
69 if ($courseid !== 0) $url->param('course', $courseid);
70 if ($cal_y !== 0) $url->param('cal_y', $cal_y);
71 if ($cal_m !== 0) $url->param('cal_m', $cal_m);
72 if ($cal_d !== 0) $url->param('cal_d', $cal_d);
73 $PAGE->set_url($url);
75 if ($action === 'delete' && $eventid>0) {
76     $deleteurl = new moodle_url('/calendar/delete.php', array('id'=>$eventid));
77     if ($courseid > 0) {
78         $deleteurl->param('course', $courseid);
79     }
80     redirect($deleteurl);
81 }
83 $viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php');
84 $viewcalendarurl->params($PAGE->url->params());
85 $viewcalendarurl->remove_params(array('id','action'));
87 $now = usergetdate(time());
89 if (isguestuser()) {
90     // Guests cannot do anything with events
91     redirect(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming', 'course'=>$courseid)));
92 }
94 $focus = '';
96 $site = get_site();
98 calendar_session_vars();
100 // If a course has been supplied in the URL, change the filters to show that one
101 $courseexists = false;
102 if ($courseid > 0) {
103     if ($courseid == SITEID) {
104         // If coming from the site page, show all courses
105         $SESSION->cal_courses_shown = calendar_get_default_courses(true);
106         calendar_set_referring_course(0);
107     } else if ($DB->record_exists('course', array('id'=>$courseid))) {
108         $courseexists = true;
109         // Otherwise show just this one
110         $SESSION->cal_courses_shown = $courseid;
111         calendar_set_referring_course($SESSION->cal_courses_shown);
112     }
115 if (!empty($SESSION->cal_course_referer)) {
116     // TODO: This is part of the Great $course Hack in Moodle. Replace it at some point.
117     $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer));
118 } else {
119     $course = $site;
122 require_login($course, false);
124 $strcalendar = get_string('calendar', 'calendar');
125 $link = clone($viewcalendarurl);
126 $link->param('view', 'upcoming');
128 $formoptions = new stdClass;
130 if ($eventid !== 0) {
131     $title = get_string('editevent', 'calendar');
132     $event = calendar_event::load($eventid);
133     if (!calendar_edit_event_allowed($event)) {
134         print_error('nopermissions');
135                 }
136     $event->action = $action;
137     $event->course = $courseid;
138     $event->timedurationuntil = $event->timestart + $event->timeduration;
139     $event->count_repeats();
141     if (!calendar_add_event_allowed($event)) {
142         print_error('nopermissions');
143         }
144             } else {
145     $title = get_string('newevent', 'calendar');
146     calendar_get_allowed_types($formoptions->eventtypes, $USER->id);
147     $event = new calendar_event();
148     $event->action = $action;
149     $event->course = $courseid;
150     $event->timeduration = 0;
151     if ($formoptions->eventtypes->courses) {
152         if ($courseexists) {
153             $event->courseid = $courseid;
154             $event->eventtype = 'course';
155         } else {
156             unset($formoptions->eventtypes->courses);
157             unset($formoptions->eventtypes->groups);
158         }
159         }
161         if($cal_y && $cal_m && $cal_d && checkdate($cal_m, $cal_d, $cal_y)) {
162         $event->timestart = make_timestamp($cal_y, $cal_m, $cal_d, 0, 0, 0);
163     } else if($cal_y && $cal_m && checkdate($cal_m, 1, $cal_y)) {
164             if($cal_y == $now['year'] && $cal_m == $now['mon']) {
165             $event->timestart = make_timestamp($cal_y, $cal_m, $now['mday'], 0, 0, 0);
166         } else {
167             $event->timestart = make_timestamp($cal_y, $cal_m, 1, 0, 0, 0);
168             }
169             }
171     if (!calendar_add_event_allowed($event)) {
172         print_error('nopermissions');
173         }
174         }
176 $properties = $event->properties(true);
177 $formoptions->event = $event;
178 $formoptions->hasduration = ($event->timeduration > 0);
179 $mform = new event_form(null, $formoptions);
180 $mform->set_data($properties);
181 $data = $mform->get_data();
182 if ($data) {
183     if ($data->duration == 1) {
184         $data->timeduration = $data->timedurationuntil- $data->timestart;
185     } else if ($data->duration == 2) {
186         $data->timeduration = $data->timedurationminutes * MINSECS;
187     } else {
188         $data->timeduration = 0;
189     }
191     $event->update($data);
192     $eventurl = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'day'));
193     if (!empty($event->courseid)) {
194         $eventurl->param('course', $event->courseid);
195     }
196     $eventurl->param('cal_d', date('j', $event->timestart));
197     $eventurl->param('cal_m', date('n', $event->timestart));
198     $eventurl->param('cal_y', date('Y', $event->timestart));
199     $eventurl->set_anchor('event_'.$event->id);
200     redirect($eventurl);
203 $PAGE->navbar->add($strcalendar, $link);
204 $PAGE->navbar->add($title);
205 $PAGE->set_title($site->shortname.': '.$strcalendar.': '.$title);
206 $PAGE->set_heading($COURSE->fullname);
208 echo $OUTPUT->header();
211 echo '<table class="calendarlayout">';
212 echo '<tr><td class="maincalendar">';
213 echo $OUTPUT->heading($title);
214 $mform->display();
215 echo '</td>';
217 // when adding an event you can not be a guest, so it's reasonalbe to ignore defaultcourses MDL-10353
218 calendar_set_filters($courses, $groups, $users);
219 list($prevmon, $prevyr) = calendar_sub_month((int)$now['mon'], (int)$now['year']);
220 list($nextmon, $nextyr) = calendar_add_month((int)$now['mon'], (int)$now['year']);
222 echo '<td class="sidecalendar">';
224 $block = $OUTPUT->box_start('block');
225 $block .= $OUTPUT->box($OUTPUT->heading(get_string('eventskey', 'calendar')), 'header');
226 $block .= $OUTPUT->box(calendar_filter_controls('event', 'action='.$action.'&amp;id='.$event->id), 'filters content');
227 $block .= $OUTPUT->box_end();
228 $block .= $OUTPUT->box_start('block');
229 $block .= $OUTPUT->box($OUTPUT->heading(get_string('monthlyview', 'calendar')), 'header');
230 $block .= $OUTPUT->box_start('content');
231 $block .= $OUTPUT->box_start('minicalendarblock minicalendartop');
232 $block .= calendar_top_controls('display', array('id' => $courseid, 'm' => $prevmon, 'y' => $prevyr));
233 $block .= calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
234 $block .= $OUTPUT->box_end();
235 $block .= $OUTPUT->box_start('minicalendarblock');
236 $block .= calendar_top_controls('display', array('id' => $courseid, 'm' => (int)$now['mon'], 'y' => (int)$now['year']));
237 $block .= calendar_get_mini($courses, $groups, $users, (int)$now['mon'], (int)$now['year']);
238 $block .= $OUTPUT->box_end();
239 $block .= $OUTPUT->box_start('minicalendarblock');
240 $block .= calendar_top_controls('display', array('id' => $courseid, 'm' => $nextmon, 'y' => $nextyr));
241 $block .= calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
242 $block .= $OUTPUT->box_end();
243 $block .= $OUTPUT->box_end();
244 $block .= $OUTPUT->box_end();
245 echo $block;
247 echo '</td></tr>';
248 echo '</table>';
250 echo $OUTPUT->footer();