-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquiz.c
355 lines (343 loc) · 14.4 KB
/
quiz.c
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
#include "common.h"
#include "quiz.h"
/*int main() {
init(0);
takeQuiz(0);
return 0;
}*/
void init(int index) {
char a[4], b[2];
currentuser.ID=0;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < max_alternative_q; ++j) {
sprintf(a, "%d+", i);
sprintf(b, "%d", j);
strcpy(question[index][i][j].statement, strcat(a,b));
response[currentuser.ID][index][i][j].status=78;
question[index][i][j].marks=2;
}
}
strcpy(quizlist.quiz[index].name, "Test Quiz-1");
quizlist.quiz[index].no_of_questions=10;
quizlist.quiz[index].no_of_max_attempts=1;
}
void takeQuiz(int index) {
clearscr();
//If reattempt is possible
if (quizes_attempted[currentuser.ID][index].no_attempts<quizlist.quiz[index].no_of_max_attempts) {
//Set next attempt number
int attempt=quizes_attempted[currentuser.ID][index].no_attempts;
//Make quiz
makeQuiz(index, attempt);
//Go to quiz matrix
// record start
record_time(0);
qMatrix(index, attempt);
//Update attempt data
quizlist.quiz[index].no_of_students_attempted++;
quizes_attempted[currentuser.ID][index].no_attempts++;
//Autograde this attempt
autoGradeAttempt(index, attempt);
clearscr();
}
//If reattempt not possible
else {
printf("No more reattempts are allowed!\n\n");
}
}
void qMatrix(int index, int attempt) {
printf("-------------------------------------------\n %s ", quizlist.quiz[index].name);
print_time_left(index);
printf("-------------------------------------------\n Question Matrix \n\n");
char color[8];
for (int i = 0; i < quizlist.quiz[index].no_of_questions; ++i) {
char status=response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status;
if (status=='U') {
strcpy(color,yellow);
}
else if (status=='S') {
strcpy(color,cyan);
}
else if (status=='A') {
strcpy(color,green);
}
if (i%5==0) {
printf("\n");
}
if (i+1<10) {
printf(" %d) %s[%c]%s (%d) ", i+1, color, status, normal, question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].marks);
}
else {
printf("%d) %s[%c]%s (%d) ", i+1, color, status, normal, question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].marks);
}
}
printf("\n\nKey:\n%s[U]%s Unattempted (p/q) p Marks Obtained Out of q\n", yellow, normal);
printf("%s[S]%s Seen\n", cyan, normal);
printf("%s[A]%s Attempted\n", green, normal);
printf("\n\nEnter question number you wish to view or '0' to submit quiz,\n");
int num;
scanf("%d", &num);
clearBuf();
if (num==0) {
printf("\nAre you sure you want to submit the quiz?\nAnswer 'y' for yes and 'n' for no,\n");
char com;
scanf("%c", &com);
clearBuf();
if (com==110) { //If 'n'
clearscr();
qMatrix(index, attempt);
}
else if (com==121) { //If 'y'
//record submit quiz time
quizes_attempted[currentuser.ID][index].attempt[attempt].time_taken =(current-start);
clearscr();
printf("-------------------------------------------\n %s \n", quizlist.quiz[index].name);
printf("-------------------------------------------\n");
printf("Quiz submitted!\nHit ENTER to proceed to main menu,\n");
wait_for_enter();
}
else { //If not 'n' or y'
clearscr();
printf("%sInvalid Input!%s\n", red, normal);
qMatrix(index, attempt);
}
}
else if (num>0&&num<=quizlist.quiz[index].no_of_questions) {
clearscr();
askQuestion(num-1, index, attempt);
}
else {
clearscr();
printf("%sInvalid Input!%s\n", red, normal);
qMatrix(index, attempt);
}
}
void askQuestion(int i, int index, int attempt) {
record_time(1);
if (!autosubmit(index)) {
printf("-------------------------------------------\n %s ", quizlist.quiz[index].name);
print_time_left(index);
printf("-------------------------------------------\n Question %d (%d Marks)\n\n", i+1, question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].marks);
printf("Question:\n");
printf("%s\n\n", question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].statement);
//Has the question been answered already?
if (response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status=='A') {
printf("Your current answer:\n%s\n\n", response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].answer);
printf("Change the answer (enter # to exit without changing answer):\n");
}
else {
printf("Enter the answer (enter # to exit without answering):\n");
//Set status to seen
response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status='S';
}
char answer[max_answer_length];
//Capture answer
smart_fgets(answer, max_answer_length, stdin);
// submit time of question
record_time(1);
// Check for time discrepancies
if (cheating()) {
quizes_attempted[currentuser.ID][index].attempt[attempt].time_taken =(current-start);
clearscr();
printf("-------------------------------------------\n %s \n", quizlist.quiz[index].name);
printf("-------------------------------------------\n");
printf("Cheating has been detected!!\nThis answer could not be saved!\n");
printf("Quiz submitted!\nHit ENTER to proceed to main menu,\n");
wait_for_enter();
}
else {
//time checker
if (autosubmit(index)) {
// Timeout!
//record submit quiz time
quizes_attempted[currentuser.ID][index].attempt[attempt].time_taken =(current-start);
clearscr();
printf("-------------------------------------------\n %s \n", quizlist.quiz[index].name);
printf("-------------------------------------------\n");
printf("Time out!!\nThis answer could not be saved!\n");
printf("Quiz submitted!\nHit ENTER to proceed to main menu,\n");
wait_for_enter();
}
else {
//If # is not the answer
if (answer[0]!='#') {
//Update response
strcpy(response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].answer, answer);
//Mark as attempted
response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status='A';
appdata_save(0);
}
clearscr();
quizNav(i, index, attempt);
}
}
}
else {
// Timeout!
//record submit quiz time
quizes_attempted[currentuser.ID][index].attempt[attempt].time_taken =(current-start);
clearscr();
printf("-------------------------------------------\n %s \n", quizlist.quiz[index].name);
printf("-------------------------------------------\n");
printf("Time out!!\n");
printf("Quiz submitted!\nHit ENTER to proceed to main menu,\n");
wait_for_enter();
}
}
void quizNav(int i, int index, int attempt) {
printf("-------------------------------------------\n %s ", quizlist.quiz[index].name);
print_time_left(index);
printf("-------------------------------------------\n");
char com;
printf("Would you like to proceed to next question or return to question matrix?\n\nEnter:\n'n' for next question\n'm' for question matrix\n");
scanf("%c", &com);
clearBuf();
if (com==110) { //'n'
clearscr();
if (i+1<quizlist.quiz[index].no_of_questions) { //next question index is less than equal last question index
askQuestion(++i, index, attempt);
}
else { //If not go to first question
askQuestion(0, index, attempt);
}
}
else if (com==109) { //'m'
clearscr();
qMatrix(index, attempt);
}
else { //neither 'n' nor 'm'
clearscr();
printf("%sInvalid Option!%s\n", red, normal);
quizNav(i, index, attempt);
}
}
int randNum(int lower, int upper) {
return ((rand()%(upper - lower + 1)) + lower);
}
void makeQuiz(int index, int attempt) {
int rand;
if (quizes_attempted[currentuser.ID][index].no_attempts<quizlist.quiz[index].no_of_max_attempts) {
int q_status[quizlist.quiz[index].no_of_questions];
for (int i = 0; i < quizlist.quiz[index].no_of_questions; ++i) {
quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]=-1;
quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]=-1;
q_status[i]=0;
}
//Randomise Question Order
for (int i = 0; i < quizlist.quiz[index].no_of_questions; ++i) {
//Check random questions until unassigned one is found
while(quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]==-1) {
rand=randNum(0,quizlist.quiz[index].no_of_questions-1);
//If not assigned
if (q_status[rand]==0) {
//Assign
quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]=rand;
q_status[rand]=1;
}
}
}
//Go through randomly chosen question order
for (int i = 0; i < quizlist.quiz[index].no_of_questions; ++i) {
//Run until the alternative is set
while(quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]==-1) {
//Generate random alternative number
rand=randNum(0,max_alternative_q-1);
//See if this alternative has 'N' status
if (response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][rand].status=='N') {
//Set the alternative
quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]=rand;
//Set status of this alternative to 'U'
response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][rand].status='U';
}
}
}
}
}
void autoGradeAttempt(int index, int attempt) {
for (int i = 0; i < quizlist.quiz[index].no_of_questions; ++i) { //Grade individual questions
//If status is attempted
if (response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status=='A') {
//Check if correct
if (!strcmp(response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].answer, question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].solution)) {
//Give full marks
quizes_attempted[currentuser.ID][index].attempt[attempt].marks[i] = question[index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].marks;
//Update result for this attempt
response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status='C';
quizes_attempted[currentuser.ID][index].attempt[attempt].result.attempted++;
quizes_attempted[currentuser.ID][index].attempt[attempt].result.correct++;
quizes_attempted[currentuser.ID][index].attempt[attempt].result.score += quizes_attempted[currentuser.ID][index].attempt[attempt].marks[i];
}
//Otherwise it must be wrong
else {
//Give zero marks
quizes_attempted[currentuser.ID][index].attempt[attempt].marks[i] = 0;
//Update result for this attempt
response[currentuser.ID][index][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][0]][quizes_attempted[currentuser.ID][index].attempt[attempt].q_bank[i][1]].status='W';
quizes_attempted[currentuser.ID][index].attempt[attempt].result.attempted++;
quizes_attempted[currentuser.ID][index].attempt[attempt].result.incorrect++;
}
}
}
}
void view_instructions(int index) {
printf("-------------------------------------------\n %s \n", quizlist.quiz[index].name);
printf("-------------------------------------------\n\n");
printf("%s\n\n", quizlist.quiz[index].instructions);
printf("Attempts: %d/%d\n", quizes_attempted[currentuser.ID][index].no_attempts, quizlist.quiz[index].no_of_max_attempts);
if (quizlist.quiz[index].max_time%60<10) {
printf("Max time: %d:0%d min\n\n",quizlist.quiz[index].max_time/60, quizlist.quiz[index].max_time%60);
}
else {
printf("Max time: %d:%d min\n\n",quizlist.quiz[index].max_time/60, quizlist.quiz[index].max_time%60);
}
if (quizes_attempted[currentuser.ID][index].no_attempts>=quizlist.quiz[index].no_of_max_attempts) {
printf("\nYou have given maximum number of allowed attempts.\n");
}
if (quizlist.quiz[index].visible<=1) {
printf("\nInstructor has not allowed you to give this quiz yet.\n");
}
printf("\nWhat would you like to do?\n");
if (quizlist.quiz[index].visible>1 && quizes_attempted[currentuser.ID][index].no_attempts<quizlist.quiz[index].no_of_max_attempts) {
printf("- Start the quiz (Enter q)\n");
}
printf("- Exit (Enter e)\n");
char com;
E:
scanf("%c",&com);
clearBuf();
if (com=='q' && quizlist.quiz[index].visible>1 && quizes_attempted[currentuser.ID][index].no_attempts<quizlist.quiz[index].no_of_max_attempts) {
takeQuiz(index);
}
else if (com=='e') {
return;
}
else {
printf("%sInvalid option please try again,%s\n", red, normal);
goto E;
}
}
void print_time_left(int index) {
// Refresh current time
record_time(1);
int tl_min = (quizlist.quiz[index].max_time-current+start)/60;
if (tl_min==quizlist.quiz[index].max_time/60) {
tl_min=quizlist.quiz[index].max_time/60-1;
}
int tl_sec = 59-((current-start)%60);
char color[8];
if (tl_min==0 && tl_sec<30) {
strcpy(color, red);
}
else if (tl_min==0) {
strcpy(color, yellow);
}
else {
strcpy(color, green);
}
if (tl_sec<10) {
printf("Time Left: %s%d:0%d%s\n", color, tl_min, tl_sec, normal);
}
else {
printf("Time Left: %s%d:%d%s\n", color, tl_min, tl_sec, normal);
}
}