Skip to content

Commit

Permalink
Merge branch 'MDL-36621-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	calendar/lib.php
	lib/db/caches.php
	version.php
  • Loading branch information
Sam Hemelryk committed Jan 22, 2013
2 parents 68a6d87 + e73b527 commit 3654684
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 24 deletions.
1 change: 1 addition & 0 deletions admin/settings/appearance.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
$temp->add(new admin_setting_configselect('calendar_exportlookahead', new lang_string('configexportlookahead','admin'), new lang_string('helpexportlookahead', 'admin'), 365, $days));
$temp->add(new admin_setting_configselect('calendar_exportlookback', new lang_string('configexportlookback','admin'), new lang_string('helpexportlookback', 'admin'), 5, $days));
$temp->add(new admin_setting_configtext('calendar_exportsalt', new lang_string('calendarexportsalt','admin'), new lang_string('configcalendarexportsalt', 'admin'), random_string(60)));
$temp->add(new admin_setting_configcheckbox('calendar_showicalsource', new lang_string('configshowicalsource', 'admin'), new lang_string('helpshowicalsource','admin'), 1));
$ADMIN->add('appearance', $temp);

// blog
Expand Down
86 changes: 63 additions & 23 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ function calendar_get_days() {
return array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
}

/**
* Get the subscription from a given id
*
* @since Moodle 2.5
* @param int $id id of the subscription
* @return stdClass Subscription record from DB
* @throws moodle_exception for an invalid id
*/
function calendar_get_subscription($id) {
global $DB;

$cache = cache::make('core', 'calendar_subscriptions');
$subscription = $cache->get($id);
if (empty($subscription)) {
$subscription = $DB->get_record('event_subscriptions', array('id' => $id), '*', MUST_EXIST);
// cache the data.
$cache->set($id, $subscription);
}
return $subscription;
}

