-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.php
87 lines (75 loc) · 2.82 KB
/
data.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
<?php
use Brus\Session;
use AstroQuiz\ConfigureFile;
use AstroQuiz\QuestionFile;
use AstroQuiz\DatabaseFile;
use AstroQuiz\Question;
require_once __DIR__.'/vendor/autoload.php';
$loader = new Twig_Loader_Filesystem(__DIR__.'/templates');
$twig = new Twig_Environment($loader);
$loadDataError = NULL;
$displayLoginForm = NULL;
$typeAnotherPassword = NULL;
$formPassword = $_POST['admin_password'];
try {
$configureFile = new ConfigureFile("astroquiz.cfg");
$password = $configureFile->getPassword();
$fileWithQuestions = $configureFile->getFilenameWithQuestions();
$questionFile = new QuestionFile("files/" . $fileWithQuestions);
$databaseFile = new DatabaseFile("database/database");
} catch (AstroQuiz\Exception\WrongConfiguration $err) {
$loadDataError = $err->getMessage();
} catch (Brus\Exception\NoFileException $err) {
$loadDataError = $err->getMessage();
} catch (Brus\Exception\NoFileAccessException $err) {
$loadDataError = $err->getMessage();
}
if ($loadDataError == NULL && $databaseFile->isEmpty() === TRUE) {
$loadDataError = "Database is empty. Nothing to study";
}
if (isset($password) && ($password != $formPassword)) {
$loadDataError = NULL;
$displayLoginForm = TRUE;
$typeAnotherPassword = TRUE;
echo $twig->render('admin.html.twig', array(
'loadDataError' => $loadDataError,
'displayLoginForm' => $displayLoginForm,
'typeAnotherPassword' => $typeAnotherPassword
));
exit;
}
if ($loadDataError == NULL) {
if ($questionFile->properSize() === FALSE) {
$loadDataError = $questionFile->error();
} else {
$database = $databaseFile->getDatabaseSortedByResults("DESC");
$amountUsers = $databaseFile->amountUsers();
$amountQuestions = $questionFile->amountQuestions();
$allQuestions = array();
$percentCorrectAnswers = array();
for ($i = 0; $i < $amountQuestions; $i++) {
$allQuestions[$i] = new Question($questionFile->readQuestion());
$amountCorrectQuestions = 0;
foreach ($database as $row) {
if ($row[$i + 1] != 0) {
$amountCorrectQuestions += 1;
}
}
$percentCorrectAnswers[$i] = 100*$amountCorrectQuestions/$amountUsers;
}
echo $twig->render('admin.html.twig', array(
'loadDataError' => $loadDataError,
'displayLoginForm' => $displayLoginForm,
'typeAnotherPassword' => $typeAnotherPassword,
'allQuestions' => $allQuestions,
'database' => $database,
'percentCorrectAnswers' => $percentCorrectAnswers
));
exit;
}
}
echo $twig->render('admin.html.twig', array(
'loadDataError' => $loadDataError,
'displayLoginForm' => $displayLoginForm,
'typeAnotherPassword' => $typeAnotherPassword,
));