@pie-lib/controller-utils 0.18.1-next.0 → 0.19.0

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/esm/index.js ADDED
@@ -0,0 +1,129 @@
1
+ import get from 'lodash/get';
2
+ import shuffle from 'lodash/shuffle';
3
+ import isEmpty from 'lodash/isEmpty';
4
+
5
+ var enabled = (config, env, defaultValue) => {
6
+ // if model.partialScoring = false
7
+ // - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring
8
+ // else if model.partialScoring = true || undefined
9
+ // - if env.partialScoring = false, use dichotomous scoring
10
+ // - else if env.partialScoring = true, use partial scoring
11
+ config = config || {};
12
+ env = env || {};
13
+
14
+ if (config.partialScoring === false) {
15
+ return false;
16
+ }
17
+
18
+ if (env.partialScoring === false) {
19
+ return false;
20
+ }
21
+
22
+ return typeof defaultValue === 'boolean' ? defaultValue : true;
23
+ };
24
+
25
+ var partialScoring = /*#__PURE__*/Object.freeze({
26
+ __proto__: null,
27
+ enabled: enabled
28
+ });
29
+
30
+ var lg = n => console[n].bind(console, 'controller-utils:');
31
+
32
+ var debug = lg('debug');
33
+ var log = lg('log');
34
+ var warn = lg('warn');
35
+ var error = lg('error');
36
+ var compact = arr => {
37
+ if (Array.isArray(arr)) {
38
+ return arr.filter(v => v !== null && v !== undefined);
39
+ }
40
+
41
+ return arr;
42
+ };
43
+ var getShuffledChoices = (choices, session, updateSession, choiceKey) => new Promise(resolve => {
44
+ var _session$data;
45
+
46
+ log('updateSession type: ', typeof updateSession);
47
+ log('session: ', session);
48
+ var currentShuffled = compact((session === null || session === void 0 ? void 0 : (_session$data = session.data) === null || _session$data === void 0 ? void 0 : _session$data.shuffledValues) || (session === null || session === void 0 ? void 0 : session.shuffledValues) || []);
49
+
50
+ if (!session) {
51
+ // eslint-disable-next-line quotes
52
+ warn("unable to save shuffled choices because there's no session.");
53
+ resolve(undefined);
54
+ } else if (!isEmpty(currentShuffled)) {
55
+ var _session$data2;
56
+
57
+ debug('use shuffledValues to sort the choices...', (_session$data2 = session.data) === null || _session$data2 === void 0 ? void 0 : _session$data2.shuffledValues);
58
+ resolve(compact(currentShuffled.map(v => choices.find(c => c[choiceKey] === v))));
59
+ } else {
60
+ var shuffledChoices = shuffle(choices);
61
+
62
+ if (updateSession && typeof updateSession === 'function') {
63
+ try {
64
+ //Note: session.id refers to the id of the element within a session
65
+ var shuffledValues = compact(shuffledChoices.map(c => c[choiceKey]));
66
+ log('try to save shuffledValues to session...', shuffledValues);
67
+ log('call updateSession... ', session.id, session.element);
68
+
69
+ if (isEmpty(shuffledValues)) {
70
+ error("shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ".concat(JSON.stringify(shuffledChoices), ", key: ").concat(choiceKey));
71
+ } else {
72
+ updateSession(session.id, session.element, {
73
+ shuffledValues
74
+ }).catch(e => // eslint-disable-next-line no-console
75
+ console.error('update session failed for: ', session.id, e));
76
+ }
77
+ } catch (e) {
78
+ warn('unable to save shuffled order for choices');
79
+ error(e);
80
+ }
81
+ } else {
82
+ warn('unable to save shuffled choices, shuffle will happen every time.');
83
+ } //save this shuffle to the session for later retrieval
84
+
85
+
86
+ resolve(shuffledChoices);
87
+ }
88
+ });
89
+ /**
90
+ * If we return:
91
+ * - true - that means that the order of the choices will be ordinal (as is created in the configure item)
92
+ * - false - that means the getShuffledChoices above will be called and that in turn means that we either
93
+ * return the shuffled values on the session (if any exists) or we shuffle the choices
94
+ * @param model - model to check if we should lock order
95
+ * @param session - session to check if we should lock order
96
+ * @param env - env to check if we should lock order
97
+ * @returns {boolean}
98
+ */
99
+
100
+ var lockChoices = (model, session, env) => {
101
+ if (model.lockChoiceOrder) {
102
+ return true;
103
+ }
104
+
105
+ log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));
106
+
107
+ if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {
108
+ return true;
109
+ }
110
+
111
+ var role = get(env, 'role', 'student');
112
+
113
+ if (role === 'instructor') {
114
+ // TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then
115
+
116
+ /*const alreadyShuffled = hasShuffledValues(session);
117
+ if (alreadyShuffled) {
118
+ return false;
119
+ }
120
+ return true;*/
121
+ return true;
122
+ } // here it's a student, so don't lock and it will shuffle if needs be
123
+
124
+
125
+ return false;
126
+ };
127
+
128
+ export { getShuffledChoices, lockChoices, partialScoring };
129
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/partial-scoring.js","../src/persistence.js"],"sourcesContent":["export const enabled = (config, env, defaultValue) => {\n // if model.partialScoring = false\n // - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring\n // else if model.partialScoring = true || undefined\n // - if env.partialScoring = false, use dichotomous scoring\n // - else if env.partialScoring = true, use partial scoring\n config = config || {};\n env = env || {};\n\n if (config.partialScoring === false) {\n return false;\n }\n\n if (env.partialScoring === false) {\n return false;\n }\n\n return typeof defaultValue === 'boolean' ? defaultValue : true;\n};\n","import get from 'lodash/get';\nimport shuffle from 'lodash/shuffle';\nimport isEmpty from 'lodash/isEmpty';\n\n// eslint-disable-next-line no-console\nconst lg = (n) => console[n].bind(console, 'controller-utils:');\nconst debug = lg('debug');\nconst log = lg('log');\nconst warn = lg('warn');\nconst error = lg('error');\n\nexport const compact = (arr) => {\n if (Array.isArray(arr)) {\n return arr.filter((v) => v !== null && v !== undefined);\n }\n return arr;\n};\n\nexport const getShuffledChoices = (choices, session, updateSession, choiceKey) =>\n new Promise((resolve) => {\n log('updateSession type: ', typeof updateSession);\n log('session: ', session);\n\n const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);\n\n if (!session) {\n // eslint-disable-next-line quotes\n warn(\"unable to save shuffled choices because there's no session.\");\n resolve(undefined);\n } else if (!isEmpty(currentShuffled)) {\n debug('use shuffledValues to sort the choices...', session.data?.shuffledValues);\n resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));\n } else {\n const shuffledChoices = shuffle(choices);\n\n if (updateSession && typeof updateSession === 'function') {\n try {\n //Note: session.id refers to the id of the element within a session\n const shuffledValues = compact(shuffledChoices.map((c) => c[choiceKey]));\n log('try to save shuffledValues to session...', shuffledValues);\n log('call updateSession... ', session.id, session.element);\n if (isEmpty(shuffledValues)) {\n error(\n `shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(\n shuffledChoices,\n )}, key: ${choiceKey}`,\n );\n } else {\n updateSession(session.id, session.element, { shuffledValues }).catch((e) =>\n // eslint-disable-next-line no-console\n console.error('update session failed for: ', session.id, e),\n );\n }\n } catch (e) {\n warn('unable to save shuffled order for choices');\n error(e);\n }\n } else {\n warn('unable to save shuffled choices, shuffle will happen every time.');\n }\n //save this shuffle to the session for later retrieval\n resolve(shuffledChoices);\n }\n });\n\n/**\n * If we return:\n * - true - that means that the order of the choices will be ordinal (as is created in the configure item)\n * - false - that means the getShuffledChoices above will be called and that in turn means that we either\n * return the shuffled values on the session (if any exists) or we shuffle the choices\n * @param model - model to check if we should lock order\n * @param session - session to check if we should lock order\n * @param env - env to check if we should lock order\n * @returns {boolean}\n */\nexport const lockChoices = (model, session, env) => {\n if (model.lockChoiceOrder) {\n return true;\n }\n\n log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));\n\n if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {\n return true;\n }\n\n const role = get(env, 'role', 'student');\n\n if (role === 'instructor') {\n // TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then\n /*const alreadyShuffled = hasShuffledValues(session);\n\n if (alreadyShuffled) {\n return false;\n }\n\n return true;*/\n return true;\n }\n\n // here it's a student, so don't lock and it will shuffle if needs be\n return false;\n};\n"],"names":["enabled","config","env","defaultValue","partialScoring","lg","n","console","bind","debug","log","warn","error","compact","arr","Array","isArray","filter","v","undefined","getShuffledChoices","choices","session","updateSession","choiceKey","Promise","resolve","currentShuffled","data","shuffledValues","isEmpty","map","find","c","shuffledChoices","shuffle","id","element","JSON","stringify","catch","e","lockChoices","model","lockChoiceOrder","get","role"],"mappings":";;;;AAAO,IAAMA,OAAO,GAAG,CAACC,MAAD,EAASC,GAAT,EAAcC,YAAd,KAA+B;AACpD;AACA;AACA;AACA;AACA;AACAF,EAAAA,MAAM,GAAGA,MAAM,IAAI,EAAnB;AACAC,EAAAA,GAAG,GAAGA,GAAG,IAAI,EAAb;;AAEA,EAAA,IAAID,MAAM,CAACG,cAAP,KAA0B,KAA9B,EAAqC;AACnC,IAAA,OAAO,KAAP;AACD,EAAA;;AAED,EAAA,IAAIF,GAAG,CAACE,cAAJ,KAAuB,KAA3B,EAAkC;AAChC,IAAA,OAAO,KAAP;AACD,EAAA;;AAED,EAAA,OAAO,OAAOD,YAAP,KAAwB,SAAxB,GAAoCA,YAApC,GAAmD,IAA1D;AACD,CAlBM;;;;;;;ACKP,IAAME,EAAE,GAAIC,CAAD,IAAOC,OAAO,CAACD,CAAD,CAAP,CAAWE,IAAX,CAAgBD,OAAhB,EAAyB,mBAAzB,CAAlB;;AACA,IAAME,KAAK,GAAGJ,EAAE,CAAC,OAAD,CAAhB;AACA,IAAMK,GAAG,GAAGL,EAAE,CAAC,KAAD,CAAd;AACA,IAAMM,IAAI,GAAGN,EAAE,CAAC,MAAD,CAAf;AACA,IAAMO,KAAK,GAAGP,EAAE,CAAC,OAAD,CAAhB;AAEO,IAAMQ,OAAO,GAAIC,GAAD,IAAS;AAC9B,EAAA,IAAIC,KAAK,CAACC,OAAN,CAAcF,GAAd,CAAJ,EAAwB;AACtB,IAAA,OAAOA,GAAG,CAACG,MAAJ,CAAYC,CAAD,IAAOA,CAAC,KAAK,IAAN,IAAcA,CAAC,KAAKC,SAAtC,CAAP;AACD,EAAA;;AACD,EAAA,OAAOL,GAAP;AACD,CALM;IAOMM,kBAAkB,GAAG,CAACC,OAAD,EAAUC,OAAV,EAAmBC,aAAnB,EAAkCC,SAAlC,KAChC,IAAIC,OAAJ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P;AACD,EAAA;;AAEDlC,EAAAA,GAAG,CAAC,mBAAD,EAAsBmC,GAAG,CAAC3C,GAAD,EAAM,CAAC,cAAD,EAAiB,iBAAjB,CAAN,EAA2C,KAA3C,CAAzB,CAAH;;AAEA,EAAA,IAAI2C,GAAG,CAAC3C,GAAD,EAAM,CAAC,cAAD,EAAiB,iBAAjB,CAAN,EAA2C,KAA3C,CAAP,EAA0D;AACxD,IAAA,OAAO,IAAP;AACD,EAAA;;AAED,EAAA,IAAM4C,IAAI,GAAGD,GAAG,CAAC3C,GAAD,EAAM,MAAN,EAAc,SAAd,CAAhB;;AAEA,EAAA,IAAI4C,IAAI,KAAK,YAAb,EAA2B;AACzB;;AACA;AACJ;AACA;AACA;AACA;AAGI,IAAA,OAAO,IAAP;AACD,EAAA,CAvBiD;;;AA0BlD,EAAA,OAAO,KAAP;AACD;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/controller-utils",
3
- "version": "0.18.1-next.0+a5d1550f",
3
+ "version": "0.19.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "module": "src/index.js",
@@ -14,5 +14,12 @@
14
14
  "scripts": {},
15
15
  "author": "",
16
16
  "license": "ISC",
17
- "gitHead": "a5d1550faec7e27c8824e5aa4b4ef29ad4ee525a"
17
+ "gitHead": "d6cccc4cca72fde471ababc7dce4c6236f5cbc0a",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./esm/index.js",
21
+ "require": "./lib/index.js",
22
+ "default": "./esm/index.js"
23
+ }
24
+ }
18
25
  }