Skip to content

Commit

Permalink
MDL-16660 calendar: Cleaned u several areas of the iCal patch in prep…
Browse files Browse the repository at this point in the history
…aration of the next peer-review

* Added a bit of AMOS to copy existing strings and save a little translation effort
* Cleaned up fixed strings in several places
* Cleaned up some existing strings as required.
* Fixed install and upgrade code. Split upgrade into two parts (one for each table).
* Fixed fatal error caused by missing forms lib inclusion
* Added param types to forms.
* Converted file_get_content to use curl for URL's.
* Cleaned things up per coding style.
* Separated subscription management and form into separate files.
* Tidied up bennu inclusion to just where required.
* Lots of other small fixes as well.

AMOS BEGIN
  CPY [calendar,calendar],[colcalendar,calendar]
  CPY [actions,moodle],[actions,calendar]
  CPY [never,moodle],[never,calendar]
AMOS END
  • Loading branch information
Sam Hemelryk committed Oct 30, 2012
1 parent 4c349ad commit e30390a
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 333 deletions.
423 changes: 112 additions & 311 deletions calendar/lib.php

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions calendar/managesubscriptions.php
@@ -0,0 +1,107 @@
<?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/>.

/**
* Allows the user to manage calendar subscriptions.
*
* @copyright 2012 Jonathan Harker
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package calendar
*/

require_once('../config.php');
require_once($CFG->libdir.'/bennu/bennu.inc.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/calendar/lib.php');
require_once($CFG->dirroot.'/calendar/managesubscriptions_form.php');

// Required use.
$courseid = optional_param('course', SITEID, PARAM_INT);
// Used for processing subscription actions.
$subscriptionid = optional_param('id', 0, PARAM_INT);
$pollinterval = optional_param('pollinterval', 0, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);

$url = new moodle_url('/calendar/managesubscriptions.php');
if ($courseid != SITEID) {
$url->param('course', $courseid);
}
navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
$PAGE->set_url($url);
$PAGE->set_pagelayout('standard');
$PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));

if ($courseid != SITEID && !empty($courseid)) {
$course = $DB->get_record('course', array('id' => $courseid));
$courses = array($course->id => $course);
} else {
$course = get_site();
$courses = calendar_get_default_courses();
}
require_course_login($course);
if (!calendar_user_can_add_event($course)) {
print_error('errorcannotimport', 'calendar');
}

$form = new calendar_addsubscription_form(null);
$form->set_data(array(
'course' => $course->id
));

$importresults = '';

$formdata = $form->get_data();
if (!empty($formdata)) {
require_sesskey(); // Must have sesskey for all actions.
$subscriptionid = calendar_add_subscription($formdata);
if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
// Blank the URL if it's a file import.
$formdata->url = '';
$calendar = $form->get_ical_data();
$ical = new iCalendar();
$ical->unserialize($calendar);
$importresults = calendar_import_icalendar_events($ical, $courseid, $subscriptionid);
} else {
$importresults = calendar_update_subscription_events($subscriptionid);
}
// Redirect to prevent refresh issues.
redirect($PAGE->url);
} else if (!empty($subscriptionid)) {
// The user is wanting to perform an action upon an existing subscription.
require_sesskey(); // Must have sesskey for all actions.
$importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
}

$sql = 'SELECT *
FROM {event_subscriptions}
WHERE courseid = :courseid
OR (courseid = 0 AND userid = :userid)';
$params = array('courseid' => $courseid, 'userid' => $USER->id);
$subscriptions = $DB->get_records_sql($sql, $params);

// Print title and header.
$PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
$PAGE->set_heading($course->fullname);
$PAGE->set_button(calendar_preferences_button($course));

$renderer = $PAGE->get_renderer('core_calendar');

echo $OUTPUT->header();
// Display a table of subscriptions.
echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
// Display the add subscription form.
$form->display();
echo $OUTPUT->footer();
133 changes: 133 additions & 0 deletions calendar/managesubscriptions_form.php
@@ -0,0 +1,133 @@
<?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/>.

/**
* Allows the user to manage calendar subscriptions.
*
* @copyright 2012 Jonathan Harker
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package calendar
*/

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}

require_once($CFG->libdir.'/formslib.php');

