-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassrecord.cpp
More file actions
287 lines (284 loc) · 7.84 KB
/
classrecord.cpp
File metadata and controls
287 lines (284 loc) · 7.84 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @file classrecord.h
* @brief 班级记录类
* @author howdy213
* @date 2026-3-1
* @version 1.3.0
*
* Copyright (C) 2025-2026 howdy213
*
* This file is part of QuantifyPlugin.
*
* QuantifyPlugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QuantifyPlugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "classrecord.h"
#include "QXlsx.h"
using namespace QXlsx;
///
/// \brief ClassRecord::ClassRecord
/// \param folder
/// \param engine
///
ClassRecord::ClassRecord(QString folder, RuleEngine engine)
: rules(folder, this, engine) {
this->engine = engine;
this->folder = folder;
QDir dir(folder);
}
///
/// \brief ClassRecord::~ClassRecord
///
ClassRecord::~ClassRecord() {}
bool ClassRecord::addRecord(QString student, QString reason, Record rec) {
auto it = this->students.find(student);
if (it == this->students.end())
return false;
else {
it->records.insert(reason, rec);
return true;
}
return true;
}
///
/// \brief ClassRecord::isRecordValid
/// \param file
/// \return
///
CheckResult ClassRecord::isRecordValid(const QString &file) {
return rules.isRecordValid(file);
}
///
/// \brief ClassRecord::isRuleValid
/// \param rule
/// \return
///
CheckResult ClassRecord::isRuleValid(const QString &rule) {
return rules.isRuleValid(rule);
}
///
/// \brief ClassRecord::isGroupFileValid
/// \param file
/// \return
///
CheckResult ClassRecord::isGroupFileValid(const QString &file) {
QString data = file;
QTextStream in(&data);
QString firstLine = in.readLine();
CheckResult res;
res.success = false;
if (firstLine.isNull()) {
res.info = "文件为空";
return res;
}
QStringList parts = firstLine.split(' ', Qt::SkipEmptyParts);
if (parts.size() < 2) {
res.info = "第一行格式错误,应为 '组名 中文组名' 两个字段";
return res;
} else {
if (groups.contains(parts[0]))
res.info += "组已经定义,";
}
QString groupKey = parts[0];
if (groupKey.isEmpty()) {
res.info += "组名不能为空";
return res;
}
// 组名不能包含 '-'(避免与排除语法冲突)
if (groupKey.contains('-')) {
return {false, "组名不能包含 '-' 字符"};
return res;
}
int lineNum = 2;
while (!in.atEnd()) {
QString member = in.readLine().trimmed();
if (member.isEmpty()) {
res.info += QString("第 %1 行: 存在空行").arg(lineNum);
return res;
}
if (!students.contains(member)) {
res.info += QString("第 %1 行: 成员 '%2' 不存在于学生名单中")
.arg(lineNum)
.arg(member);
return res;
}
lineNum++;
}
res.info += "组文件有效";
res.success = true;
return res;
}
///
/// \brief ClassRecord::index
/// \param abbr
/// \param name
/// \return
///
int ClassRecord::index(QString abbr, QString name) {
int index = 0;
for (auto it = this->students.begin(); it != students.end(); it++, index++) {
bool ab = it.key() == abbr || abbr.isEmpty();
bool ch = it->name_ch == name || name.isEmpty();
if (ab && ch)
return index;
}
return -1;
}
///
/// \brief ClassRecord::week
/// \return
///
unsigned int ClassRecord::week() { return weekCount; }
///
/// \brief ClassRecord::getRecord
/// \param name
/// \param rule
/// \return
///
Record ClassRecord::getRecord(QString name, QString rule) {
auto it = students.find(name);
if (it == students.end())
return RuleBase::empty;
else {
auto it2 = it.value().records.find(rule);
if (it2 == it.value().records.end())
return RuleBase::empty;
else
return it2.value();
}
}
///
/// \brief ClassRecord::getScore
/// \param name
/// \return
///
Record ClassRecord::getScore(QString name) {
auto it = students.find(name);
if (it == students.end())
return RuleBase::empty;
return it->getScore();
}
///
/// \brief ClassRecord::clear
///
void ClassRecord::clear() {
weekCount = 0;
groups.clear();
students.clear();
rules.clear();
}
///
/// \brief ClassRecord::refresh
///
void ClassRecord::refresh() {
clear();
readStudentFile();
readGroupFile();
readRuleFile();
readRecordFile();
}
///
/// \brief ClassRecord::readStudentFile
///
void ClassRecord::readStudentFile() {
QString str = QDir(folder).filePath("namelist.xlsx");
Document xlsxR(str);
if (xlsxR.load()) {
for (int row = 1;; row++) {
QString name = xlsxR.read(row, 1).toString();
if (name == "")
break;
QString name_ch = xlsxR.read(row, 2).toString();
StudentRecord sr;
sr.name_ch = name_ch;
this->students.insert(name, sr);
}
}
GroupRecord gr;
gr.name_ch = "全部";
for (auto it = this->students.begin(); it != this->students.end(); it++)
gr.members.push_back(it.key());
this->groups.insert("ALL", gr);
}
///
/// \brief ClassRecord::readGroupFile
///
void ClassRecord::readGroupFile() {
QDir dir = QDir(folder).filePath("group");
QStringList filter("*.group");
dir.setNameFilters(filter);
QList<QFileInfo> fileInfo(dir.entryInfoList(filter));
for (int i = 0; i <= fileInfo.size() - 1; i++) {
QString str = fileInfo.at(i).filePath();
QFile file(str);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
GroupRecord gr;
QString str = in.readLine();
auto list = str.split(' ');
if (list.length() <= 1)
continue;
gr.name_ch = list[1];
while (!in.atEnd()) {
gr.members.push_back(in.readLine());
}
this->groups.insert(list[0], gr);
file.close();
}
}
}
///
/// \brief ClassRecord::readRuleFile
///
void ClassRecord::readRuleFile() {
QDir dir = QDir(folder).filePath("rule");
QStringList filter("*.rule");
dir.setNameFilters(filter);
QList<QFileInfo> fileInfo(dir.entryInfoList(filter));
for (int i = 0; i <= fileInfo.size() - 1; i++) {
QString str = fileInfo.at(i).filePath();
QFile file(str);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString content;
while (!in.atEnd()) {
content += in.readLine() + '\n';
}
file.close();
rules.analyzeRuleFile(content);
}
}
}
///
/// \brief ClassRecord::readRecordFile
///
void ClassRecord::readRecordFile() {
QDir dir = QDir(folder).filePath("record");
QStringList filter("*.record");
dir.setNameFilters(filter);
QList<QFileInfo> fileInfo(dir.entryInfoList(filter));
for (int i = 0; i < fileInfo.size(); i++) {
QString str = fileInfo.at(i).filePath();
QFile file(str);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString content;
while (!in.atEnd()) {
content += in.readLine() + '\n';
}
file.close();
rules.analyzeRecordFile(content,
fileInfo.at(i).fileName().split(".").first());
}
}
}