~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/worksheet.py

  • Committer: Matt Giuca
  • Date: 2009-02-25 03:04:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1119.
  • Revision ID: matt.giuca@gmail.com-20090225030408-5e13v4felqc4aybr
(almost) all HTML pages updated: Moved <h1>s outside of the ivle-padding div.
    Added new <h1>s to pages which didn't have any, and reduced <h1s> down to
    <h2s> if they didn't represent the page heading.

h1s now represent the top-level heading of a page. The new style sheet styles
them specially, designed to appear directly underneath the tab bar. Therefore,
they need to appear outside of any padding.

util.py: Removed the <h1> from the default-generated terms of service page.
    (All other TOS pages should follow). There is now a <h1> at the top of the
    TOS template page instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
This module provides functions for tutorial and worksheet computations.
24
24
"""
25
25
 
26
 
from storm.locals import And, Asc, Desc
 
26
from storm.locals import And, Asc, Desc, Store
 
27
import genshi
 
28
 
27
29
import ivle.database
 
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
 
31
                          WorksheetExercise, Exercise
28
32
 
29
33
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
30
34
           'get_exercise_attempts', 'get_exercise_attempt',
31
35
          ]
32
36
 
33
 
def get_exercise_status(store, user, exercise):
 
37
def get_exercise_status(store, user, worksheet_exercise):
34
38
    """Given a storm.store, User and Exercise, returns information about
35
39
    the user's performance on that problem.
36
40
    Returns a tuple of:
39
43
          including the first successful attempt (or the total number of
40
44
          attempts, if not yet successful).
41
45
    """
42
 
    ExerciseAttempt = ivle.database.ExerciseAttempt
43
46
    # A Storm expression denoting all active attempts by this user for this
44
47
    # exercise.
45
48
    is_relevant = ((ExerciseAttempt.user_id == user.id) &
46
 
                   (ExerciseAttempt.exercise_id == exercise.id) &
47
 
                   (ExerciseAttempt.active == True))
 
49
            (ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
 
50
            (ExerciseAttempt.active == True))
48
51
 
49
52
    # Get the first successful active attempt, or None if no success yet.
50
53
    # (For this user, for this exercise).
66
69
 
67
70
    return first_success is not None, num_attempts
68
71
 
69
 
def get_exercise_stored_text(store, user, exercise):
70
 
    """Given a storm.store, User and Exercise, returns an
 
72
def get_exercise_stored_text(store, user, worksheet_exercise):
 
73
    """Given a storm.store, User and WorksheetExercise, returns an
71
74
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
72
75
    this question (note that ExerciseAttempt is a subclass of ExerciseSave).
73
76
    Returns None if the user has not saved or made an attempt on this
75
78
    If the user has both saved and submitted, it returns whichever was
76
79
    made last.
77
80
    """
78
 
    ExerciseSave = ivle.database.ExerciseSave
79
 
    ExerciseAttempt = ivle.database.ExerciseAttempt
80
81
 
81
82
    # Get the saved text, or None
82
83
    saved = store.find(ExerciseSave,
83
84
                ExerciseSave.user_id == user.id,
84
 
                ExerciseSave.exercise_id == exercise.id).one()
 
85
                ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
85
86
 
86
87
    # Get the most recent attempt, or None
87
88
    attempt = store.find(ExerciseAttempt,
88
89
            ExerciseAttempt.user_id == user.id,
89
 
            ExerciseAttempt.exercise_id == exercise.id,
90
90
            ExerciseAttempt.active == True,
 
91
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id
91
92
        ).order_by(Asc(ExerciseAttempt.date)).last()
92
93
 
93
94
    # Pick the most recent of these two
102
103
        else:
103
104
            return None
104
105
 
105
 
def _get_exercise_attempts(store, user, exercise, as_of=None,
 
106
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
106
107
        allow_inactive=False):
107
108
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
108
109
    into a list."""
109
 
    ExerciseAttempt = ivle.database.ExerciseAttempt
110
110
 
111
111
    # Get the most recent attempt before as_of, or None
112
112
    return store.find(ExerciseAttempt,
113
113
            ExerciseAttempt.user_id == user.id,
114
 
            ExerciseAttempt.exercise_id == exercise.id,
 
114
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
115
115
            True if allow_inactive else ExerciseAttempt.active == True,
116
116
            True if as_of is None else ExerciseAttempt.date <= as_of,
117
117
        ).order_by(Desc(ExerciseAttempt.date))
118
118
 
119
 
def get_exercise_attempts(store, user, exercise, as_of=None,
 
119
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
120
120
        allow_inactive=False):
121
121
    """Given a storm.store, User and Exercise, returns a list of
122
122
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
126
126
        attempts made before or at this time.
127
127
    allow_inactive: If True, will return disabled attempts.
128
128
    """
129
 
    return list(_get_exercise_attempts(store, user, exercise, as_of,
 
129
    return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
130
130
        allow_inactive))
131
131
 
132
 
def get_exercise_attempt(store, user, exercise, as_of=None,
 
132
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
133
133
        allow_inactive=False):
134
 
    """Given a storm.store, User and Exercise, returns an
 
134
    """Given a storm.store, User and WorksheetExercise, returns an
135
135
    ivle.database.ExerciseAttempt object for the last submitted attempt for
136
136
    this question.
137
137
    Returns None if the user has not made an attempt on this
141
141
        attempts made before or at this time.
142
142
    allow_inactive: If True, will return disabled attempts.
143
143
    """
144
 
    return _get_exercise_attempts(store, user, exercise, as_of,
 
144
    return _get_exercise_attempts(store, user, worksheet_exercise, as_of,
145
145
        allow_inactive).first()
146
146
 
147
 
def save_exercise(store, user, exercise, text, date):
 
147
def save_exercise(store, user, worksheet_exercise, text, date):
148
148
    """Save an exercise for a user.
149
149
 
150
 
    Given a store, User, Exercise and text and date, save the text to the
 
150
    Given a store, User, WorksheetExercise, text and date, save the text to the
151
151
    database. This will create the ExerciseSave if needed.
152
152
    """
153
153
    saved = store.find(ivle.database.ExerciseSave,
154
154
                ivle.database.ExerciseSave.user_id == user.id,
155
 
                ivle.database.ExerciseSave.exercise_id == exercise.id).one()
 
155
                ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
 
156
                ).one()
156
157
    if saved is None:
157
 
        saved = ivle.database.ExerciseSave(user=user, exercise=exercise)
 
158
        saved = ivle.database.ExerciseSave(user=user, 
 
159
                                        worksheet_exercise=worksheet_exercise)
158
160
        store.add(saved)
159
161
 
160
162
    saved.date = date
178
180
    # Get the student's pass/fail for each exercise in this worksheet
179
181
    for worksheet_exercise in worksheet.worksheet_exercises:
180
182
        exercise = worksheet_exercise.exercise
 
183
        worksheet = worksheet_exercise.worksheet
181
184
        optional = worksheet_exercise.optional
182
185
 
183
 
        done, _ = get_exercise_status(store, user, exercise)
 
186
        done, _ = get_exercise_status(store, user, worksheet_exercise)
184
187
        # done is a bool, whether this student has completed that problem
185
188
        if optional:
186
189
            opt_total += 1
190
193
            if done: mand_done += 1
191
194
 
192
195
    return mand_done, mand_total, opt_done, opt_total
 
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.
 
203
    worksheetdata = genshi.XML(worksheet.data)
 
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