Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#447 - Separate studentquiz_state_history creation from data migration #448

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ function xmldb_studentquiz_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2021102501, 'studentquiz');
}

if ($oldversion < 2021102502) {
if ($oldversion < 2021102502.01) {
// Define table studentquiz_state_history to be created.
$table = new xmldb_table('studentquiz_state_history');

Expand All @@ -902,39 +902,49 @@ function xmldb_studentquiz_upgrade($oldversion) {
// Conditionally launch create table for studentquiz_state_history.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
upgrade_mod_savepoint(true, 2021102502.01, 'studentquiz');
}

$sql = "SELECT sqq.questionid, sqq.state, q.createdby, q.timecreated
FROM {studentquiz_question} sqq
JOIN {question} q ON q.id = sqq.questionid";
$sqlcount = "SELECT COUNT(DISTINCT sqq.questionid)
FROM {studentquiz_question} sqq
JOIN {question} q ON q.id = sqq.questionid";

$total = $DB->count_records_sql($sqlcount);

if ($total > 0) {
$progressbar = new progress_bar('updatestatequestions', 500, true);
$sqquestions = $DB->get_recordset_sql($sql);
$transaction = $DB->start_delegated_transaction();
$i = 1;
foreach ($sqquestions as $sqquestion) {
// Create action new question by onwer.
utils::question_save_action($sqquestion->questionid, $sqquestion->createdby,
studentquiz_helper::STATE_NEW, $sqquestion->timecreated);

if (!($sqquestion->state == studentquiz_helper::STATE_NEW)) {
utils::question_save_action($sqquestion->questionid, get_admin()->id, $sqquestion->state, null);
}
$progressbar->update($i, $total, "Update the state for question - {$i}/{$total}.");
$i++;
if ($oldversion < 2021102502.02) {

$sql = "SELECT sqq.questionid, sqq.state, q.createdby, q.timecreated
FROM {studentquiz_question} sqq
JOIN {question} q ON q.id = sqq.questionid";
$sqlcount = "SELECT COUNT(DISTINCT sqq.questionid)
FROM {studentquiz_question} sqq
JOIN {question} q ON q.id = sqq.questionid";

$total = $DB->count_records_sql($sqlcount);

if ($total > 0) {
$progressbar = new progress_bar('updatestatequestions', 500, true);
$sqquestions = $DB->get_recordset_sql($sql);
$transaction = $DB->start_delegated_transaction();
$i = 1;
foreach ($sqquestions as $sqquestion) {
// Create action new question by owner.
$data = new \stdClass();
$data->questionid = $sqquestion->questionid;
$data->userid = $sqquestion->createdby;
$data->state = studentquiz_helper::STATE_NEW;
$data->timecreated = $sqquestion->timecreated;

$DB->insert_record('studentquiz_state_history', $data);

if (!($sqquestion->state == studentquiz_helper::STATE_NEW)) {
$data->userid = get_admin()->id;
$data->timecreated = time();
$DB->insert_record('studentquiz_state_history', $data);
}
$transaction->allow_commit();
$sqquestions->close();
$progressbar->update($i, $total, "Update the state for question - {$i}/{$total}.");
$i++;
}
$transaction->allow_commit();
$sqquestions->close();
}

// Studentquiz savepoint reached.
upgrade_mod_savepoint(true, 2021102502, 'studentquiz');
upgrade_mod_savepoint(true, 2021102502.02, 'studentquiz');
}

if ($oldversion < 2021120200) {
Expand Down