Skip to content

Commit

Permalink
MDL-61380 Quiz: Migrate "random" questions data into quiz_slots
Browse files Browse the repository at this point in the history
Search for all "random" questions in the qbank and move the configuration data of each one to the quiz slots table.
  • Loading branch information
rezaies committed Mar 1, 2018
1 parent b6c38f7 commit 6fc5a83
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
35 changes: 35 additions & 0 deletions mod/quiz/db/upgrade.php
Expand Up @@ -128,5 +128,40 @@ function xmldb_quiz_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2018020700, 'quiz');
}

if ($oldversion < 2018020701) {
// This SQL fetches all "random" questions from the question bank.
$fromclause = "FROM {quiz_slots} qs
JOIN {question} q ON q.id = qs.questionid
WHERE q.qtype = ?";

// Get the total record count - used for the progress bar.
$total = $DB->count_records_sql("SELECT count(qs.id) $fromclause", array('random'));

// Get the records themselves.
$rs = $DB->get_recordset_sql("SELECT qs.id, q.category, q.questiontext $fromclause", array('random'));

$a = new stdClass();
$a->total = $total;
$a->done = 0;

// For each question, move the configuration data to the quiz_slots table.
$pbar = new progress_bar('updatequizslotswithrandom', 500, true);
foreach ($rs as $record) {
$data = new stdClass();
$data->id = $record->id;
$data->questioncategoryid = $record->category;
$data->includingsubcategories = empty($record->questiontext) ? 0 : 1;
$DB->update_record('quiz_slots', $data);

// Update progress.
$a->done++;
$pbar->update($a->done, $a->total, get_string('updatequizslotswithrandomxofy', 'quiz', $a));
}
$rs->close();

// Quiz savepoint reached.
upgrade_mod_savepoint(true, 2018020701, 'quiz');
}

return true;
}
1 change: 1 addition & 0 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -917,6 +917,7 @@
$string['unit'] = 'Unit';
$string['unknowntype'] = 'Question type not supported at line {$a}. The question will be ignored';
$string['updatesettings'] = 'Update quiz settings';
$string['updatequizslotswithrandomxofy'] = 'Updating quiz slots with "random" question data ({$a->done}/{$a->total})';
$string['updatingatttemptgrades'] = 'Updating attempt grades.';
$string['updatingfinalgrades'] = 'Updating final grades.';
$string['updatingthegradebook'] = 'Updating the gradebook.';
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/version.php
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2018020700;
$plugin->version = 2018020701;
$plugin->requires = 2017110800;
$plugin->component = 'mod_quiz';
$plugin->cron = 60;

0 comments on commit 6fc5a83

Please sign in to comment.