Merge branch 'MDL-36056-master-enrolkeywhitespace' of git://github.com/mudrd8mz/moodle
[moodle.git] / calendar / export.php
blob89ca1e0b401e98c27b6dc176f49af94eb19cc78e
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 User section Moodle
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
49 require_once('../config.php');
50 require_once($CFG->dirroot.'/course/lib.php');
51 require_once($CFG->dirroot.'/calendar/lib.php');
53 if (empty($CFG->enablecalendarexport)) {
54 die('no export');
57 $courseid = optional_param('course', SITEID, PARAM_INT);
58 $action = optional_param('action', '', PARAM_ALPHA);
59 $day = optional_param('cal_d', 0, PARAM_INT);
60 $mon = optional_param('cal_m', 0, PARAM_INT);
61 $year = optional_param('cal_y', 0, PARAM_INT);
62 $time = optional_param('time', 0, PARAM_INT);
63 $generateurl = optional_param('generateurl', 0, PARAM_BOOL);
66 // If a day, month and year were passed then convert it to a timestamp. If these were passed
67 // then we can assume the day, month and year are passed as Gregorian, as no where in core
68 // should we be passing these values rather than the time. This is done for BC.
69 if (!empty($day) && !empty($mon) && !empty($year)) {
70 if (checkdate($mon, $day, $year)) {
71 $time = make_timestamp($year, $mon, $day);
72 } else {
73 $time = time();
75 } else if (empty($time)) {
76 $time = time();
79 if ($courseid != SITEID && !empty($courseid)) {
80 // Course ID must be valid and existing.
81 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
82 $courses = array($course->id => $course);
83 $issite = false;
84 } else {
85 $course = get_site();
86 $courses = calendar_get_default_courses();
87 $issite = true;
89 require_login($course, false);
91 $url = new moodle_url('/calendar/export.php', array('time' => $time));
93 if ($action !== '') {
94 $url->param('action', $action);
97 if ($course !== NULL) {
98 $url->param('course', $course->id);
100 $PAGE->set_url($url);
102 $calendar = new calendar_information(0, 0, 0, $time);
103 $calendar->set_sources($course, $courses);
105 $pagetitle = get_string('export', 'calendar');
107 // Print title and header
108 if ($issite) {
109 $PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
111 $link = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming', 'course'=>$calendar->courseid));
112 $PAGE->navbar->add(get_string('calendar', 'calendar'), calendar_get_link_href($link, 0, 0, 0, $time));
113 $PAGE->navbar->add($pagetitle);
115 $PAGE->set_title($course->shortname.': '.get_string('calendar', 'calendar').': '.$pagetitle);
116 $PAGE->set_heading($course->fullname);
117 $PAGE->set_pagelayout('standard');
119 $renderer = $PAGE->get_renderer('core_calendar');
120 $calendar->add_sidecalendar_blocks($renderer);
122 // Get the calendar type we are using.
123 $calendartype = \core_calendar\type_factory::get_calendar_instance();
124 $now = $calendartype->timestamp_to_date_array($time);
126 $weekend = CALENDAR_DEFAULT_WEEKEND;
127 if (isset($CFG->calendar_weekend)) {
128 $weekend = intval($CFG->calendar_weekend);
130 $numberofdaysinweek = $calendartype->get_num_weekdays();
132 $formdata = array(
133 // Let's populate some vars to let "common tasks" be somewhat smart...
134 // If today it's weekend, give the "next week" option.
135 'allownextweek' => $weekend & (1 << $now['wday']),
136 // If it's the last week of the month, give the "next month" option.
137 'allownextmonth' => calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek,
138 // If today it's weekend but tomorrow it isn't, do NOT give the "this week" option.
139 'allowthisweek' => !(($weekend & (1 << $now['wday'])) && !($weekend & (1 << (($now['wday'] + 1) % $numberofdaysinweek))))
141 $exportform = new core_calendar_export_form(null, $formdata);
142 $calendarurl = '';
143 if ($data = $exportform->get_data()) {
144 $password = $DB->get_record('user', array('id' => $USER->id), 'password');
145 $params = array();
146 $params['userid'] = $USER->id;
147 $params['authtoken'] = sha1($USER->id . (isset($password->password) ? $password->password : '') . $CFG->calendar_exportsalt);
148 $params['preset_what'] = $data->events['exportevents'];
149 $params['preset_time'] = $data->period['timeperiod'];
151 $link = new moodle_url('/calendar/export_execute.php', $params);
152 if (!empty($data->generateurl)) {
153 $urlclasses = array('class' => 'generalbox calendarurl');
154 $calendarurl = html_writer::tag( 'div', get_string('calendarurl', 'calendar', $link->out()), $urlclasses);
157 if (!empty($data->export)) {
158 redirect($link);
162 echo $OUTPUT->header();
163 echo $renderer->start_layout();
164 echo $OUTPUT->heading(get_string('exportcalendar', 'calendar'));
166 if ($action != 'advanced') {
167 $exportform->display();
170 echo $calendarurl;
172 echo $renderer->complete_layout();
174 echo $OUTPUT->footer();