/
ModuleQuizReader.php
135 lines (108 loc) · 5.07 KB
/
ModuleQuizReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
namespace HeimrichHannot\QuizBundle\Module;
use Contao\Controller;
use Contao\Module;
use Contao\System;
use Haste\Util\Url;
use HeimrichHannot\QuizBundle\Entity\QuizSession;
use HeimrichHannot\QuizBundle\Manager\TokenManager;
use HeimrichHannot\Request\Request;
use Patchwork\Utf8;
class ModuleQuizReader extends Module
{
protected $strTemplate = 'mod_quiz';
/**
* @var int
*/
protected $count;
/**
* @var QuizSession
*/
protected $session;
/**
* @var string
*/
protected $token;
/**
* @var string
*/
protected $quiz;
public function generate()
{
if (TL_MODE === 'BE') {
$objTemplate = new \BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### '.Utf8::strtoupper($GLOBALS['TL_LANG']['FMD'][$this->type][0]).' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id='.$this->id;
return $objTemplate->parse();
}
$this->session = new QuizSession();
// Set the item from the auto_item parameter
if (!isset($_GET['items']) && \Config::get('useAutoItem') && isset($_GET['auto_item'])) {
Request::setGet('items', Request::getGet('auto_item'));
}
if ($this->singleQuiz) {
$this->quiz = $this->quizArchive;
} elseif (Request::hasGet('items')) {
$this->quiz = Request::getGet('items');
}
if ($this->quiz <= 0 && $this->quizArchive) {
$this->quiz = $this->quizArchive;
}
if (Request::hasGet('token')) {
$this->token = Request::getGet('token');
$data = System::getContainer()->get('huh.quiz.token.manager')->getDataFromJwtToken($this->token);
if (!$data->quizId) {
$token = System::getContainer()->get('huh.quiz.token.manager')->addDataToJwtToken($this->token, $this->quiz, TokenManager::QUIZ_NAME);
$url = System::getContainer()->get('contao.framework')->getAdapter(Url::class)->addQueryString('token='.$token, System::getContainer()->get('request_stack')->getCurrentRequest()->getUri());
System::getContainer()->get('contao.framework')->getAdapter(Controller::class)->redirect($url);
}
} else {
System::getContainer()->get('huh.quiz.token.manager')->addDataToJwtToken($this->token, $this->quiz, TokenManager::QUIZ_NAME);
}
return parent::generate();
}
protected function compile()
{
if ($this->quiz <= 0) {
return $this->Template->quiz = System::getContainer()->get('translator')->trans('huh.quiz.error');
}
$quizModel = \System::getContainer()->get('huh.quiz.manager')->findOneBy('id', $this->quiz);
// apply module fields to template
$this->Template->headline = $this->headline;
$this->Template->hl = $this->hl;
$this->Template->title = $quizModel->title;
$this->Template->text = $quizModel->text;
if (null === $quizModel) {
return $this->Template->quiz = System::getContainer()->get('translator')->trans('huh.quiz.error');
}
$this->count = System::getContainer()->get('huh.quiz.question.manager')->countByPid($quizModel->id);
if (Request::hasGet('answer')) {
return $this->Template->quiz = System::getContainer()->get('huh.quiz.answer.solving.manager')->parseAnswerSolving(Request::getGet('answer'), $this->quiz, $this->token);
}
if (Request::hasGet('finished')) {
$reachablePoints = System::getContainer()->get('huh.quiz.question.manager')->getMaxReachablePointsPerQuiz($this->quiz);
return $this->Template->quiz = System::getContainer()->get('huh.quiz.evaluation.manager')->parseQuizEvaluation($this->quiz, $reachablePoints, $this->token);
}
if (Request::hasGet('question')) {
$question = System::getContainer()->get('huh.quiz.question.manager')->findBy('id', Request::getGet('question'));
if (null === $question) {
return $this->Template->quiz = System::getContainer()->get('translator')->trans('huh.quiz.question.error');
}
return $this->Template->quiz = System::getContainer()->get('huh.quiz.question.manager')->prepareQuestion($question, $quizModel, $this->count, $this->imgSize);
}
$questionModel = System::getContainer()->get('huh.quiz.question.manager')->findOnePublishedByPid($quizModel->id);
if (null === $questionModel) {
return $this->Template->quiz = System::getContainer()->get('translator')->trans('huh.quiz.question.error');
}
$this->session->reset(QuizSession::USED_QUESTIONS_NAME);
$this->Template->quiz = System::getContainer()->get('huh.quiz.question.manager')->prepareQuestion($questionModel, $quizModel, $this->count, $this->imgSize);
}
}