-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinish.php
57 lines (48 loc) · 1.64 KB
/
finish.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
<?php
use Brus\Session;
use AstroQuiz\DatabaseFile;
require_once __DIR__.'/vendor/autoload.php';
Session::start();
$user = @Session::getVar('user');
if (isset($user) === FALSE) {
header('Location: index.php');
exit;
}
$loader = new Twig_Loader_Filesystem(__DIR__.'/templates');
$twig = new Twig_Environment($loader);
$accessDatabaseError = NULL;
$allQuestions = Session::getVar('allQuestions');
try {
$databaseFile = new DatabaseFile("database/database", "a");
$databaseFile->saveUserData($user, $allQuestions);
} catch (AstroQuiz\FailureDataSavingException $err) {
$accessDatabaseError = $err->getMessage();
} catch (Brus\Exception\NoFileException $err) {
$accessDatabaseError = $err->getMessage();
} catch (Brus\Exception\NoFileAccessException $err) {
$accessDatabaseError = $err->getMessage();
}
$amountQuestions = Session::getVar('amountQuestions');
$score = 0;
$numberCorrectQuestions = 0;
$maxScore = 0;
for ($i = 0; $i < $amountQuestions; $i++) {
$maxScore += $allQuestions[$i]->weight();
if ($allQuestions[$i]->evaluate() != 0) {
$score += $allQuestions[$i]->evaluate();
$numberCorrectQuestions++;
}
}
$perScore = (int)(100 * $score / $maxScore);
$perQuestions = (int)(100 * $numberCorrectQuestions / $amountQuestions);
echo $twig->render('finish.html.twig', array(
'user' => $user,
'score' => $score,
'maxScore' => $maxScore,
'perScore' => $perScore,
'numberCorrectQuestions' => $numberCorrectQuestions,
'amountQuestions' => $amountQuestions,
'perQuestions' => $perQuestions,
'accessDatabaseError' => $accessDatabaseError
));
Session::stop();