/**
* Form for adding a subscription to a Moodle course calendar.
* @copyright 2012 Jonathan Harker
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_addsubscription_form extends moodleform {

/**
* Defines the form used to add calendar subscriptions.
*/
public function definition() {
$mform = $this->_form;
$courseid = optional_param('course', 0, PARAM_INT);

$mform->addElement('header', 'addsubscriptionform', get_string('importcalendarheading', 'calendar'));

// Name.
$mform->addElement('text', 'name', get_string('subscriptionname', 'calendar'), array('maxsize' => '255', 'size' => '40'));
$mform->addRule('name', get_string('required'), 'required');
$mform->setType('name', PARAM_TEXT);

// Import from (url | importfile).
$mform->addElement('html', get_string('importfrominstructions', 'calendar'));
$choices = array(CALENDAR_IMPORT_FROM_FILE => get_string('importfromfile', 'calendar'),
CALENDAR_IMPORT_FROM_URL => get_string('importfromurl', 'calendar'));
$mform->addElement('select', 'importfrom', get_string('importcalendarfrom', 'calendar'), $choices);
$mform->setDefault('importfrom', CALENDAR_IMPORT_FROM_URL);

// URL.
$mform->addElement('text', 'url', get_string('importfromurl', 'calendar'), array('maxsize' => '255', 'size' => '50'));
$mform->setType('url', PARAM_URL);

// Import file
$mform->addElement('filepicker', 'importfile', get_string('importfromfile', 'calendar'));

$mform->disabledIf('url', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_FILE);
$mform->disabledIf('importfile', 'importfrom', 'eq', CALENDAR_IMPORT_FROM_URL);

// Poll interval
$choices = calendar_get_pollinterval_choices();
$mform->addElement('select', 'pollinterval', get_string('pollinterval', 'calendar'), $choices);
$mform->setDefault('pollinterval', 604800);
$mform->addHelpButton('pollinterval', 'pollinterval', 'calendar');
$mform->setType('pollinterval', PARAM_INT);

// Eventtype: 0 = user, 1 = global, anything else = course ID.
list($choices, $groups) = calendar_get_eventtype_choices($courseid);
$mform->addElement('select', 'eventtype', get_string('eventkind', 'calendar'), $choices);
$mform->addRule('eventtype', get_string('required'), 'required');
$mform->setType('eventtype', PARAM_INT);

if (!empty($groups) and is_array($groups)) {
$groupoptions = array();
foreach ($groups as $group) {
$groupoptions[$group->id] = $group->name;
}
$mform->addElement('select', 'groupid', get_string('typegroup', 'calendar'), $groupoptions);
$mform->setType('groupid', PARAM_INT);
$mform->disabledIf('groupid', 'eventtype', 'noteq', 'group');
}

$mform->addElement('hidden', 'course');
$mform->addElement('hidden', 'sesskey', sesskey());
$mform->addElement('submit', 'add', get_string('add'));
}

/**
* Validates the returned data.
*
* @param array $data
* @param array $files
* @return array
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['url']) && empty($data['importfile'])) {
if (!empty($data['importfrom']) && $data['importfrom'] == CALENDAR_IMPORT_FROM_FILE) {
$errors['importfile'] = get_string('errorrequiredurlorfile', 'calendar');
} else {
$errors['url'] = get_string('errorrequiredurlorfile', 'calendar');
}
}
return $errors;
}

/**
* Returns the ical content either from the uploaded file, or from the URL.
*
* @return bool|mixed|string
*/
public function get_ical_data() {
$formdata = $this->get_data();
switch ($formdata->importfrom) {
case CALENDAR_IMPORT_FROM_FILE:
$calendar = $this->get_file_content('importfile');
break;
case CALENDAR_IMPORT_FROM_URL:
$calendar = download_file_content($formdata->importurl);
break;
}
return $calendar;
}
}
99 changes: 99 additions & 0 deletions calendar/renderer.php
Expand Up @@ -23,6 +23,10 @@
* @package calendar
*/

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}

/**
* The primary renderer for the calendar.
*/
Expand Down Expand Up @@ -718,4 +722,99 @@ protected function course_filter_selector(moodle_url $returnurl, $label=null) {
}
return $this->output->render($select);
}

