diff --git a/calendar/classes/export_form.php b/calendar/classes/export_form.php
new file mode 100644
index 00000000000..18fbe3336dc
--- /dev/null
+++ b/calendar/classes/export_form.php
@@ -0,0 +1,89 @@
+.
+
+/**
+ * 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'), '
');
+ $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'), 'monthnow');
+ 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'), '
');
+ $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);
+ }
+}
\ No newline at end of file
diff --git a/calendar/export.php b/calendar/export.php
index 92c2db337f2..7de7d5cab4a 100644
--- a/calendar/export.php
+++ b/calendar/export.php
@@ -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);
+$calendarurl = '';
+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 . (isset($password->password) ? $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');
+ $calendarurl = 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 $calendarurl;
+
echo $renderer->complete_layout();
echo $OUTPUT->footer();
diff --git a/calendar/renderer.php b/calendar/renderer.php
index 91e7a4813e3..bc9d7d59a9c 100644
--- a/calendar/renderer.php
+++ b/calendar/renderer.php
@@ -32,96 +32,6 @@ if (!defined('MOODLE_INTERNAL')) {
*/
class core_calendar_renderer extends plugin_renderer_base {
- /**
- * Creates a basic export form
- *
- * @param bool $allowthisweek
- * @param bool $allownextweek
- * @param bool $allownextmonth
- * @param int $userid
- * @param string $authtoken
- * @return string
- */
- public function basic_export_form($allowthisweek, $allownextweek, $allownextmonth, $userid, $authtoken) {
- global $CFG;
-
- $output = html_writer::tag('div', get_string('export', 'calendar'), array('class'=>'header'));
- $output .= html_writer::start_tag('fieldset');
- $output .= html_writer::tag('legend', get_string('commontasks', 'calendar'));
- $output .= html_writer::start_tag('form', array('action'=>new moodle_url('/calendar/export_execute.php'), 'method'=>'get'));
-
- $output .= html_writer::tag('div', get_string('iwanttoexport', 'calendar'));
-
- $output .= html_writer::start_tag('div', array('class'=>'indent'));
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_all', 'value'=>'all', 'checked'=>'checked'));
- $output .= html_writer::tag('label', get_string('eventsall', 'calendar'), array('for'=>'pw_all'));
- $output .= html_writer::empty_tag('br');
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_what', 'id'=>'pw_course', 'value'=>'courses'));
- $output .= html_writer::tag('label', get_string('eventsrelatedtocourses', 'calendar'), array('for'=>'pw_course'));
- $output .= html_writer::empty_tag('br');
- $output .= html_writer::end_tag('div');
-
- $output .= html_writer::tag('div', get_string('for', 'calendar').':');
-
- $output .= html_writer::start_tag('div', array('class'=>'indent'));
- if ($allowthisweek) {
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknow', 'value'=>'weeknow', 'checked'=>'checked'));
- $output .= html_writer::tag('label', get_string('weekthis', 'calendar'), array('for'=>'pt_wknow'));
- $output .= html_writer::empty_tag('br');
- }
- if ($allownextweek) {
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_wknext', 'value'=>'weeknext'));
- $output .= html_writer::tag('label', get_string('weeknext', 'calendar'), array('for'=>'pt_wknext'));
- $output .= html_writer::empty_tag('br');
- }
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnow', 'value'=>'monthnow'));
- $output .= html_writer::tag('label', get_string('monththis', 'calendar'), array('for'=>'pt_monnow'));
- $output .= html_writer::empty_tag('br');
- if ($allownextmonth) {
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_monnext', 'value'=>'monthnext'));
- $output .= html_writer::tag('label', get_string('monthnext', 'calendar'), array('for'=>'pt_monnext'));
- $output .= html_writer::empty_tag('br');
- }
- $output .= html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'preset_time', 'id'=>'pt_recupc', 'value'=>'recentupcoming'));
- $output .= html_writer::tag('label', get_string('recentupcoming', 'calendar'), array('for'=>'pt_recupc'));
- $output .= html_writer::empty_tag('br');
-
- 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'));
- $output .= html_writer::empty_tag('input', array('type' => 'radio', 'name' => 'preset_time', 'id' => 'pt_custom', 'value' => 'custom'));
- $output .= html_writer::tag('label', get_string('customexport', 'calendar', $a), array('for' => 'pt_custom'));
- $output .= html_writer::empty_tag('br');
- }
-
- $output .= html_writer::end_tag('div');
- $output .= html_writer::start_tag('div', array('class'=>'rightalign'));
- $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_d', 'value'=>''));
- $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_m', 'value'=>''));
- $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'cal_y', 'value'=>''));
- $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'userid', 'value'=>$userid));
- $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'authtoken', 'value'=>$authtoken));
-
- $output .= html_writer::empty_tag('input', array('type'=>'submit', 'name' => 'generateurl', 'id'=>'generateurl', 'value'=>get_string('generateurlbutton', 'calendar')));
- $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('exportbutton', 'calendar')));
-
- $output .= html_writer::end_tag('div');
-
- $output .= html_writer::end_tag('form');
- $output .= html_writer::end_tag('fieldset');
-
- $output .= html_writer::start_tag('div', array('id'=>'urlbox', 'style'=>'display:none;'));
- $output .= html_writer::tag('p', get_string('urlforical', 'calendar'));
- $output .= html_writer::tag('div', '', array('id'=>'url', 'style'=>'overflow:scroll;width:650px;'));
- $output .= html_writer::end_tag('div');
-
- return $output;
- }
-
/**
* Starts the standard layout for the page
*