@universis/evaluations 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/models/ClassInstructorEvaluationEvent.json +16 -2
- package/dist/config/models/EvaluationAccessToken.json +15 -0
- package/dist/config/models/GenerateInstructorTokenAction.json +19 -2
- package/dist/config/models/SendInstructorTokenAction.json +52 -0
- package/dist/config/models/SendTokenAction.json +45 -0
- package/dist/models/access-token-model.js +4 -1
- package/dist/models/access-token-model.js.map +1 -1
- package/dist/models/class-instructor-evaluation-event-model.js +220 -8
- package/dist/models/class-instructor-evaluation-event-model.js.map +1 -1
- package/dist/models/evaluation-event-model.js +49 -11
- package/dist/models/evaluation-event-model.js.map +1 -1
- package/package.json +1 -1
- package/src/config/models/ClassInstructorEvaluationEvent.json +16 -2
- package/src/config/models/EvaluationAccessToken.json +15 -0
- package/src/config/models/GenerateInstructorTokenAction.json +19 -2
- package/src/config/models/SendInstructorTokenAction.json +52 -0
- package/src/config/models/SendTokenAction.json +45 -0
- package/src/models/access-token-model.js +8 -5
- package/src/models/class-instructor-evaluation-event-model.js +213 -1
- package/src/models/evaluation-event-model.js +45 -7
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {EdmMapping, EdmType} from '@themost/data';
|
|
2
2
|
import EvaluationEvent from "./evaluation-event-model";
|
|
3
|
-
import {
|
|
3
|
+
import {DataError, TraceUtils, DataNotFoundError} from "@themost/common";
|
|
4
|
+
import {getMailer} from "@themost/mailer";
|
|
5
|
+
import moment from "moment";
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* @class
|
|
@@ -114,5 +116,215 @@ class ClassInstructorEvaluationEvent extends EvaluationEvent {
|
|
|
114
116
|
});
|
|
115
117
|
return tokenInfo;
|
|
116
118
|
}
|
|
119
|
+
|
|
120
|
+
@EdmMapping.action('SendEvaluationTokens', 'SendInstructorTokenAction')
|
|
121
|
+
async sendEvaluationTokens() {
|
|
122
|
+
const context = this.context;
|
|
123
|
+
// get event and check if event endDate has passed
|
|
124
|
+
// get current date
|
|
125
|
+
const currentDate = moment(new Date()).startOf('day').toDate(); // get only date format
|
|
126
|
+
const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.getId())
|
|
127
|
+
.and('date(endDate)').greaterOrEqual(currentDate)
|
|
128
|
+
.expand({
|
|
129
|
+
name: 'courseClassInstructor',
|
|
130
|
+
options: {
|
|
131
|
+
'$expand': 'courseClass($expand=course), instructor' // expand course and instructor so their attributes are available in the mail template
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
name: 'actions',
|
|
136
|
+
options: {
|
|
137
|
+
'$expand': 'actionStatus'
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
.getItem();
|
|
141
|
+
// validate event
|
|
142
|
+
if (event == null) {
|
|
143
|
+
throw new DataNotFoundError('The event cannot be found or is inaccessible.');
|
|
144
|
+
}
|
|
145
|
+
// validate status
|
|
146
|
+
if (!(event.eventStatus && event.eventStatus.alternateName === 'EventOpened')) {
|
|
147
|
+
throw new DataError('Send tokens action may only be executed for Open/Active events.');
|
|
148
|
+
}
|
|
149
|
+
// validate event actions
|
|
150
|
+
if (!(event.actions && event.actions.length)) {
|
|
151
|
+
throw new DataNotFoundError('There are not generate token actions for the specified event or they are inaccessible.');
|
|
152
|
+
}
|
|
153
|
+
// find completed token generation action (can only be one)
|
|
154
|
+
const successfulTokenAction = event.actions.find(generateTokenAction => generateTokenAction.actionStatus.alternateName === 'CompletedActionStatus');
|
|
155
|
+
if (successfulTokenAction == null) {
|
|
156
|
+
throw new DataError('The specified event does not have a completed token generation action.');
|
|
157
|
+
}
|
|
158
|
+
// get tokenInfo for this event
|
|
159
|
+
const tokenInfo = await this.getTokenStatistics();
|
|
160
|
+
// validate sent-used seperately for descriptive errors
|
|
161
|
+
if (tokenInfo.sent) {
|
|
162
|
+
throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.sent} of them is/are already sent.`);
|
|
163
|
+
}
|
|
164
|
+
if (tokenInfo.used) {
|
|
165
|
+
throw new DataError(`Cannot proceed with sending tokens, because ${tokenInfo.used} of them is/are already used.`);
|
|
166
|
+
}
|
|
167
|
+
// start gathering students
|
|
168
|
+
let students;
|
|
169
|
+
const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
|
|
170
|
+
const eventInstructor = event.courseClassInstructor && event.courseClassInstructor.instructor;
|
|
171
|
+
// get class sections
|
|
172
|
+
const sections = await context.model('CourseClassSection').where('courseClass').equal(courseClass.id).getItems();
|
|
173
|
+
// check if course class has sections
|
|
174
|
+
if (sections && sections.length > 0 && courseClass.mustRegisterSection !== 0) {
|
|
175
|
+
// get only number of students registered at specific section
|
|
176
|
+
const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
|
|
177
|
+
.and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getItems();
|
|
178
|
+
if (instructorSections && instructorSections.length === 0) {
|
|
179
|
+
throw new DataError('Instructor is not related with any of sections');
|
|
180
|
+
}
|
|
181
|
+
const sectionIds = instructorSections.map(section => {
|
|
182
|
+
return section.sectionId;
|
|
183
|
+
});
|
|
184
|
+
// get students for specific course class sections
|
|
185
|
+
students = await context.model('StudentCourseClass')
|
|
186
|
+
.where('courseClass').equal(courseClass.id)
|
|
187
|
+
.and('section').in(sectionIds)
|
|
188
|
+
.select('email')
|
|
189
|
+
.silent()
|
|
190
|
+
.getAllItems();
|
|
191
|
+
} else {
|
|
192
|
+
students = await context.model('StudentCourseClass')
|
|
193
|
+
.where('courseClass').equal(courseClass.id)
|
|
194
|
+
.select('email')
|
|
195
|
+
.silent()
|
|
196
|
+
.getAllItems();
|
|
197
|
+
}
|
|
198
|
+
if (!(Array.isArray(students) && students.length)) {
|
|
199
|
+
throw new DataError('No recipients/students found.');
|
|
200
|
+
}
|
|
201
|
+
// check total of students
|
|
202
|
+
if (students.length !== tokenInfo.total) {
|
|
203
|
+
throw new DataError(`Cannot proceed to sending tokens. Students are ${students.length} and generated tokens are ${tokenInfo.total}.`);
|
|
204
|
+
}
|
|
205
|
+
// validate student emails, must all exist and have a valid format
|
|
206
|
+
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
|
|
207
|
+
const mailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
208
|
+
const invalidEmail = students.find(student => {
|
|
209
|
+
return !(student.email && mailRegex.test(student.email));
|
|
210
|
+
});
|
|
211
|
+
if (invalidEmail) {
|
|
212
|
+
throw new DataError(`Cannot procced to sending tokens because at least one student's email is null or invalid. Found invalid email: ${invalidEmail.email || 'empty'}`);
|
|
213
|
+
}
|
|
214
|
+
// get tokens
|
|
215
|
+
let tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
216
|
+
.select('access_token')
|
|
217
|
+
.silent()
|
|
218
|
+
.getAllItems();
|
|
219
|
+
// and map them to an array
|
|
220
|
+
tokens = tokens.map(token => token.access_token);
|
|
221
|
+
// get mail template
|
|
222
|
+
const mailTemplate = await context.model('MailConfiguration')
|
|
223
|
+
.where('target').equal('SendInstructorTokenAction')
|
|
224
|
+
.silent()
|
|
225
|
+
.getItem();
|
|
226
|
+
if (mailTemplate == null) {
|
|
227
|
+
throw new DataError('Cannot execute send tokens action because template is missing for SendInstructorTokenAction.');
|
|
228
|
+
}
|
|
229
|
+
const succeeded = [];
|
|
230
|
+
const failed = [];
|
|
231
|
+
try {
|
|
232
|
+
// format mail subject expected params ${course}, ${instructor}
|
|
233
|
+
const exp = new RegExp('\\$\\{\\w+\\}+','ig');
|
|
234
|
+
let subject = mailTemplate.subject;
|
|
235
|
+
const params = subject.match(exp);
|
|
236
|
+
if (params.length>0) {
|
|
237
|
+
// parse only custom parameters ${instructor},${course}
|
|
238
|
+
params.forEach(param => {
|
|
239
|
+
if (param === '${instructor}') {
|
|
240
|
+
// get instructor full name
|
|
241
|
+
subject = subject.replace(param, eventInstructor? `${eventInstructor.familyName} ${eventInstructor.givenName}` : '');
|
|
242
|
+
} else if (param === '${course}') {
|
|
243
|
+
// get courseClass code and title
|
|
244
|
+
subject = subject.replace(param, courseClass.course ? `${courseClass.course.displayCode} ${courseClass.title}` : '');
|
|
245
|
+
} else {
|
|
246
|
+
subject = subject.replace(param, '');
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
// use an async function to send emails
|
|
251
|
+
await (async () => {
|
|
252
|
+
for (const student of students) {
|
|
253
|
+
// refresh mailer
|
|
254
|
+
const mailer = getMailer(context); // maybe investigate why this has to be refreshed.
|
|
255
|
+
// pop an access token
|
|
256
|
+
const access_token = tokens.pop();
|
|
257
|
+
// send message
|
|
258
|
+
await new Promise(resolve => {
|
|
259
|
+
mailer
|
|
260
|
+
.template(mailTemplate.template)
|
|
261
|
+
.subject(subject)
|
|
262
|
+
.to(student.email)
|
|
263
|
+
.send(Object.assign(event, {
|
|
264
|
+
access_token
|
|
265
|
+
}), (err) => {
|
|
266
|
+
if (err) {
|
|
267
|
+
try {
|
|
268
|
+
TraceUtils.error(`SendEvaluationTokensAction', 'An error occurred while trying to send token to student email ${student.email}`);
|
|
269
|
+
TraceUtils.error(err);
|
|
270
|
+
} catch (err1) {
|
|
271
|
+
// do nothing
|
|
272
|
+
}
|
|
273
|
+
failed.push(access_token);
|
|
274
|
+
return resolve();
|
|
275
|
+
}
|
|
276
|
+
succeeded.push(access_token);
|
|
277
|
+
return resolve();
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
})();
|
|
282
|
+
// update successfully sent tokens
|
|
283
|
+
for (const succededToken of succeeded) {
|
|
284
|
+
await context.model('EvaluationAccessToken').silent().save({
|
|
285
|
+
access_token: succededToken, // access_token is primary
|
|
286
|
+
sent: true,
|
|
287
|
+
$state: 2
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
// create send tokens action
|
|
291
|
+
const sendTokensAction = {
|
|
292
|
+
event: this.getId(),
|
|
293
|
+
numberOfStudents: students.length,
|
|
294
|
+
total: tokenInfo.total,
|
|
295
|
+
sent: succeeded.length,
|
|
296
|
+
failed: failed.length,
|
|
297
|
+
actionStatus: {
|
|
298
|
+
alternateName: 'CompletedActionStatus'
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// and save
|
|
302
|
+
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
303
|
+
} catch (err) {
|
|
304
|
+
// log error
|
|
305
|
+
TraceUtils.error(err);
|
|
306
|
+
// update successfully sent tokens
|
|
307
|
+
for (const succededToken of succeeded) {
|
|
308
|
+
await context.model('EvaluationAccessToken').silent().save({
|
|
309
|
+
access_token: succededToken, // access_token is primary
|
|
310
|
+
sent: true,
|
|
311
|
+
$state: 2
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// create send tokens action
|
|
315
|
+
const sendTokensAction = {
|
|
316
|
+
event: this.getId(),
|
|
317
|
+
numberOfStudents: students.length,
|
|
318
|
+
total: tokenInfo.total,
|
|
319
|
+
sent: succeeded.length,
|
|
320
|
+
failed: succeeded.length ? tokenInfo.total - succeeded.length : tokenInfo.total,
|
|
321
|
+
actionStatus: {
|
|
322
|
+
alternateName: 'CompletedActionStatus'
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// and save
|
|
326
|
+
return await context.model('SendInstructorTokenAction').silent().save(sendTokensAction);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
117
329
|
}
|
|
118
330
|
module.exports = ClassInstructorEvaluationEvent;
|
|
@@ -48,17 +48,20 @@ class EvaluationEvent extends DataObject {
|
|
|
48
48
|
}
|
|
49
49
|
// get evaluation event
|
|
50
50
|
// get current date
|
|
51
|
-
const startDate = new Date(); // get current date with time
|
|
52
|
-
const endDate = moment(new Date()).startOf('day').toDate(); // get only date format
|
|
53
51
|
let evaluationEvent = await context.model('EvaluationEvent')
|
|
54
52
|
.where('id').equal(token.sub)
|
|
55
|
-
.and('date(startDate)').lowerOrEqual(startDate)
|
|
56
|
-
.and('date(endDate)').greaterOrEqual(endDate)
|
|
57
53
|
.and('eventStatus/alternateName').equal('EventOpened')
|
|
58
54
|
.silent().getTypedItem();
|
|
59
55
|
if (evaluationEvent == null) {
|
|
60
56
|
throw new HttpNotFoundError('The specified evaluation cannot be found');
|
|
61
57
|
}
|
|
58
|
+
// check dates
|
|
59
|
+
const currentDate = new Date().setHours(0,0,0,0);
|
|
60
|
+
const startDate = evaluationEvent.startDate.setHours(0,0,0,0);
|
|
61
|
+
const endDate = evaluationEvent.endDate.setHours(0,0,0,0);
|
|
62
|
+
if (startDate>currentDate || endDate<currentDate) {
|
|
63
|
+
throw new HttpNotFoundError('The specified evaluation cannot be found, it is expired or not started.');
|
|
64
|
+
}
|
|
62
65
|
if (evaluationEvent.additionalType !== 'EvaluationEvent') {
|
|
63
66
|
if (evaluationEvent.additionalType === 'ClassInstructorEvaluationEvent') {
|
|
64
67
|
const event = await context.model('ClassInstructorEvaluationEvent')
|
|
@@ -228,11 +231,16 @@ class EvaluationEvent extends DataObject {
|
|
|
228
231
|
attribute.isNumber = dataTypes[attribute.type].type === 'number';
|
|
229
232
|
}
|
|
230
233
|
selectArguments.push(`${attribute.name}`);
|
|
234
|
+
// clear std,avg and responses from model attribute
|
|
235
|
+
attribute.std = 0;
|
|
236
|
+
attribute.avg = 0;
|
|
237
|
+
delete attribute.responses;
|
|
231
238
|
result.push(attribute);
|
|
232
239
|
}
|
|
233
240
|
|
|
234
241
|
});
|
|
235
|
-
|
|
242
|
+
const showResults = await this.showResults(context);
|
|
243
|
+
if (selectArguments.length && showResults === true) {
|
|
236
244
|
const responses = await q.select.apply(q, selectArguments).where('evaluationEvent').equal(this.getId()).silent().getAllItems();
|
|
237
245
|
for (let i = 0; i < result.length; i++) {
|
|
238
246
|
const qElement = result[i];
|
|
@@ -255,6 +263,36 @@ class EvaluationEvent extends DataObject {
|
|
|
255
263
|
return result;
|
|
256
264
|
}
|
|
257
265
|
|
|
266
|
+
async showResults (context) {
|
|
267
|
+
|
|
268
|
+
// this is a custom way to get permissions based on eventStatus and user groups
|
|
269
|
+
let showResults = true;
|
|
270
|
+
let found = false;
|
|
271
|
+
const eventStatusesResults = context.getConfiguration().getSourceAt('settings/evaluation/results/statuses') || [];
|
|
272
|
+
const groupsAllowed = context.getConfiguration().getSourceAt('settings/evaluation/results/groupsAllowed') || [];
|
|
273
|
+
if (eventStatusesResults.length > 0) {
|
|
274
|
+
// check if eventStatus is allowed
|
|
275
|
+
if (groupsAllowed.length > 0) {
|
|
276
|
+
// but first check if user belongs to allowed groups
|
|
277
|
+
const userName = context.interactiveUser ? context.interactiveUser.name : context.user.name;
|
|
278
|
+
const user = await context.model('User').where('name').equal(userName).expand('groups').getItem();
|
|
279
|
+
found = user.groups.some(r => groupsAllowed.indexOf(r.name) >= 0);
|
|
280
|
+
}
|
|
281
|
+
if (!found) {
|
|
282
|
+
const evaluationEvent = await context.model('EvaluationEvent').where('id').equal(this.getId())
|
|
283
|
+
.select('id', 'eventStatus/alternateName as eventStatus')
|
|
284
|
+
.getItem();
|
|
285
|
+
if (evaluationEvent == null) {
|
|
286
|
+
throw new HttpNotFoundError('The specified evaluation cannot be found');
|
|
287
|
+
}
|
|
288
|
+
showResults = eventStatusesResults.findIndex(x => {
|
|
289
|
+
return x === evaluationEvent.eventStatus;
|
|
290
|
+
}) >= 0;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return showResults;
|
|
294
|
+
}
|
|
295
|
+
|
|
258
296
|
findByKey(array, key) {
|
|
259
297
|
for (let index = 0; index < array.length; index++) {
|
|
260
298
|
const element = array[index];
|
|
@@ -345,11 +383,11 @@ class EvaluationEvent extends DataObject {
|
|
|
345
383
|
n += newItems.length;
|
|
346
384
|
items.push.apply(items, newItems);
|
|
347
385
|
}
|
|
348
|
-
await context.model('EvaluationAccessToken').save(items);
|
|
386
|
+
await context.model('EvaluationAccessToken').silent().save(items);
|
|
349
387
|
return n
|
|
350
388
|
} else {
|
|
351
389
|
items = await EvaluationEvent.generateManyTokens(context, generateOptions);
|
|
352
|
-
await context.model('EvaluationAccessToken').save(items);
|
|
390
|
+
await context.model('EvaluationAccessToken').silent().save(items);
|
|
353
391
|
return items.length;
|
|
354
392
|
}
|
|
355
393
|
}
|