/**
* Renders a table containing information about calendar subscriptions.
*
* @param int $courseid
* @param array $subscriptions
* @param string $importresults
* @return string
*/
public function subscription_details($courseid, $subscriptions, $importresults = '') {
$table = new html_table();
$table->head = array(
get_string('colcalendar', 'calendar'),
get_string('collastupdated', 'calendar'),
get_string('colpoll', 'calendar'),
get_string('colactions', 'calendar')
);
$table->align = array('left', 'left', 'left', 'center');
$table->width = '100%';
$table->data = array();

if (empty($subscriptions)) {
$cell = new html_table_cell(get_string('nocalendarsubscriptions', 'calendar'));
$cell->colspan = 4;
$table->data[] = new html_table_row(array($cell));
}
$strnever = new lang_string('never', 'calendar');
foreach ($subscriptions as $sub) {
$label = $sub->name;
if (!empty($sub->url)) {
$label = html_writer::link($sub->url, $label);
}
if (empty($sub->lastupdated)) {
$lastupdated = $strnever->out();
} else {
$lastupdated = userdate($sub->lastupdated, get_string('strftimedatetimeshort', 'langconfig'));
}

$cell = new html_table_cell($this->subscription_action_form($sub, $courseid));
$cell->colspan = 2;

$table->data[] = new html_table_row(array(
new html_table_cell($label),
new html_table_cell($lastupdated),
$cell
));
}

$out = $this->output->box_start('generalbox calendarsubs');

$out .= $importresults;
$out .= html_writer::table($table);
$out .= $this->output->box_end();
return $out;
}

/**
* Creates a form to perform actions on a given subscription.
*
* @param stdClass $subscription
* @param int $courseid
* @return string
*/
protected function subscription_action_form($subscription, $courseid) {
// Assemble form for the subscription row.
$html = html_writer::start_tag('form', array('action' => new moodle_url('/calendar/managesubscriptions.php'), 'method' => 'post'));
if (empty($subscription->url)) {
// Don't update an iCal file, which has no URL.
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'pollinterval', 'value' => '0'));
} else {
// Assemble pollinterval control.
$html .= html_writer::start_tag('div', array('style' => 'float:left;'));
$html .= html_writer::start_tag('select', array('name' => 'pollinterval'));
foreach (calendar_get_pollinterval_choices() as $k => $v) {
$attributes = array();
if ($k == $subscription->pollinterval) {
$attributes['selected'] = 'selected';
}
$html .= html_writer::tag('option', $v, $attributes);
}
$html .= html_writer::end_tag('select');
$html .= html_writer::end_tag('div');
}
$html .= html_writer::start_tag('div', array('style' => 'float:right;'));
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'course', 'value' => $courseid));
$html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $subscription->id));
if (!empty($subscription->url)) {
$html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('update')));
}
$html .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'action', 'value' => get_string('remove')));
$html .= html_writer::end_tag('div');
$html .= html_writer::end_tag('form');
return $html;
}
}
14 changes: 5 additions & 9 deletions calendar/view.php
Expand Up @@ -80,10 +80,6 @@
}
require_course_login($course);

if (calendar_user_can_add_event($course)) {
$importresults = calendar_process_subscription_form($courseid);
}

$calendar = new calendar_information($day, $mon, $yr);
$calendar->prepare_for_view($course, $courses);

Expand Down Expand Up @@ -149,13 +145,13 @@
break;
}

//Link to calendar export page
//Link to calendar export page.
echo $OUTPUT->container_start('bottom');
if (calendar_user_can_add_event($course)) {
echo calendar_show_subscriptions($courseid, $importresults);
}
if (!empty($CFG->enablecalendarexport)) {
echo $OUTPUT->single_button(new moodle_url('export.php', array('course'=>$courseid)), get_string('exportcalendar', 'calendar'));
if (calendar_user_can_add_event($course)) {
echo $OUTPUT->single_button(new moodle_url('/calendar/managesubscriptions.php', array('course'=>$courseid)), get_string('managesubscriptions', 'calendar'));
}
if (isloggedin()) {
$authtoken = sha1($USER->id . $USER->password . $CFG->calendar_exportsalt);
$link = new moodle_url('/calendar/export_execute.php', array('preset_what'=>'all', 'preset_time'=>'recentupcoming', 'userid' => $USER->id, 'authtoken'=>$authtoken));
Expand All @@ -167,4 +163,4 @@
echo $OUTPUT->container_end();
echo html_writer::end_tag('div');
echo $renderer->complete_layout();
echo $OUTPUT->footer();
echo $OUTPUT->footer();

0 comments on commit e30390a

Please sign in to comment.