1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
Worksheet Utility Functions
23
This module provides functions for tutorial and worksheet computations.
26
from storm.locals import And, Asc, Desc
29
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
30
'get_exercise_attempts', 'get_exercise_attempt',
33
def get_exercise_status(store, user, exercise):
34
"""Given a storm.store, User and Exercise, returns information about
35
the user's performance on that problem.
37
- A boolean, whether they have successfully passed this exercise.
38
- An int, the number of attempts they have made up to and
39
including the first successful attempt (or the total number of
40
attempts, if not yet successful).
42
ExerciseAttempt = ivle.database.ExerciseAttempt
43
# A Storm expression denoting all active attempts by this user for this
45
is_relevant = ((ExerciseAttempt.user_id == user.id) &
46
(ExerciseAttempt.exercise_id == exercise.id) &
47
(ExerciseAttempt.active == True))
49
# Get the first successful active attempt, or None if no success yet.
50
# (For this user, for this exercise).
51
first_success = store.find(ExerciseAttempt, is_relevant,
52
ExerciseAttempt.complete == True
53
).order_by(Asc(ExerciseAttempt.date)).first()
55
if first_success is not None:
56
# Get the total number of active attempts up to and including the
57
# first successful attempt.
58
# (Subsequent attempts don't count, because the user had already
60
num_attempts = store.find(ExerciseAttempt, is_relevant,
61
ExerciseAttempt.date <= first_success.date).count()
63
# User has not yet succeeded.
64
# Get the total number of active attempts.
65
num_attempts = store.find(ExerciseAttempt, is_relevant).count()
67
return first_success is not None, num_attempts
69
def get_exercise_stored_text(store, user, exercise):
70
"""Given a storm.store, User and Exercise, returns an
71
ivle.database.ExerciseSave object for the last saved/submitted attempt for
72
this question (note that ExerciseAttempt is a subclass of ExerciseSave).
73
Returns None if the user has not saved or made an attempt on this
75
If the user has both saved and submitted, it returns whichever was
78
ExerciseSave = ivle.database.ExerciseSave
79
ExerciseAttempt = ivle.database.ExerciseAttempt
81
# Get the saved text, or None
82
saved = store.find(ExerciseSave,
83
ExerciseSave.user_id == user.id,
84
ExerciseSave.exercise_id == exercise.id).one()
86
# Get the most recent attempt, or None
87
attempt = store.find(ExerciseAttempt,
88
ExerciseAttempt.user_id == user.id,
89
ExerciseAttempt.exercise_id == exercise.id,
90
ExerciseAttempt.active == True,
91
).order_by(Asc(ExerciseAttempt.date)).last()
93
# Pick the most recent of these two
95
if attempt is not None:
96
return saved if saved.date > attempt.date else attempt
100
if attempt is not None:
105
def _get_exercise_attempts(store, user, exercise, as_of=None,
106
allow_inactive=False):
107
"""Same as get_exercise_attempts, but doesn't convert Storm's iterator
109
ExerciseAttempt = ivle.database.ExerciseAttempt
111
# Get the most recent attempt before as_of, or None
112
return store.find(ExerciseAttempt,
113
ExerciseAttempt.user_id == user.id,
114
ExerciseAttempt.exercise_id == exercise.id,
115
True if allow_inactive else ExerciseAttempt.active == True,
116
True if as_of is None else ExerciseAttempt.date <= as_of,
117
).order_by(Desc(ExerciseAttempt.date))
119
def get_exercise_attempts(store, user, exercise, as_of=None,
120
allow_inactive=False):
121
"""Given a storm.store, User and Exercise, returns a list of
122
ivle.database.ExerciseAttempt objects, one for each attempt made for the
123
exercise, sorted from latest to earliest.
125
as_of: Optional datetime.datetime object. If supplied, only returns
126
attempts made before or at this time.
127
allow_inactive: If True, will return disabled attempts.
129
return list(_get_exercise_attempts(store, user, exercise, as_of,
132
def get_exercise_attempt(store, user, exercise, as_of=None,
133
allow_inactive=False):
134
"""Given a storm.store, User and Exercise, returns an
135
ivle.database.ExerciseAttempt object for the last submitted attempt for
137
Returns None if the user has not made an attempt on this
140
as_of: Optional datetime.datetime object. If supplied, only returns
141
attempts made before or at this time.
142
allow_inactive: If True, will return disabled attempts.
144
return _get_exercise_attempts(store, user, exercise, as_of,
145
allow_inactive).first()
147
def calculate_score(store, user, worksheet):
149
Given a storm.store, User, Exercise and Worksheet, calculates a score for
150
the user on the given worksheet.
151
Returns a 4-tuple of ints, consisting of:
152
(No. mandatory exercises completed,
153
Total no. mandatory exercises,
154
No. optional exercises completed,
155
Total no. optional exercises)
162
# Get the student's pass/fail for each exercise in this worksheet
163
for worksheet_exercise in worksheet.worksheet_exercises:
164
exercise = worksheet_exercise.exercise
165
optional = worksheet_exercise.optional
167
done, _ = get_exercise_status(store, user, exercise)
168
# done is a bool, whether this student has completed that problem
171
if done: opt_done += 1
174
if done: mand_done += 1
176
return mand_done, mand_total, opt_done, opt_total