/**
* Gets the first day of the week
*
Expand Down Expand Up @@ -314,7 +335,7 @@ function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_y
if (!isset($events[$eventid])) {
continue;
}
$event = $events[$eventid];
$event = new calendar_event($events[$eventid]);
$popupalt = '';
$component = 'moodle';
if(!empty($event->modulename)) {
Expand All @@ -335,7 +356,15 @@ function calendar_get_mini($courses, $groups, $users, $cal_month = false, $cal_y

$popupcontent .= html_writer::start_tag('div');
$popupcontent .= $OUTPUT->pix_icon($popupicon, $popupalt, $component);
$popupcontent .= html_writer::link($dayhref, format_string($event->name, true));
$name = format_string($event->name, true);
// Show ical source if needed.
if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
$a = new stdClass();
$a->name = $name;
$a->source = $event->subscription->name;
$name = get_string('namewithsource', 'calendar', $a);
}
$popupcontent .= html_writer::link($dayhref, $name);
$popupcontent .= html_writer::end_tag('div');
}

Expand Down Expand Up @@ -1910,6 +1939,10 @@ public function __construct($data=null) {
$data->id = null;
}

if (!empty($data->subscriptionid)) {
$data->subscription = calendar_get_subscription($data->subscriptionid);
}

// Default to a user event
if (empty($data->eventtype)) {
$data->eventtype = 'user';
Expand Down Expand Up @@ -2743,12 +2776,18 @@ function calendar_add_subscription($sub) {
$sub->pollinterval = 0;
}

$cache = cache::make('core', 'calendar_subscriptions');

if (!empty($sub->name)) {
if (empty($sub->id)) {
$id = $DB->insert_record('event_subscriptions', $sub);
// we cannot cache the data here because $sub is not complete.
return $id;
} else {
// Why are we doing an update here?
$DB->update_record('event_subscriptions', $sub);
// update cache.
$cache->set($sub->id, $sub);
return $sub->id;
}
} else {
Expand All @@ -2762,9 +2801,10 @@ function calendar_add_subscription($sub) {
* @param object $event The RFC-2445 iCalendar event
* @param int $courseid The course ID
* @param int $subscriptionid The iCalendar subscription ID
* @throws dml_exception A DML specific exception is thrown for invalid subscriptionids.
* @return int Code: 1=updated, 2=inserted, 0=error
*/
function calendar_add_icalendar_event($event, $courseid, $subscriptionid = null) {
function calendar_add_icalendar_event($event, $courseid, $subscriptionid) {
global $DB, $USER;

// Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event.
Expand Down Expand Up @@ -2805,16 +2845,13 @@ function calendar_add_icalendar_event($event, $courseid, $subscriptionid = null)
$eventrecord->timemodified = time();

// Add the iCal subscription details if required.
if ($sub = $DB->get_record('event_subscriptions', array('id' => $subscriptionid))) {
$eventrecord->subscriptionid = $subscriptionid;
$eventrecord->userid = $sub->userid;
$eventrecord->groupid = $sub->groupid;
$eventrecord->courseid = $sub->courseid;
$eventrecord->eventtype = $sub->eventtype;
} else {
// We should never do anything with an event without a subscription reference.
return 0;
}
// We should never do anything with an event without a subscription reference.
$sub = calendar_get_subscription($subscriptionid);
$eventrecord->subscriptionid = $subscriptionid;
$eventrecord->userid = $sub->userid;
$eventrecord->groupid = $sub->groupid;
$eventrecord->courseid = $sub->courseid;
$eventrecord->eventtype = $sub->eventtype;

if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid))) {
$eventrecord->id = $updaterecord->id;
Expand All @@ -2838,20 +2875,18 @@ function calendar_add_icalendar_event($event, $courseid, $subscriptionid = null)
* @param int $subscriptionid The ID of the subscription we are acting upon.
* @param int $pollinterval The poll interval to use.
* @param int $action The action to be performed. One of update or remove.
* @throws dml_exception if invalid subscriptionid is provided
* @return string A log of the import progress, including errors
*/
function calendar_process_subscription_row($subscriptionid, $pollinterval, $action) {
global $DB;

if (empty($subscriptionid)) {
return '';
}

// Fetch the subscription from the database making sure it exists.
$sub = $DB->get_record('event_subscriptions', array('id' => $subscriptionid), '*', MUST_EXIST);
$sub = calendar_get_subscription($subscriptionid);

$strupdate = get_string('update');
$strremove = get_string('remove');

// Update or remove the subscription, based on action.
switch ($action) {
case $strupdate:
Expand All @@ -2862,6 +2897,10 @@ function calendar_process_subscription_row($subscriptionid, $pollinterval, $acti
$sub->pollinterval = $pollinterval;
$DB->update_record('event_subscriptions', $sub);

// update the cache.
$cache = cache::make('core', 'calendar_subscriptions');
$cache->set($sub->id, $sub);

// Update the events.
return "<p>".get_string('subscriptionupdated', 'calendar', $sub->name)."</p>" . calendar_update_subscription_events($subscriptionid);

Expand Down Expand Up @@ -2890,6 +2929,7 @@ function calendar_delete_subscription($subscription) {
// Delete subscription and related events.
$DB->delete_records('event', array('subscriptionid' => $subscription));
$DB->delete_records('event_subscriptions', array('id' => $subscription));
cache_helper::invalidate_by_definition('core', 'calendar_subscriptions', array(), array($subscription));
}
/**
* From a URL, fetch the calendar and return an iCalendar object.
Expand Down Expand Up @@ -2976,10 +3016,7 @@ function calendar_import_icalendar_events($ical, $courseid, $subscriptionid = nu
function calendar_update_subscription_events($subscriptionid) {
global $DB;

$sub = $DB->get_record('event_subscriptions', array('id' => $subscriptionid));
if (empty($sub)) {
print_error('errorbadsubscription', 'calendar');
}
$sub = calendar_get_subscription($subscriptionid);
// Don't update a file subscription. TODO: Update from a new uploaded file.
if (empty($sub->url)) {
return 'File subscription not updated.';
Expand All @@ -2988,6 +3025,9 @@ function calendar_update_subscription_events($subscriptionid) {
$return = calendar_import_icalendar_events($ical, $sub->courseid, $subscriptionid);
$sub->lastupdated = time();
$DB->update_record('event_subscriptions', $sub);
// Update the cache.
$cache = cache::make('core', 'calendar_subscriptions');
$cache->set($subscriptionid, $sub);
return $return;
}

Expand All @@ -3005,7 +3045,7 @@ function calendar_can_edit_subscription($subscriptionorid) {
} else if (is_object($subscriptionorid)) {
$subscription = $subscriptionorid;
} else {
$subscription = $DB->get_record('event_subscriptions', array('id' => $subscriptionorid), '*', MUST_EXIST);
$subscription = calendar_get_subscription($subscriptionorid);
}
$allowed = new stdClass;
$courseid = $subscription->courseid;
Expand Down
12 changes: 12 additions & 0 deletions calendar/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public function show_day(calendar_information $calendar, moodle_url $returnurl =
* @return string
*/
public function event(calendar_event $event, $showactions=true) {
global $CFG;

$event = calendar_add_event_metadata($event);

$anchor = html_writer::tag('a', '', array('name'=>'event_'.$event->id));
Expand Down Expand Up @@ -320,6 +322,16 @@ public function event(calendar_event $event, $showactions=true) {
if (!empty($event->courselink)) {
$table->data[0]->cells[1]->text .= html_writer::tag('div', $event->courselink, array('class'=>'course'));
}
// Show subscription source if needed.
if (!empty($event->subscription) && $CFG->calendar_showicalsource) {
if (!empty($event->subscription->url)) {
$source = html_writer::link($event->subscription->url, get_string('subsource', 'calendar', $event->subscription));
} else {
// File based ical.
$source = get_string('subsource', 'calendar', $event->subscription);
}
$table->data[0]->cells[1]->text .= html_writer::tag('div', $source, array('class' => 'subscription'));
}
if (!empty($event->time)) {
$table->data[0]->cells[1]->text .= html_writer::tag('span', $event->time, array('class'=>'date'));
} else {
Expand Down
5 changes: 5 additions & 0 deletions calendar/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
This files describes API changes in /calendar/* ,
information provided here is intended especially for developers.

=== 2.5 ===
required changes in code:
* calendar_add_icalendar_event() now requires a valid subscriptionid
* calendar_process_subscription_row() throws exception for invalid subscriptionid
* calendar_update_subscription_events() now throws a dml_exception instead of moodle_exception for bad subscriptions

=== 2.4 ===

Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
$string['configsessioncookiedomain'] = 'This allows you to change the domain that the Moodle cookies are available from. This is useful for Moodle customisations (e.g. authentication or enrolment plugins) that need to share Moodle session information with a web application on another subdomain. <strong>WARNING: it is strongly recommended to leave this setting at the default (empty) - an incorrect value will prevent all logins to the site.</strong>';
$string['configsessioncookiepath'] = 'If you need to change where browsers send the Moodle cookies, you can change this setting to specify a subdirectory of your web site. Otherwise the default \'/\' should be fine.';
$string['configsessiontimeout'] = 'If people logged in to this site are idle for a long time (without loading pages) then they are automatically logged out (their session is ended). This variable specifies how long this time should be.';
$string['configshowicalsource'] = 'Show source information for ical events.';
$string['configshowcommentscount'] = 'Show comments count, it will cost one more query when display comments link';
$string['configshowsiteparticipantslist'] = 'All of these site students and site teachers will be listed on the site participants list. Who shall be allowed to see this site participants list?';
$string['configsitedefaultlicense'] = 'Default site licence';
Expand Down Expand Up @@ -568,6 +569,7 @@
$string['helpexportlookahead'] = 'How many days in the future does the calendar look for events during export for the custom export option?';
$string['helpexportlookback'] = 'How many days in the past does the calendar look for events during export for the custom export option?';
$string['helpforcetimezone'] = 'You can allow users to individually select their timezone, or force a timezone for everyone.';
$string['helpshowicalsource'] = 'Enable this setting if you want to show the ical subscription name and link for ical imported events.';
$string['helpsitemaintenance'] = 'For upgrades and other work';
$string['helpstartofweek'] = 'Which day starts the week in the calendar?';
$string['helpupcominglookahead'] = 'How many days in the future does the calendar look for upcoming events by default?';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
$string['monthlyview'] = 'Monthly view';
$string['monthnext'] = 'Next month';
$string['monththis'] = 'This month';
$string['namewithsource'] = '{$a->name}({$a->source})';
$string['never'] = 'Never';
$string['newevent'] = 'New event';
$string['notitle'] = 'no title';
Expand Down Expand Up @@ -168,6 +169,7 @@
$string['subscriptionname'] = 'Calendar name';
$string['subscriptionremoved'] = 'Calendar subscription {$a} removed';
$string['subscriptionupdated'] = 'Calendar subscription {$a} updated';
$string['subsource'] = 'Event source: {$a->name}';
$string['sun'] = 'Sun';
$string['sunday'] = 'Sunday';
$string['thu'] = 'Thu';
Expand Down
7 changes: 7 additions & 0 deletions lib/db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@
'persist' => true, // Likely there will be a couple of calls to this.
'persistmaxsize' => 2, // The original cache used 1, we've increased that to two.
)
// Used to cache calendar subscriptions.
'calendar_subscriptions' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
'persistent' => true,
),
);
2 changes: 2 additions & 0 deletions theme/base/style/calendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
.dir-rtl.path-calendar .maincalendar .eventlist .event .topic .name {float:right;}
.path-calendar .maincalendar .eventlist .event .topic .date {float:right;}
.dir-rtl.path-calendar .maincalendar .eventlist .event .topic .date {float:left;}
.path-calendar .maincalendar .eventlist .event .subscription {float:left;clear:left;}
.dir-rtl.path-calendar .maincalendar .eventlist .event .subscription {float:right;clear:right;}
.path-calendar .maincalendar .eventlist .event .course {float:left;clear:left;}
.dir-rtl.path-calendar .maincalendar .eventlist .event .course {float:right;clear:right;}
.path-calendar .maincalendar .eventlist .event .side {width:32px;}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();


$version = 2013011800.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2013011800.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

Expand Down

0 comments on commit 3654684

Please sign in to comment.