-
Notifications
You must be signed in to change notification settings - Fork 5
/
HomeParticipant.vue
452 lines (390 loc) · 14.6 KB
/
HomeParticipant.vue
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<template>
<div>
<div v-if="isFetching" class="loading-container py-8">
<AppSpinner style="margin-bottom: 1rem" />
<span>{{ $t('homeParticipant.loadingAssignments') }}</span>
</div>
<div v-else>
<div v-if="!hasAssignments">
<div class="col-full text-center py-8">
<h1>{{ $t('homeParticipant.noAssignments') }}</h1>
<p class="text-center">{{ $t('homeParticipant.contactAdministrator') }}</p>
<PvButton
:label="$t('navBar.signOut')"
class="no-underline bg-primary border-none border-round p-2 text-white hover:bg-red-900"
icon="pi pi-sign-out"
@click="signOut"
/>
</div>
</div>
<div v-else>
<h2 v-if="userAdministrations?.length == 1" class="p-float-label dropdown-container">
{{ userAdministrations.at(0).publicName || userAdministrations.at(0).name }}
</h2>
<div class="flex flex-row-reverse align-items-end gap-2 justify-content-between">
<div
v-if="optionalAssessments.length !== 0"
class="switch-container flex flex-row align-items-center justify-content-end mr-6 gap-2"
>
<PvInputSwitch
v-model="showOptionalAssessments"
input-id="switch-optional"
data-cy="switch-show-optional-assessments"
/>
<label for="switch-optional" class="mr-2 text-gray-500">{{
$t('homeParticipant.showOptionalAssignments')
}}</label>
</div>
<div
v-if="userAdministrations?.length > 0"
class="flex flex-row justify-center align-items-center p-float-label dropdown-container gap-4 w-full"
>
<div class="assignment-select-container flex flex-row justify-content-between justify-content-start">
<div class="flex flex-column align-content-start justify-content-start w-3">
<PvDropdown
v-if="userAdministrations.every((administration) => administration.publicName)"
v-model="selectedAdmin"
:options="sortedUserAdministrations ?? []"
option-label="publicName"
input-id="dd-assignment"
data-cy="dropdown-select-administration"
@change="toggleShowOptionalAssessments"
/>
<PvDropdown
v-else
v-model="selectedAdmin"
:options="sortedUserAdministrations ?? []"
option-label="name"
input-id="dd-assignment"
data-cy="dropdown-select-administration"
@change="toggleShowOptionalAssessments"
/>
<label for="dd-assignment">{{ $t('homeParticipant.selectAssignment') }}</label>
</div>
</div>
</div>
</div>
<div class="tabs-container">
<ParticipantSidebar :total-games="totalGames" :completed-games="completeGames" :student-info="studentInfo" />
<Transition name="fade" mode="out-in">
<GameTabs
v-if="showOptionalAssessments && userData"
:games="optionalAssessments"
:sequential="isSequential"
:user-data="userData"
/>
<GameTabs
v-else-if="requiredAssessments && userData"
:games="requiredAssessments"
:sequential="isSequential"
:user-data="userData"
/>
</Transition>
</div>
</div>
</div>
</div>
<ConsentModal
v-if="showConsent && !isLevante"
:consent-text="confirmText"
:consent-type="consentType"
:on-confirm="updateConsent"
/>
</template>
<script setup>
import { onMounted, ref, watch, computed } from 'vue';
import _filter from 'lodash/filter';
import _get from 'lodash/get';
import _find from 'lodash/find';
import _without from 'lodash/without';
import _isEmpty from 'lodash/isEmpty';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import { storeToRefs } from 'pinia';
import useUserDataQuery from '@/composables/queries/useUserDataQuery';
import useUserAssignmentsQuery from '@/composables/queries/useUserAssignmentsQuery';
import useAdministrationsQuery from '@/composables/queries/useAdministrationsQuery';
import useTasksQuery from '@/composables/queries/useTasksQuery';
import useSurveyReponsesQuery from '@/composables/queries/useSurveyResponsesQuery';
import useUpdateConsentMutation from '@/composables/mutations/useUpdateConsentMutation';
import useSignOutMutation from '@/composables/mutations/useSignOutMutation';
import ConsentModal from '@/components/ConsentModal.vue';
import GameTabs from '@/components/GameTabs.vue';
import ParticipantSidebar from '@/components/ParticipantSidebar.vue';
const showConsent = ref(false);
const consentVersion = ref('');
const confirmText = ref('');
const consentType = ref('');
const consentParams = ref({});
const isLevante = import.meta.env.MODE === 'LEVANTE';
const { mutateAsync: updateConsentStatus } = useUpdateConsentMutation();
const { mutate: signOut } = useSignOutMutation();
let unsubscribe;
const initialized = ref(false);
const init = () => {
if (unsubscribe) unsubscribe();
initialized.value = true;
};
const authStore = useAuthStore();
const { roarfirekit, showOptionalAssessments } = storeToRefs(authStore);
unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
onMounted(async () => {
if (roarfirekit.value.restConfig) init();
});
const gameStore = useGameStore();
const { selectedAdmin } = storeToRefs(gameStore);
const {
isLoading: isLoadingUserData,
isFetching: isFetchingUserData,
data: userData,
} = useUserDataQuery(null, {
enabled: initialized,
});
const {
isLoading: isLoadingAssignments,
isFetching: isFetchingAssignments,
data: userAssignments,
} = useUserAssignmentsQuery({
enabled: initialized,
});
const administrationIds = computed(() => (userAssignments.value ?? []).map((assignment) => assignment.id));
const administrationQueryEnabled = computed(() => !isLoadingAssignments.value && !_isEmpty(administrationIds.value));
const {
isLoading: isLoadingAdmins,
isFetching: isFetchingAdmins,
data: userAdministrations,
} = useAdministrationsQuery(administrationIds, {
enabled: administrationQueryEnabled,
});
const sortedUserAdministrations = computed(() => {
return [...(userAdministrations.value ?? [])].sort((a, b) => a.name.localeCompare(b.name));
});
const taskIds = computed(() => (selectedAdmin.value?.assessments ?? []).map((assessment) => assessment.taskId));
const tasksQueryEnabled = computed(() => !isLoadingAssignments.value && !_isEmpty(taskIds.value));
const {
isLoading: isLoadingTasks,
isFetching: isFetchingTasks,
data: userTasks,
} = useTasksQuery(false, taskIds, {
enabled: tasksQueryEnabled,
});
const { data: surveyResponsesData } = useSurveyReponsesQuery({
enabled: initialized.value && isLevante,
});
const isLoading = computed(() => {
return isLoadingUserData.value || isLoadingAssignments.value || isLoadingAdmins.value || isLoadingTasks.value;
});
const isFetching = computed(() => {
return isFetchingUserData.value || isFetchingAssignments.value || isFetchingAdmins.value || isFetchingTasks.value;
});
const hasAssignments = computed(() => {
if (isFetching.value || isLoading.value) return false;
return assessments.value.length !== 0;
});
async function checkConsent() {
const dob = new Date(userData.value?.studentData?.dob);
const grade = userData.value?.studentData?.grade;
const currentDate = new Date();
const age = currentDate.getFullYear() - dob.getFullYear();
const legal = selectedAdmin.value?.legal;
if (!legal?.consent) {
// Always show consent form for this test student when running Cypress tests
// @TODO: Remove this once we update the E2E tests to handle the consent form without persisting state. This would
// improve the test relability as enforcing the below condition defeats parts of the test purpose.
if (userData.value?.id === 'O75V6IcVeiTwW8TRjXb76uydlwV2') {
consentType.value = 'consent';
confirmText.value = 'This is a test student. Please do not accept this form.';
showConsent.value = true;
}
return;
}
const isAdult = age >= 18;
const isSeniorGrade = grade >= 12;
const isOlder = isAdult || isSeniorGrade;
let docTypeKey = isOlder ? 'consent' : 'assent';
let docType = legal[docTypeKey][0]?.type.toLowerCase();
let docAmount = legal?.amount;
let docExpectedTime = legal?.expectedTime;
consentType.value = docType;
const consentStatus = userData.value?.legal?.[consentType.value];
const consentDoc = await authStore.getLegalDoc(docType);
consentVersion.value = consentDoc.version;
if (consentStatus?.[consentDoc.version]) {
const legalDocs = consentStatus?.[consentDoc.version];
let found = false;
let signedBeforeAugFirst = false;
const augustFirstThisYear = new Date(currentDate.getFullYear(), 7, 1); // August 1st of the current year
for (const document of legalDocs) {
const signedDate = new Date(document.dateSigned);
if (document.amount === docAmount && document.expectedTime === docExpectedTime) {
found = true;
if (signedDate < augustFirstThisYear && currentDate >= augustFirstThisYear) {
signedBeforeAugFirst = true;
break;
}
}
if (isNaN(new Date(document.dateSigned)) && currentDate >= augustFirstThisYear) {
signedBeforeAugFirst = true;
break;
}
}
// If any document is signed after August 1st, do not show the consent form
if (!found || signedBeforeAugFirst) {
if (docAmount !== '' || docExpectedTime !== '' || signedBeforeAugFirst) {
confirmText.value = consentDoc.text;
showConsent.value = true;
return;
}
}
} else if (age > 7 || grade > 1) {
confirmText.value = consentDoc.text;
showConsent.value = true;
return;
}
}
async function updateConsent() {
consentParams.value = {
amount: selectedAdmin.value?.legal.amount,
expectedTime: selectedAdmin.value?.legal.expectedTime,
dateSigned: new Date(),
};
await updateConsentStatus({
consentType,
consentVersion,
consentParams,
});
}
const toggleShowOptionalAssessments = async () => {
await checkConsent();
showOptionalAssessments.value = null;
};
// Assessments to populate the game tabs.
// Generated based on the current selected administration Id
const assessments = computed(() => {
if (!isFetching.value && selectedAdmin.value && (userTasks.value ?? []).length > 0) {
const fetchedAssessments = _without(
selectedAdmin.value.assessments.map((assessment) => {
// Get the matching assessment from userAssignments
const matchingAssignment = _find(userAssignments.value, { id: selectedAdmin.value.id });
const matchingAssessments = matchingAssignment?.assessments ?? [];
const matchingAssessment = _find(matchingAssessments, { taskId: assessment.taskId });
// If no matching assessments were found, then this assessment is not assigned to the user.
// It is in the administration but the user does not meet the conditional requirements for assignment.
// Return undefined, which will be filtered out using lodash _without above.
if (!matchingAssessment) return undefined;
const optionalAssessment = _find(matchingAssessments, { taskId: assessment.taskId, optional: true });
const combinedAssessment = {
...matchingAssessment,
...optionalAssessment,
...assessment,
taskData: {
..._find(userTasks.value ?? [], { id: assessment.taskId }),
variantURL: assessment?.params?.variantURL,
},
};
return combinedAssessment;
}),
undefined,
);
if (authStore.userData?.userType === 'student' && isLevante) {
// This is just to mark the card as complete
if (gameStore.isSurveyCompleted || surveyResponsesData.value?.length) {
fetchedAssessments.forEach((assessment) => {
if (assessment.taskId === 'Survey') {
assessment.completedOn = new Date();
}
});
}
}
return fetchedAssessments;
}
return [];
});
const requiredAssessments = computed(() => {
return _filter(assessments.value, (assessment) => !assessment.optional);
});
const optionalAssessments = computed(() => {
return _filter(assessments.value, (assessment) => assessment.optional);
});
// Grab the sequential key from the current administration's data object
const isSequential = computed(() => {
return (
_get(
_find(userAdministrations.value, (administration) => {
return administration.id === selectedAdmin.value.id;
}),
'sequential',
) ?? true
);
});
// Total games completed from the current list of assessments
let totalGames = computed(() => {
return requiredAssessments.value.length ?? 0;
});
// Total games included in the current assessment
let completeGames = computed(() => {
return _filter(requiredAssessments.value, (task) => task.completedOn).length ?? 0;
});
// Set up studentInfo for sidebar
const studentInfo = computed(() => {
if (isLevante) return null;
return {
grade: userData.value?.studentData?.grade,
};
});
watch(
[userData, selectedAdmin, userAdministrations],
async ([updatedUserData, updatedSelectedAdmin]) => {
if (!_isEmpty(updatedUserData) && updatedSelectedAdmin) {
await checkConsent();
}
const selectedAdminId = selectedAdmin.value?.id;
const allAdminIds = (userAdministrations.value ?? []).map((administration) => administration.id);
// If there is no selected administration or if the selected administration is not in the list
// of all administrations choose the first one after sorting alphabetically by publicName
if (allAdminIds.length > 0 && (!selectedAdminId || !allAdminIds.includes(selectedAdminId))) {
// Choose the first sorted administration
selectedAdmin.value = sortedUserAdministrations.value[0];
}
},
{ immediate: true },
);
</script>
<style scoped>
.tabs-container {
display: flex;
flex-direction: row;
max-width: 100vw;
padding: 2rem;
gap: 2rem;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
.dropdown-container {
margin-top: 2rem;
margin-left: 2rem;
}
.assignment-select-container {
min-width: 100%;
}
.switch-container {
min-width: 24%;
}
@media screen and (max-width: 1100px) {
.tabs-container {
flex-direction: row;
}
}
.loading-container {
width: 100%;
text-align: center;
}
</style>