23
23
This module provides functions for tutorial and worksheet computations.
28
from storm.locals import And, Asc, Desc, Store
26
from storm.locals import And, Asc, Desc
31
27
import ivle.database
32
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
33
WorksheetExercise, Exercise
34
import ivle.webapp.tutorial.test
36
__all__ = ['ExerciseNotFound', 'get_exercise_status',
37
'get_exercise_stored_text', 'get_exercise_attempts',
38
'get_exercise_attempt', 'test_exercise_submission',
29
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
30
'get_exercise_attempts', 'get_exercise_attempt',
41
class ExerciseNotFound(Exception):
44
def get_exercise_status(store, user, worksheet_exercise, as_of=None):
33
def get_exercise_status(store, user, exercise):
45
34
"""Given a storm.store, User and Exercise, returns information about
46
35
the user's performance on that problem.
47
@param store: A storm.store
49
@param worksheet_exercise: An Exercise.
50
@param as_of: Optional datetime. If supplied, gets the status as of as_of.
51
36
Returns a tuple of:
52
37
- A boolean, whether they have successfully passed this exercise.
53
38
- An int, the number of attempts they have made up to and
54
39
including the first successful attempt (or the total number of
55
40
attempts, if not yet successful).
42
ExerciseAttempt = ivle.database.ExerciseAttempt
57
43
# A Storm expression denoting all active attempts by this user for this
59
45
is_relevant = ((ExerciseAttempt.user_id == user.id) &
60
(ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
61
(ExerciseAttempt.active == True))
63
is_relevant &= ExerciseAttempt.date <= as_of
46
(ExerciseAttempt.exercise_id == exercise.id) &
47
(ExerciseAttempt.active == True))
65
49
# Get the first successful active attempt, or None if no success yet.
66
50
# (For this user, for this exercise).
119
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
105
def _get_exercise_attempts(store, user, exercise, as_of=None,
120
106
allow_inactive=False):
121
107
"""Same as get_exercise_attempts, but doesn't convert Storm's iterator
109
ExerciseAttempt = ivle.database.ExerciseAttempt
124
111
# Get the most recent attempt before as_of, or None
125
112
return store.find(ExerciseAttempt,
126
113
ExerciseAttempt.user_id == user.id,
127
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
114
ExerciseAttempt.exercise_id == exercise.id,
128
115
True if allow_inactive else ExerciseAttempt.active == True,
129
116
True if as_of is None else ExerciseAttempt.date <= as_of,
130
117
).order_by(Desc(ExerciseAttempt.date))
132
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
119
def get_exercise_attempts(store, user, exercise, as_of=None,
133
120
allow_inactive=False):
134
121
"""Given a storm.store, User and Exercise, returns a list of
135
122
ivle.database.ExerciseAttempt objects, one for each attempt made for the
154
141
attempts made before or at this time.
155
142
allow_inactive: If True, will return disabled attempts.
157
return _get_exercise_attempts(store, user, worksheet_exercise, as_of,
144
return _get_exercise_attempts(store, user, exercise, as_of,
158
145
allow_inactive).first()
160
def save_exercise(store, user, worksheet_exercise, text, date):
147
def save_exercise(store, user, exercise, text, date):
161
148
"""Save an exercise for a user.
163
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
164
151
database. This will create the ExerciseSave if needed.
166
153
saved = store.find(ivle.database.ExerciseSave,
167
154
ivle.database.ExerciseSave.user_id == user.id,
168
ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
155
ivle.database.ExerciseSave.exercise_id == exercise.id).one()
170
156
if saved is None:
171
saved = ivle.database.ExerciseSave(user=user,
172
worksheet_exercise=worksheet_exercise)
157
saved = ivle.database.ExerciseSave(user=user, exercise=exercise)
175
160
saved.date = date
176
161
saved.text = text
178
def calculate_score(store, user, worksheet, as_of=None):
163
def calculate_score(store, user, worksheet):
180
165
Given a storm.store, User, Exercise and Worksheet, calculates a score for
181
166
the user on the given worksheet.
182
@param store: A storm.store
184
@param worksheet: A Worksheet.
185
@param as_of: Optional datetime. If supplied, gets the score as of as_of.
186
167
Returns a 4-tuple of ints, consisting of:
187
168
(No. mandatory exercises completed,
188
169
Total no. mandatory exercises,
210
190
if done: mand_done += 1
212
192
return mand_done, mand_total, opt_done, opt_total
214
def calculate_mark(mand_done, mand_total):
215
"""Calculate a subject mark, given the result of all worksheets.
216
@param mand_done: The total number of mandatory exercises completed by
217
some student, across all worksheets.
218
@param mand_total: The total number of mandatory exercises across all
219
worksheets in the offering.
220
@return: (percent, mark, mark_total)
221
percent: The percentage of exercises the student has completed, as an
222
integer between 0 and 100 inclusive.
223
mark: The mark the student has received, based on the percentage.
224
mark_total: The total number of marks available (currently hard-coded
227
# We want to display a students mark out of 5. However, they are
228
# allowed to skip 1 in 5 questions and still get 'full marks'.
229
# Hence we divide by 16, essentially making 16 percent worth
230
# 1 star, and 80 or above worth 5.
232
percent_int = (100 * mand_done) // mand_total
234
# Avoid Div0, just give everyone 0 marks if there are none available
236
# percent / 16, rounded down, with a maximum mark of 5
238
mark = min(percent_int // 16, max_mark)
239
return (percent_int, mark, max_mark)
241
def update_exerciselist(worksheet):
242
"""Runs through the worksheetstream, generating the appropriate
243
WorksheetExercises, and de-activating the old ones."""
245
# Turns the worksheet into an xml stream, and then finds all the
246
# exercise nodes in the stream.
247
worksheetdata = genshi.XML(worksheet.get_xml())
248
for kind, data, pos in worksheetdata:
249
if kind is genshi.core.START:
250
# Data is a tuple of tag name and a list of name->value tuples
251
if data[0] == 'exercise':
257
if attr[0] == 'optional':
258
optional = attr[1] == 'true'
260
exercises.append((src, optional))
262
# Set all current worksheet_exercises to be inactive
263
db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise,
264
WorksheetExercise.worksheet_id == worksheet.id)
265
for worksheet_exercise in db_worksheet_exercises:
266
worksheet_exercise.active = False
268
for exerciseid, optional in exercises:
269
worksheet_exercise = Store.of(worksheet).find(WorksheetExercise,
270
WorksheetExercise.worksheet_id == worksheet.id,
271
Exercise.id == WorksheetExercise.exercise_id,
272
Exercise.id == exerciseid).one()
273
if worksheet_exercise is None:
274
exercise = Store.of(worksheet).find(Exercise,
275
Exercise.id == exerciseid
278
raise ExerciseNotFound(exerciseid)
279
worksheet_exercise = WorksheetExercise()
280
worksheet_exercise.worksheet_id = worksheet.id
281
worksheet_exercise.exercise_id = exercise.id
282
Store.of(worksheet).add(worksheet_exercise)
283
worksheet_exercise.active = True
284
worksheet_exercise.seq_no = ex_num
285
worksheet_exercise.optional = optional
288
def test_exercise_submission(config, user, exercise, code):
289
"""Test the given code against an exercise.
291
The code is run in a console process as the provided user.
293
# Start a console to run the tests on
294
jail_path = os.path.join(config['paths']['jails']['mounts'],
296
working_dir = os.path.join("/home", user.login)
297
cons = ivle.console.Console(config, user.unixid, jail_path,
300
# Parse the file into a exercise object using the test suite
301
exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
304
# Run the test cases. Get the result back as a JSONable object.
306
test_results = exercise_obj.run_tests(code)
314
class FakeWorksheetForMarks:
315
"""This class represents a worksheet and a particular students progress
318
Do not confuse this with a worksheet in the database. This worksheet
319
has extra information for use in the output, such as marks."""
320
def __init__(self, id, name, assessable):
323
self.assessable = assessable
324
self.complete_class = ''
325
self.optional_message = ''
329
return ("Worksheet(id=%s, name=%s, assessable=%s)"
330
% (repr(self.id), repr(self.name), repr(self.assessable)))
333
# XXX: This really shouldn't be needed.
334
def create_list_of_fake_worksheets_and_stats(store, user, offering):
335
"""Take an offering's real worksheets, converting them into stats.
337
The worksheet listing views expect special fake worksheet objects
338
that contain counts of exercises, whether they've been completed,
339
that sort of thing. A fake worksheet object is used to contain
340
these values, because nobody has managed to refactor the need out
347
# Offering.worksheets is ordered by the worksheets seq_no
348
for worksheet in offering.worksheets:
349
new_worksheet = FakeWorksheetForMarks(
350
worksheet.identifier, worksheet.name, worksheet.assessable)
351
if new_worksheet.assessable:
352
# Calculate the user's score for this worksheet
353
mand_done, mand_total, opt_done, opt_total = (
354
ivle.worksheet.utils.calculate_score(store, user, worksheet))
356
optional_message = " (excluding optional exercises)"
358
optional_message = ""
359
if mand_done >= mand_total:
360
new_worksheet.complete_class = "complete"
362
new_worksheet.complete_class = "semicomplete"
364
new_worksheet.complete_class = "incomplete"
365
problems_done += mand_done
366
problems_total += mand_total
367
new_worksheet.mand_done = mand_done
368
new_worksheet.total = mand_total
369
new_worksheet.optional_message = optional_message
370
new_worksheets.append(new_worksheet)
372
return new_worksheets, problems_total, problems_done