You are here

function quiz_report_form_submit in Quiz 8.4

Same name and namespace in other branches
  1. 6.4 quiz.pages.inc \quiz_report_form_submit()
  2. 7.6 quiz.pages.inc \quiz_report_form_submit()
  3. 7 quiz.pages.inc \quiz_report_form_submit()
  4. 7.4 quiz.pages.inc \quiz_report_form_submit()
  5. 7.5 quiz.pages.inc \quiz_report_form_submit()

Submit the report form

1 string reference to 'quiz_report_form_submit'
quiz_report_form in ./quiz.pages.inc
Form for showing feedback, and for editing the feedback if necessary...

File

./quiz.pages.inc, line 677
Page callback file for the quiz module.

Code

function quiz_report_form_submit($form, &$form_state) {

  /* We go through the form state values and submit all
   * questiontypes with validation functions declared.
   */
  $user = \Drupal::currentUser();
  foreach ($form_state['values'] as $key => $q_values) {

    // Questions has numeric keys in the report form
    if (!is_numeric($key)) {
      continue;
    }

    // Questions store the name of the validation function with the key 'submit'
    if (!isset($q_values['submit'])) {
      continue;
    }

    // The submit function must exist
    if (!function_exists($q_values['submit'])) {
      continue;
    }

    // Load the quiz
    if (!isset($quiz)) {
      $result = db_query('SELECT nid, uid, vid FROM {quiz_node_results} WHERE result_id = :result_id', array(
        ':result_id' => $q_values['rid'],
      ))
        ->fetchObject();
      $quiz = node_load($result->nid, $result->vid);
      $rid = $q_values['rid'];
    }
    $q_values['quiz'] = $quiz;

    // We call the submit function provided by the question
    call_user_func($q_values['submit'], $q_values);
  }

  // Scores may have been changed. We take the necessary actions
  quiz_update_total_score_fast($rid, $quiz
    ->getRevisionId());
  $changed = db_update('quiz_node_results')
    ->fields(array(
    'is_evaluated' => 1,
  ))
    ->condition('result_id', $rid)
    ->execute();
  $results_got_deleted = _quiz_maintain_results($quiz, $rid);

  // A message saying the quiz is unscored has already been set. We unset it here...
  if ($changed > 0) {
    _quiz_remove_unscored_message();
  }

  // Notify the user if results got deleted as a result of him scoring an answer.
  $add = $quiz->keep_results == QUIZ_KEEP_BEST && $results_got_deleted ? ' ' . t('Note that this quiz is set to only keep each users best answer.') : '';
  $score_data = quiz_get_score_array($rid, $quiz
    ->getRevisionId(), TRUE);
  module_invoke_all('quiz_scored', $quiz, $score_data, $rid);
  drupal_set_message(t('The scoring data you provided has been saved.') . $add);
  if (user_access('score taken quiz answer') && !user_access('view any quiz results')) {
    if ($result && $result->uid == $user
      ->id()) {
      $form_state['redirect'] = 'node/' . $quiz
        ->id() . '/results/' . $rid;
    }
  }
}