This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sigaa-quiz-student.ts
executable file
·144 lines (129 loc) · 3.45 KB
/
sigaa-quiz-student.ts
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
import { UpdatableResourceData } from '@resources/sigaa-resource-manager';
import {
AbstractUpdatableResource,
UpdatableResource,
UpdatableResourceCallback
} from '@resources/updatable-resource';
import { HTTP } from '@session/sigaa-http';
import { SigaaForm } from '@session/sigaa-page';
/**
* @category Internal
*/
export interface QuizData extends UpdatableResourceData {
title: string;
id: string;
startDate: Date;
endDate: Date;
formSendAnswers?: SigaaForm;
formViewAnswersSubmitted?: SigaaForm;
}
/**
* @category Public
*/
export interface Quiz extends UpdatableResource<QuizData> {
readonly title: string;
readonly type: 'quiz';
readonly endDate: Date;
readonly startDate: Date;
readonly id: string;
/**
* TODO
* @param retry
*/
getAnswersSubmitted(): Promise<void>;
}
/**
* @category Internal
*/
export class SigaaQuiz extends AbstractUpdatableResource implements Quiz {
readonly type = 'quiz';
readonly errorDeadlineToReadClosed =
'SIGAA: Deadline to read as answer closed.';
readonly errorQuizYetNoSendAnswers = 'Sigaa: Quiz yet no sent answers.';
private _formViewAnswersSubmitted?: SigaaForm;
private _formSendAnswers?: SigaaForm;
private _startDate!: Date;
private _endDate!: Date;
private _title!: string;
private _id!: string;
constructor(
private http: HTTP,
options: QuizData,
updater: UpdatableResourceCallback
) {
super(options.instanceIndentifier, updater);
this.update(options);
}
update(options: QuizData): void {
this._title = options.title;
this._id = options.id;
this._startDate = options.startDate;
this._endDate = options.endDate;
this._formSendAnswers = options.formSendAnswers;
this._formViewAnswersSubmitted = options.formViewAnswersSubmitted;
this.isClosed = false;
}
get title(): string {
this.checkIfItWasClosed();
return this._title;
}
get id(): string {
this.checkIfItWasClosed();
return this._id;
}
get endDate(): Date {
this.checkIfItWasClosed();
return this._endDate;
}
get startDate(): Date {
this.checkIfItWasClosed();
return this._startDate;
}
/**
* TODO
* @param retry
*/
async getAnswersSubmitted(retry = true): Promise<void> {
try {
if (this._formViewAnswersSubmitted === undefined)
throw new Error('SIGAA: Quiz form is undefined.');
const page = await this.http.post(
this._formViewAnswersSubmitted.action.href,
this._formViewAnswersSubmitted.postValues
);
switch (page.statusCode) {
case 200:
if (
page.bodyDecoded.includes(
'Acabou o prazo para visualizar as respostas.'
)
)
throw new Error(this.errorDeadlineToReadClosed);
if (
page.bodyDecoded.includes(
'Você ainda não enviou respostas para este questionário'
)
)
throw new Error(this.errorQuizYetNoSendAnswers);
break;
case 302:
throw new Error('SIGAA: Quiz expired.');
default:
throw new Error('SIGAA: Quiz page status code unexpected.');
}
} catch (err) {
if (
err.message === this.errorDeadlineToReadClosed ||
err.message === this.errorQuizYetNoSendAnswers
) {
throw err;
}
if (retry) {
await this.updateInstance();
this.getAnswersSubmitted(false);
} else {
throw err;
}
}
}
}