#173 Fix race condition on certificate issues in scheduled task

This commit is contained in:
Eugene Venter 2018-03-27 15:30:50 +13:00 committed by Mark Nelson
parent f55f3cf399
commit 9f4e1f43b4

View file

@ -125,16 +125,21 @@ class email_certificate_task extends \core\task\scheduled_task {
} }
} }
// Ok, issue them the certificate. // Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
$customcertissue = new \stdClass(); $issueid = $DB->get_field('customcert_issues', 'id',
$customcertissue->customcertid = $customcert->id; array('userid' => $enroluser->id, 'customcertid' => $customcert->id));
$customcertissue->userid = $enroluser->id; if (empty($issueid)) {
$customcertissue->code = \mod_customcert\certificate::generate_code(); // Ok, issue them the certificate.
$customcertissue->emailed = 0; $customcertissue = new \stdClass();
$customcertissue->timecreated = time(); $customcertissue->customcertid = $customcert->id;
$customcertissue->userid = $enroluser->id;
$customcertissue->code = \mod_customcert\certificate::generate_code();
$customcertissue->emailed = 0;
$customcertissue->timecreated = time();
// Insert the record into the database. // Insert the record into the database.
$issueid = $DB->insert_record('customcert_issues', $customcertissue); $issueid = $DB->insert_record('customcert_issues', $customcertissue);
}
// Add them to the array so we email them. // Add them to the array so we email them.
$enroluser->issueid = $issueid; $enroluser->issueid = $issueid;