diff --git a/calendar/lib.php b/calendar/lib.php index 14b4999d6b1..8401de36add 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -2696,13 +2696,13 @@ function calendar_get_eventtype_choices($courseid) { calendar_get_allowed_types($allowed, $courseid); if ($allowed->user) { - $choices[0] = get_string('userevents', 'calendar'); + $choices['user'] = get_string('userevents', 'calendar'); } if ($allowed->site) { - $choices[SITEID] = get_string('siteevents', 'calendar'); + $choices['site'] = get_string('siteevents', 'calendar'); } if (!empty($allowed->courses)) { - $choices[$courseid] = get_string('courseevents', 'calendar'); + $choices['course'] = get_string('courseevents', 'calendar'); } if (!empty($allowed->groups) and is_array($allowed->groups)) { $choices['group'] = get_string('group'); @@ -2718,11 +2718,15 @@ function calendar_get_eventtype_choices($courseid) { * @return int The insert ID, if any. */ function calendar_add_subscription($sub) { - global $DB, $USER; + global $DB, $USER, $SITE; - $sub->courseid = $sub->eventtype; - if ($sub->eventtype == 'group') { + if ($sub->eventtype === 'site') { + $sub->courseid = $SITE->id; + } else if ($sub->eventtype === 'group' || $sub->eventtype === 'course') { $sub->courseid = $sub->course; + } else { + // User events. + $sub->courseid = 0; } $sub->userid = $USER->id; @@ -2798,10 +2802,10 @@ function calendar_add_icalendar_event($event, $courseid, $subscriptionid = null) $eventrecord->userid = $sub->userid; $eventrecord->groupid = $sub->groupid; $eventrecord->courseid = $sub->courseid; + $eventrecord->eventtype = $sub->eventtype; } else { - $eventrecord->userid = $USER->id; - $eventrecord->groupid = 0; // TODO: ??? - $eventrecord->courseid = $courseid; + // We should never do anything with an event without a subscription reference. + return 0; } if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid))) { diff --git a/calendar/managesubscriptions_form.php b/calendar/managesubscriptions_form.php index 731bbd37027..63921cf0249 100644 --- a/calendar/managesubscriptions_form.php +++ b/calendar/managesubscriptions_form.php @@ -80,7 +80,7 @@ class calendar_addsubscription_form extends moodleform { 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); + $mform->setType('eventtype', PARAM_ALPHA); if (!empty($groups) and is_array($groups)) { $groupoptions = array(); diff --git a/lib/db/install.xml b/lib/db/install.xml index 5bd64ecb4ce..f60af6cb9b4 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2888,8 +2888,9 @@ - - + + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index d630e2171d0..97d4f06d7f5 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1472,6 +1472,20 @@ function xmldb_main_upgrade($oldversion) { // Main savepoint reached. upgrade_main_savepoint(true, 2012111200.01); } + if ($oldversion < 2012111601.01) { + + // Define field eventtype to be added to event_subscriptions. + $table = new xmldb_table('event_subscriptions'); + $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'userid'); + + // Conditionally launch add field eventtype. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2012111601.01); + } if ($oldversion < 2012111601.01) { // Clea up after old shared memory caching support.