You are here

function _quiz_get_questions_from_form_state in Quiz 6.4

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \_quiz_get_questions_from_form_state()
  2. 7 quiz.admin.inc \_quiz_get_questions_from_form_state()
  3. 7.4 quiz.admin.inc \_quiz_get_questions_from_form_state()

Returns the questions that was in the question list when the form was submitted using ajax.

Parameters

$form_state: FAPI form_state(array)

Return value

$questions Array of questions as objects

1 call to _quiz_get_questions_from_form_state()
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

./quiz.admin.inc, line 963
Administrator interface for Quiz module.

Code

function _quiz_get_questions_from_form_state(&$form_state, &$questions_to_add) {
  $questions = array();

  // We first store all data from the post in a temporary array.
  // Then we fetch more data for each question from the database.
  $cur_questions = array();
  $vids = array();
  foreach ($form_state['post']['weights'] as $id => $value) {
    $cur_question = new stdClass();

    // Find nid and vid
    $matches = array();
    preg_match('/([0-9]+)-([0-9]+)/', $id, $matches);
    $cur_question->nid = $matches[1];
    if (!is_numeric($matches[2])) {
      continue;
    }
    $vids[] = $cur_question->vid = $matches[2];
    $cur_question->max_score = intval($form_state['post']['max_scores'][$id]);
    $cur_question->weight = intval($value);
    $cur_question->staying = $form_state['post']['stayers'][$id] === '1';
    $cur_question->question_status = QUESTION_ALWAYS;
    if ($cur_question->staying == TRUE) {
      $questions_to_add[] = $id;
    }
    $cur_questions[$cur_question->nid] = $cur_question;
  }

  // Fetching the rest of the data we need for each question...
  $sql = 'SELECT n.nid, n.type, n.vid AS latest_vid, r.title
          FROM {node_revisions} r
          JOIN {node} n
          ON n.nid = r.nid
          WHERE r.vid IN (' . implode(', ', $vids) . ')';

  // We use db_rewrite_sql to add node access security
  $res = db_query(db_rewrite_sql($sql));
  while ($res_o = db_fetch_object($res)) {
    $cur_questions[$res_o->nid]->type = $res_o->type;
    $cur_questions[$res_o->nid]->title = $res_o->title;
    $cur_questions[$res_o->nid]->latest_vid = $res_o->latest_vid;
    $questions[] = $cur_questions[$res_o->nid];
  }
  return $questions;
}