|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egzaminer\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Egzaminer\Exam\CalculateScore; |
|
6
|
|
|
use Egzaminer\Exam\CompareUserAnswersWithQuestions; |
|
7
|
|
|
use Egzaminer\Model\AnswersModel; |
|
8
|
|
|
use Egzaminer\Model\ExamModel; |
|
9
|
|
|
use Egzaminer\Model\QuestionsModel; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
|
|
12
|
|
|
class ExamController extends AbstractController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Exam page with questions. |
|
16
|
|
|
* GET /exam/[i:id]. |
|
17
|
|
|
* |
|
18
|
|
|
* @param int $examID Exam ID |
|
19
|
|
|
* |
|
20
|
|
|
* @throws Exception |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public function showAction(int $examID): string |
|
25
|
|
|
{ |
|
26
|
|
|
$examInfo = (new ExamModel($this->get('dbh')))->getInfo($examID); |
|
27
|
|
|
|
|
28
|
|
|
if (empty($examInfo)) { |
|
29
|
|
|
throw new Exception('Exam not exists!'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$questions = (new QuestionsModel($this->get('dbh')))->getByExamId($examID); |
|
33
|
|
|
$answers = (new AnswersModel($this->get('dbh')))->getAnswersByQuestionsById($questions); |
|
34
|
|
|
|
|
35
|
|
|
$scoreInfo = null; |
|
36
|
|
|
|
|
37
|
|
|
// if form was send |
|
38
|
|
|
if (!empty($this->getFromRequest('post'))) { |
|
39
|
|
|
$compareAnswers = new CompareUserAnswersWithQuestions( |
|
40
|
|
|
$this->getFromRequest('post'), |
|
41
|
|
|
$questions |
|
42
|
|
|
); |
|
43
|
|
|
$questions = $compareAnswers->getCompared(); |
|
44
|
|
|
|
|
45
|
|
|
$score = new CalculateScore($examInfo, $questions); |
|
46
|
|
|
$scoreInfo = $score->getScoreInfo(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// put answers to questions |
|
50
|
|
|
foreach ($questions as $qkey => $qvalue) { |
|
51
|
|
|
foreach ($answers[$qvalue['id']] as $avalue) { |
|
52
|
|
|
$questions[$qkey]['answers'][] = $avalue; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->render('front/exam', [ |
|
57
|
|
|
'title' => $examInfo['title'], |
|
58
|
|
|
'exam' => $examInfo, |
|
59
|
|
|
'questions' => $questions, |
|
60
|
|
|
'score' => $scoreInfo, |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|