mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-16660 calendar: Cleaned u several areas of the iCal patch in prep…
…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
Showing
8 changed files
with
477 additions
and
333 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.