23
23
This module provides functions for tutorial and worksheet computations.
28
26
from storm.locals import And, Asc, Desc, Store
31
29
import ivle.database
32
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
33
WorksheetExercise, Exercise, User
34
import ivle.webapp.tutorial.test
31
WorksheetExercise, Exercise
36
__all__ = ['ExerciseNotFound', 'get_exercise_status',
37
'get_exercise_statistics',
38
'get_exercise_stored_text', 'get_exercise_attempts',
39
'get_exercise_attempt', 'test_exercise_submission',
33
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
34
'get_exercise_attempts', 'get_exercise_attempt',
42
class ExerciseNotFound(Exception):
45
def get_exercise_status(store, user, worksheet_exercise, as_of=None):
37
def get_exercise_status(store, user, worksheet_exercise):
46
38
"""Given a storm.store, User and Exercise, returns information about
47
39
the user's performance on that problem.
48
@param store: A storm.store
50
@param worksheet_exercise: An Exercise.
51
@param as_of: Optional datetime. If supplied, gets the status as of as_of.
52
40
Returns a tuple of:
53
41
- A boolean, whether they have successfully passed this exercise.
54
42
- An int, the number of attempts they have made up to and
84
70
return first_success is not None, num_attempts
86
def get_exercise_statistics(store, worksheet_exercise):
87
"""Return statistics about an exercise (with respect to a given
89
(number of students completed, number of students attempted)."""
90
# Count the set of Users whose ID matches an attempt in this worksheet
91
num_completed = store.find(User, User.id == ExerciseAttempt.user_id,
92
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
93
ExerciseAttempt.complete == True).config(distinct=True).count()
94
num_attempted = store.find(User, User.id == ExerciseAttempt.user_id,
95
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
96
).config(distinct=True).count()
97
return num_completed, num_attempted
99
72
def get_exercise_stored_text(store, user, worksheet_exercise):
100
73
"""Given a storm.store, User and WorksheetExercise, returns an
101
74
ivle.database.ExerciseSave object for the last saved/submitted attempt for
189
162
saved.date = date
190
163
saved.text = text
192
def calculate_score(store, user, worksheet, as_of=None):
165
def calculate_score(store, user, worksheet):
194
167
Given a storm.store, User, Exercise and Worksheet, calculates a score for
195
168
the user on the given worksheet.
196
@param store: A storm.store
198
@param worksheet: A Worksheet.
199
@param as_of: Optional datetime. If supplied, gets the score as of as_of.
200
169
Returns a 4-tuple of ints, consisting of:
201
170
(No. mandatory exercises completed,
202
171
Total no. mandatory exercises,
226
195
return mand_done, mand_total, opt_done, opt_total
228
def calculate_mark(mand_done, mand_total):
229
"""Calculate a subject mark, given the result of all worksheets.
230
@param mand_done: The total number of mandatory exercises completed by
231
some student, across all worksheets.
232
@param mand_total: The total number of mandatory exercises across all
233
worksheets in the offering.
234
@return: (percent, mark, mark_total)
235
percent: The percentage of exercises the student has completed, as an
236
integer between 0 and 100 inclusive.
237
mark: The mark the student has received, based on the percentage.
238
mark_total: The total number of marks available (currently hard-coded
241
# We want to display a students mark out of 5. However, they are
242
# allowed to skip 1 in 5 questions and still get 'full marks'.
243
# Hence we divide by 16, essentially making 16 percent worth
244
# 1 star, and 80 or above worth 5.
246
percent_int = (100 * mand_done) // mand_total
248
# Avoid Div0, just give everyone 0 marks if there are none available
250
# percent / 16, rounded down, with a maximum mark of 5
252
mark = min(percent_int // 16, max_mark)
253
return (percent_int, mark, max_mark)
255
197
def update_exerciselist(worksheet):
256
198
"""Runs through the worksheetstream, generating the appropriate
257
199
WorksheetExercises, and de-activating the old ones."""
259
201
# Turns the worksheet into an xml stream, and then finds all the
260
202
# exercise nodes in the stream.
261
worksheetdata = genshi.XML(worksheet.data_xhtml)
203
worksheetdata = genshi.XML(worksheet.data)
262
204
for kind, data, pos in worksheetdata:
263
205
if kind is genshi.core.START:
264
206
# Data is a tuple of tag name and a list of name->value tuples
298
240
worksheet_exercise.seq_no = ex_num
299
241
worksheet_exercise.optional = optional
302
def test_exercise_submission(config, user, exercise, code):
303
"""Test the given code against an exercise.
305
The code is run in a console process as the provided user.
307
# Start a console to run the tests on
308
jail_path = os.path.join(config['paths']['jails']['mounts'],
310
working_dir = os.path.join("/home", user.login)
311
cons = ivle.console.Console(config, user, jail_path,
314
# Parse the file into a exercise object using the test suite
315
exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
318
# Run the test cases. Get the result back as a JSONable object.
320
test_results = exercise_obj.run_tests(code)
328
class FakeWorksheetForMarks:
329
"""This class represents a worksheet and a particular students progress
332
Do not confuse this with a worksheet in the database. This worksheet
333
has extra information for use in the output, such as marks."""
334
def __init__(self, id, name, assessable, published):
337
self.assessable = assessable
338
self.published = published
339
self.complete_class = ''
340
self.optional_message = ''
344
return ("Worksheet(id=%s, name=%s, assessable=%s)"
345
% (repr(self.id), repr(self.name), repr(self.assessable)))
348
# XXX: This really shouldn't be needed.
349
def create_list_of_fake_worksheets_and_stats(config, store, user, offering,
351
"""Take an offering's real worksheets, converting them into stats.
353
The worksheet listing views expect special fake worksheet objects
354
that contain counts of exercises, whether they've been completed,
355
that sort of thing. A fake worksheet object is used to contain
356
these values, because nobody has managed to refactor the need out
363
# Offering.worksheets is ordered by the worksheets seq_no
364
worksheets = offering.worksheets
366
# Unless we can edit worksheets, hide unpublished ones.
367
if 'edit_worksheets' not in offering.get_permissions(user, config):
368
worksheets = worksheets.find(published=True)
370
for worksheet in worksheets:
371
new_worksheet = FakeWorksheetForMarks(
372
worksheet.identifier, worksheet.name, worksheet.assessable,
374
if new_worksheet.assessable:
375
# Calculate the user's score for this worksheet
376
mand_done, mand_total, opt_done, opt_total = (
377
ivle.worksheet.utils.calculate_score(store, user, worksheet,
380
optional_message = " (excluding optional exercises)"
382
optional_message = ""
383
if mand_done >= mand_total:
384
new_worksheet.complete_class = "complete"
386
new_worksheet.complete_class = "semicomplete"
388
new_worksheet.complete_class = "incomplete"
389
problems_done += mand_done
390
problems_total += mand_total
391
new_worksheet.mand_done = mand_done
392
new_worksheet.total = mand_total
393
new_worksheet.optional_message = optional_message
394
new_worksheets.append(new_worksheet)
396
return new_worksheets, problems_total, problems_done