From 79e8411dc2b2777ae16a1511f75da30a542c8f44 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Fri, 28 Dec 2007 19:14:01 +0000 Subject: [PATCH] Prevent race condition in event creation. MDL-5956. Credit goes to Penny, Luke and Martin @ Catalyst. Merged from MOODLE_19_STABLE --- backup/restorelib.php | 29 ++++++++++++++++------------- calendar/event.php | 11 ++++++----- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/backup/restorelib.php b/backup/restorelib.php index 353cb40a41..1af2ee4ce9 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -3250,19 +3250,6 @@ define('RESTORE_GROUPS_GROUPINGS', 3); $eve->userid = $adminid; } - //We must recode the repeatid if the event has it - if (!empty($eve->repeatid)) { - $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid); - if ($repeat_rec) { //Exists, so use it... - $eve->repeatid = $repeat_rec->new_id; - } else { //Doesn't exists, calculate the next and save it - $oldrepeatid = $eve->repeatid; - $max_rec = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event'); - $eve->repeatid = empty($max_rec) ? 1 : $max_rec->repeatid + 1; - backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid); - } - } - //We have to recode the groupid field $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid); if ($group) { @@ -3274,6 +3261,22 @@ define('RESTORE_GROUPS_GROUPINGS', 3); //The structure is equal to the db, so insert the event $newid = insert_record ("event",$eve); + + //We must recode the repeatid if the event has it + //The repeatid now refers to the id of the original event. (see Bug#5956) + if ($newid && !empty($eve->repeatid)) { + $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid); + if ($repeat_rec) { //Exists, so use it... + $eve->repeatid = $repeat_rec->new_id; + } else { //Doesn't exists, calculate the next and save it + $oldrepeatid = $eve->repeatid; + $eve->repeatid = $newid; + backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid); + } + $eve->id = $newid; + // update the record to contain the correct repeatid + update_record('event',$eve); + } } else { //get current event id $newid = $eve_db->id; diff --git a/calendar/event.php b/calendar/event.php index bc37b72d44..ac650ac810 100644 --- a/calendar/event.php +++ b/calendar/event.php @@ -217,13 +217,14 @@ if (count($err) == 0) { $form->timemodified = time(); - if ($form->repeat) { - $fetch = get_record_sql('SELECT 1, MAX(repeatid) AS repeatid FROM '.$CFG->prefix.'event'); - $form->repeatid = empty($fetch) ? 1 : $fetch->repeatid + 1; - } - /// Get the event id for the log record. $eventid = insert_record('event', $form, true); + + /// Use the event id as the repeatid to link repeat entries together + if ($form->repeat) { + $form->repeatid = $form->id = $eventid; + update_record('event', $form); // update the row, to set its repeatid + } /// Log the event entry. add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, stripslashes($form->name)); -- 2.39.5