You are here

function QuizController::take in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/Controller/QuizController.php \Drupal\quiz\Controller\QuizController::take()
  2. 6.x src/Controller/QuizController.php \Drupal\quiz\Controller\QuizController::take()

Take the quiz.

Return value

type

1 string reference to 'QuizController::take'
quiz.routing.yml in ./quiz.routing.yml
quiz.routing.yml

File

src/Controller/QuizController.php, line 18

Class

QuizController

Namespace

Drupal\quiz\Controller

Code

function take(Quiz $quiz) {
  $page = [];
  $page['#cache'] = [
    'max-age' => 0,
  ];

  /* @var $result AccessResultReasonInterface */
  $result = $quiz
    ->access('take', NULL, TRUE);
  $message = '';
  if (is_subclass_of($result, AccessResultReasonInterface::class)) {
    $message = $result
      ->getReason();
  }
  $success = !$result
    ->isForbidden();
  if (!$success) {

    // Not allowed.
    $page['body']['#markup'] = $message;
    return $page;
  }
  elseif ($message) {

    // Allowed, but we have a message.
    \Drupal::messenger()
      ->addMessage($message);
  }
  if ($quiz_result = $this
    ->resume($quiz)) {

    // Resuming attempt.
    if (!empty($quiz_result->resume)) {

      // Show a message if this was reloaded from the database and not just
      // from the session.
      \Drupal::messenger()
        ->addStatus(t('Resuming a previous @quiz in-progress.', array(
        '@quiz' => _quiz_get_quiz_name(),
      )));
    }
    return $this
      ->redirect('quiz.question.take', [
      'quiz' => $quiz
        ->id(),
      'question_number' => $_SESSION['quiz'][$quiz
        ->id()]['current'],
    ]);
  }
  else {

    // Create new result.
    if ($success) {

      // Test a build of questions.
      $questions = $quiz
        ->buildLayout();
      if (empty($questions)) {
        \Drupal::messenger()
          ->addError(t('Not enough questions were found. Please add more questions before trying to take this @quiz.', array(
          '@quiz' => _quiz_get_quiz_name(),
        )));
        return $this
          ->redirect('entity.quiz.canonical', [
          'quiz' => $quiz
            ->id(),
        ]);
      }

      // Creat a new Quiz result.
      $quiz_result = QuizResult::create(array(
        'qid' => $quiz
          ->id(),
        'vid' => $quiz
          ->getRevisionId(),
        'uid' => \Drupal::currentUser()
          ->id(),
        'type' => $quiz
          ->get('result_type')
          ->getString(),
      ));
      $build_on_last = $quiz
        ->get('build_on_last')
        ->getString() != 'fresh' && $quiz_result
        ->findOldResult();
      $instances = Drupal::service('entity_field.manager')
        ->getFieldDefinitions('quiz_result', $quiz
        ->get('result_type')
        ->getString());
      foreach ($instances as $field_name => $field) {
        if ($build_on_last || is_a($field, Drupal\field\Entity\FieldConfig::class) && $field
          ->getThirdPartySetting('quiz', 'show_field')) {

          // We found a field to be filled out.
          $redirect_url = \Drupal\Core\Url::fromRoute('entity.quiz.take', [
            'quiz' => $quiz_result
              ->getQuiz()
              ->id(),
          ]);
          $form = \Drupal::service('entity.form_builder')
            ->getForm($quiz_result, 'default', [
            'redirect' => $redirect_url,
          ]);
          return $form;
        }
      }
    }
    else {
      $page['body']['#markup'] = $result['message'];
      return $page;
    }
  }

  // New attempt.
  $quiz_result
    ->save();
  $_SESSION['quiz'][$quiz
    ->id()]['result_id'] = $quiz_result
    ->id();
  $_SESSION['quiz'][$quiz
    ->id()]['current'] = 1;
  return $this
    ->redirect('quiz.question.take', [
    'quiz' => $quiz
      ->id(),
    'question_number' => 1,
  ]);
}