23
23
This module provides functions for tutorial and worksheet computations.
26
from storm.locals import And, Asc, Desc
26
from storm.locals import And, Asc, Desc, Store
27
29
import ivle.database
28
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
31
WorksheetExercise, Exercise
30
33
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
31
34
'get_exercise_attempts', 'get_exercise_attempt',
34
def get_exercise_status(store, user, exercise, worksheet):
37
def get_exercise_status(store, user, worksheet_exercise, as_of=None):
35
38
"""Given a storm.store, User and Exercise, returns information about
36
39
the user's performance on that problem.
40
@param store: A storm.store
42
@param worksheet_exercise: An Exercise.
43
@param as_of: Optional datetime. If supplied, gets the status as of as_of.
37
44
Returns a tuple of:
38
45
- A boolean, whether they have successfully passed this exercise.
39
46
- An int, the number of attempts they have made up to and
43
50
# A Storm expression denoting all active attempts by this user for this
45
52
is_relevant = ((ExerciseAttempt.user_id == user.id) &
46
(ExerciseAttempt.exercise_id == exercise.id) &
47
(ExerciseAttempt.active == True) &
48
(ExerciseAttempt.worksheetid == worksheet.id))
53
(ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
54
(ExerciseAttempt.active == True))
56
is_relevant &= ExerciseAttempt.date <= as_of
50
58
# Get the first successful active attempt, or None if no success yet.
51
59
# (For this user, for this exercise).
68
76
return first_success is not None, num_attempts
70
def get_exercise_stored_text(store, user, exercise, worksheet):
71
"""Given a storm.store, User and Exercise, returns an
78
def get_exercise_stored_text(store, user, worksheet_exercise):
79
"""Given a storm.store, User and WorksheetExercise, returns an
72
80
ivle.database.ExerciseSave object for the last saved/submitted attempt for
73
81
this question (note that ExerciseAttempt is a subclass of ExerciseSave).
74
82
Returns None if the user has not saved or made an attempt on this
80
88
# Get the saved text, or None
81
89
saved = store.find(ExerciseSave,
82
90
ExerciseSave.user_id == user.id,
83
ExerciseSave.exercise_id == exercise.id,
84
ExerciseSave.worksheetid == worksheet.id).one()
91
ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
86
93
# Get the most recent attempt, or None
87
94
attempt = store.find(ExerciseAttempt,
88
95
ExerciseAttempt.user_id == user.id,
89
ExerciseAttempt.exercise_id == exercise.id,
90
ExerciseAttempt.worksheetid == worksheet.id,
91
96
ExerciseAttempt.active == True,
97
ExerciseAttempt.ws_ex_id == worksheet_exercise.id
92
98
).order_by(Asc(ExerciseAttempt.date)).last()
94
100
# Pick the most recent of these two
111
117
# Get the most recent attempt before as_of, or None
112
118
return store.find(ExerciseAttempt,
113
119
ExerciseAttempt.user_id == user.id,
114
ExerciseAttempt.exercise_id == exercise.id,
115
ExerciseAttempt.worksheetid == worksheet.id,
120
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
116
121
True if allow_inactive else ExerciseAttempt.active == True,
117
122
True if as_of is None else ExerciseAttempt.date <= as_of,
118
123
).order_by(Desc(ExerciseAttempt.date))
120
def get_exercise_attempts(store, user, exercise, worksheet, as_of=None,
125
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
121
126
allow_inactive=False):
122
127
"""Given a storm.store, User and Exercise, returns a list of
123
128
ivle.database.ExerciseAttempt objects, one for each attempt made for the
127
132
attempts made before or at this time.
128
133
allow_inactive: If True, will return disabled attempts.
130
return list(_get_exercise_attempts(store, user, exercise, worksheet, as_of,
135
return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
133
def get_exercise_attempt(store, user, exercise, worksheet, as_of=None,
138
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
134
139
allow_inactive=False):
135
"""Given a storm.store, User and Exercise, returns an
140
"""Given a storm.store, User and WorksheetExercise, returns an
136
141
ivle.database.ExerciseAttempt object for the last submitted attempt for
138
143
Returns None if the user has not made an attempt on this
142
147
attempts made before or at this time.
143
148
allow_inactive: If True, will return disabled attempts.
145
return _get_exercise_attempts(store, user, exercise, worksheet, as_of,
150
return _get_exercise_attempts(store, user, worksheet_exercise, as_of,
146
151
allow_inactive).first()
148
def save_exercise(store, user, exercise, worksheet, text, date):
153
def save_exercise(store, user, worksheet_exercise, text, date):
149
154
"""Save an exercise for a user.
151
Given a store, User, Exercise and text and date, save the text to the
156
Given a store, User, WorksheetExercise, text and date, save the text to the
152
157
database. This will create the ExerciseSave if needed.
154
159
saved = store.find(ivle.database.ExerciseSave,
155
160
ivle.database.ExerciseSave.user_id == user.id,
156
ivle.database.ExerciseSave.exercise_id == exercise.id,
157
ivle.database.ExerciseSave.worksheetid == worksheet.id
161
ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
159
163
if saved is None:
160
saved = ivle.database.ExerciseSave(user=user, exercise=exercise,
164
saved = ivle.database.ExerciseSave(user=user,
165
worksheet_exercise=worksheet_exercise)
164
168
saved.date = date
165
169
saved.text = text
167
def calculate_score(store, user, worksheet):
171
def calculate_score(store, user, worksheet, as_of=None):
169
173
Given a storm.store, User, Exercise and Worksheet, calculates a score for
170
174
the user on the given worksheet.
175
@param store: A storm.store
177
@param worksheet: A Worksheet.
178
@param as_of: Optional datetime. If supplied, gets the score as of as_of.
171
179
Returns a 4-tuple of ints, consisting of:
172
180
(No. mandatory exercises completed,
173
181
Total no. mandatory exercises,
182
190
# Get the student's pass/fail for each exercise in this worksheet
183
191
for worksheet_exercise in worksheet.worksheet_exercises:
184
192
exercise = worksheet_exercise.exercise
193
worksheet = worksheet_exercise.worksheet
185
194
optional = worksheet_exercise.optional
187
done, _ = get_exercise_status(store, user, exercise)
196
done, _ = get_exercise_status(store, user, worksheet_exercise, as_of)
188
197
# done is a bool, whether this student has completed that problem
194
203
if done: mand_done += 1
196
205
return mand_done, mand_total, opt_done, opt_total
207
def calculate_mark(mand_done, mand_total):
208
"""Calculate a subject mark, given the result of all worksheets.
209
@param mand_done: The total number of mandatory exercises completed by
210
some student, across all worksheets.
211
@param mand_total: The total number of mandatory exercises across all
212
worksheets in the offering.
213
@return: (percent, mark, mark_total)
214
percent: The percentage of exercises the student has completed, as an
215
integer between 0 and 100 inclusive.
216
mark: The mark the student has received, based on the percentage.
217
mark_total: The total number of marks available (currently hard-coded
220
# We want to display a students mark out of 5. However, they are
221
# allowed to skip 1 in 5 questions and still get 'full marks'.
222
# Hence we divide by 16, essentially making 16 percent worth
223
# 1 star, and 80 or above worth 5.
225
percent_int = (100 * mand_done) // mand_total
227
# Avoid Div0, just give everyone 0 marks if there are none available
229
# percent / 16, rounded down, with a maximum mark of 5
231
mark = min(percent_int // 16, max_mark)
232
return (percent_int, mark, max_mark)
234
def update_exerciselist(worksheet):
235
"""Runs through the worksheetstream, generating the appropriate
236
WorksheetExercises, and de-activating the old ones."""
238
# Turns the worksheet into an xml stream, and then finds all the
239
# exercise nodes in the stream.
240
worksheetdata = genshi.XML(worksheet.get_xml())
241
for kind, data, pos in worksheetdata:
242
if kind is genshi.core.START:
243
# Data is a tuple of tag name and a list of name->value tuples
244
if data[0] == 'exercise':
250
if attr[0] == 'optional':
251
optional = attr[1] == 'true'
253
exercises.append((src, optional))
255
# Set all current worksheet_exercises to be inactive
256
db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise,
257
WorksheetExercise.worksheet_id == worksheet.id)
258
for worksheet_exercise in db_worksheet_exercises:
259
worksheet_exercise.active = False
261
for exerciseid, optional in exercises:
262
worksheet_exercise = Store.of(worksheet).find(WorksheetExercise,
263
WorksheetExercise.worksheet_id == worksheet.id,
264
Exercise.id == WorksheetExercise.exercise_id,
265
Exercise.id == exerciseid).one()
266
if worksheet_exercise is None:
267
exercise = Store.of(worksheet).find(Exercise,
268
Exercise.id == exerciseid
272
worksheet_exercise = WorksheetExercise()
273
worksheet_exercise.worksheet_id = worksheet.id
274
worksheet_exercise.exercise_id = exercise.id
275
Store.of(worksheet).add(worksheet_exercise)
276
worksheet_exercise.active = True
277
worksheet_exercise.seq_no = ex_num
278
worksheet_exercise.optional = optional