1080.1.56
by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2009 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
# Author: Matt Giuca
|
|
19 |
||
20 |
"""
|
|
21 |
Worksheet Utility Functions
|
|
22 |
||
23 |
This module provides functions for tutorial and worksheet computations.
|
|
24 |
"""
|
|
25 |
||
1102
by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet, |
26 |
from storm.locals import And, Asc, Desc, Store |
27 |
import genshi |
|
28 |
||
1080.1.56
by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for |
29 |
import ivle.database |
1102
by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet, |
30 |
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \ |
31 |
WorksheetExercise, Exercise |
|
1080.1.56
by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for |
32 |
|
1080.1.59
by Matt Giuca
ivle.worksheet, ivle.database: Added/updated __all__. |
33 |
__all__ = ['get_exercise_status', 'get_exercise_stored_text', |
34 |
'get_exercise_attempts', 'get_exercise_attempt', |
|
35 |
]
|
|
36 |
||
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
37 |
def get_exercise_status(store, user, worksheet_exercise): |
1080.1.56
by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for |
38 |
"""Given a storm.store, User and Exercise, returns information about
|
39 |
the user's performance on that problem.
|
|
40 |
Returns a tuple of:
|
|
41 |
- A boolean, whether they have successfully passed this exercise.
|
|
42 |
- An int, the number of attempts they have made up to and
|
|
43 |
including the first successful attempt (or the total number of
|
|
44 |
attempts, if not yet successful).
|
|
45 |
"""
|
|
46 |
# A Storm expression denoting all active attempts by this user for this
|
|
47 |
# exercise.
|
|
48 |
is_relevant = ((ExerciseAttempt.user_id == user.id) & |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
49 |
(ExerciseAttempt.ws_ex_id == worksheet_exercise.id) & |
50 |
(ExerciseAttempt.active == True)) |
|
1080.1.56
by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for |
51 |
|
52 |
# Get the first successful active attempt, or None if no success yet.
|
|
53 |
# (For this user, for this exercise).
|
|
54 |
first_success = store.find(ExerciseAttempt, is_relevant, |
|
55 |
ExerciseAttempt.complete == True |
|
56 |
).order_by(Asc(ExerciseAttempt.date)).first() |
|
57 |
||
58 |
if first_success is not None: |
|
59 |
# Get the total number of active attempts up to and including the
|
|
60 |
# first successful attempt.
|
|
61 |
# (Subsequent attempts don't count, because the user had already
|
|
62 |
# succeeded by then).
|
|
63 |
num_attempts = store.find(ExerciseAttempt, is_relevant, |
|
64 |
ExerciseAttempt.date <= first_success.date).count() |
|
65 |
else: |
|
66 |
# User has not yet succeeded.
|
|
67 |
# Get the total number of active attempts.
|
|
68 |
num_attempts = store.find(ExerciseAttempt, is_relevant).count() |
|
69 |
||
70 |
return first_success is not None, num_attempts |
|
1080.1.57
by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from |
71 |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
72 |
def get_exercise_stored_text(store, user, worksheet_exercise): |
73 |
"""Given a storm.store, User and WorksheetExercise, returns an
|
|
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
74 |
ivle.database.ExerciseSave object for the last saved/submitted attempt for
|
75 |
this question (note that ExerciseAttempt is a subclass of ExerciseSave).
|
|
1080.1.57
by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from |
76 |
Returns None if the user has not saved or made an attempt on this
|
77 |
problem.
|
|
78 |
If the user has both saved and submitted, it returns whichever was
|
|
79 |
made last.
|
|
80 |
"""
|
|
81 |
||
82 |
# Get the saved text, or None
|
|
83 |
saved = store.find(ExerciseSave, |
|
84 |
ExerciseSave.user_id == user.id, |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
85 |
ExerciseSave.ws_ex_id == worksheet_exercise.id).one() |
1080.1.57
by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from |
86 |
|
87 |
# Get the most recent attempt, or None
|
|
88 |
attempt = store.find(ExerciseAttempt, |
|
89 |
ExerciseAttempt.user_id == user.id, |
|
90 |
ExerciseAttempt.active == True, |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
91 |
ExerciseAttempt.ws_ex_id == worksheet_exercise.id |
1080.1.57
by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from |
92 |
).order_by(Asc(ExerciseAttempt.date)).last() |
93 |
||
94 |
# Pick the most recent of these two
|
|
95 |
if saved is not None: |
|
96 |
if attempt is not None: |
|
97 |
return saved if saved.date > attempt.date else attempt |
|
98 |
else: |
|
99 |
return saved |
|
100 |
else: |
|
101 |
if attempt is not None: |
|
102 |
return attempt |
|
103 |
else: |
|
104 |
return None |
|
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
105 |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
106 |
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
107 |
allow_inactive=False): |
108 |
"""Same as get_exercise_attempts, but doesn't convert Storm's iterator
|
|
109 |
into a list."""
|
|
110 |
||
111 |
# Get the most recent attempt before as_of, or None
|
|
112 |
return store.find(ExerciseAttempt, |
|
113 |
ExerciseAttempt.user_id == user.id, |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
114 |
ExerciseAttempt.ws_ex_id == worksheet_exercise.id, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
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)) |
|
118 |
||
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
119 |
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
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.
|
|
124 |
||
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.
|
|
128 |
"""
|
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
129 |
return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
130 |
allow_inactive)) |
131 |
||
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
132 |
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
133 |
allow_inactive=False): |
1099.1.198
by William Grant
Fix retrieval of old exercise attempts. |
134 |
"""Given a storm.store, User and WorksheetExercise, returns an
|
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
135 |
ivle.database.ExerciseAttempt object for the last submitted attempt for
|
136 |
this question.
|
|
137 |
Returns None if the user has not made an attempt on this
|
|
138 |
problem.
|
|
139 |
||
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.
|
|
143 |
"""
|
|
1099.1.198
by William Grant
Fix retrieval of old exercise attempts. |
144 |
return _get_exercise_attempts(store, user, worksheet_exercise, as_of, |
1080.1.58
by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt. |
145 |
allow_inactive).first() |
1080.1.60
by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of |
146 |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
147 |
def save_exercise(store, user, worksheet_exercise, text, date): |
1080.1.88
by William Grant
ivle.worksheet: Add a save_exercise function. |
148 |
"""Save an exercise for a user.
|
149 |
||
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
150 |
Given a store, User, WorksheetExercise, text and date, save the text to the
|
1080.1.88
by William Grant
ivle.worksheet: Add a save_exercise function. |
151 |
database. This will create the ExerciseSave if needed.
|
152 |
"""
|
|
153 |
saved = store.find(ivle.database.ExerciseSave, |
|
154 |
ivle.database.ExerciseSave.user_id == user.id, |
|
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
155 |
ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id |
1099.1.150
by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and |
156 |
).one() |
1080.1.88
by William Grant
ivle.worksheet: Add a save_exercise function. |
157 |
if saved is None: |
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
158 |
saved = ivle.database.ExerciseSave(user=user, |
159 |
worksheet_exercise=worksheet_exercise) |
|
1080.1.88
by William Grant
ivle.worksheet: Add a save_exercise function. |
160 |
store.add(saved) |
161 |
||
162 |
saved.date = date |
|
163 |
saved.text = text |
|
164 |
||
1080.1.60
by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of |
165 |
def calculate_score(store, user, worksheet): |
166 |
"""
|
|
167 |
Given a storm.store, User, Exercise and Worksheet, calculates a score for
|
|
168 |
the user on the given worksheet.
|
|
169 |
Returns a 4-tuple of ints, consisting of:
|
|
170 |
(No. mandatory exercises completed,
|
|
171 |
Total no. mandatory exercises,
|
|
172 |
No. optional exercises completed,
|
|
173 |
Total no. optional exercises)
|
|
174 |
"""
|
|
175 |
mand_done = 0 |
|
176 |
mand_total = 0 |
|
177 |
opt_done = 0 |
|
178 |
opt_total = 0 |
|
179 |
||
180 |
# Get the student's pass/fail for each exercise in this worksheet
|
|
181 |
for worksheet_exercise in worksheet.worksheet_exercises: |
|
182 |
exercise = worksheet_exercise.exercise |
|
1099.4.3
by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets |
183 |
worksheet = worksheet_exercise.worksheet |
1080.1.60
by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of |
184 |
optional = worksheet_exercise.optional |
185 |
||
1099.1.180
by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively |
186 |
done, _ = get_exercise_status(store, user, worksheet_exercise) |
1080.1.60
by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of |
187 |
# done is a bool, whether this student has completed that problem
|
188 |
if optional: |
|
189 |
opt_total += 1 |
|
190 |
if done: opt_done += 1 |
|
191 |
else: |
|
192 |
mand_total += 1 |
|
193 |
if done: mand_done += 1 |
|
194 |
||
195 |
return mand_done, mand_total, opt_done, opt_total |
|
1102
by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet, |
196 |
|
197 |
def update_exerciselist(worksheet): |
|
198 |
"""Runs through the worksheetstream, generating the appropriate
|
|
199 |
WorksheetExercises, and de-activating the old ones."""
|
|
200 |
exercises = [] |
|
201 |
# Turns the worksheet into an xml stream, and then finds all the
|
|
202 |
# exercise nodes in the stream.
|
|
1099.1.220
by Nick Chadwick
Merged from trunk |
203 |
worksheetdata = genshi.XML(worksheet.get_xml()) |
1102
by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet, |
204 |
for kind, data, pos in worksheetdata: |
205 |
if kind is genshi.core.START: |
|
206 |
# Data is a tuple of tag name and a list of name->value tuples
|
|
207 |
if data[0] == 'exercise': |
|
208 |
src = "" |
|
209 |
optional = False |
|
210 |
for attr in data[1]: |
|
211 |
if attr[0] == 'src': |
|
212 |
src = attr[1] |
|
213 |
if attr[0] == 'optional': |
|
214 |
optional = attr[1] == 'true' |
|
215 |
if src != "": |
|
216 |
exercises.append((src, optional)) |
|
217 |
ex_num = 0 |
|
218 |
# Set all current worksheet_exercises to be inactive
|
|
219 |
db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise, |
|
220 |
WorksheetExercise.worksheet_id == worksheet.id) |
|
221 |
for worksheet_exercise in db_worksheet_exercises: |
|
222 |
worksheet_exercise.active = False |
|
223 |
||
224 |
for exerciseid, optional in exercises: |
|
225 |
worksheet_exercise = Store.of(worksheet).find(WorksheetExercise, |
|
226 |
WorksheetExercise.worksheet_id == worksheet.id, |
|
227 |
Exercise.id == WorksheetExercise.exercise_id, |
|
228 |
Exercise.id == exerciseid).one() |
|
229 |
if worksheet_exercise is None: |
|
230 |
exercise = Store.of(worksheet).find(Exercise, |
|
231 |
Exercise.id == exerciseid |
|
232 |
).one() |
|
233 |
if exercise is None: |
|
234 |
raise NotFound() |
|
235 |
worksheet_exercise = WorksheetExercise() |
|
236 |
worksheet_exercise.worksheet_id = worksheet.id |
|
237 |
worksheet_exercise.exercise_id = exercise.id |
|
238 |
Store.of(worksheet).add(worksheet_exercise) |
|
239 |
worksheet_exercise.active = True |
|
240 |
worksheet_exercise.seq_no = ex_num |
|
241 |
worksheet_exercise.optional = optional |
|
242 |