MDL-46814 calendar: Export calendar uses moodle forms

This commit is contained in:
Brian Barnes
2014-12-09 15:44:10 +01:00
committed by Eloy Lafuente (stronk7)
parent 23da79a464
commit a008de2a58
2 changed files with 133 additions and 34 deletions
+90
View File
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* The mform for exporting calendar events
*
* @package core_calendar
* @copyright 2014 Brian Barnes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Always include formslib.
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* The mform class for creating and editing a calendar
*
* @copyright 2014 Brian Barnes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_calendar_export_form extends moodleform {
/**
* The export form definition
* @throws coding_exception
*/
public function definition() {
global $CFG;
$mform = $this->_form;
$export = array();
$export[] = $mform->createElement('radio', 'exportevents', '', get_string('eventsall', 'calendar'), 'all');
$export[] = $mform->createElement('radio', 'exportevents', '', get_string('eventsrelatedtocourses', 'calendar'), 'courses');
$mform->addGroup($export, 'events', get_string('export', 'calendar'), '<br/>');
$mform->addGroupRule('events', get_string('required'), 'required');
$mform->setDefault('events', 'all');
$range = array();
if ($this->_customdata['allowthisweek']) {
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('weekthis', 'calendar'), 'weeknow');
}
if ($this->_customdata['allownextweek']) {
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('weeknext', 'calendar'), 'weeknext');
}
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('monththis', 'calendar'), 'monththis');
if ($this->_customdata['allownextmonth']) {
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('monthnext', 'calendar'), 'monthnext');
}
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('recentupcoming', 'calendar'), 'recentupcoming');
if ($CFG->calendar_customexport) {
$a = new stdClass();
$now = time();
$time = $now - $CFG->calendar_exportlookback * DAYSECS;
$a->timestart = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
$time = $now + $CFG->calendar_exportlookahead * DAYSECS;
$a->timeend = userdate($time, get_string('strftimedatefullshort', 'langconfig'));
$range[] = $mform->createElement('radio', 'timeperiod', '', get_string('customexport', 'calendar', $a), 'custom');
}
$mform->addGroup($range, 'period', get_string('for', 'calendar'), '<br/>');
$mform->addGroupRule('period', get_string('required'), 'required');
$mform->setDefault('period', 'recentupcoming');
$buttons = array();
$buttons[] = $mform->createElement('submit', 'generateurl', get_string('generateurlbutton', 'calendar'));
$buttons[] = $mform->createElement('submit', 'export', get_string('exportbutton', 'calendar'));
$mform->addGroup($buttons);
}
}
+43 -34
View File
@@ -62,8 +62,6 @@ $year = optional_param('cal_y', 0, PARAM_INT);
$time = optional_param('time', 0, PARAM_INT);
$generateurl = optional_param('generateurl', 0, PARAM_BOOL);
// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
// If a day, month and year were passed then convert it to a timestamp. If these were passed
// then we can assume the day, month and year are passed as Gregorian, as no where in core
@@ -104,7 +102,6 @@ $calendar = new calendar_information(0, 0, 0, $time);
$calendar->prepare_for_view($course, $courses);
$pagetitle = get_string('export', 'calendar');
$now = $calendartype->timestamp_to_date_array($time);
// Print title and header
if ($issite) {
@@ -122,43 +119,55 @@ $PAGE->set_button(calendar_preferences_button($course));
$renderer = $PAGE->get_renderer('core_calendar');
$calendar->add_sidecalendar_blocks($renderer);
echo $OUTPUT->header();
echo $renderer->start_layout();
switch($action) {
case 'advanced':
// Why nothing?
break;
case '':
default:
$weekend = CALENDAR_DEFAULT_WEEKEND;
if (isset($CFG->calendar_weekend)) {
$weekend = intval($CFG->calendar_weekend);
}
// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$now = $calendartype->timestamp_to_date_array($time);
// Get the number of days.
$numberofdaysinweek = $calendartype->get_num_weekdays();
$authtoken = sha1($USER->id . $DB->get_field('user', 'password', array('id'=>$USER->id)). $CFG->calendar_exportsalt);
// Let's populate some vars to let "common tasks" be somewhat smart...
// If today it's weekend, give the "next week" option.
$allownextweek = $weekend & (1 << $now['wday']);
// If it's the last week of the month, give the "next month" option.
$allownextmonth = calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek;
// If today it's weekend but tomorrow it isn't, do NOT give the "this week" option.
$allowthisweek = !(($weekend & (1 << $now['wday'])) && !($weekend & (1 << (($now['wday'] + 1) % $numberofdaysinweek))));
echo $renderer->basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $USER->id, $authtoken);
break;
$weekend = CALENDAR_DEFAULT_WEEKEND;
if (isset($CFG->calendar_weekend)) {
$weekend = intval($CFG->calendar_weekend);
}
$numberofdaysinweek = $calendartype->get_num_weekdays();
if (!empty($generateurl)) {
$params['userid'] = optional_param('userid', 0, PARAM_INT);
$params['authtoken'] = optional_param('authtoken', '', PARAM_ALPHANUM);
$params['preset_what'] = optional_param('preset_what', 'all', PARAM_ALPHA);
$params['preset_time'] = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
$formdata = array(
// Let's populate some vars to let "common tasks" be somewhat smart...
// If today it's weekend, give the "next week" option.
'allownextweek' => $weekend & (1 << $now['wday']),
// If it's the last week of the month, give the "next month" option.
'allownextmonth' => calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < $numberofdaysinweek,
// If today it's weekend but tomorrow it isn't, do NOT give the "this week" option.
'allowthisweek' => !(($weekend & (1 << $now['wday'])) && !($weekend & (1 << (($now['wday'] + 1) % $numberofdaysinweek))))
);
$exportform = new core_calendar_export_form(null, $formdata);
$calenderurl = '';
if ($data = $exportform->get_data()) {
$password = $DB->get_record('user', array('id' => $USER->id), 'password');
$params = array();
$params['userid'] = $USER->id;
$params['authtoken'] = sha1($USER->id . $password->password . $CFG->calendar_exportsalt);
$params['preset_what'] = $data->events['exportevents'];
$params['preset_time'] = $data->period['timeperiod'];
$link = new moodle_url('/calendar/export_execute.php', $params);
print html_writer::tag('div', get_string('calendarurl', 'calendar', $link->out()), array('class' => 'generalbox calendarurl'));
if (!empty($data->generateurl)) {
$urlclasses = array('class' => 'generalbox calendarurl');
$calenderurl = html_writer::tag( 'div', get_string('calendarurl', 'calendar', $link->out()), $urlclasses);
}
if (!empty($data->export)) {
redirect($link);
}
}
echo $OUTPUT->header();
echo $renderer->start_layout();
echo $OUTPUT->heading(get_string('exportcalendar', 'calendar'));
if ($action != 'advanced') {
$exportform->display();
}
echo $calenderurl;
echo $renderer->complete_layout();
echo $OUTPUT->footer();