-
Notifications
You must be signed in to change notification settings - Fork 9
/
submini_polls.cpp
366 lines (312 loc) · 14.5 KB
/
submini_polls.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <nlohmann/json.hpp>
#include "student.h"
#include "submini_polls.h"
std::map<std::string,std::map<std::string,LectureResult> > GLOBAL_students;
std::map<int,std::pair<std::string,int> > GLOBAL_poll_id_map;
std::map<std::string,std::map<int,Poll> > GLOBAL_lectures;
// Polls are organized into buckets by date
void group_polls_by_date(int poll_id, const std::string &date, std::string &lecture, int &which) {
which = poll_id;
lecture = date;
}
// Helper function to print debug information about the poll
std::ostream& operator<<(std::ostream& ostr, const Poll &p) {
if (p.getCorrectOptions().size() == 0) {
std::cerr << "ERROR WITH " << p.getName() << std::endl;
}
assert (p.getCorrectOptions().size() > 0);
ostr << " "
<< std::left
<< std::setw(40) << p.getName() << " "
<< std::setw(15) << p.getDate() << " "
<< p.getCorrectOptions().size() << " " << p.getWrongOptions().size() << " "
<< std::right << std::setw(3) << p.getCorrectClicks() << " " << std::setw(3) << p.getWrongClicks();
return ostr;
}
bool earnedlatetoday(int prev, int today) {
int earn_late[5] = { 15, 45, 75, 105, 135 };
for (int i = 0; i < 5; i++) {
if (prev < earn_late[i] && today >= earn_late[i]) return true;
}
return false;
}
std::string convert_date(const std::string &date) {
std::stringstream ss(date);
int x,y,z;
char ch,ch2;
ss >> x >> ch >> y >> ch2 >> z;
int year, month, day;
if (ch == '-' && ch2 == '-') {
// old format
year = x;
month = y;
day = z;
} else {
assert (ch == '/' && ch2 == '/');
// new format
month = x;
day = y;
year = z;
}
assert (year > 2000);
assert (month >= 1 && month <= 12);
assert (day >= 1 && day <= 31);
std::stringstream out;
out << std::setw(2) << std::setfill('0') << month << "-"
<< std::setw(2) << std::setfill('0') << day << "-" << year;
return out.str();
}
std::string getToday() {
static std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
time_t tt = std::chrono::system_clock::to_time_t(now);
tm local_tm = *localtime(&tt);
std::stringstream ss;
ss << local_tm.tm_year + 1900 << "-"
<< std::setw(2) << std::setfill('0') << local_tm.tm_mon + 1 << "-"
<< std::setw(2) << std::setfill('0') << local_tm.tm_mday;
std::string today_string = ss.str();
return today_string;
}
// =======================================================================================
// =======================================================================================
// Load all poll data
void LoadPolls(const std::vector<Student*> &students) {
// ----------------------------------------------------------------------------
// initialize a structure with the names of all students in the course
std::string path = "../raw_data";
for (unsigned int i = 0; i < students.size(); i++) {
std::string tmp = students[i]->getUserName();
std::string section = students[i]->getSection();
if (section == "null") continue;
GLOBAL_students.insert(std::make_pair(tmp,std::map<std::string,LectureResult>()));
}
// ----------------------------------------------------------------------------
// prepare a structure with the core poll information (name & correct responses)
std::ifstream data_file("raw_data/polls/poll_questions.json");
if (!data_file.good()) return;
nlohmann::json j_data = nlohmann::json::parse(data_file);
std::string today_string = getToday();
for (nlohmann::json::iterator itr = j_data.begin(); itr != j_data.end(); itr++) {
int poll_id = itr->find("id")->get<int>();
assert (poll_id >= 0);
std::string poll_name = itr->find("name")->get<std::string>();
std::string release_date = itr->find("release_date")->get<std::string>();
std::string question_type = "single-response-multiple-correct";
nlohmann::json::iterator itr2 = itr->find("question_type");
if (itr2 != itr->end()) question_type = itr2->get<std::string>();
std::string status = "ended";
// original format: status stored as string, "closed", "open", or "ended"
itr2 = itr->find("status");
if (itr2 != itr->end()) status = itr2->get<std::string>();
// new format (April 2024): polls have an end time
std::string end_time = "";
itr2 = itr->find("end_time");
if (itr2 != itr->end()) {
if (itr2->is_null()) {
// if end_time is NULL that means the poll was never opened
status = "closed";
} else {
end_time = itr->find("end_time")->get<std::string>();
assert (end_time.size() == 10);
assert (today_string.size() == 10);
if (end_time < today_string) {
// if end_time is in the past, then the poll was used/released to the class and is now ended
status = "ended";
} else {
// otherwise, the poll might either be currently open or will be open in the future
// NOTE: this may need revision in a future PR, if timestamps are added to the end_time,
// to make sure we handle polls from today correctly
status = "closed";
}
}
}
assert (status == "ended" || status == "closed" || status == "open");
assert (release_date.size() == 10);
assert (today_string.size() == 10);
if (release_date < today_string && status != "ended") {
// this should only happen if a poll was scheduled for a date in the past, but it wasn't used on that date.
// NOTE: this may need revision in a future PR, if timestamps are added to the end_time or
// if logic / handling of unreleased past polls changes.
std::cout << "Date inconsistency - this poll wasn't used/released." << std::endl;
std::cout << " today = " << today_string << " poll release date = " << release_date << std::endl;
std::cout << " setting release_date to infinity" << std::endl;
release_date = "9999-01-01";
}
std::string lecture;
int which;
group_polls_by_date(poll_id,release_date,lecture,which);
GLOBAL_lectures[lecture].insert(std::make_pair(which,Poll(which,poll_name,release_date,question_type)));
GLOBAL_poll_id_map[poll_id] = std::make_pair(lecture,which);
const typename nlohmann::json &tmp = (*itr)["correct_responses"];
for (nlohmann::json::const_iterator itr2 = tmp.begin(); itr2 != tmp.end(); itr2++) {
int option_id = itr2->get<int>();
std::map<int,Poll>::iterator itr3 = GLOBAL_lectures[lecture].find(which);
assert (itr3 != GLOBAL_lectures[lecture].end());
itr3->second.addCorrectOption(option_id);
}
}
// ----------------------------------------------------------------------------
// load and store the responses from each student for each poll
std::ifstream responses_file("raw_data/polls/poll_responses.json");
assert (responses_file.good());
nlohmann::json j_responses = nlohmann::json::parse(responses_file);
for (nlohmann::json::iterator itr = j_responses.begin(); itr != j_responses.end(); itr++) {
int poll_id = itr->find("id")->get<int>();
assert (poll_id >= 0);
const typename nlohmann::json &tmp = (*itr)["responses"];
std::pair<std::string,int> w = GLOBAL_poll_id_map[poll_id];
for (nlohmann::json::const_iterator itr2 = tmp.begin(); itr2 != tmp.end(); itr2++) {
std::string username = itr2.key();
// student response will now always be an array
std::vector<int> student_responses;
if ((*itr2).is_array()) {
student_responses = itr2->get<std::vector<int> >();
} else {
// backwards compatible -- previously only one response was allowed
int response = itr2->get<int>();
student_responses.push_back(response);
}
std::map<int,Poll>::iterator itr3 = GLOBAL_lectures[w.first].find(w.second);
assert (itr3 != GLOBAL_lectures[w.first].end());
const std::vector<int> &c_options = itr3->second.getCorrectOptions();
const std::vector<int> &w_options = itr3->second.getWrongOptions();
const std::string &question_type = itr3->second.getType();
// count the number of correct & incorrect choices
int incorrect_choices = 0;
int correct_choices = 0;
for (unsigned int i = 0; i < student_responses.size(); i++) {
int r = student_responses[i];
std::vector<int>::const_iterator c_itr = std::find(c_options.begin(),c_options.end(),r);
if (c_itr != itr3->second.getCorrectOptions().end()) {
correct_choices++;
} else {
//std::vector<int>::const_iterator w_itr = std::find(w_options.begin(),w_options.end(),r);
//assert (w_itr != itr3->second.getWrongOptions().end());
incorrect_choices++;
}
}
// "grade" the response to this question, depending on the question type
bool full_credit = false;
if (question_type == "single-response-single-correct") {
assert (c_options.size() == 1);
assert (correct_choices+incorrect_choices <= 1);
full_credit = (correct_choices == 1);
} else if (question_type == "single-response-multiple-correct") {
assert (c_options.size() >= 1);
assert (correct_choices+incorrect_choices <= 1);
full_credit = (correct_choices == 1);
} else if (question_type == "single-response-survey") {
assert (c_options.size() >= 1);
assert (w_options.size() == 0);
assert (correct_choices <= 1);
//assert (incorrect_choices == 0);
full_credit = (correct_choices == 1);
} else if (question_type == "multiple-response-exact") {
assert (c_options.size() >= 1);
full_credit = (incorrect_choices == 0 && correct_choices == int(c_options.size()));
} else if (question_type == "multiple-response-flexible") {
assert (c_options.size() >= 1);
full_credit = (incorrect_choices == 0 && correct_choices >= 1);
} else if (question_type == "multiple-response-survey") {
assert (c_options.size() >= 1);
assert (w_options.size() == 0);
//assert (incorrect_choices == 0);
full_credit = (correct_choices >= 1);
} else {
std::cout << "OOPS: unknown question type '" << question_type << "'" << std::endl;
assert (0);
}
if (full_credit) {
itr3->second.incrCorrectClicks();
GLOBAL_students[username][w.first].correct++;
} else {
itr3->second.incrWrongClicks();
GLOBAL_students[username][w.first].wrong++;
}
}
}
//Update the correct and incorrect counts for each student
for (unsigned int i = 0; i < students.size(); i++) {
std::string tmp = students[i]->getUserName();
std::string section = students[i]->getSection();
if (section == "null") continue;
for(std::map<std::string, LectureResult>::const_iterator it = GLOBAL_students[tmp].begin(); it != GLOBAL_students[tmp].end(); it++){
students[i]->incrementPollsCorrect((it->second).correct);
students[i]->incrementPollsIncorrect((it->second).wrong);
}
}
}
// =======================================================================================
// =======================================================================================
// Output an individual report for each student (which will be pasted
// into their individual rainbow grades report).
//
void SavePollReports(const std::vector<Student*> &students) {
std::string today_string = getToday();
std::cout << "TODAY " << today_string << std::endl;
std::ofstream late_days_ostr("late_days.csv");
if (GLOBAL_lectures.size() == 0) return;
system ("mkdir -p student_poll_reports");
for (std::map<std::string,std::map<std::string,LectureResult> >::iterator s = GLOBAL_students.begin();
s != GLOBAL_students.end(); s++) {
std::string username = s->first;
std::ofstream student_ostr("student_poll_reports/"+username+".html");
assert (student_ostr.good());
student_ostr << "<h3> Lecture Participation Polls for: " << username << "</h3>" << std::endl;
student_ostr << "<table cellpadding=5 style=\"border:1px solid #aaaaaa; background-color:#ffffff;\">" << std::endl;
student_ostr << "<tr><td align=center>Lecture</td><td># correct</td><td># incorrect</td><td># no response</td><td>Total Points</td><td> </td></tr>" << std::endl;
int late_days = 1;
float total = 0;
int prev_total = 0;
int which_lecture = 0;
for (std::map<std::string,std::map<int,Poll> >::iterator it = GLOBAL_lectures.begin(); it != GLOBAL_lectures.end(); it++) {
which_lecture++;
int num = it->second.size();
std::string lect = it->first;
if (lect > today_string) continue;
int correct = s->second[lect].correct;
int wrong = s->second[lect].wrong;
total += correct;
total += 0.5 * wrong;
Poll p = it->second.begin()->second;
std::string foo = convert_date(p.getDate());
std::string THING = "";
if (earnedlatetoday(prev_total,total)) {
late_days++;
late_days_ostr << username << "," << foo << "," << late_days << "\r" << std::endl;
THING = "EARNED LATE DAY #" + std::to_string(late_days) + " on " + foo;
}
Student *student_obj = NULL;
for (unsigned int i = 0; i < students.size(); i++) {
if (students[i]->getUserName() == username)
student_obj = students[i];
}
assert (student_obj != NULL);
if (student_obj->get_bonus_late_day(which_lecture)) {
late_days++;
late_days_ostr << username << "," << foo << "," << late_days << "\r" << std::endl;
THING += " BONUS LATE DAY #" + std::to_string(late_days) + " on " + foo;
}
prev_total = total;
student_ostr << "<tr><td align=center>" << lect << "</td><td align=center>"
<< correct << "</td><td align=center>"
<< wrong << "</td><td align=center>"
<< num-correct-wrong << "</td><td align=center>"
<< total << "</td><td>"
<< THING << "</td></tr>" << std::endl;
}
student_ostr << "</table>" << std::endl;
student_ostr << "<br><p>IMPORTANT: Late days must be earned before the homework due date.<br>You may not use a late day earned on Friday on the homework due the night before.<br>You earn your second late day after the first 15 points, and an additional day for<br>every 30 points after that (45, 75, 105, 135)</p></table>" << std::endl;
}
}
// =======================================================================================