-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestingwindow.cpp
More file actions
146 lines (120 loc) · 4.73 KB
/
testingwindow.cpp
File metadata and controls
146 lines (120 loc) · 4.73 KB
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
136
137
138
139
140
141
142
143
144
145
146
#include "testingwindow.h"
#include "ui_testingwindow.h"
TestingWindow::TestingWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TestingWindow)
{
ui->setupUi(this);
DisplayFirstQuestion();
}
TestingWindow::~TestingWindow()
{
delete ui;
}
void TestingWindow::DisplayFirstQuestion()
{
// - Получаем путь к БД из менеджера файлов (FileManager.h)
FileManager FManager;
QString DatabaseFile_PATH = FManager.DatabaseFile_PATH;
// - Загружаме БД по полученному пути
QSqlDatabase QuestionsDB = QSqlDatabase::addDatabase("QSQLITE");
QuestionsDB.setDatabaseName(DatabaseFile_PATH);
// - Если БД не существует (например пользователь удалил), то вызвать окно с ошибкой
if( !QuestionsDB.open( ) )
return;
query = QSqlQuery(QuestionsDB);
query.prepare("select * from questions");
query.exec();
query.first();
ui -> QuestionTextBox -> setText(query.value("TEXT").toString());
ui -> option1TextBox -> setText(query.value("OPTION1").toString());
ui -> option2TextBox -> setText(query.value("OPTION2").toString());
ui -> option3TextBox -> setText(query.value("OPTION3").toString());
ui -> option4TextBox -> setText(query.value("OPTION4").toString());
ui -> option5TextBox -> setText(query.value("OPTION5").toString());
}
void TestingWindow::on_nextQuestion_clicked()
{
DisplayNextQuestion();
}
void TestingWindow::DisplayNextQuestion()
{
if(query.next())
{
// - Если в БД далее есть записи то считываем & выводим следующую
ui -> QuestionTextBox -> setText(query.value("TEXT").toString());
ui -> option1TextBox -> setText(query.value("OPTION1").toString());
ui -> option2TextBox -> setText(query.value("OPTION2").toString());
ui -> option3TextBox -> setText(query.value("OPTION3").toString());
ui -> option4TextBox -> setText(query.value("OPTION4").toString());
ui -> option5TextBox -> setText(query.value("OPTION5").toString());
// - Обновляем номер вопроса
questionNumber++;
// - Очищаем интерфейс
ClearUI();
}
else
{
// - Если записи кончились, то выводим окно с результатами
int score = CalculateUserScore();
// int mark = CalculateUserMark(score);
QMessageBox::information(0, "Конец тестирования", "Ваш результат: " + QString::number(score) + " из " + QString::number(userAnswers.size()) + "\nВаша оценка: " + QString::number(CalculateUserMark(score)));
}
}
void TestingWindow::on_option1Button_clicked() { registerAnswer(1); }
void TestingWindow::on_option2Button_clicked() { registerAnswer(2); }
void TestingWindow::on_option3Button_clicked() { registerAnswer(3); }
void TestingWindow::on_option4Button_clicked() { registerAnswer(4); }
void TestingWindow::on_option5Button_clicked() { registerAnswer(5); }
void TestingWindow::registerAnswer(int index)
{
if(index == query.value("RIGHTANSWER"))
{
// - Если ответ совпадает с верным (из БД) то заносим в вектор 1
userAnswers.push_back(1);
}
else
{
// - Иначе 0
userAnswers.push_back(0);
}
}
int TestingWindow::CalculateUserScore()
{
int score = 0;
for(size_t i = 0; i < userAnswers.size(); i++)
{
// - Если элемент == 1 -> ответ верный, считаем как 1 балл
if(userAnswers[i])
score++;
}
return score;
}
int TestingWindow::CalculateUserMark(int score)
{
// - Оценить пользователя
int mark;
if(score < 2) { mark = 2; }
else if(score < 5) { mark = 3; }
else if(score < 8) { mark = 4; }
else { mark = 5; }
return mark;
}
void TestingWindow::ClearUI()
{
ui -> option1Button -> setAutoExclusive(false);
ui -> option1Button -> setChecked(false);
ui -> option1Button -> setAutoExclusive(true);
ui -> option2Button -> setAutoExclusive(false);
ui -> option2Button -> setChecked(false);
ui -> option2Button -> setAutoExclusive(true);
ui -> option3Button -> setAutoExclusive(false);
ui -> option3Button -> setChecked(false);
ui -> option3Button -> setAutoExclusive(true);
ui -> option4Button -> setAutoExclusive(false);
ui -> option4Button -> setChecked(false);
ui -> option4Button -> setAutoExclusive(true);
ui -> option5Button -> setAutoExclusive(false);
ui -> option5Button -> setChecked(false);
ui -> option5Button -> setAutoExclusive(true);
}