Newer
Older
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
<script lang="ts">
import DateInput from '$lib/components/utils/dateInput.svelte';
import Draggable from './Draggable.svelte';
import autosize from 'svelte-autosize';
import { toastWarning, toastAlert } from '$lib/utils/toasts';
import { getUserByEmailAPI } from '$lib/api/users';
import { Icon, MagnifyingGlass } from 'svelte-hero-icons';
import { t } from '$lib/services/i18n';
import User from '$lib/types/user';
import type Study from '$lib/types/study';
import { onMount } from 'svelte';
import { TestTask, TestTyping, type Test } from '$lib/types/tests';
import type SurveyTypingSvelte from '$lib/types/surveyTyping.svelte';
let {
study = $bindable(),
possibleTests,
mode,
data,
form
}: {
study: Study | null;
possibleTests: (Test | SurveyTypingSvelte)[];
mode: string; //"create" or "edit"
data: any;
form: any;
} = $props();
let hasToLoggin: boolean = $state(false);
let title = study ? study.title : '';
let description = study ? study.description : '';
let startDate = study ? study.startDate : new Date();
let endDate = study ? study.endDate : new Date();
let nbSession = study ? study.nbSession : 8;
let tests = $state(study ? [...study.tests] : []);
let consentParticipation =
form?.consentParticipation ??
(study ? study.consentParticipation : $t('studies.consentParticipation'));
let consentPrivacy =
form?.consentPrivacy ?? (study ? study.consentPrivacy : $t('studies.consentPrivacy'));
let consentRights =
form?.consentRights ?? (study ? study.consentRights : $t('studies.consentRights'));
let studyOrganisation = study ? study.studyOrganisation : '';
let studyAddress = form?.studyAddress ?? (study ? study.studyAddress : $t('studies.addressD'));
let studyContact = study ? study.studyContact : '';
let studyPIemail = study ? study.studyPIemail : '';
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
let newUsername: string = $state('');
let newUserModal = $state(false);
let selectedTest: Test | undefined = $state();
let users: User[] = $state(study?.users ?? []);
/**
* Triggers the autosize update for all textarea elements in the document, to adjust the textarea
* size based on the initial content.
*/
function triggerAutosize() {
const textareas = document.querySelectorAll('textarea');
textareas.forEach((textarea) => {
autosize.update(textarea);
});
}
onMount(() => {
triggerAutosize();
});
/**
* Opens the participant search dialog to allow adding a new user.
*/
function addUser() {
newUserModal = true;
}
/**
* Add the new participant to the user list if the email given is properly formated and is found
* in the database.
* If successful close the dialog to search participant.
* @async
*/
async function searchUser() {
if (!newUsername || !newUsername.includes('@')) {
toastWarning($t('studies.invalidEmail'));
return;
}
const userData = await getUserByEmailAPI(fetch, newUsername);
if (!userData) {
toastWarning($t('studies.userNotFound'));
return;
}
const user = User.parse(userData);
if (!user) {
toastAlert($t('studies.userNotFound'));
return;
}
users = [...users, user];
newUsername = '';
newUserModal = false;
}
/**
* Remove the user from the list of users.
* @param user the user to be removed
*/
function removeUser(user: User) {
users = users.filter((u) => u.id !== user.id);
}
/**
* Deletes the current study from the database and redirects to the admin studies page.
* @async
*/
async function deleteStudy() {
if (!study) return;
await study?.delete();
window.location.href = '/admin/studies';
}
</script>
<div class="mx-auto w-full max-w-5xl px-4">
<h2 class="text-xl font-bold m-5 text-center">
{$t(mode === 'create' ? 'studies.createTitle' : 'studies.editTitle')}
</h2>
<!-- if error message to display -->
{#if form?.message}
<div class="alert alert-error mb-4">
{form.message}
</div>
{/if}
<form method="post">
<!-- Title & description -->
<label class="label" for="title">{$t('utils.words.title')} *</label>
<input class="input w-full" type="text" id="title" name="title" required bind:value={title} />
<label class="label" for="description">{$t('studies.studyDescription')}</label>
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
<textarea
use:autosize
class="input w-full max-h-52"
id="description"
name="description"
bind:value={description}
></textarea>
<!-- Dates -->
<label class="label" for="startDate">{$t('studies.startDate')} *</label>
<DateInput class="input w-full" id="startDate" name="startDate" date={startDate} required />
<label class="label" for="endDate">{$t('studies.endDate')} *</label>
<DateInput class="input w-full" id="endDate" name="endDate" date={endDate} required />
<!-- number of Sessions -->
<label class="label" for="nbSession">{$t('studies.nbSession')} *</label>
<input
class="input w-full"
type="number"
id="nbSession"
name="nbSession"
min="0"
bind:value={nbSession}
required
/>
<!-- Tests Section -->
<h3 class="py-2 px-1 capitalize">{$t('utils.words.tests')}</h3>
<Draggable bind:items={tests} name="tests[]" />
<div class="flex">
<select class="select select-bordered flex-grow" bind:value={selectedTest}>
<optgroup label={$t('tests.taskTests')}>
{#each possibleTests as test}
{#if test instanceof TestTask}
<option value={test}
>{test.title} - {test.groups.length} groups - {test.numQuestions} questions</option
>
{/if}
{/each}
</optgroup>
<optgroup label={$t('tests.typingTests')}>
{#each possibleTests as test}
{#if test instanceof TestTyping}
<option value={test}>{test.title}</option>
{/if}
{/each}
</optgroup>
</select>
<button
class="ml-2 button"
onclick={(e) => {
e.preventDefault();
if (selectedTest === undefined) return;
tests = [...tests, selectedTest];
}}
>
+
</button>
</div>
<!-- Login Requirement -->
<div class="flex items-center mt-2">
<label class="label flex-grow" for="typingTest">{$t('studies.hasToLoggin')}*</label>
<input
type="checkbox"
class="checkbox checkbox-primary size-8"
id="typingTest"
bind:checked={hasToLoggin}
/>
</div>
<!-- Users Section -->
<label class="label" for="users">{$t('utils.words.users')}</label>
<table class="table">
<thead>
<tr>
<td>{$t('users.category')}</td>
<td>{$t('users.nickname')}</td>
<td>{$t('users.email')}</td>
<td></td>
</tr>
</thead>
<tbody>
{#each users ?? [] as user (user.id)}
<tr>
<td>{$t('users.type.' + user.type)}</td>
<td>
{user.nickname}
<input type="hidden" name="users[]" value={user.id} />
</td>
<td>{user.email}</td>
<td>
<button
type="button"
class="btn btn-sm btn-error text-white"
onclick={() => removeUser(user)}
>
{$t('button.remove')}
</button>
</td>
</tr>
{/each}
</tbody>
</table>
<button type="button" class="btn btn-primary block mx-auto mt-3" onclick={addUser}>
{$t('studies.addUserButton')}
</button>
<!-- Consent Section -->
<h3 class="py-2 px-1">{$t('register.consent.title')}</h3>
<label class="label text-sm" for="consentParticipation"
>{$t('register.consent.participation')} *</label
>
<textarea
use:autosize
class="input w-full max-h-52"
id="consentParticipation"
name="consentParticipation"
value={consentParticipation}
required
></textarea>
<label class="label text-sm" for="consentPrivacy">{$t('register.consent.privacy')} *</label>
<textarea
use:autosize
class="input w-full max-h-52"
id="consentPrivacy"
name="consentPrivacy"
value={consentPrivacy}
required
></textarea>
<label class="label text-sm" for="consentRights">{$t('register.consent.rights')} *</label>
<textarea
use:autosize
class="input w-full max-h-52"
id="consentRights"
name="consentRights"
value={consentRights}
required
></textarea>
<h3 class="py-2 px-1">{$t('register.consent.studyData.title')}</h3>
<label class="label text-sm" for="StudyOrganisation"
>{$t('utils.words.OrganisationUni')} *</label
>
<textarea
use:autosize
class="input w-full max-h-52"
id="StudyOrganisation"
name="StudyOrganisation"
value={studyOrganisation}
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
<label class="label text-sm" for="StudyAddress">{$t('utils.words.Address')} *</label>
<textarea
use:autosize
class="input w-full max-h-52"
id="StudyAddress"
name="StudyAddress"
value={studyAddress}
required
></textarea>
<label class="label text-sm" for="StudyContact">{$t('studies.contact')} *</label>
<textarea
use:autosize
class="input w-full max-h-52"
id="StudyContact"
name="StudyContact"
value={studyContact}
required
></textarea>
<label class="label text-sm" for="StudyPIemail">
{$t('studies.email')} *
</label>
<input
class="input w-full"
id="StudyPIemail"
name="StudyPIemail"
type="email"
bind:value={studyPIemail}
required
/>
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
<!-- submit, cancel and delete buttons -->
<div class="mt-4 mb-6">
<input type="hidden" name="studyId" value={study ? study.id : ''} />
<button type="submit" class="button">
{$t(mode === 'create' ? 'button.create' : 'button.update')}
</button>
<a class="btn btn-outline float-end ml-2" href="/admin/studies">
{$t('button.cancel')}
</a>
{#if mode === 'edit'}
<button
type="button"
class="btn btn-error btn-outline float-end"
onclick={() => confirm($t('studies.deleteConfirm')) && deleteStudy()}
>
{$t('button.delete')}
</button>
{/if}
</div>
</form>
</div>
<!-- Dialog for the research of participant to be added -->
<dialog class="modal bg-black bg-opacity-50" open={newUserModal}>
<div class="modal-box">
<h2 class="text-xl font-bold mb-4">{$t('studies.newUser')}</h2>
<div class="w-full flex">
<input
type="text"
placeholder={$t('utils.words.email')}
bind:value={newUsername}
class="input flex-grow mr-2"
onkeypress={(e) => e.key === 'Enter' && searchUser()}
/>
<button class="button w-16" onclick={searchUser}>
<Icon src={MagnifyingGlass} />
</button>
</div>
<button class="btn float-end mt-4" onclick={() => (newUserModal = false)}>Close</button>
</div>
</dialog>