diff --git a/mod/assign/db/upgrade.php b/mod/assign/db/upgrade.php index 528ca095e50..a78cff41f61 100644 --- a/mod/assign/db/upgrade.php +++ b/mod/assign/db/upgrade.php @@ -93,5 +93,42 @@ function xmldb_assign_upgrade($oldversion) { // Automatically generated Moodle v4.0.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 286b6252321..b5eb7ce2b98 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -3168,12 +3168,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; @@ -3950,11 +3952,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 7cd20de9f23..4a5c6b1ea22 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 = 2022041900; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2022071300; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2022041200; // Requires this Moodle version.