@universis/evaluations 1.0.33 → 1.0.35
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/GenerateStudyProgramTokenAction.json +65 -0
- package/dist/config/models/SendStudyProgramTokenAction.json +66 -0
- package/dist/config/models/StudyProgramEvaluationEvent.json +143 -0
- package/dist/listeners/class-instructor-event-listener.js +5 -0
- package/dist/listeners/class-instructor-event-listener.js.map +1 -1
- package/dist/locales/el.json +2 -1
- package/dist/models/evaluation-event-model.js +24 -10
- package/dist/models/evaluation-event-model.js.map +1 -1
- package/dist/models/study-program-evaluation-event-model.js +308 -0
- package/dist/models/study-program-evaluation-event-model.js.map +1 -0
- package/package.json +1 -1
- package/src/config/models/GenerateStudyProgramTokenAction.json +65 -0
- package/src/config/models/SendStudyProgramTokenAction.json +66 -0
- package/src/config/models/StudyProgramEvaluationEvent.json +143 -0
- package/src/listeners/class-instructor-event-listener.js +5 -0
- package/src/locales/el.json +2 -1
- package/src/models/evaluation-event-model.js +19 -5
- package/src/models/study-program-evaluation-event-model.js +307 -0
|
@@ -12,6 +12,11 @@ async function beforeSaveAsync(event) {
|
|
|
12
12
|
.select('courseClass/course/department').value();
|
|
13
13
|
event.target.organizer = organizer;
|
|
14
14
|
}
|
|
15
|
+
if (event.target.studyProgram) {
|
|
16
|
+
event.target.organizer = await context.model('StudyProgram')
|
|
17
|
+
.where('id').equal(event.target.studyProgram)
|
|
18
|
+
.select('department').value();
|
|
19
|
+
}
|
|
15
20
|
if (event.state===DataObjectState.Update)
|
|
16
21
|
{
|
|
17
22
|
const previous = event.previous;
|
package/src/locales/el.json
CHANGED
|
@@ -18,5 +18,6 @@
|
|
|
18
18
|
"Evaluation access token has been expired or is invalid.": "Ο κωδικός αξιολόγησης έχει λήξει ή δεν είναι έγκυρος",
|
|
19
19
|
"The bulk sending process of tokens is already in progress": "Η μαζική αποστολή των μηνυμάτων έχει ήδη ξεκινήσει",
|
|
20
20
|
"Select at least one instructor to evaluate":"Επιλέξτε τουλάχιστον έναν/μία διδάσκοντα/ουσα για να αξιολογήσετε",
|
|
21
|
-
"A previous action for generating evaluation tokens has been found.": "Μια ενέργεια έκδοσης tokens είναι ήδη σε εξέλιξη"
|
|
21
|
+
"A previous action for generating evaluation tokens has been found.": "Μια ενέργεια έκδοσης tokens είναι ήδη σε εξέλιξη",
|
|
22
|
+
"No students available to evaluate this study program": "Δε βρέθηκαν φοιτητές προς αξιολόγηση αυτού του προγράμματος σπουδών"
|
|
22
23
|
}
|
|
@@ -19,7 +19,7 @@ import { JsonFormInspector } from '@themost/form-inspector';
|
|
|
19
19
|
import EvaluationDocument from './evaluation-document-model';
|
|
20
20
|
|
|
21
21
|
const EvaluationAccessToken = require('./evaluation-access-token-model');
|
|
22
|
-
import {lt} from 'semver';
|
|
22
|
+
import { lt, neq, valid, coerce } from 'semver';
|
|
23
23
|
import util from "util";
|
|
24
24
|
import fs from "fs";
|
|
25
25
|
import moment from "moment";
|
|
@@ -176,7 +176,11 @@ class EvaluationEvent extends DataObject {
|
|
|
176
176
|
let shouldUpgrade = true;
|
|
177
177
|
let model = this.context.model(form.resultType);
|
|
178
178
|
if (model != null) {
|
|
179
|
-
|
|
179
|
+
// try to convert model and form versions to a valid semver format
|
|
180
|
+
const modelVersion = valid(coerce(model.version || '1.0.0'));
|
|
181
|
+
const formVersion = valid(coerce(form.version || '1.0.0'));
|
|
182
|
+
// and upgrade evaluation document only if its form has been upgraded
|
|
183
|
+
shouldUpgrade = lt(modelVersion, formVersion);
|
|
180
184
|
}
|
|
181
185
|
if (shouldUpgrade) {
|
|
182
186
|
// load for inspector
|
|
@@ -233,9 +237,19 @@ class EvaluationEvent extends DataObject {
|
|
|
233
237
|
if (model.hidden) {
|
|
234
238
|
builder.removeEntitySet(model.name);
|
|
235
239
|
}
|
|
236
|
-
//
|
|
237
|
-
|
|
238
|
-
|
|
240
|
+
// try to convert model and form versions to a valid semver format
|
|
241
|
+
const modelVersion = valid(coerce(model.version || '1.0.0'));
|
|
242
|
+
const formVersion = valid(coerce(form.version || '1.0.0'));
|
|
243
|
+
// and if they are different
|
|
244
|
+
if (neq(modelVersion, formVersion)) {
|
|
245
|
+
// update evaluation document version
|
|
246
|
+
await this.context.model('EvaluationDocument').silent().save({
|
|
247
|
+
id: form.id,
|
|
248
|
+
// note: save semver version to satisfy the pattern of the field
|
|
249
|
+
// see "pattern": "^\\d+.\\d+.\\d+$"
|
|
250
|
+
version: modelVersion
|
|
251
|
+
});
|
|
252
|
+
}
|
|
239
253
|
}
|
|
240
254
|
// check if event has sub events and append form to result
|
|
241
255
|
if (this.subEvents && this.subEvents.length) {
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {EdmMapping, EdmType} from '@themost/data';
|
|
2
|
+
import EvaluationEvent from "./evaluation-event-model";
|
|
3
|
+
import {DataError, TraceUtils, DataNotFoundError, LangUtils} from "@themost/common";
|
|
4
|
+
import moment from "moment";
|
|
5
|
+
/**
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
@EdmMapping.entityType('StudyProgramEvaluationEvent')
|
|
9
|
+
class StudyProgramEvaluationEvent extends EvaluationEvent {
|
|
10
|
+
/**
|
|
11
|
+
* @constructor
|
|
12
|
+
*/
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@EdmMapping.func('Results', EdmType.CollectionOf('Object'))
|
|
18
|
+
async getResults() {
|
|
19
|
+
// check access to StudyProgramEvaluationEvent and get results silently
|
|
20
|
+
const event = await this.context.model('StudyProgramEvaluationEvent').where('id').equal(this.getId());
|
|
21
|
+
if (event) {
|
|
22
|
+
const form = await this.getForm();
|
|
23
|
+
return this.context.model(form.properties.name).where('evaluationEvent').equal(this.id).silent().prepare();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@EdmMapping.action('GenerateEvaluationTokens', 'GenerateStudyProgramTokenAction')
|
|
28
|
+
async generateEvaluationTokens() {
|
|
29
|
+
const context = this.context;
|
|
30
|
+
const action = {
|
|
31
|
+
event: this.id,
|
|
32
|
+
numberOfStudents: 0,
|
|
33
|
+
total: 0,
|
|
34
|
+
actionStatus: {alternateName: 'PotentialActionStatus'}
|
|
35
|
+
};
|
|
36
|
+
// get event
|
|
37
|
+
const event = await context.model('StudyProgramEvaluationEvent').where('id').equal(this.id)
|
|
38
|
+
.expand('studyProgram')
|
|
39
|
+
.getItem();
|
|
40
|
+
if (event) {
|
|
41
|
+
//check if a GenerateTokenAction exists
|
|
42
|
+
const prevAction = await context.model('GenerateStudyProgramTokenAction').where('event').equal(this.id)
|
|
43
|
+
.and('actionStatus/alternateName').notEqual('FailedActionStatus')
|
|
44
|
+
.silent().getItem();
|
|
45
|
+
if (prevAction) {
|
|
46
|
+
throw new Error(context.__('A previous action for generating evaluation tokens has been found.'));
|
|
47
|
+
}
|
|
48
|
+
const studyProgram = event.studyProgram;
|
|
49
|
+
const studentStatuses = context.getConfiguration().getSourceAt('settings/universis/evaluations/studyProgramStudentStatuses') || ['graduated','declared'];
|
|
50
|
+
let students;
|
|
51
|
+
try {
|
|
52
|
+
students = await this.getStudents(studyProgram, event.academicYear, event.academicPeriod, studentStatuses);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
throw new DataError(err);
|
|
56
|
+
}
|
|
57
|
+
if (!(Array.isArray(students) && students.length)) {
|
|
58
|
+
throw new DataError('E_STUDENTS_FOUND', context.__('No students available to evaluate this study program'));
|
|
59
|
+
}
|
|
60
|
+
action.numberOfStudents = students.length;
|
|
61
|
+
// save action and then generateTokens
|
|
62
|
+
const generateAction = await context.model('GenerateStudyProgramTokenAction').save(action);
|
|
63
|
+
const generateOptions =
|
|
64
|
+
{
|
|
65
|
+
evaluationEvent: event.id,
|
|
66
|
+
n: action.numberOfStudents,
|
|
67
|
+
expires: event.endDate
|
|
68
|
+
};
|
|
69
|
+
try {
|
|
70
|
+
// save number of students to event
|
|
71
|
+
event.numberOfStudents = action.numberOfStudents;
|
|
72
|
+
await context.model('StudyProgramEvaluationEvent').save(event);
|
|
73
|
+
if (event.numberOfStudents > 0) {
|
|
74
|
+
await EvaluationEvent.generateTokens(context, generateOptions);
|
|
75
|
+
}
|
|
76
|
+
generateAction.total = action.numberOfStudents;
|
|
77
|
+
generateAction.actionStatus = {alternateName: 'CompletedActionStatus'}
|
|
78
|
+
return await context.model('GenerateStudyProgramTokenAction').save(generateAction);
|
|
79
|
+
} catch (err) {
|
|
80
|
+
TraceUtils.error(err);
|
|
81
|
+
// update also action status
|
|
82
|
+
generateAction.total = 0;
|
|
83
|
+
generateAction.actionStatus = {alternateName: 'FailedActionStatus'}
|
|
84
|
+
generateAction.description = err.message;
|
|
85
|
+
return await context.model('GenerateStudyProgramTokenAction').save(generateAction);
|
|
86
|
+
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async getStudents(studyProgram, academicYear, academicPeriod, studentStatuses) {
|
|
94
|
+
try {
|
|
95
|
+
const serviceProvider = await this.context.model('ServiceProvider')
|
|
96
|
+
.where('alternateName').equal('universis')
|
|
97
|
+
.silent().getTypedItem();
|
|
98
|
+
if (serviceProvider == null) {
|
|
99
|
+
TraceUtils.error(`Universis service provider not found.`);
|
|
100
|
+
return new DataError(`Cannot get students for study program. Universis service provider not found.`);
|
|
101
|
+
}
|
|
102
|
+
let url = `Students?$select=id as student,person/email as email&$filter=studyProgram eq ${studyProgram.id}`
|
|
103
|
+
if (Array.isArray(studentStatuses) && studentStatuses.length > 0) {
|
|
104
|
+
const filters = [];
|
|
105
|
+
// get active student for specific inscription year and period
|
|
106
|
+
if (studentStatuses.includes('active')) {
|
|
107
|
+
filters.push(`(studentStatus/alternateName eq 'active' and inscriptionYear eq ${academicYear} and inscriptionPeriod eq ${academicPeriod}))`);
|
|
108
|
+
}
|
|
109
|
+
if (studentStatuses.includes('graduated')) {
|
|
110
|
+
filters.push(`(studentStatus/alternateName eq 'graduated' and graduationYear eq ${academicYear} and graduationPeriod eq ${academicPeriod})`);
|
|
111
|
+
}
|
|
112
|
+
if (studentStatuses.includes('declared')) {
|
|
113
|
+
filters.push(
|
|
114
|
+
`(studentStatus/alternateName eq 'declared' and declaration/declaredYear eq ${academicYear} and declaration/declaredPeriod eq ${academicPeriod})`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
if (filters.length > 0) {
|
|
118
|
+
url += ` and (${filters.join(' or ')})`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const response = await serviceProvider.execute({
|
|
122
|
+
"url": url,
|
|
123
|
+
"method": 'GET'
|
|
124
|
+
});
|
|
125
|
+
return response && response.value;
|
|
126
|
+
} catch (err) {
|
|
127
|
+
TraceUtils.error(err);
|
|
128
|
+
throw (err);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@EdmMapping.func('TokenInfo', EdmType.CollectionOf('Object'))
|
|
133
|
+
async getTokenStatistics() {
|
|
134
|
+
const tokenInfo = {
|
|
135
|
+
total: 0,
|
|
136
|
+
used: 0,
|
|
137
|
+
sent: 0
|
|
138
|
+
};
|
|
139
|
+
// get number of tokens
|
|
140
|
+
const tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
141
|
+
.select('count(evaluationEvent) as total', 'used', 'sent')
|
|
142
|
+
.groupBy('used', 'sent', 'evaluationEvent')
|
|
143
|
+
.silent().getItems();
|
|
144
|
+
tokens.map(item => {
|
|
145
|
+
tokenInfo.total += item.total;
|
|
146
|
+
tokenInfo.used += item.used ? item.total : 0;
|
|
147
|
+
tokenInfo.sent += item.sent ? item.total : 0;
|
|
148
|
+
});
|
|
149
|
+
return tokenInfo;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@EdmMapping.action('SendEvaluationTokens', 'SendStudyProgramTokenAction')
|
|
153
|
+
async sendEvaluationTokens() {
|
|
154
|
+
|
|
155
|
+
const context = this.context;
|
|
156
|
+
let sendAction = {};
|
|
157
|
+
let subject;
|
|
158
|
+
// get event and check if event endDate has passed
|
|
159
|
+
try {
|
|
160
|
+
// get current date
|
|
161
|
+
const currentDate = moment(new Date()).startOf('day').toDate(); // get only date format
|
|
162
|
+
const event = await context.model('StudyProgramEvaluationEvent').where('id').equal(this.getId())
|
|
163
|
+
.and('date(endDate)').greaterOrEqual(currentDate)
|
|
164
|
+
.expand({
|
|
165
|
+
name: 'studyProgram',
|
|
166
|
+
options: {
|
|
167
|
+
'$expand': 'department'
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
name: 'actions',
|
|
172
|
+
options: {
|
|
173
|
+
'$expand': 'actionStatus'
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
.getItem();
|
|
177
|
+
// validate event
|
|
178
|
+
if (event == null) {
|
|
179
|
+
throw new DataNotFoundError('The event cannot be found or is inaccessible.');
|
|
180
|
+
}
|
|
181
|
+
// validate status
|
|
182
|
+
if (!(event.eventStatus && event.eventStatus.alternateName === 'EventOpened')) {
|
|
183
|
+
throw new DataError('E_INVALID_PERIOD', context.__('Send tokens action may only be executed for Open/Active events'));
|
|
184
|
+
}
|
|
185
|
+
// validate event actions
|
|
186
|
+
if (!(event.actions && event.actions.length)) {
|
|
187
|
+
throw new DataNotFoundError('E_NO_TOKENS', context.__('Token actions were not found for the specified event or they are inaccessible.'));
|
|
188
|
+
}
|
|
189
|
+
// find completed token generation action (can only be one)
|
|
190
|
+
const successfulTokenAction = event.actions.find(generateTokenAction => generateTokenAction.actionStatus.alternateName === 'CompletedActionStatus');
|
|
191
|
+
if (successfulTokenAction == null) {
|
|
192
|
+
throw new DataError('E_NO_TOKENS', 'The specified event does not have a completed token generation action.');
|
|
193
|
+
}
|
|
194
|
+
// get tokenInfo for this event
|
|
195
|
+
const tokenInfo = await this.getTokenStatistics();
|
|
196
|
+
// validate sent-used separately for descriptive errors
|
|
197
|
+
if (tokenInfo.sent) {
|
|
198
|
+
throw new DataError('E_ALREADY_SENT', `${context.__('The bulk sending of the tokens could not be completed because some of them had already been sent')} (${tokenInfo.sent})`);
|
|
199
|
+
}
|
|
200
|
+
if (tokenInfo.used) {
|
|
201
|
+
throw new DataError('E_ALREADY_USED', `${context.__('The bulk sending of the tokens could not be completed because some of them had already been used')} (${tokenInfo.used})`);
|
|
202
|
+
}
|
|
203
|
+
// start gathering students
|
|
204
|
+
let students;
|
|
205
|
+
const studyProgram = event.studyProgram;
|
|
206
|
+
// get students
|
|
207
|
+
const studentStatuses = ['graduated','declared','active']
|
|
208
|
+
students = await this.getStudents(studyProgram,event.academicYear,event.academicPeriod,studentStatuses);
|
|
209
|
+
|
|
210
|
+
if (!(Array.isArray(students) && students.length)) {
|
|
211
|
+
throw new DataError('E_STUDENTS_FOUND', 'No recipients/students found.');
|
|
212
|
+
}
|
|
213
|
+
// check total of students
|
|
214
|
+
if (students.length !== tokenInfo.total) {
|
|
215
|
+
throw new DataError('E_TOTAL_STUDENTS',
|
|
216
|
+
`${this.context.__("The number of students in the study program differs from the generated tokens")}` +
|
|
217
|
+
`(${this.context.__("Students")}: ${students.length}, ${this.context.__("Number of tokens")}:${tokenInfo.total}.`);
|
|
218
|
+
}
|
|
219
|
+
// validate student emails, must all exist and have a valid format
|
|
220
|
+
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])?)*$/;
|
|
221
|
+
const invalidEmail = students.find(student => {
|
|
222
|
+
return !(student.email && mailRegex.test(student.email));
|
|
223
|
+
});
|
|
224
|
+
if (invalidEmail) {
|
|
225
|
+
throw new DataError('E_INVALID_EMAIL', `${context.__('At least one student\'s email is null or invalid')} (email:${invalidEmail.email || ' '} ${context.__('Student')}:${invalidEmail.student})`);
|
|
226
|
+
}
|
|
227
|
+
// get tokens
|
|
228
|
+
let tokens = await this.context.model('EvaluationAccessToken').where('evaluationEvent').equal(this.getId())
|
|
229
|
+
.silent()
|
|
230
|
+
.getAllItems();
|
|
231
|
+
|
|
232
|
+
// get mail template
|
|
233
|
+
const mailTemplate = await context.model('MailConfiguration')
|
|
234
|
+
.where('target').equal('SendStudyProgramTokenAction')
|
|
235
|
+
.silent()
|
|
236
|
+
.getItem();
|
|
237
|
+
if (mailTemplate == null) {
|
|
238
|
+
throw new DataError('E_MISSING_TEMPLATE', 'Cannot execute send tokens action because template is missing for SendStudyProgramTokenAction.');
|
|
239
|
+
}
|
|
240
|
+
// check if there is already an active sendTokensAction
|
|
241
|
+
const alreadySendingTokens = await context.model('SendStudyProgramTokenAction')
|
|
242
|
+
.where('event').equal(event.id)
|
|
243
|
+
.and('actionStatus/alternateName').equal('ActiveActionStatus')
|
|
244
|
+
.silent().count();
|
|
245
|
+
if (alreadySendingTokens) {
|
|
246
|
+
throw new DataError('E_ALREADY_SENDING', `${context.__('The bulk sending process of tokens is already in progress')}`);
|
|
247
|
+
}
|
|
248
|
+
// save SendInstructorTokenAction
|
|
249
|
+
const sendTokenAction = {
|
|
250
|
+
event: this.getId(),
|
|
251
|
+
numberOfStudents: students.length,
|
|
252
|
+
total: tokenInfo.total,
|
|
253
|
+
sent: 0,
|
|
254
|
+
failed: 0,
|
|
255
|
+
actionStatus: {
|
|
256
|
+
alternateName: 'ActiveActionStatus'
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
sendAction = await context.model('SendStudyProgramTokenAction').silent().save(sendTokenAction);
|
|
260
|
+
// format mail subject expected params ${course}, ${instructor}
|
|
261
|
+
const exp = new RegExp('\\$\\{\\w+\\}+', 'ig');
|
|
262
|
+
subject = mailTemplate.subject;
|
|
263
|
+
const params = subject.match(exp);
|
|
264
|
+
if (params.length > 0) {
|
|
265
|
+
// parse only custom parameters ${studyProgram}
|
|
266
|
+
params.forEach(param => {
|
|
267
|
+
if (param === '${studyProgram}') {
|
|
268
|
+
// get studyProgram full name
|
|
269
|
+
subject = subject.replace(param, studyProgram ? `${studyProgram.name}` : '');
|
|
270
|
+
} else {
|
|
271
|
+
subject = subject.replace(param, '');
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
EvaluationEvent.sendMessages(context, students, tokens, tokenInfo, mailTemplate, subject, sendAction, event);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
TraceUtils.error(err);
|
|
278
|
+
// create send tokens action
|
|
279
|
+
const sendTokenAction = {
|
|
280
|
+
event: this.getId(),
|
|
281
|
+
numberOfStudents: 0,
|
|
282
|
+
total: 0,
|
|
283
|
+
sent: 0,
|
|
284
|
+
failed: 0,
|
|
285
|
+
actionStatus: {
|
|
286
|
+
alternateName: 'FailedActionStatus'
|
|
287
|
+
},
|
|
288
|
+
description: err.message
|
|
289
|
+
}
|
|
290
|
+
// and save
|
|
291
|
+
if (sendAction && sendAction.id) {
|
|
292
|
+
sendTokenAction.id = sendAction.id;
|
|
293
|
+
}
|
|
294
|
+
if (!subject) {
|
|
295
|
+
// load courseClass
|
|
296
|
+
const item = await context.model('StudyProgramEvaluationEvent').where('id').equal(this.getId())
|
|
297
|
+
.expand('studyProgram')
|
|
298
|
+
.getItem();
|
|
299
|
+
const studyProgram = item && item.studyProgram
|
|
300
|
+
subject = studyProgram ? `${studyProgram.name}` : '';
|
|
301
|
+
}
|
|
302
|
+
EvaluationEvent.sendSse(context.user, context, this.getId(), subject, err);
|
|
303
|
+
return await context.model('SendStudyProgramTokenAction').silent().save(sendTokenAction);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
module.exports = StudyProgramEvaluationEvent;
|