/** * Checks if mapped questions are exact valid, or marks them to be created * * @global $DB * @throws moodle_exception */ protected function define_execution() { global $DB; $restoreid = $this->get_restoreid(); $courseid = $this->get_courseid(); $userid = $this->task->get_userid(); $workaround_qtypes = explode(',', get_config('block_sharing_cart', 'workaround_qtypes')); // @see /backup/util/dbops/restore_dbops.class.php#prechek_precheck_qbanks_by_level $contexts = restore_dbops::restore_get_question_banks($restoreid); foreach ($contexts as $contextid => $contextlevel) { $categories = restore_dbops::restore_get_question_categories($restoreid, $contextid); $canadd = false; if ($targetcontext = restore_dbops::restore_find_best_target_context($categories, $courseid, $contextlevel)) { $canadd = has_capability('moodle/question:add', $targetcontext, $userid); } foreach ($categories as $category) { $questions = restore_dbops::restore_get_questions($restoreid, $category->id); foreach ($questions as $question) { if (!in_array($question->qtype, $workaround_qtypes)) { continue; } $mapping = restore_dbops::get_backup_ids_record($restoreid, 'question', $question->id); if ($mapping && $mapping->newitemid && !self::is_question_valid($question->qtype, $mapping->newitemid)) { if (!$canadd) { throw new moodle_exception('questioncannotberestored', 'backup', '', $question); } $catmapping = restore_dbops::get_backup_ids_record($restoreid, 'question_category', $category->id); $matchquestions = $DB->get_records('question', array('category' => $catmapping->newitemid, 'qtype' => $question->qtype, 'stamp' => $question->stamp, 'version' => $question->version)); $newitemid = 0; // to be created if no valid duplicate exists foreach ($matchquestions as $q) { if ($q->id == $mapping->newitemid) { continue; } if (self::is_question_valid($question->qtype, $q->id)) { $newitemid = $q->id; // updates mapping if a valid one found break; } } $this->update_mapping($mapping, $newitemid); } } } } }
protected function define_execution() { global $DB; $contexts = restore_dbops::restore_get_question_banks($this->get_restoreid(), CONTEXT_MODULE); foreach ($contexts as $contextid => $contextlevel) { // Only if context mapping exists (i.e. the module has been restored) if ($newcontext = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $contextid)) { // Update all the qcats having their parentitemid set to the original contextid $modulecats = $DB->get_records_sql("SELECT itemid, newitemid\n FROM {backup_ids_temp}\n WHERE backupid = ?\n AND itemname = 'question_category'\n AND parentitemid = ?", array($this->get_restoreid(), $contextid)); foreach ($modulecats as $modulecat) { $DB->set_field('question_categories', 'contextid', $newcontext->newitemid, array('id' => $modulecat->newitemid)); // And set new contextid also in question_category mapping (will be // used by {@link restore_create_question_files} later restore_dbops::set_backup_ids_record($this->get_restoreid(), 'question_category', $modulecat->itemid, $modulecat->newitemid, $newcontext->newitemid); } } } }