From a008de2a58cae82121bf88826bbeade33f550fa7 Mon Sep 17 00:00:00 2001 From: Brian Barnes Date: Tue, 28 Oct 2014 13:24:31 +1300 Subject: [PATCH] MDL-46814 calendar: Export calendar uses moodle forms --- calendar/classes/export_form.php | 90 ++++++++++++++++++++++++++++++++ calendar/export.php | 81 +++++++++++++++------------- 2 files changed, 135 insertions(+), 36 deletions(-) create mode 100644 calendar/classes/export_form.php diff --git a/calendar/classes/export_form.php b/calendar/classes/export_form.php new file mode 100644 index 00000000000..e5d2e161c6a --- /dev/null +++ b/calendar/classes/export_form.php @@ -0,0 +1,90 @@ +. + +/* + * 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'), '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'), '
'); + $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..8e0a6795059 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 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; -} +// Get the calendar type we are using. +$calendartype = \core_calendar\type_factory::get_calendar_instance(); +$now = $calendartype->timestamp_to_date_array($time); -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); +$weekend = CALENDAR_DEFAULT_WEEKEND; +if (isset($CFG->calendar_weekend)) { + $weekend = intval($CFG->calendar_weekend); +} +$numberofdaysinweek = $calendartype->get_num_weekdays(); + +$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(); -- 2.43.0