View source
<?php
namespace Drupal\quiz\Controller;
use Drupal;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Entity\Controller\EntityController;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizResult;
use Drupal\views\Views;
class QuizController extends EntityController {
function take(Quiz $quiz) {
$page = [];
$page['#cache'] = [
'max-age' => 0,
];
$result = $quiz
->access('take', NULL, TRUE);
$message = '';
if (is_subclass_of($result, AccessResultReasonInterface::class)) {
$message = $result
->getReason();
}
$success = !$result
->isForbidden();
if (!$success) {
$page['body']['#markup'] = $message;
return $page;
}
elseif ($message) {
\Drupal::messenger()
->addMessage($message);
}
if ($quiz_result = $this
->resume($quiz)) {
if (!empty($quiz_result->resume)) {
\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 {
if ($success) {
$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(),
]);
}
$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')) {
$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;
}
}
$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,
]);
}
function manageQuestions(Quiz $quiz) {
if ($quiz
->get('randomization')
->getString() < 3) {
$manage_questions = Drupal::formBuilder()
->getForm(\Drupal\quiz\Form\QuizQuestionsForm::class, $quiz);
return $manage_questions;
$question_bank = Views::getView('quiz_question_bank')
->preview();
$form['vert_tabs'] = array(
'#type' => 'x',
'#weight' => 0,
'#default_tab' => 'edit-questions',
);
$form['vert_tabs']['questions'] = array(
'#type' => 'details',
'#title' => t('Manage questions'),
'#group' => 'vert_tabs',
'questions' => $manage_questions,
);
$form['vert_tabs']['bank'] = array(
'#type' => 'details',
'#title' => t('Question bank'),
'#group' => 'vert_tabs',
'bank' => $question_bank,
);
return $manage_questions;
}
else {
$form = \Drupal::service('entity.manager')
->getFormObject('quiz', 'default')
->setEntity($quiz);
$form = \Drupal::formBuilder()
->getForm($form);
}
foreach (\Drupal\Core\Render\Element::children($form) as $key) {
if (in_array($key, array_keys($quiz
->getFieldDefinitions())) || $form[$key]['#type'] == 'details') {
if (!in_array($key, [
'quiz_terms',
'random',
'quiz',
])) {
$form[$key]['#access'] = FALSE;
}
}
}
return $form;
}
function resume($quiz) {
$user = \Drupal::currentUser();
$result_id = !empty($_SESSION['quiz'][$quiz
->id()]['result_id']) ? $_SESSION['quiz'][$quiz
->id()]['result_id'] : NULL;
if ($result_id && ($quiz_result = QuizResult::load($result_id))) {
return $quiz_result;
}
else {
if ($quiz
->get('allow_resume')
->getString() && $user
->isAuthenticated()) {
if ($quiz_result = $quiz
->getResumeableResult($user)) {
$_SESSION['quiz'][$quiz
->id()]['result_id'] = $quiz_result
->id();
$_SESSION['quiz'][$quiz
->id()]['current'] = 1;
$prev = NULL;
foreach ($quiz_result
->getLayout() as $qra) {
if ($prev) {
if ($qra
->get('answer_timestamp')
->isEmpty() && !$prev
->get('answer_timestamp')
->isEmpty()) {
$_SESSION['quiz'][$quiz
->id()]['current'] = $qra
->get('number')
->getString();
}
}
$prev = clone $qra;
}
$quiz_result->resume = TRUE;
return $quiz_result;
}
}
}
return FALSE;
}
}