@universis/evaluations 1.0.3 → 1.0.6

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.
@@ -0,0 +1,95 @@
1
+ import {EdmMapping} from '@themost/data';
2
+ import EvaluationEvent from "./evaluation-event-model";
3
+ import {TraceUtils} from "@themost/common";
4
+
5
+ /**
6
+ * @class
7
+ */
8
+ @EdmMapping.entityType('ClassInstructorEvaluationEvent')
9
+ class ClassInstructorEvaluationEvent extends EvaluationEvent {
10
+ /**
11
+ * @constructor
12
+ */
13
+ constructor() {
14
+ super();
15
+ }
16
+
17
+ @EdmMapping.action('GenerateEvaluationTokens', 'GenerateInstructorTokenAction')
18
+ async generateEvaluationTokens() {
19
+ const context = this.context;
20
+ const action = {
21
+ event: this.id,
22
+ numberOfStudents: 0,
23
+ total: 0,
24
+ actionStatus: {alternateName: 'PotentialActionStatus'}
25
+ };
26
+ // get courseClass instructor
27
+ const event = await context.model('ClassInstructorEvaluationEvent').where('id').equal(this.id)
28
+ .expand('courseClassInstructor')
29
+ .getItem();
30
+ if (event) {
31
+ //check if a GenerateInstructorTokenAction exists
32
+ const prevAction = await context.model('GenerateInstructorTokenAction').where('event').equal(this.id)
33
+ .and('actionStatus/alternateName').notEqual('FailedActionStatus')
34
+ .silent().getItem();
35
+ if (prevAction) {
36
+ throw new Error('A previous action for generating evaluation tokens has been found.');
37
+ }
38
+ const courseClass = event.courseClassInstructor && event.courseClassInstructor.courseClass;
39
+ const eventInstructor = event.courseClassInstructor && event.courseClassInstructor.instructor;
40
+ // get class sections
41
+ const sections = await context.model('CourseClassSection').where('courseClass').equal(courseClass.id).getItems();
42
+ // check if course class has sections
43
+ if (sections && sections.length > 0 && courseClass.mustRegisterSection !== 0) {
44
+ // get only number of students registered at specific section
45
+ const instructorSections = await context.model('CourseClassSectionInstructor').where('courseClass').equal(courseClass.id)
46
+ .and('instructor').equal(eventInstructor).select('section/section as sectionId').silent().getItems();
47
+ if (instructorSections && instructorSections.length === 0) {
48
+ action.actionStatus.alternateName = 'FailedActionStatus';
49
+ action.description = 'Instructor is not related with any of sections';
50
+ return await context.model('GenerateInstructorTokenAction').save(action);
51
+ } else {
52
+ const sectionIds = instructorSections.map(section => {
53
+ return section.sectionId;
54
+ });
55
+ // get number of students for specific course class sections
56
+ action.numberOfStudents = await context.model('StudentCourseClass').where('courseClass').equal(courseClass.id)
57
+ .and('section').in(sectionIds)
58
+ .silent().count();
59
+ }
60
+ } else {
61
+ action.numberOfStudents = await context.model('StudentCourseClass').where('courseClass').equal(courseClass.id).silent().count();
62
+ }
63
+ // save action and then generateTokens
64
+ const generateAction = await context.model('GenerateInstructorTokenAction').save(action);
65
+ const generateOptions =
66
+ {
67
+ evaluationEvent: event.id,
68
+ n: action.numberOfStudents,
69
+ expires : event.endDate
70
+ };
71
+ try {
72
+ // save number of students to event
73
+ event.numberOfStudents = action.numberOfStudents;
74
+ await context.model('ClassInstructorEvaluationEvent').save(event);
75
+ if (event.numberOfStudents>0) {
76
+ await EvaluationEvent.generateTokens(context, generateOptions);
77
+ }
78
+ generateAction.total = action.numberOfStudents;
79
+ generateAction.actionStatus = {alternateName: 'CompletedActionStatus'}
80
+ return await context.model('GenerateInstructorTokenAction').save(generateAction);
81
+ }
82
+ catch (err) {
83
+ TraceUtils.error(err);
84
+ // update also action status
85
+ generateAction.total = 0;
86
+ generateAction.actionStatus = {alternateName: 'FailedActionStatus'}
87
+ generateAction.description = err.message;
88
+ return await context.model('GenerateInstructorTokenAction').save(generateAction);
89
+
90
+ }
91
+
92
+ }
93
+ }
94
+ }
95
+ module.exports = ClassInstructorEvaluationEvent;
@@ -66,7 +66,7 @@ class EvaluationDocument extends DataObject {
66
66
  return reject(new Error('The specified file type is not supported by the system.'))
67
67
  }
