You are here

function _multichoice_move_old_answers in Quiz 7.4

Same name and namespace in other branches
  1. 8.4 question_types/multichoice/multichoice_update_6400.inc \_multichoice_move_old_answers()
  2. 6.4 question_types/multichoice/multichoice_update_6400.inc \_multichoice_move_old_answers()
  3. 7 question_types/multichoice/multichoice_update_6400.inc \_multichoice_move_old_answers()

Move/convert user answer data from the old data model to the new

Parameters

$to_return: Array where progress can be reported

$sandbox: Array where persistent data can be stored, and progress can be found

File

question_types/multichoice/multichoice_update_6400.inc, line 85

Code

function _multichoice_move_old_answers(&$to_return, &$sandbox = NULL) {

  // Store persistent data in the sandbox variable
  if (!isset($sandbox['step_progress'])) {
    $sandbox['step_progress'] = 0;
    $sandbox['last_rid'] = -1;
    $sandbox['last_nid'] = -1;
    $sandbox['last_vid'] = -1;
    $sandbox['last_id'] = -1;
    $sandbox['step_max'] = db_query('SELECT COUNT(DISTINCT id) FROM {quiz_multichoice_user_answers}')
      ->fetchField();
  }

  // Fetch old user ans
  $sql = 'SELECT id, answer_id, question_nid, question_vid, result_id
          FROM {quiz_multichoice_user_answers}
          WHERE result_id >= %d
          ORDER BY result_id, question_nid, question_vid, answer_id';
  $res = db_query_range('SELECT id, answer_id, question_nid, question_vid, result_id
          FROM {quiz_multichoice_user_answers}
          WHERE result_id >= :result_id
          ORDER BY result_id, question_nid, question_vid, answer_id', array(
    ':result_id' => $sandbox['last_rid'],
  ));
  $progress_to_add = 0;
  while ($res_o = db_fetch_object($res)) {
    if ($res_o->question_nid == $sandbox['last_nid'] && $res_o->question_vid == $sandbox['last_vid'] && $res_o->result_id == $sandbox['last_rid']) {

      // We only keep one row for each question result in quiz_multichoice_user_answers
      if ($res_o->id > $sandbox['last_id']) {
        $sql2 = 'DELETE FROM {quiz_multichoice_user_answers}
                 WHERE id = %d';

        // TODO Please review the conversion of this statement to the D7 database API syntax.

        /* db_query($sql2, $res_o->id) */
        db_delete('quiz_multichoice_user_answers')
          ->condition('id', $res_o->id)
          ->execute();
        if ($progress_to_add + $sandbox['step_progress'] + 1 >= $sandbox['step_max']) {
          $sandbox['step_progress'] = $sandbox['step_max'];
        }
      }
    }
    else {

      // This is the first row for this question result
      $sandbox['last_nid'] = $res_o->question_nid;
      $sandbox['last_vid'] = $res_o->question_vid;
      $sandbox['last_rid'] = $res_o->result_id;
      $sandbox['last_id'] = $res_o->id;
      $sandbox['step_progress'] += $progress_to_add;
      $progress_to_add = 0;
    }

    // We insert ther answers in quiz_multichoice_user_answer_multi
    $sql = 'INSERT IGNORE INTO {quiz_multichoice_user_answer_multi}
            (user_answer_id, answer_id)
            VALUES(%d, %d)';

    // TODO Please convert this statement to the D7 database API syntax.

    /* db_query($sql, $sandbox['last_id'], $res_o->answer_id) */
    NULL;
    $progress_to_add++;
  }

  // We move on if all user answers have been moved
  if ($sandbox['step_progress'] >= $sandbox['step_max']) {
    _multichoice_next_step($to_return, $sandbox);
  }
}