MDL-16660: WIP - fixes as per review
[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
797cedc7 47$courseid = optional_param('course', SITEID, PARAM_INT);
93c91ee4 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
a6855934 53$url = new moodle_url('/calendar/view.php');
797cedc7 54if ($courseid != SITEID) {
93c91ee4 55 $url->param('course', $courseid);
56}
57if ($view !== 'upcoming') {
58 $url->param('view', $view);
59}
60if ($day !== 0) {
61 $url->param('cal_d', $day);
62}
63if ($mon !== 0) {
64 $url->param('cal_m', $mon);
65}
66if ($yr !== 0) {
67 $url->param('cal_y', $yr);
68}
69$PAGE->set_url($url);
c9c9fb90 70
797cedc7
SH
71if ($courseid != SITEID && !empty($courseid)) {
72 $course = $DB->get_record('course', array('id' => $courseid));
73 $courses = array($course->id => $course);
74 $issite = false;
75 navigation_node::override_active_url(new moodle_url('/course/view.php', array('id' => $course->id)));
ca74470c 76} else {
797cedc7
SH
77 $course = get_site();
78 $courses = calendar_get_default_courses();
79 $issite = true;
93c91ee4 80}
797cedc7 81require_course_login($course);
2d4fb02f 82
b5a52acd
JH
83if (calendar_user_can_add_event($course)) {
84 $importresults = calendar_process_subscription_form($courseid);
85}
86
36dc3b71 87$calendar = new calendar_information($day, $mon, $yr);
797cedc7 88$calendar->prepare_for_view($course, $courses);
37d87d11 89
93c91ee4 90$now = usergetdate(time());
91$pagetitle = '';
7423f116 92
93c91ee4 93$strcalendar = get_string('calendar', 'calendar');
7423f116 94
797cedc7 95if (!checkdate($mon, $day, $yr)) {
93c91ee4 96 $day = intval($now['mday']);
97 $mon = intval($now['mon']);
98 $yr = intval($now['year']);
99}
100$time = make_timestamp($yr, $mon, $day);
101
102switch($view) {
103 case 'day':
104 $PAGE->navbar->add(userdate($time, get_string('strftimedate')));
3fe4a2fa 105 $pagetitle = get_string('dayviewtitle', 'calendar', userdate($time, get_string('strftimedaydate')));
93c91ee4 106 break;
107 case 'month':
108 $PAGE->navbar->add(userdate($time, get_string('strftimemonthyear')));
3fe4a2fa 109 $pagetitle = get_string('detailedmonthviewtitle', 'calendar', userdate($time, get_string('strftimemonthyear')));
93c91ee4 110 break;
111 case 'upcoming':
112 $pagetitle = get_string('upcomingevents', 'calendar');
113 break;
114}
1e1ff33b 115
93c91ee4 116// Print title and header
36dc3b71 117$PAGE->set_pagelayout('standard');
797cedc7
SH
118$PAGE->set_title("$course->shortname: $strcalendar: $pagetitle");
119$PAGE->set_heading($COURSE->fullname);
120$PAGE->set_button(calendar_preferences_button($course));
f22f1333 121
36dc3b71
SH
122$renderer = $PAGE->get_renderer('core_calendar');
123$calendar->add_sidecalendar_blocks($renderer, true, $view);
7423f116 124
36dc3b71
SH
125echo $OUTPUT->header();
126echo $renderer->start_layout();
127echo html_writer::start_tag('div', array('class'=>'heightcontainer'));
7423f116 128
93c91ee4 129switch($view) {
130 case 'day':
36dc3b71 131 echo $renderer->show_day($calendar);
93c91ee4 132 break;
133 case 'month':
05f9d136 134 echo $renderer->show_month_detailed($calendar, $url);
93c91ee4 135 break;
136 case 'upcoming':
797cedc7
SH
137 $defaultlookahead = CALENDAR_DEFAULT_UPCOMING_LOOKAHEAD;
138 if (isset($CFG->calendar_lookahead)) {
139 $defaultlookahead = intval($CFG->calendar_lookahead);
140 }
141 $lookahead = get_user_preferences('calendar_lookahead', $defaultlookahead);
142
143 $defaultmaxevents = CALENDAR_DEFAULT_UPCOMING_MAXEVENTS;
144 if (isset($CFG->calendar_maxevents)) {
145 $defaultmaxevents = intval($CFG->calendar_maxevents);
146 }
147 $maxevents = get_user_preferences('calendar_maxevents', $defaultmaxevents);
148 echo $renderer->show_upcoming_events($calendar, $lookahead, $maxevents);
93c91ee4 149 break;
150}
3c49918a 151
93c91ee4 152//Link to calendar export page
153echo $OUTPUT->container_start('bottom');
b5a52acd
JH
154if (calendar_user_can_add_event($course)) {
155 echo calendar_show_subscriptions($courseid, $importresults);
156}
93c91ee4 157if (!empty($CFG->enablecalendarexport)) {
5c2ed7e2 158 echo $OUTPUT->single_button(new moodle_url('export.php', array('course'=>$courseid)), get_string('exportcalendar', 'calendar'));
4f0c2d00 159 if (isloggedin()) {
d52777b4
RW
160 $authtoken = sha1($USER->id . $USER->password . $CFG->calendar_exportsalt);
161 $link = new moodle_url('/calendar/export_execute.php', array('preset_what'=>'all', 'preset_time'=>'recentupcoming', 'userid' => $USER->id, 'authtoken'=>$authtoken));
36dc3b71
SH
162 $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')));
163 echo html_writer::tag('a', $icon, array('href'=>$link));
ea185313 164 }
93c91ee4 165}
797cedc7 166
93c91ee4 167echo $OUTPUT->container_end();
36dc3b71
SH
168echo html_writer::end_tag('div');
169echo $renderer->complete_layout();
93c91ee4 170echo $OUTPUT->footer();