-
Notifications
You must be signed in to change notification settings - Fork 0
/
testswriter.cpp
172 lines (136 loc) · 4.38 KB
/
testswriter.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <QDebug>
#include <iostream>
#include <vector>
#include <string>
#include <nlohmann/json.hpp>
#include "testswriter.h"
#include "exceptions.h"
using json = nlohmann::json;
TestsWriter::Question::Question()
{
_isReadable = false;
}
TestsWriter::Question::Question(const QString &question,
const QVector<QString> &filenames,
const AnswerType &answerType,
const QVector<QString> &answers,
const QVector<size_t> &correctAnswerIndicies)
{
_question = question;
_filenames = filenames;
_answerType = answerType;
_answers = answers;
_correctAnswerIndicies = correctAnswerIndicies;
_isReadable = false;
}
QString TestsWriter::Question::getQuestionText() const
{
return _question;
}
QVector<QString> TestsWriter::Question::getFilenames() const
{
return _filenames;
}
TestsWriter::Question::AnswerType TestsWriter::Question::getAnswerType() const
{
return _answerType;
}
QVector<QString> TestsWriter::Question::getAnswers() const
{
return _answers;
}
QVector<size_t> TestsWriter::Question::getCorrectAnswerIndicies() const
{
return _correctAnswerIndicies;
}
void TestsWriter::Question::setReadable(bool flag)
{
_isReadable = flag;
}
bool TestsWriter::Question::readable() const
{
return _isReadable;
}
void TestsWriter::setQuestionsCount(size_t count)
{
_questions.resize(count);
_questionsCount = count;
}
bool TestsWriter::addQuestion(const Question &question, size_t index)
{
if (index >= _questionsCount)
return false;
_questions[index] = question;
_questions[index].setReadable(true);
return true;
}
bool TestsWriter::getQuestion(Question &question, size_t index) const
{
if (index >= _questionsCount || !_questions[index].readable())
return false;
question = _questions[index];
return true;
}
QVector<size_t> TestsWriter::getIncompleteQuestionIndicies() const
{
QVector<size_t> incompleteQuestionIndicies;
for (size_t i = 0; i < _questionsCount; ++i)
if (!_questions[i].readable() || \
_questions[i].getQuestionText() == "" || \
_questions[i].getAnswerType() == TestsWriter::Question::NONE || \
_questions[i].getAnswers().isEmpty() || \
_questions[i].getCorrectAnswerIndicies().isEmpty())
{
incompleteQuestionIndicies.push_back(i);
}
return incompleteQuestionIndicies;
}
void TestsWriter::resetTest()
{
_questionsCount = 0;
_questions.clear();
}
void TestsWriter::saveTest(std::ofstream &file)
{
json test;
test["count"] = _questionsCount;
json questionsArray = json::array();
for (size_t i = 0; i < _questionsCount; ++i)
{
json question;
question["text"] = _questions[i].getQuestionText().toStdString();
std::vector<std::string> filenames;
for (auto &filename : _questions[i].getFilenames())
filenames.push_back(filename.toStdString());
question["question type"] = filenames.size() ? "with images" : "text only";
question["images"] = filenames;
Question::AnswerType answerType = _questions[i].getAnswerType();
switch (answerType)
{
case Question::AnswerType::SINGLE_CHOICE:
question["answer type"] = "single choice";
break;
case Question::AnswerType::MULTIPLE_CHOICE:
question["answer type"] = "multiple choice";
break;
case Question::AnswerType::STRING_ANSWER:
question["answer type"] = "text answer";
break;
default:
throw SavingException();
break;
}
std::vector<std::string> answers;
for (auto &answer : _questions[i].getAnswers())
answers.push_back(answer.toStdString());
question["answers"] = answers;
std::vector<size_t> correctAnswerIndicies;
for (auto &answerIndex : _questions[i].getCorrectAnswerIndicies())
correctAnswerIndicies.push_back(answerIndex);
question["correct answers"] = correctAnswerIndicies;
questionsArray.push_back(question);
}
test["questions"] = questionsArray;
file << test.dump(4) << std::endl;
std::cout << "Test successfully saved." << std::endl;
}