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
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
from typing import Optional
from sqlalchemy.orm import Session
import models
import schemas
import crud
from io import StringIO
import csv
from fastapi.responses import StreamingResponse
from utils import extract_text_between_angle_bracket
def create_study(db: Session, study: schemas.StudyCreate) -> models.Study:
db_study = models.Study(
**study.model_dump(exclude_unset=True, exclude={"user_ids", "test_ids"})
)
if study.model_fields_set & {"user_ids", "test_ids"}:
if db_study:
if "user_ids" in study.model_fields_set:
db_study.users = (
db.query(models.User)
.filter(models.User.id.in_(study.user_ids))
.all()
)
if "test_ids" in study.model_fields_set:
db_study.tests = (
db.query(models.Test)
.filter(models.Test.id.in_(study.test_ids))
.all()
)
db.add(db_study)
db.commit()
db.refresh(db_study)
return db_study
def get_study(db: Session, study_id: int) -> Optional[models.Study]:
return db.query(models.Study).filter(models.Study.id == study_id).first()
def get_studies(db: Session, skip: int = 0) -> list[models.Study]:
return db.query(models.Study).offset(skip).all()
def update_study(db: Session, study: schemas.StudyCreate, study_id: int) -> None:
db.query(models.Study).filter(models.Study.id == study_id).update(
{**study.model_dump(exclude_unset=True, exclude={"user_ids", "test_ids"})}
)
if study.model_fields_set & {"user_ids", "test_ids"}:
if (
study_obj := db.query(models.Study)
.filter(models.Study.id == study_id)
.first()
):
if "user_ids" in study.model_fields_set:
study_obj.users = (
db.query(models.User)
.filter(models.User.id.in_(study.user_ids))
.all()
)
if "test_ids" in study.model_fields_set:
study_obj.tests = (
db.query(models.Test)
.filter(models.Test.id.in_(study.test_ids))
.all()
)
db.commit()
def delete_study(db: Session, study_id: int) -> None:
db.query(models.Study).filter(models.Study.id == study_id).delete()
db.commit()
def download_study(db: Session, study_id: int):
output = StringIO()
writer = csv.writer(output)
header = [
"study_id",
"test_id",
"group_id",
"group_name",
"item_id",
"user_id",
"code",
"item_type",
"response",
"correct",
"response_time",
]
writer.writerow(header)
db_entries = (
db.query(models.TestEntry).filter(models.TestEntry.study_id == study_id).all()
)
for entry in db_entries:
if entry.entry_task is None:
continue
test_id = entry.test_id
group_id = entry.entry_task.test_group_id
group = crud.get_group(db, group_id).title
item = entry.entry_task.test_question_id
user_id = entry.user_id
code = entry.code
response_time = entry.entry_task.response_time
if entry.entry_task.entry_task_qcm:
selected_id = entry.entry_task.entry_task_qcm.selected_id
correct_id = entry.entry_task.test_question.question_qcm.correct
correct_answer = int(selected_id == correct_id)
item_type = "qcm"
row = [
study_id,
test_id,
group_id,
group,
item,
user_id,
code,
item_type,
selected_id,
correct_answer,
response_time,
]
writer.writerow(row)
if entry.entry_task.entry_task_gapfill:
answer = entry.entry_task.entry_task_gapfill.text
correct = extract_text_between_angle_bracket(
entry.entry_task.test_question.question
)
correct_answer = int(answer == correct)
item_type = "gapfill"
row = [
study_id,
test_id,
group_id,
group,
item,
user_id,
code,
item_type,
answer,
correct_answer,
response_time,
]
writer.writerow(row)
output.seek(0)
return StreamingResponse(
output,
media_type="text/csv",
headers={"Content-Disposition": f"attachment; filename={study_id}-surveys.csv"},
)
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
def download_study_wide(db: Session, study_id: int):
output = StringIO()
writer = csv.writer(output)
data = {}
question_ids = set()
db_entries = (
db.query(models.TestEntry).filter(models.TestEntry.study_id == study_id).all()
)
for entry in db_entries:
if entry.entry_task is None:
continue
user_id = entry.user_id
code = entry.code
item_id = entry.entry_task.test_question_id
key = (user_id, code)
if key not in data:
if user_id is not None:
user = crud.get_user(db, user_id)
data[key] = {
"study_id": study_id,
"user_id": user_id,
"code": code,
"home_language": user.home_language,
"target_language": user.target_language,
"gender": user.gender,
"birthdate": user.birthdate,
}
else:
data[key] = {"study_id": study_id, "user_id": user_id, "code": code}
if entry.entry_task.entry_task_qcm:
selected_id = entry.entry_task.entry_task_qcm.selected_id
correct_id = entry.entry_task.test_question.question_qcm.correct
correct_answer = int(selected_id == correct_id)
data[key][item_id] = correct_answer
question_ids.add(item_id)
if entry.entry_task.entry_task_gapfill:
answer = entry.entry_task.entry_task_gapfill.text
correct = extract_text_between_angle_bracket(
entry.entry_task.test_question.question
)
correct_answer = int(answer == correct)
data[key][item_id] = correct_answer
question_ids.add(item_id)
# Sort question IDs for consistent column order
question_ids = sorted(question_ids)
header = [
"study_id",
"user_id",
"code",
"home_language",
"target_language",
"gender",
"birthdate",
] + question_ids
writer.writerow(header)
for (user_id, code), values in data.items():
row = [values.get(col, "") for col in header]
writer.writerow(row)
output.seek(0)
return StreamingResponse(
output,
media_type="text/csv",
headers={
"Content-Disposition": f"attachment; filename={study_id}-surveys-wide.csv"
},
)
def create_study_info(
db: Session, study_id: int, study_info: schemas.StudyInfoCreate
) -> models.StudyInfo:
db_study_info = models.StudyInfo(
study_id=study_id, **study_info.dict(exclude_unset=True)
)
db.add(db_study_info)
db.commit()
db.refresh(db_study_info)
return db_study_info