You are here

function _multichoice_check_answers in Quiz 8.4

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

We go through the alternatives and update question properties and field formats

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 268

Code

function _multichoice_check_answers(&$to_return, &$sandbox) {

  // Init persistent variables
  if (!isset($sandbox['multichoice_check_answers'])) {
    $sandbox['step_progress'] = 0;
    $sandbox['multichoice_check_answers'] = TRUE;
    $sandbox['last_nid'] = 0;
    $sandbox['last_vid'] = 0;
    $sandbox['step_max'] = db_query('SELECT COUNT(DISTINCT id) FROM {quiz_multichoice_answers}')
      ->fetchField();
  }
  $sandbox['num_corrects'] = 0;
  $sql = 'SELECT id, question_nid, question_vid, score_if_chosen, score_if_not_chosen
          FROM {quiz_multichoice_answers}
          WHERE question_nid >= %d AND question_vid >= %d
          ORDER BY question_nid, question_vid';
  $res = db_query_range('SELECT id, question_nid, question_vid, score_if_chosen, score_if_not_chosen
          FROM {quiz_multichoice_answers}
          WHERE question_nid >= :question_nid AND question_vid >= :question_vid
          ORDER BY question_nid, question_vid', array(
    ':question_nid' => $sandbox['last_nid'],
    ':question_vid' => $sandbox['last_vid'],
  ));
  $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']) {

      // This is the same node as we processed in the last loop
      if ($progress_to_add + $sandbox['step_progress'] + 1 >= $sandbox['step_max']) {
        $sandbox['step_progress'] = $sandbox['step_max'];
      }
    }
    else {

      // New node
      $sandbox['step_progress'] += $progress_to_add;
      $progress_to_add = 0;
      $sandbox['last_nid'] = $res_o->question_nid;
      $sandbox['last_vid'] = $res_o->question_vid;
      $sandbox['num_corrects'] = 0;

      // Store the format for this node
      $sql = 'SELECT format
              FROM {node_revision}
              WHERE vid = %d';
      $res2 = db_query('SELECT format
              FROM {node_revision}
              WHERE vid = :vid', array(
        ':vid' => $res_o->question_vid,
      ));
      $sandbox['last_format'] = $res2
        ->fetchField();
      $sql = 'INSERT INTO {quiz_multichoice_properties}
              (nid, vid, choice_boolean)
              VALUES(%d, %d, 1)';

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

      /* db_query($sql, $res_o->question_nid, $res_o->question_vid) */
      NULL;
    }
    if ($res_o->score_if_chosen == 1) {
      $sandbox['num_corrects']++;
      if ($sandbox['num_corrects'] == 2) {

        // If more than one answer is correct we set the choice_multi property to one.
        $sql = 'UPDATE {quiz_multichoice_properties}
                SET choice_multi = 1
                WHERE nid = %d AND vid = %d';

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

        /* db_query($sql, $res_o->question_nid, $res_o->question_vid) */
        db_update('quiz_multichoice_properties')
          ->fields(array(
          'choice_multi' => 1,
        ))
          ->condition('nid', $res_o->question_nid)
          ->condition('vid', $res_o->question_vid)
          ->execute();
      }
    }

    // Update all the formats
    $sql = 'UPDATE {quiz_multichoice_answers}
            SET answer_format = %d, feedback_if_chosen_format = %d, feedback_if_not_chosen_format = %d
            WHERE question_vid = %d';

    // This table haven't been indexed yet, making this query very slow... :/
    $lf = $sandbox['last_format'];

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

    /* db_query($sql, $lf, $lf, $lf, $res_o->question_vid) */
    db_update('quiz_multichoice_answers')
      ->fields(array(
      'answer_format' => $lf,
      'feedback_if_chosen_format' => $lf,
      'feedback_if_not_chosen_format' => $lf,
    ))
      ->condition('question_vid', $res_o->question_vid)
      ->execute();
    $progress_to_add++;
  }
  $sandbox['step_progress'] += $progress_to_add;

  // Check if we are finished, and jump to the next step if we are
  if ($sandbox['step_progress'] >= $sandbox['step_max']) {
    _multichoice_next_step($to_return, $sandbox);
  }
}