diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 08dc660848ae2..540613d1826d3 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -614,13 +614,20 @@ public static function prechek_precheck_qbanks_by_level($restoreid, $courseid, $ } else { self::set_backup_ids_record($restoreid, 'question_category', $category->id, $matchcat->id, $targetcontext->id); $questions = self::restore_get_questions($restoreid, $category->id); + + // Collect all the questions for this category into memory so we only talk to the DB once. + $questioncache = $DB->get_records_sql_menu("SELECT ".$DB->sql_concat('stamp', "' '", 'version').", id + FROM {question} + WHERE category = ?", array($matchcat->id)); + foreach ($questions as $question) { - $matchq = $DB->get_record('question', array( - 'category' => $matchcat->id, - 'stamp' => $question->stamp, - 'version' => $question->version)); + if (isset($questioncache[$question->stamp." ".$question->version])) { + $matchqid = $questioncache[$question->stamp." ".$question->version]; + } else { + $matchqid = false; + } // 5a) No match, check if user can add q - if (!$matchq) { + if (!$matchqid) { // 6a) User can, mark the q to be created if ($canadd) { // Nothing to mark, newitemid means create @@ -645,7 +652,7 @@ public static function prechek_precheck_qbanks_by_level($restoreid, $courseid, $ // 5b) Match, mark q to be mapped } else { - self::set_backup_ids_record($restoreid, 'question', $question->id, $matchq->id); + self::set_backup_ids_record($restoreid, 'question', $question->id, $matchqid); } } }