diff --git a/mod/assign/db/upgrade.php b/mod/assign/db/upgrade.php index 25105e9c978..dca535dcce3 100644 --- a/mod/assign/db/upgrade.php +++ b/mod/assign/db/upgrade.php @@ -65,5 +65,42 @@ function xmldb_assign_upgrade($oldversion) { // Automatically generated Moodle v3.11.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2022071300) { + // The most recent assign submission should always have latest = 1, we want to find all records where this is not the case. + // Find the records with the maximum timecreated for each assign and user combination where latest is also 0. + $sqluser = "SELECT s.id + FROM {assign_submission} s + WHERE s.timecreated = ( + SELECT MAX(timecreated) timecreated + FROM {assign_submission} sm + WHERE s.assignment = sm.assignment + AND s.userid = sm.userid + AND sm.groupid = 0) + AND s.groupid = 0 + AND s.latest = 0"; + $idstofixuser = $DB->get_records_sql($sqluser, null); + + $sqlgroup = "SELECT s.id + FROM {assign_submission} s + WHERE s.timecreated = ( + SELECT MAX(timecreated) timecreated + FROM {assign_submission} sm + WHERE s.assignment = sm.assignment + AND s.groupid = sm.groupid + AND sm.groupid <> 0) + AND s.groupid <> 0 + AND s.latest = 0"; + $idstofixgroup = $DB->get_records_sql($sqlgroup, null); + + $idstofix = array_merge(array_keys($idstofixuser), array_keys($idstofixgroup)); + + if (count($idstofix)) { + [$insql, $inparams] = $DB->get_in_or_equal($idstofix); + $DB->set_field_select('assign_submission', 'latest', 1, "id $insql", $inparams); + } + + // Assignment savepoint reached. + upgrade_mod_savepoint(true, 2022071300, 'assign'); + } return true; } diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 31f37f1bb6b..33e3692bcec 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -3066,12 +3066,14 @@ class assign { $submission->latest = 1; } } + $transaction = $DB->start_delegated_transaction(); if ($submission->latest) { // This is the case when we need to set latest to 0 for all the other attempts. $DB->set_field('assign_submission', 'latest', 0, $params); } $submission->status = ASSIGN_SUBMISSION_STATUS_NEW; $sid = $DB->insert_record('assign_submission', $submission); + $transaction->allow_commit(); return $DB->get_record('assign_submission', array('id' => $sid)); } return false; @@ -3835,11 +3837,13 @@ class assign { $submission->latest = 1; } } + $transaction = $DB->start_delegated_transaction(); if ($submission->latest) { // This is the case when we need to set latest to 0 for all the other attempts. $DB->set_field('assign_submission', 'latest', 0, $params); } $sid = $DB->insert_record('assign_submission', $submission); + $transaction->allow_commit(); return $DB->get_record('assign_submission', array('id' => $sid)); } return false; diff --git a/mod/assign/version.php b/mod/assign/version.php index ebbe199a0fa..fa909e7bdda 100644 --- a/mod/assign/version.php +++ b/mod/assign/version.php @@ -25,5 +25,5 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'mod_assign'; // Full name of the plugin (used for diagnostics). -$plugin->version = 2021051700; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2022071300; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2021051100; // Requires this Moodle version.