-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpersistence.js
More file actions
93 lines (80 loc) · 3.32 KB
/
Copy pathpersistence.js
File metadata and controls
93 lines (80 loc) · 3.32 KB
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
import { get, isEmpty, shuffle } from 'lodash-es';
// eslint-disable-next-line no-console
const lg = (n) => console[n].bind(console, 'controller-utils:');
const debug = lg('debug');
const log = lg('log');
const warn = lg('warn');
const error = lg('error');
export const compact = (arr) => {
if (Array.isArray(arr)) {
return arr.filter((v) => v !== null && v !== undefined);
}
return arr;
};
export const getShuffledChoices = (choices, session, updateSession, choiceKey) =>
new Promise((resolve) => {
log('updateSession type: ', typeof updateSession);
log('session: ', session);
const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);
if (!session) {
// eslint-disable-next-line quotes
warn("unable to save shuffled choices because there's no session.");
resolve(undefined);
} else if (!isEmpty(currentShuffled)) {
debug('use shuffledValues to sort the choices...', currentShuffled);
resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));
} else {
const shuffledChoices = shuffle(choices);
if (updateSession && typeof updateSession === 'function') {
try {
//Note: session.id refers to the id of the element within a session
const shuffledValues = compact(shuffledChoices.map((c) => c[choiceKey]));
log('try to save shuffledValues to session...', shuffledValues);
log('call updateSession... ', session.id, session.element);
if (isEmpty(shuffledValues)) {
error(
`shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(
shuffledChoices,
)}, key: ${choiceKey}`,
);
} else {
updateSession(session.id, session.element, { shuffledValues }).catch((e) =>
// eslint-disable-next-line no-console
console.error('update session failed for: ', session.id, e),
);
}
} catch (e) {
warn('unable to save shuffled order for choices');
error(e);
}
} else {
warn('unable to save shuffled choices, shuffle will happen every time.');
}
//save this shuffle to the session for later retrieval
resolve(shuffledChoices);
}
});
/**
* If we return:
* - true - that means that the order of the choices will be ordinal (as is created in the configure item)
* - false - that means the getShuffledChoices above will be called and that in turn means that we either
* return the shuffled values on the session (if any exists) or we shuffle the choices
*
* Note: the role (student/instructor) is intentionally not considered here — instructor mode
* will respect the same `lockChoiceOrder` value as students, instead of forcing the order to be locked.
*
* @param model - model to check if we should lock order
* @param session - session to check if we should lock order
* @param env - env to check if we should lock order
* @returns {boolean}
*/
export const lockChoices = (model, session, env) => {
if (model.lockChoiceOrder) {
return true;
}
log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));
if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {
return true;
}
return false;
};