Prevent race condition in event creation. MDL-5956.

Credit goes to Penny, Luke and Martin @ Catalyst.

Merged from MOODLE_17_STABLE
This commit is contained in:
stronk7 2007-12-28 18:39:50 +00:00
parent 757bc06941
commit d0a2161941
2 changed files with 22 additions and 18 deletions

View file

@ -2521,19 +2521,6 @@
//Assign it to admin //Assign it to admin
$eve->userid = $adminid; $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 //We have to recode the groupid field
$group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid); $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
@ -2546,6 +2533,22 @@
//The structure is equal to the db, so insert the event //The structure is equal to the db, so insert the event
$newid = insert_record ("event",$eve); $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 { } else {
//get current event id //get current event id
$newid = $eve_db->id; $newid = $eve_db->id;

View file

@ -212,13 +212,14 @@
if (count($err) == 0) { if (count($err) == 0) {
$form->timemodified = time(); $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. /// Get the event id for the log record.
$eventid = insert_record('event', $form, true); $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. /// Log the event entry.
add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, stripslashes($form->name)); add_to_log($form->courseid, 'calendar', 'add', 'event.php?action=edit&id='.$eventid, stripslashes($form->name));