/**
  * Processes the answer element (question answers). Common for various qtypes.
  * It handles both creation (if the question is being created) and mapping
  * (if the question already existed and is being reused)
  */
 public function process_question_answer($data)
 {
     global $DB;
     $data = (object) $data;
     $oldid = $data->id;
     // Detect if the question is created or mapped
     $oldquestionid = $this->get_old_parentid('question');
     $newquestionid = $this->get_new_parentid('question');
     $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
     // If the question has been created by restore, we need to create its question_answers too
     if ($questioncreated) {
         // Adjust some columns
         $data->question = $newquestionid;
         $data->answer = $data->answertext;
         // Insert record
         $newitemid = $DB->insert_record('question_answers', $data);
         // The question existed, we need to map the existing question_answers
     } else {
         // Look in question_answers by answertext matching
         $sql = 'SELECT id
                   FROM {question_answers}
                  WHERE question = ?
                    AND ' . $DB->sql_compare_text('answer', 255) . ' = ' . $DB->sql_compare_text('?', 255);
         $params = array($newquestionid, $data->answertext);
         $newitemid = $DB->get_field_sql($sql, $params);
         // If we haven't found the newitemid, something has gone really wrong, question in DB
         // is missing answers, exception
         if (!$newitemid) {
             $info = new stdClass();
             $info->filequestionid = $oldquestionid;
             $info->dbquestionid = $newquestionid;
             $info->answer = $data->answertext;
             throw restore_step_exception('error_question_answers_missing_in_db', $info);
         }
     }
     // Create mapping (we'll use this intensively when restoring question_states. And also answerfeedback files)
     $this->set_mapping('question_answer', $oldid, $newitemid);
 }
 /**
  * Process the qtype/matches/match element
  */
 public function process_match($data)
 {
     global $DB;
     $data = (object) $data;
     $oldid = $data->id;
     // Detect if the question is created or mapped
     $oldquestionid = $this->get_old_parentid('question');
     $newquestionid = $this->get_new_parentid('question');
     $questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
     // If the question has been created by restore, we need to create its question_match_sub too
     if ($questioncreated) {
         // Adjust some columns
         $data->question = $newquestionid;
         // Insert record
         $newitemid = $DB->insert_record('question_match_sub', $data);
         // Create mapping (there are files and states based on this)
         $this->set_mapping('question_match_sub', $oldid, $newitemid);
         // match questions require mapping of question_match_sub, because
         // they are used by question_states->answer
     } else {
         // Look for matching subquestion (by question, questiontext and answertext)
         $sub = $DB->get_record_select('question_match_sub', 'question = ? AND ' . $DB->sql_compare_text('questiontext') . ' = ' . $DB->sql_compare_text('?') . ' AND answertext = ?', array($newquestionid, $data->questiontext, $data->answertext), 'id', IGNORE_MULTIPLE);
         // Found, let's create the mapping
         if ($sub) {
             $this->set_mapping('question_match_sub', $oldid, $sub->id);
             // Something went really wrong, cannot map subquestion for one match question
         } else {
             throw restore_step_exception('error_question_match_sub_missing_in_db', $data);
         }
     }
 }