You are here

function quiz_questions_form_submit in Quiz 7

Same name and namespace in other branches
  1. 8.4 quiz.admin.inc \quiz_questions_form_submit()
  2. 5.2 quiz.module \quiz_questions_form_submit()
  3. 5 quiz.module \quiz_questions_form_submit()
  4. 6.6 quiz.admin.inc \quiz_questions_form_submit()
  5. 6.2 quiz.admin.inc \quiz_questions_form_submit()
  6. 6.3 quiz.admin.inc \quiz_questions_form_submit()
  7. 6.4 quiz.admin.inc \quiz_questions_form_submit()
  8. 6.5 quiz.admin.inc \quiz_questions_form_submit()
  9. 7.6 quiz.admin.inc \quiz_questions_form_submit()
  10. 7.4 quiz.admin.inc \quiz_questions_form_submit()
  11. 7.5 quiz.admin.inc \quiz_questions_form_submit()

Submit function for quiz_questions.

Updates from the "manage questions" tab.

1 string reference to 'quiz_questions_form_submit'
quiz_questions_form in ./quiz.admin.inc
Handles "manage questions" tab.

File

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

Code

function quiz_questions_form_submit($form, &$form_state) {
  if (isset($form_state['#from_ahah'])) {
    return;
  }

  // Load the quiz node
  $quiz = node_load(intval(arg(1)));

  // Update the refresh latest quizzes table so that we know what the users latest quizzes are
  if (variable_get('quiz_auto_revisioning', 1)) {
    $is_new_revision = quiz_has_been_answered($quiz);
  }
  else {
    $is_new_revision = (bool) $form_state['values']['new_revision'];
  }
  _quiz_question_browser_submit($form, $form_state);
  $weight_map = $form_state['values']['weights'];
  $max_scores = $form_state['values']['max_scores'];
  $refreshes = isset($form_state['values']['revision']) ? $form_state['values']['revision'] : NULL;
  $stayers = $form_state['values']['stayers'];
  $compulsories = isset($form_state['values']['compulsories']) ? $form_state['values']['compulsories'] : NULL;
  $num_random = isset($form_state['values']['num_random_questions']) ? $form_state['values']['num_random_questions'] : 0;
  $quiz->max_score_for_random = isset($form_state['values']['max_score_for_random']) ? $form_state['values']['max_score_for_random'] : 1;
  $term_id = isset($form_state['values']['random_term_id']) ? (int) $form_state['values']['random_term_id'] : 0;

  // Store what questions belong to the quiz
  $questions = _quiz_update_items($quiz, $weight_map, $max_scores, $is_new_revision, $refreshes, $stayers, $compulsories);

  // If using random questions and no term ID is specified, make sure we have enough.
  if (empty($term_id)) {
    $assigned_random = 0;
    foreach ($questions as $question) {
      if ($question->state == QUESTION_RANDOM) {
        ++$assigned_random;
      }
    }

    // Adjust number of random questions downward to match number of selected questions..
    if ($num_random > $assigned_random) {
      $num_random = $assigned_random;
      drupal_set_message(t('The number of random questions for this @quiz have been lowered to %anum to match the number of questions you assigned.', array(
        '@quiz' => QUIZ_NAME,
        '%anum' => $assigned_random,
      ), array(
        'langcode' => 'warning',
      )));
    }
  }
  else {

    // Warn user if not enough questions available with this term_id.
    $available_random = count(_quiz_get_random_taxonomy_question_ids($term_id, $num_random));
    if ($num_random > $available_random) {
      $num_random = $available_random;
      drupal_set_message(t('There are currently not enough questions assigned to this term (@random). Please lower the number of random quetions or assign more questions to this taxonomy term before taking this @quiz.', array(
        '@random' => $available_random,
        '@quiz' => QUIZ_NAME,
      )), 'error');
    }
  }
  if ($quiz->type == 'quiz') {

    // Update the quiz node properties.
    $success = db_update('quiz_node_properties')
      ->fields(array(
      'number_of_random_questions' => $num_random ? $num_random : 0,
      'max_score_for_random' => $quiz->max_score_for_random,
      'tid' => $term_id,
    ))
      ->condition('vid', $quiz->vid)
      ->condition('nid', $quiz->nid)
      ->execute();

    // Get sum of max_score
    $query = db_select('quiz_node_relationship', 'qnr');
    $query
      ->addExpression('SUM(max_score)', 'sum');
    $query
      ->condition('parent_vid', $quiz->vid);
    $query
      ->condition('question_status', QUESTION_ALWAYS);
    $score = $query
      ->execute()
      ->fetchAssoc();
    $success2 = db_update('quiz_node_properties')
      ->expression('max_score', 'max_score_for_random * number_of_random_questions + :sum', array(
      ':sum' => (int) $score['sum'],
    ))
      ->condition('vid', $quiz->vid)
      ->execute();
  }
  if (isset($success) && isset($success2)) {
    drupal_set_message(t('Questions updated successfully.'));
  }
  else {
    drupal_set_message(t('There was an error updating the @quiz.', array(
      '@quiz' => QUIZ_NAME,
    )), 'error');
  }
}