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

« back to all changes in this revision

Viewing changes to ivle/worksheet.py

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

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