You are here

function _multichoice_check_answers in Quiz 6.4

Same name and namespace in other branches
  1. 8.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 257

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_result(db_query('SELECT COUNT(DISTINCT id) FROM {quiz_multichoice_answers}'));
  }
  $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($sql, $sandbox['last_nid'], $sandbox['last_vid'], 0, 500);
  $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_revisions}
              WHERE vid = %d';
      $res2 = db_query($sql, $res_o->question_vid);
      $sandbox['last_format'] = db_result($res2);
      $sql = 'INSERT INTO {quiz_multichoice_properties}
              (nid, vid, choice_boolean)
              VALUES(%d, %d, 1)';
      db_query($sql, $res_o->question_nid, $res_o->question_vid);
    }
    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';
        db_query($sql, $res_o->question_nid, $res_o->question_vid);
      }
    }

    // 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'];
    db_query($sql, $lf, $lf, $lf, $res_o->question_vid);
    $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);
  }
}