on-demand release 2.1beta
[moodle.git] / calendar / view.php
CommitLineData
93c91ee4 1<?php
7423f116 2
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: //
bdcb26b7 12// Avgoustos Tsinakos (tsinakos@teikav.edu.gr) //
13// Jon Papaioannou (pj@moodle.org) //
7423f116 14// //
15// Programming and development: //
bdcb26b7 16// Jon Papaioannou (pj@moodle.org) //
7423f116 17// //
18// For bugs, suggestions, etc contact: //
bdcb26b7 19// Jon Papaioannou (pj@moodle.org) //
7423f116 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/////////////////////////////////////////////////////////////////////////////
40
41// Display the calendar page.
42
93c91ee4 43require_once('../config.php');
44require_once($CFG->dirroot.'/course/lib.php');
45require_once($CFG->dirroot.'/calendar/lib.php');
707fbef4 46
93c91ee4 47$courseid = optional_param('course', 0, PARAM_INT);
48$view = optional_param('view', 'upcoming', PARAM_ALPHA);
49$day = optional_param('cal_d', 0, PARAM_INT);
50$mon = optional_param('cal_m', 0, PARAM_INT);
51$yr = optional_param('cal_y', 0, PARAM_INT);
7423f116 52
2a250a0b 53$site = get_site();
7423f116 54
a6855934 55$url = new moodle_url('/calendar/view.php');
93c91ee4 56if ($courseid !== 0) {
57 $url->param('course', $courseid);
58}
59if ($view !== 'upcoming') {
60 $url->param('view', $view);
61}
62if ($day !== 0) {
63 $url->param('cal_d', $day);
64}
65if ($mon !== 0) {
66 $url->param('cal_m', $mon);
67}
68if ($yr !== 0) {
69 $url->param('cal_y', $yr);
70}
71$PAGE->set_url($url);
c9c9fb90 72
ca74470c
PS
73//TODO: the courseid handling in /calendar/ is a bloody mess!!!
74
9b610c98 75if ($courseid && $courseid != SITEID) {
93c91ee4 76 require_login($courseid);
77} else if ($CFG->forcelogin) {
ca74470c 78 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); //TODO: wrong
93c91ee4 79 require_login();
ca74470c
PS
80} else {
81 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); //TODO: wrong
93c91ee4 82}
2d4fb02f 83
36dc3b71
SH
84$calendar = new calendar_information($day, $mon, $yr);
85$calendar->courseid = $courseid;
86
93c91ee4 87// Initialize the session variables
88calendar_session_vars();
37d87d11 89
93c91ee4 90//add_to_log($course->id, "course", "view", "view.php?id=$course->id", "$course->id");
91$now = usergetdate(time());
92$pagetitle = '';
7423f116 93
93c91ee4 94$strcalendar = get_string('calendar', 'calendar');
7423f116 95
0f927f1e 96$link = calendar_get_link_href(new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming', 'course'=>$courseid)),
93c91ee4 97 $now['mday'], $now['mon'], $now['year']);
0f927f1e 98$PAGE->navbar->add($strcalendar, $link);
93c91ee4 99
100if(!checkdate($mon, $day, $yr)) {
101 $day = intval($now['mday']);
102 $mon = intval($now['mon']);
103 $yr = intval($now['year']);
104}
105$time = make_timestamp($yr, $mon, $day);
106
107switch($view) {
108 case 'day':
109 $PAGE->navbar->add(userdate($time, get_string('strftimedate')));
110 $pagetitle = get_string('dayview', 'calendar');
111 break;
112 case 'month':
113 $PAGE->navbar->add(userdate($time, get_string('strftimemonthyear')));
114 $pagetitle = get_string('detailedmonthview', 'calendar');
115 break;
116 case 'upcoming':
117 $pagetitle = get_string('upcomingevents', 'calendar');
118 break;
119}
93c91ee4 120// If a course has been supplied in the URL, change the filters to show that one
121if (!empty($courseid)) {
122 if ($course = $DB->get_record('course', array('id'=>$courseid))) {
123 if ($course->id == SITEID) {
124 // If coming from the home page, show all courses
125 $SESSION->cal_courses_shown = calendar_get_default_courses(true);
126 calendar_set_referring_course(0);
127
128 } else {
129 // Otherwise show just this one
130 $SESSION->cal_courses_shown = $course->id;
131 calendar_set_referring_course($SESSION->cal_courses_shown);
227bc46b 132 }
133 }
93c91ee4 134} else {
135 $course = null;
136}
4f0c2d00 137if (!isloggedin() or isguestuser()) {
93c91ee4 138 $defaultcourses = calendar_get_default_courses();
36dc3b71 139 calendar_set_filters($calendar->courses, $calendar->groups, $calendar->users, $defaultcourses, $defaultcourses);
93c91ee4 140} else {
36dc3b71 141 calendar_set_filters($calendar->courses, $calendar->groups, $calendar->users);
93c91ee4 142}
93c91ee4 143// Let's see if we are supposed to provide a referring course link
144// but NOT for the "main page" course
145if ($SESSION->cal_course_referer != SITEID &&
146 ($shortname = $DB->get_field('course', 'shortname', array('id'=>$SESSION->cal_course_referer))) !== false) {
ca74470c 147 require_login(); //TODO: very wrong!!
93c91ee4 148 if (empty($course)) {
149 $course = $DB->get_record('course', array('id'=>$SESSION->cal_course_referer)); // Useful to have around
7423f116 150 }
93c91ee4 151}
7423f116 152
93c91ee4 153$strcalendar = get_string('calendar', 'calendar');
154$prefsbutton = calendar_preferences_button();
1e1ff33b 155
93c91ee4 156// Print title and header
157$PAGE->set_title("$site->shortname: $strcalendar: $pagetitle");
bd08a24a 158$PAGE->set_heading($COURSE->fullname);
93c91ee4 159$PAGE->set_button($prefsbutton);
36dc3b71 160$PAGE->set_pagelayout('standard');
f22f1333 161
36dc3b71
SH
162$renderer = $PAGE->get_renderer('core_calendar');
163$calendar->add_sidecalendar_blocks($renderer, true, $view);
7423f116 164
36dc3b71
SH
165echo $OUTPUT->header();
166echo $renderer->start_layout();
167echo html_writer::start_tag('div', array('class'=>'heightcontainer'));
7423f116 168
93c91ee4 169switch($view) {
170 case 'day':
36dc3b71 171 echo $renderer->show_day($calendar);
93c91ee4 172 break;
173 case 'month':
36dc3b71 174 echo $renderer->show_month_detailed($calendar);
93c91ee4 175 break;
176 case 'upcoming':
36dc3b71 177 echo $renderer->show_upcoming_events($calendar, get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS), get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS));
93c91ee4 178 break;
179}
3c49918a 180
93c91ee4 181//Link to calendar export page
182echo $OUTPUT->container_start('bottom');
183if (!empty($CFG->enablecalendarexport)) {
5c2ed7e2 184 echo $OUTPUT->single_button(new moodle_url('export.php', array('course'=>$courseid)), get_string('exportcalendar', 'calendar'));
4f0c2d00 185 if (isloggedin()) {
93c91ee4 186 $authtoken = sha1($USER->username . $USER->password . $CFG->calendar_exportsalt);
36dc3b71
SH
187 $link = new moodle_url('/calendar/export_execute.php', array('preset_what'=>'all', 'prest_time'=>'recentupcoming', 'username'=>$USER->username, 'authtoken'=>$authtoken));
188 $icon = html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('i/ical'), 'height'=>'14', 'width'=>'36', 'alt'=>get_string('ical', 'calendar'), 'title'=>get_string('quickdownloadcalendar', 'calendar')));
189 echo html_writer::tag('a', $icon, array('href'=>$link));
ea185313 190 }
93c91ee4 191}
93c91ee4 192echo $OUTPUT->container_end();
36dc3b71
SH
193echo html_writer::end_tag('div');
194echo $renderer->complete_layout();
93c91ee4 195echo $OUTPUT->footer();