You are here

function quiz_take_page in Quiz 7.6

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

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

1 string reference to 'quiz_take_page'
quiz_menu in ./quiz.module
Implements hook_menu().

File

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

Code

function quiz_take_page($quiz) {
  if (quiz_start_check($quiz)) {
    if (!quiz_availability($quiz)) {
      return array(
        'body' => array(
          '#markup' => t('This @quiz is not available.', array(
            '@quiz' => QUIZ_NAME,
          )),
        ),
      );
    }
  }
  else {
    return array(
      'body' => array(
        '#markup' => t('This @quiz is closed.', array(
          '@quiz' => QUIZ_NAME,
        )),
      ),
    );
  }
  if ($quiz_result = quiz_take_quiz($quiz)) {
    if (!empty($quiz_result->resume)) {

      // Resuming attempt.
      $_SESSION['quiz'][$quiz_result->nid]['result_id'] = $quiz_result->result_id;
      $_SESSION['quiz'][$quiz_result->nid]['current'] = 1;
      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 {
    $quiz_result = entity_create('quiz_result', array(
      'nid' => $quiz->nid,
      'vid' => $quiz->vid,
      'uid' => $GLOBALS['user']->uid,
    ));
  }
  $build_on_last = !empty($quiz->build_on_last) && QuizResultController::findOldResult($quiz_result);
  if ($build_on_last || ($instances = field_info_instances('quiz_result', 'quiz_result'))) {

    // @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']);
  }
}