You are here

function quiz_take_page in Quiz 7.5

Same name and namespace in other branches
  1. 7.6 quiz.module \quiz_take_page()

Page to either resume a quiz or display a start quiz form.

2 string references to 'quiz_take_page'
quiz_menu in ./quiz.module
Implements hook_menu().
quiz_quiztake in plugins/page_manager/tasks/quiztake.inc
Entry point for our overridden quiz.

File

./quiz.module, line 1077
quiz.module Main file for the Quiz module.

Code

function quiz_take_page($quiz) {
  $result = quiz_access('take', $quiz);
  if (!$result['success']) {
    return array(
      'body' => array(
        '#markup' => $result['message'],
      ),
    );
  }
  elseif (!empty($result['message'])) {

    // There's a friendly message available.
    drupal_set_message($result['message']);
  }
  if ($quiz_result = quiz_take_quiz($quiz)) {
    if (!empty($quiz_result->resume)) {

      // Resuming attempt.
      drupal_set_message(t('Resuming a previous @quiz in-progress.', array(
        '@quiz' => QUIZ_NAME,
      )), 'status');
    }
    drupal_goto("node/{$quiz_result->nid}/take/" . $_SESSION['quiz'][$quiz_result->nid]['current']);
  }
  else {
    if ($result['success']) {
      $quiz_result = entity_create('quiz_result', array(
        'nid' => $quiz->nid,
        'vid' => $quiz->vid,
        'uid' => $GLOBALS['user']->uid,
        'type' => $quiz->quiz->result_type,
      ));
    }
    else {
      return array(
        'body' => array(
          '#markup' => $result['message'],
        ),
      );
    }
  }
  $build_on_last = !empty($quiz->build_on_last) && QuizResultController::findOldResult($quiz_result);
  if ($build_on_last || ($instances = field_info_instances('quiz_result', $quiz->quiz->result_type))) {

    // @kludge above, how are we going to check this form for fields? checking
    // for field instances is easy, but what about these one-offs? maybe we can
    // require add-on field items to put something in the $form so that we can
    // check it. I don't want to have the "start" button show if we don't have
    // anything to ask the user.
    return entity_ui_get_form('quiz_result', $quiz_result);
  }
  else {

    // New attempt.
    $quiz_result
      ->save();
    $_SESSION['quiz'][$quiz_result->nid]['result_id'] = $quiz_result->result_id;
    $_SESSION['quiz'][$quiz_result->nid]['current'] = 1;
    quiz_take_quiz($quiz);
    drupal_goto("node/{$quiz_result->nid}/take/" . $_SESSION['quiz'][$quiz_result->nid]['current']);
  }
}