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

« back to all changes in this revision

Viewing changes to ivle/worksheet.py

This commit changes the tutorial service, which now almost exclusively
uses the database to store its data.

Whilst the modifications to tutorial are not yet complete, this commit
should stabilise the database model.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
           'get_exercise_attempts', 'get_exercise_attempt',
32
32
          ]
33
33
 
34
 
def get_exercise_status(store, user, exercise, worksheet):
 
34
def get_exercise_status(store, user, worksheet_exercise):
35
35
    """Given a storm.store, User and Exercise, returns information about
36
36
    the user's performance on that problem.
37
37
    Returns a tuple of:
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) &
48
 
                   (ExerciseAttempt.worksheetid == worksheet.id))
 
46
            (ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
 
47
            (ExerciseAttempt.active == True))
49
48
 
50
49
    # Get the first successful active attempt, or None if no success yet.
51
50
    # (For this user, for this exercise).
67
66
 
68
67
    return first_success is not None, num_attempts
69
68
 
70
 
def get_exercise_stored_text(store, user, exercise, worksheet):
71
 
    """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
72
71
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
73
72
    this question (note that ExerciseAttempt is a subclass of ExerciseSave).
74
73
    Returns None if the user has not saved or made an attempt on this
80
79
    # Get the saved text, or None
81
80
    saved = store.find(ExerciseSave,
82
81
                ExerciseSave.user_id == user.id,
83
 
                ExerciseSave.exercise_id == exercise.id,
84
 
                ExerciseSave.worksheetid == worksheet.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
 
            ExerciseAttempt.worksheetid == worksheet.id,
91
87
            ExerciseAttempt.active == True,
 
88
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id
92
89
        ).order_by(Asc(ExerciseAttempt.date)).last()
93
90
 
94
91
    # Pick the most recent of these two
103
100
        else:
104
101
            return None
105
102
 
106
 
def _get_exercise_attempts(store, user, exercise, worksheet, as_of=None,
 
103
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
107
104
        allow_inactive=False):
108
105
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
109
106
    into a list."""
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,
115
 
            ExerciseAttempt.worksheetid == worksheet.id,
 
111
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
116
112
            True if allow_inactive else ExerciseAttempt.active == True,
117
113
            True if as_of is None else ExerciseAttempt.date <= as_of,
118
114
        ).order_by(Desc(ExerciseAttempt.date))
119
115
 
120
 
def get_exercise_attempts(store, user, exercise, worksheet, as_of=None,
 
116
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
121
117
        allow_inactive=False):
122
118
    """Given a storm.store, User and Exercise, returns a list of
123
119
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
127
123
        attempts made before or at this time.
128
124
    allow_inactive: If True, will return disabled attempts.
129
125
    """
130
 
    return list(_get_exercise_attempts(store, user, exercise, worksheet, as_of,
 
126
    return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
131
127
        allow_inactive))
132
128
 
133
 
def get_exercise_attempt(store, user, exercise, worksheet, as_of=None,
 
129
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
134
130
        allow_inactive=False):
135
131
    """Given a storm.store, User and Exercise, returns an
136
132
    ivle.database.ExerciseAttempt object for the last submitted attempt for
145
141
    return _get_exercise_attempts(store, user, exercise, worksheet, as_of,
146
142
        allow_inactive).first()
147
143
 
148
 
def save_exercise(store, user, exercise, worksheet, text, date):
 
144
def save_exercise(store, user, worksheet_exercise, text, date):
149
145
    """Save an exercise for a user.
150
146
 
151
 
    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
152
148
    database. This will create the ExerciseSave if needed.
153
149
    """
154
150
    saved = store.find(ivle.database.ExerciseSave,
155
151
                ivle.database.ExerciseSave.user_id == user.id,
156
 
                ivle.database.ExerciseSave.exercise_id == exercise.id,
157
 
                ivle.database.ExerciseSave.worksheetid == worksheet.id
 
152
                ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
158
153
                ).one()
159
154
    if saved is None:
160
 
        saved = ivle.database.ExerciseSave(user=user, exercise=exercise, 
161
 
                                           worksheet=worksheet)
 
155
        saved = ivle.database.ExerciseSave(user=user, 
 
156
                                        worksheet_exercise=worksheet_exercise)
162
157
        store.add(saved)
163
158
 
164
159
    saved.date = date
182
177
    # Get the student's pass/fail for each exercise in this worksheet
183
178
    for worksheet_exercise in worksheet.worksheet_exercises:
184
179
        exercise = worksheet_exercise.exercise
 
180
        worksheet = worksheet_exercise.worksheet
185
181
        optional = worksheet_exercise.optional
186
182
 
187
 
        done, _ = get_exercise_status(store, user, exercise)
 
183
        done, _ = get_exercise_status(store, user, worksheet_exercise)
188
184
        # done is a bool, whether this student has completed that problem
189
185
        if optional:
190
186
            opt_total += 1