You are here

function quiz_update_questions in Quiz 5

Same name and namespace in other branches
  1. 5.2 quiz.module \quiz_update_questions()
  2. 6.6 quiz.module \quiz_update_questions()
  3. 6.2 quiz.module \quiz_update_questions()
  4. 6.3 quiz.module \quiz_update_questions()
  5. 6.5 quiz.module \quiz_update_questions()

Updates questions assigned to the quiz

Parameters

$questions: Array of questions and their status

Return value

True if update was a success, false if three was a problem

1 call to quiz_update_questions()
quiz_questions_form_submit in ./quiz.module
Submit function for quiz_questions

File

./quiz.module, line 1247
Quiz Module

Code

function quiz_update_questions($questions) {
  $quiz = node_load(arg(1));
  $return = true;
  if (!empty($questions)) {

    // Get currently assigned questions
    $result = db_query("SELECT quiz_nid, question_nid, question_status FROM {quiz_questions} WHERE quiz_nid = %d", $quiz->nid);
    $assigned_questions = array();
    while ($assigned_question = db_fetch_object($result)) {
      $assigned_questions[$assigned_question->question_nid] = $assigned_question->question_status;
    }

    // Perform update if question is already assigned, or insert if it's a new question
    while (list($key, $value) = each($questions)) {

      // only do something if the status actually changed
      if ($assigned_questions[$key] != $value) {
        if ($value == QUESTION_NEVER) {
          $result = db_query("DELETE FROM {quiz_questions} WHERE quiz_nid = %d AND question_nid = %d", $quiz->nid, $key);
        }
        else {
          if (isset($assigned_questions[$key])) {
            $result = db_query("UPDATE {quiz_questions} SET question_status = %d WHERE quiz_nid = %d AND question_nid = %d", $value, $quiz->nid, $key);
          }
          else {
            $result = db_query("INSERT INTO {quiz_questions} (quiz_nid, question_nid, question_status) VALUES (%d, %d, %d)", $quiz->nid, $key, $value);
          }
        }
        if (!$result) {
          $return = FALSE;
        }
      }
    }
  }
  else {
    $return = false;
  }
  return $return;
}