Skip to content

Commit

Permalink
MDL-45523 quiz Cleanup duplicate question instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmerrill authored and danpoltawski committed May 13, 2014
1 parent 6794a98 commit 81d48fb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions mod/quiz/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ function xmldb_quiz_upgrade($oldversion) {
}

$questionidtoslotrowid = $DB->get_records_menu('quiz_slots',
array('quizid' => $quiz->id), '', 'questionid, id');
array('quizid' => $quiz->id), '', 'id, questionid');

$problemfound = false;
$currentpage = 1;
Expand All @@ -596,16 +596,17 @@ function xmldb_quiz_upgrade($oldversion) {
continue;
}

if (array_key_exists($questionid, $questionidtoslotrowid)) {
$key = array_search($questionid, $questionidtoslotrowid);
if ($key !== false) {
// Normal case. quiz_slots entry is present.
// Just need to add slot and page.
$quizslot = new stdClass();
$quizslot->id = $questionidtoslotrowid[$questionid];
$quizslot->id = $key;
$quizslot->slot = $slot;
$quizslot->page = $currentpage;
$DB->update_record('quiz_slots', $quizslot);

unset($questionidtoslotrowid[$questionid]); // So we can do a sanity check later.
unset($questionidtoslotrowid[$key]); // So we can do a sanity check later.
$slot++;
continue;

Expand Down Expand Up @@ -650,11 +651,11 @@ function xmldb_quiz_upgrade($oldversion) {
// quiz_slots rows linked to this quiz.
if (!empty($questionidtoslotrowid)) {
debugging('During upgrade, detected that questions ' .
implode(', ', array_keys($questionidtoslotrowid)) .
implode(', ', array_values($questionidtoslotrowid)) .
' had instances in quiz ' . $quiz->id . ' but were not actually used. ' .
'The instances have been removed.', DEBUG_NORMAL);

$DB->delete_records_list('quiz_slots', 'id', array_values($questionidtoslotrowid));
$DB->delete_records_list('quiz_slots', 'id', array_keys($questionidtoslotrowid));
$problemfound = true;
}

Expand Down

0 comments on commit 81d48fb

Please sign in to comment.