-
Notifications
You must be signed in to change notification settings - Fork 882
/
AssessmentResult.js
317 lines (280 loc) · 6.81 KB
/
AssessmentResult.js
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
import { isArray, isNumber, isUndefined } from "lodash-es";
import Mark from "./Mark";
/**
* A function that only returns an empty that can be used as an empty marker
*
* @returns {Array} A list of empty marks.
*/
const emptyMarker = function() {
return [];
};
/**
* Represents the assessment result.
*/
class AssessmentResult {
/**
* Constructs the AssessmentResult value object.
*
* @param {Object} [values] The values for this assessment result.
*/
constructor( values ) {
this._hasScore = false;
this._identifier = "";
this._hasMarks = false;
this._hasJumps = false;
this._hasEditFieldName = false;
this._marker = emptyMarker;
this._hasBetaBadge = false;
this.score = 0;
this.text = "";
this.marks = [];
this.editFieldName = "";
if ( isUndefined( values ) ) {
values = {};
}
if ( ! isUndefined( values.score ) ) {
this.setScore( values.score );
}
if ( ! isUndefined( values.text ) ) {
this.setText( values.text );
}
if ( ! isUndefined( values.marks ) ) {
this.setMarks( values.marks );
}
if ( ! isUndefined( values._hasBetaBadge ) ) {
this.setHasBetaBadge( values._hasBetaBadge );
}
if ( ! isUndefined( values._hasJumps ) ) {
this.setHasJumps( values._hasJumps );
}
if ( ! isUndefined( values.editFieldName ) ) {
this.setEditFieldName( values.editFieldName );
}
}
/**
* Checks if a score is available.
* @returns {boolean} Whether or not a score is available.
*/
hasScore() {
return this._hasScore;
}
/**
* Gets the available score
* @returns {number} The score associated with the AssessmentResult.
*/
getScore() {
return this.score;
}
/**
* Sets the score for the assessment.
* @param {number} score The score to be used for the score property
* @returns {void}
*/
setScore( score ) {
if ( isNumber( score ) ) {
this.score = score;
this._hasScore = true;
}
}
/**
* Checks if a text is available.
* @returns {boolean} Whether or not a text is available.
*/
hasText() {
return this.text !== "";
}
/**
* Gets the available text
* @returns {string} The text associated with the AssessmentResult.
*/
getText() {
return this.text;
}
/**
* Sets the text for the assessment.
* @param {string} text The text to be used for the text property
* @returns {void}
*/
setText( text ) {
if ( isUndefined( text ) ) {
text = "";
}
this.text = text;
}
/**
* Gets the available marks.
*
* @returns {array} The marks associated with the AssessmentResult.
*/
getMarks() {
return this.marks;
}
/**
* Sets the marks for the assessment.
*
* @param {array} marks The marks to be used for the marks property
*
* @returns {void}
*/
setMarks( marks ) {
if ( isArray( marks ) ) {
this.marks = marks;
this._hasMarks = marks.length > 0;
}
}
/**
* Sets the identifier
*
* @param {string} identifier An alphanumeric identifier for this result.
* @returns {void}
*/
setIdentifier( identifier ) {
this._identifier = identifier;
}
/**
* Gets the identifier
*
* @returns {string} An alphanumeric identifier for this result.
*/
getIdentifier() {
return this._identifier;
}
/**
* Sets the marker, a pure function that can return the marks for a given Paper
*
* @param {Function} marker The marker to set.
* @returns {void}
*/
setMarker( marker ) {
this._marker = marker;
}
/**
* Returns whether or not this result has a marker that can be used to mark for a given Paper
*
* @returns {boolean} Whether or this result has a marker.
*/
hasMarker() {
return this._hasMarks && this._marker !== this.emptyMarker;
}
/**
* Gets the marker, a pure function that can return the marks for a given Paper
*
* @returns {Function} The marker.
*/
getMarker() {
return this._marker;
}
/**
* Sets the value of _hasMarks to determine if there is something to mark.
*
* @param {boolean} hasMarks Is there something to mark.
* @returns {void}
*/
setHasMarks( hasMarks ) {
this._hasMarks = hasMarks;
}
/**
* Returns the value of _hasMarks to determine if there is something to mark.
*
* @returns {boolean} Is there something to mark.
*/
hasMarks() {
return this._hasMarks;
}
/**
* Sets the value of _hasBetaBadge to determine if the result has a beta badge.
*
* @param {boolean} hasBetaBadge Whether this result has a beta badge.
* @returns {void}
*/
setHasBetaBadge( hasBetaBadge ) {
this._hasBetaBadge = hasBetaBadge;
}
/**
* Returns the value of _hasBetaBadge to determine if the result has a beta badge.
*
* @returns {bool} Whether this result has a beta badge.
*/
hasBetaBadge() {
return this._hasBetaBadge;
}
/**
* Sets the value of _hasJumps to determine whether it's needed to jump to a different field.
*
* @param {boolean} hasJumps Whether this result causes a jump to a different field.
* @returns {void}
*/
setHasJumps( hasJumps ) {
this._hasJumps = hasJumps;
}
/**
* Returns the value of _hasJumps to determine whether it's needed to jump to a different field.
*
* @returns {bool} Whether this result causes a jump to a different field.
*/
hasJumps() {
return this._hasJumps;
}
/**
* Check if an edit field name is available.
* @returns {boolean} Whether or not an edit field name is available.
*/
hasEditFieldName() {
return this._hasEditFieldName;
}
/**
* Gets the edit field name.
* @returns {string} The edit field name associated with the AssessmentResult.
*/
getEditFieldName() {
return this.editFieldName;
}
/**
* Sets the edit field name to be used to create the aria label for an edit button.
* @param {string} editFieldName The string to be used for the string property
* @returns {void}
*/
setEditFieldName( editFieldName ) {
if ( editFieldName !== "" ) {
this.editFieldName = editFieldName;
this._hasEditFieldName = true;
}
}
/**
* Serializes the AssessmentResult instance to an object.
*
* @returns {Object} The serialized AssessmentResult.
*/
serialize() {
return {
_parseClass: "AssessmentResult",
identifier: this._identifier,
score: this.score,
text: this.text,
marks: this.marks.map( mark => mark.serialize() ),
_hasBetaBadge: this._hasBetaBadge,
_hasJumps: this._hasJumps,
editFieldName: this.editFieldName,
};
}
/**
* Parses the object to an AssessmentResult.
*
* @param {Object} serialized The serialized object.
*
* @returns {AssessmentResult} The parsed AssessmentResult.
*/
static parse( serialized ) {
const result = new AssessmentResult( {
text: serialized.text,
score: serialized.score,
marks: serialized.marks.map( mark => Mark.parse( mark ) ),
_hasBetaBadge: serialized._hasBetaBadge,
_hasJumps: serialized._hasJumps,
editFieldName: serialized.editFieldName,
} );
result.setIdentifier( serialized.identifier );
return result;
}
}
export default AssessmentResult;