Skip to content

Commit

Permalink
Merge branch 'MDL-58810-master' of git://github.com/junpataleta/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed May 8, 2017
2 parents 7167d25 + c3b1178 commit ac6b845
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
20 changes: 13 additions & 7 deletions calendar/classes/rrule_manager.php
Expand Up @@ -24,6 +24,7 @@


namespace core_calendar; namespace core_calendar;


use calendar_event;
use DateInterval; use DateInterval;
use DateTime; use DateTime;
use moodle_exception; use moodle_exception;
Expand Down Expand Up @@ -223,7 +224,7 @@ public function parse_rrule() {
/** /**
* Create events for specified rrule. * Create events for specified rrule.
* *
* @param \calendar_event $passedevent Properties of event to create. * @param calendar_event $passedevent Properties of event to create.
* @throws moodle_exception * @throws moodle_exception
*/ */
public function create_events($passedevent) { public function create_events($passedevent) {
Expand All @@ -243,13 +244,16 @@ public function create_events($passedevent) {
// Generate timestamps that obey the rrule. // Generate timestamps that obey the rrule.
$eventtimes = $this->generate_recurring_event_times($eventrec); $eventtimes = $this->generate_recurring_event_times($eventrec);


// Adjust the parent event's timestart, if necessary. // Update the parent event. Make sure that its repeat ID is the same as its ID.
$calevent = new calendar_event($eventrec);
$updatedata = new stdClass();
$updatedata->repeatid = $event->id;
// Also, adjust the parent event's timestart, if necessary.
if (count($eventtimes) > 0 && !in_array($eventrec->timestart, $eventtimes)) { if (count($eventtimes) > 0 && !in_array($eventrec->timestart, $eventtimes)) {
$calevent = new \calendar_event($eventrec); $updatedata->timestart = reset($eventtimes);
$updatedata = (object)['timestart' => $eventtimes[0], 'repeatid' => $eventrec->id];
$calevent->update($updatedata, false);
$eventrec->timestart = $calevent->timestart;
} }
$calevent->update($updatedata, false);
$eventrec->timestart = $calevent->timestart;


// Create the recurring calendar events. // Create the recurring calendar events.
$this->create_recurring_events($eventrec, $eventtimes); $this->create_recurring_events($eventrec, $eventtimes);
Expand Down Expand Up @@ -719,7 +723,9 @@ protected function create_recurring_events($event, $eventtimes) {
$cloneevent->repeatid = $event->id; $cloneevent->repeatid = $event->id;
$cloneevent->timestart = $time; $cloneevent->timestart = $time;
unset($cloneevent->id); unset($cloneevent->id);
\calendar_event::create($cloneevent, false); // UUID should only be set on the first instance of the recurring events.
unset($cloneevent->uuid);
calendar_event::create($cloneevent, false);
} }


// If COUNT rule is defined and the number of the generated event times is less than the the COUNT rule, // If COUNT rule is defined and the number of the generated event times is less than the the COUNT rule,
Expand Down
10 changes: 10 additions & 0 deletions calendar/tests/rrule_manager_test.php
Expand Up @@ -466,11 +466,21 @@ public function test_every_300_days_forever() {
$records = $DB->get_records('event', array('repeatid' => $this->event->id), 'timestart ASC'); $records = $DB->get_records('event', array('repeatid' => $this->event->id), 'timestart ASC');


$expecteddate = clone($startdatetime); $expecteddate = clone($startdatetime);
$first = true;
foreach ($records as $record) { foreach ($records as $record) {
$this->assertLessThanOrEqual($until, $record->timestart); $this->assertLessThanOrEqual($until, $record->timestart);
$this->assertEquals($expecteddate->format('Y-m-d H:i:s'), date('Y-m-d H:i:s', $record->timestart)); $this->assertEquals($expecteddate->format('Y-m-d H:i:s'), date('Y-m-d H:i:s', $record->timestart));
// Go to next iteration. // Go to next iteration.
$expecteddate->add($interval); $expecteddate->add($interval);
// Check UUID.
if ($first) {
// The first instance of the event contains the UUID.
$this->assertEquals('uuid', $record->uuid);
$first = false;
} else {
// Succeeding instances will not contain the UUID.
$this->assertEmpty($record->uuid);
}
} }
} }


Expand Down
22 changes: 22 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -2872,5 +2872,27 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2017050300.01); upgrade_main_savepoint(true, 2017050300.01);
} }


if ($oldversion < 2017050500.01) {
// Get the list of parent event IDs.
$sql = "SELECT DISTINCT repeatid
FROM {event}
WHERE repeatid <> 0";
$parentids = array_keys($DB->get_records_sql($sql));
// Check if there are repeating events we need to process.
if (!empty($parentids)) {
// The repeat IDs of parent events should match their own ID.
// So we need to update parent events that have non-matching IDs and repeat IDs.
list($insql, $params) = $DB->get_in_or_equal($parentids);
$updatesql = "UPDATE {event}
SET repeatid = id
WHERE id <> repeatid
AND id $insql";
$DB->execute($updatesql, $params);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2017050500.01);
}

return true; return true;
} }
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -29,7 +29,7 @@


defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();


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


Expand Down

0 comments on commit ac6b845

Please sign in to comment.