26
26
from storm.locals import And, Asc, Desc
27
27
import ivle.database
28
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
30
29
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
31
30
'get_exercise_attempts', 'get_exercise_attempt',
34
def get_exercise_status(store, user, worksheet_exercise):
33
def get_exercise_status(store, user, exercise):
35
34
"""Given a storm.store, User and Exercise, returns information about
36
35
the user's performance on that problem.
37
36
Returns a tuple of:
40
39
including the first successful attempt (or the total number of
41
40
attempts, if not yet successful).
42
ExerciseAttempt = ivle.database.ExerciseAttempt
43
43
# A Storm expression denoting all active attempts by this user for this
45
45
is_relevant = ((ExerciseAttempt.user_id == user.id) &
46
(ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
47
(ExerciseAttempt.active == True))
46
(ExerciseAttempt.exercise_id == exercise.id) &
47
(ExerciseAttempt.active == True))
49
49
# Get the first successful active attempt, or None if no success yet.
50
50
# (For this user, for this exercise).
67
67
return first_success is not None, num_attempts
69
def get_exercise_stored_text(store, user, worksheet_exercise):
70
"""Given a storm.store, User and WorksheetExercise, returns an
69
def get_exercise_stored_text(store, user, exercise):
70
"""Given a storm.store, User and Exercise, returns an
71
71
ivle.database.ExerciseSave object for the last saved/submitted attempt for
72
72
this question (note that ExerciseAttempt is a subclass of ExerciseSave).
73
73
Returns None if the user has not saved or made an attempt on this
75
75
If the user has both saved and submitted, it returns whichever was
78
ExerciseSave = ivle.database.ExerciseSave
79
ExerciseAttempt = ivle.database.ExerciseAttempt
79
81
# Get the saved text, or None
80
82
saved = store.find(ExerciseSave,
81
83
ExerciseSave.user_id == user.id,
82
ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
84
ExerciseSave.exercise_id == exercise.id).one()
84
86
# Get the most recent attempt, or None
85
87
attempt = store.find(ExerciseAttempt,
86
88
ExerciseAttempt.user_id == user.id,
89
ExerciseAttempt.exercise_id == exercise.id,
87
90
ExerciseAttempt.active == True,
88
ExerciseAttempt.ws_ex_id == worksheet_exercise.id
89
91
).order_by(Asc(ExerciseAttempt.date)).last()
91
93
# Pick the most recent of these two
103
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
105
def _get_exercise_attempts(store, user, exercise, as_of=None,
104
106
allow_inactive=False):
105
107
"""Same as get_exercise_attempts, but doesn't convert Storm's iterator
109
ExerciseAttempt = ivle.database.ExerciseAttempt
108
111
# Get the most recent attempt before as_of, or None
109
112
return store.find(ExerciseAttempt,
110
113
ExerciseAttempt.user_id == user.id,
111
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
114
ExerciseAttempt.exercise_id == exercise.id,
112
115
True if allow_inactive else ExerciseAttempt.active == True,
113
116
True if as_of is None else ExerciseAttempt.date <= as_of,
114
117
).order_by(Desc(ExerciseAttempt.date))
116
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
119
def get_exercise_attempts(store, user, exercise, as_of=None,
117
120
allow_inactive=False):
118
121
"""Given a storm.store, User and Exercise, returns a list of
119
122
ivle.database.ExerciseAttempt objects, one for each attempt made for the
123
126
attempts made before or at this time.
124
127
allow_inactive: If True, will return disabled attempts.
126
return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
129
return list(_get_exercise_attempts(store, user, exercise, as_of,
129
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
132
def get_exercise_attempt(store, user, exercise, as_of=None,
130
133
allow_inactive=False):
131
134
"""Given a storm.store, User and Exercise, returns an
132
135
ivle.database.ExerciseAttempt object for the last submitted attempt for
138
141
attempts made before or at this time.
139
142
allow_inactive: If True, will return disabled attempts.
141
return _get_exercise_attempts(store, user, exercise, worksheet, as_of,
144
return _get_exercise_attempts(store, user, exercise, as_of,
142
145
allow_inactive).first()
144
def save_exercise(store, user, worksheet_exercise, text, date):
147
def save_exercise(store, user, exercise, text, date):
145
148
"""Save an exercise for a user.
147
Given a store, User, WorksheetExercise, text and date, save the text to the
150
Given a store, User, Exercise and text and date, save the text to the
148
151
database. This will create the ExerciseSave if needed.
150
153
saved = store.find(ivle.database.ExerciseSave,
151
154
ivle.database.ExerciseSave.user_id == user.id,
152
ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
155
ivle.database.ExerciseSave.exercise_id == exercise.id).one()
154
156
if saved is None:
155
saved = ivle.database.ExerciseSave(user=user,
156
worksheet_exercise=worksheet_exercise)
157
saved = ivle.database.ExerciseSave(user=user, exercise=exercise)
159
160
saved.date = date
177
178
# Get the student's pass/fail for each exercise in this worksheet
178
179
for worksheet_exercise in worksheet.worksheet_exercises:
179
180
exercise = worksheet_exercise.exercise
180
worksheet = worksheet_exercise.worksheet
181
181
optional = worksheet_exercise.optional
183
done, _ = get_exercise_status(store, user, worksheet_exercise)
183
done, _ = get_exercise_status(store, user, exercise)
184
184
# done is a bool, whether this student has completed that problem