68
68
  const addAttributes = {};
69
- _.forEach(context.model('EvaluationDocument').attributeNames, (attribute) => {
69
+ _.forEach(context.model(file.additionalType || 'EvaluationDocument').attributeNames, (attribute) => {
70
70
  if (Object.prototype.hasOwnProperty.call(file, attribute)) {
71
71
  addAttributes[attribute] = file[attribute];
72
72
  }
@@ -80,7 +80,7 @@ class EvaluationDocument extends DataObject {
80
80
  } else {
81
81
  filePath = file.path;
82
82
  }
83
- newAttachment.additionalType = 'EvaluationDocument';
83
+ newAttachment.additionalType = file.additionalType || 'EvaluationDocument';
84
84
  svc.copyFrom(context, filePath, newAttachment, function (err) {
85
85
  if (err) {
86
86
  TraceUtils.error(err);
@@ -20,11 +20,11 @@ const EvaluationAccessToken = require('./evaluation-access-token-model');
20
20
  import {lt} from 'semver';
21
21
  import util from "util";
22
22
  import fs from "fs";
23
+ import moment from "moment";
24
+ const math = require('mathjs');
25
+
23
26
  /**
24
27
  * @class
25
-
26
- * @property {string} id
27
- * @augments {DataObject}
28
28
  */
29
29
  @EdmMapping.entityType('EvaluationEvent')
30
30
  class EvaluationEvent extends DataObject {
@@ -37,7 +37,7 @@ class EvaluationEvent extends DataObject {
37
37
 
38
38
  /**
39
39
  * Get current evaluation based on the evaluation key contained in Evaluation-Key HTTP header
40
- * @param {HttpContext} context
40
+ * @param {HttpContext} context
41
41
  */
42
42
  @EdmMapping.func('Current', 'EvaluationEvent')
43
43
  static async getCurrent(context) {
@@ -47,7 +47,15 @@ class EvaluationEvent extends DataObject {
47
47
  throw new HttpTokenExpiredError('Evaluation access token has been expired or is invalid.');
48
48
  }
49
49
  // get evaluation event
50
- const evaluationEvent = await context.model('EvaluationEvent').find(token.sub).silent().getTypedItem();
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
+ const evaluationEvent = await context.model('EvaluationEvent')
54
+ .where('id').equal(token.sub)
55
+ .and('date(startDate)').lowerOrEqual(startDate)
56
+ .and('date(endDate)').greaterOrEqual(endDate)
57
+ .and('eventStatus/alternateName').equal('EventOpened')
58
+ .silent().getTypedItem();
51
59
  if (evaluationEvent == null) {
52
60
  throw new HttpNotFoundError('The specified evaluation cannot be found');
53
61
  }
@@ -174,26 +182,60 @@ class EvaluationEvent extends DataObject {
174
182
 
175
183
  @EdmMapping.func('FormAttributes', EdmType.CollectionOf('Object'))
176
184
  async getFormAttributes() {
185
+ /**
186
+ * @type {DataContext|*}
187
+ */
188
+ const context = this.context;
177
189
  const result = [];
178
190
  const form = await this.getForm();
191
+ this.setParentLegend(form);
179
192
  // get model attributes
180
193
  /**
181
194
  * @type {DataConfigurationStrategy|*}
182
195
  */
183
- const dataConfiguration = this.context.getApplication()
196
+ const dataConfiguration = context.getApplication()
184
197
  .getConfiguration().getStrategy(DataConfigurationStrategy);
185
-
198
+ const dataTypes = dataConfiguration.dataTypes;
199
+ const q = context.model(form.properties.name).asQueryable();
200
+ const selectArguments = [];
186
201
  const model = dataConfiguration.model(form.properties.name);
187
202
  const attributes = model.fields;
188
203
  attributes.forEach(attribute => {
189
204
  const key = this.findByKey(form.components, attribute.name);
190
205
  if (key) {
191
- attribute = Object.assign(attribute,{
206
+ attribute = Object.assign(attribute, {
192
207
  name: attribute.name, label: key.label
193
208
  });
209
+ attribute.isNumber = false;
210
+ attribute.legend = key.customProperties && key.customProperties.legend;
211
+ if (Object.prototype.hasOwnProperty.call(dataTypes, attribute.type)) {
212
+ attribute.isNumber = dataTypes[attribute.type].type === 'number';
213
+ }
214
+ selectArguments.push(`${attribute.name}`);
194
215
  result.push(attribute);
195
216
  }
217
+
196
218
  });
219
+ if (selectArguments.length) {
220
+ const responses = await q.select.apply(q, selectArguments).where('evaluationEvent').equal(this.getId()).silent().getAllItems();
221
+ for (let i = 0; i < result.length; i++) {
222
+ const qElement = result[i];
223
+ qElement.responses=[];
224
+ if (qElement.isNumber) {
225
+ // add mean value and standard deviation
226
+ qElement.std = responses.length? math.std(responses.map(x => {
227
+ return x[qElement.name];
228
+ })):0;
229
+ qElement.avg = responses.length? math.mean(responses.map(x => {
230
+ return x[qElement.name];
231
+ })):0;
232
+ } else {
233
+ qElement.responses = responses.map(x => {
234
+ return x[qElement.name];
235
+ });
236
+ }
237
+ }
238
+ }
197
239
  return result;
198
240
  }
199
241
 
@@ -213,6 +255,18 @@ class EvaluationEvent extends DataObject {
213
255
  }
214
256
  }
215
257
 
258
+ setParentLegend(component) {
259
+ if (Array.isArray(component.components)) {
260
+ component.components.forEach(item => {
261
+ if (component.legend) {
262
+ item.customProperties = item.customProperties || {};
263
+ item.customProperties.legend = component.legend;
264
+ }
265
+ this.setParentLegend(item);
266
+ });
267
+ }
268
+ }
269
+
216
270
  @EdmMapping.param('data', 'Object', false, true)
217
271
  @EdmMapping.action('Evaluate', 'Object')
218
272
  async evaluate(data) {
@@ -251,7 +305,8 @@ class EvaluationEvent extends DataObject {
251
305
  }
252
306
  return context.model(EvaluationAccessToken).silent().save({
253
307
  access_token: token.access_token,
254
- expires: new Date(0) // set epoch date
308
+ expires: new Date(0), // set epoch date
309
+ used: true
255
310
  });
256
311
  }).then(() => {
257
312
  return callback();
@@ -263,13 +318,6 @@ class EvaluationEvent extends DataObject {
263
318
  return data;
264
319
  }
265
320
 
266
- /**
267
- * Generates a number of access tokens for the given event
268
- * @param {DataContext} context
269
- * @param {{evaluationEvent: number, n: number, expires?: DateTime}|*} generateOptions
270
- */
271
- @EdmMapping.param('data', 'Object', false, true)
272
- @EdmMapping.action('GenerateTokens', 'Object')
273
321
  static async generateTokens(context, generateOptions) {
274
322
  let items = [];
275
323
  if (Array.isArray(generateOptions)) {
@@ -300,7 +348,7 @@ class EvaluationEvent extends DataObject {
300
348
  Args.check(n > 0, 'The number of access tokens must be greater than zero');
301
349
  let maximumGenerateTokens = LangUtils.parseInt(context.getConfiguration().getSourceAt('settings/evaluation/maximumGenerateTokens'));
302
350
  if (maximumGenerateTokens === 0) {
303
- maximumGenerateTokens = 128;
351
+ maximumGenerateTokens = 1000;
304
352
  }
305
353
  Args.check(n < maximumGenerateTokens, `The specified number of tokens exceeded the maximum allowed (${maximumGenerateTokens})`);
306
354
  const items = [];