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

« back to all changes in this revision

Viewing changes to ivle/worksheet.py

Dispatch now generates an index for each plugin type, allowing plugins to
be written which are aware of other plugins, and other plugin types.

All view plugins now subclass from ivle.webapp.base.plugins.ViewPlugin,
as opposed to subclassing BasePlugin directly. This will allow us to
easily re-write console as an OverlayPlugin, and allow future new
plugins types to be created.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
26
from storm.locals import And, Asc, Desc
 
27
import ivle.database
 
28
 
 
29
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
 
30
           'get_exercise_attempts', 'get_exercise_attempt',
 
31
          ]
 
32
 
 
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.
 
36
    Returns a tuple of:
 
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).
 
41
    """
 
42
    ExerciseAttempt = ivle.database.ExerciseAttempt
 
43
    # A Storm expression denoting all active attempts by this user for this
 
44
    # exercise.
 
45
    is_relevant = ((ExerciseAttempt.user_id == user.id) &
 
46
                   (ExerciseAttempt.exercise_id == exercise.id) &
 
47
                   (ExerciseAttempt.active == True))
 
48
 
 
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()
 
54
 
 
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
 
59
        # succeeded by then).
 
60
        num_attempts = store.find(ExerciseAttempt, is_relevant,
 
61
                ExerciseAttempt.date <= first_success.date).count()
 
62
    else:
 
63
        # User has not yet succeeded.
 
64
        # Get the total number of active attempts.
 
65
        num_attempts = store.find(ExerciseAttempt, is_relevant).count()
 
66
 
 
67
    return first_success is not None, num_attempts
 
68
 
 
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
 
74
    problem.
 
75
    If the user has both saved and submitted, it returns whichever was
 
76
    made last.
 
77
    """
 
78
    ExerciseSave = ivle.database.ExerciseSave
 
79
    ExerciseAttempt = ivle.database.ExerciseAttempt
 
80
 
 
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()
 
85
 
 
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()
 
92
 
 
93
    # Pick the most recent of these two
 
94
    if saved is not None:
 
95
        if attempt is not None:
 
96
            return saved if saved.date > attempt.date else attempt
 
97
        else:
 
98
            return saved
 
99
    else:
 
100
        if attempt is not None:
 
101
            return attempt
 
102
        else:
 
103
            return None
 
104
 
 
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
 
108
    into a list."""
 
109
    ExerciseAttempt = ivle.database.ExerciseAttempt
 
110
 
 
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))
 
118
 
 
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.
 
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
    """
 
129
    return list(_get_exercise_attempts(store, user, exercise, as_of,
 
130
        allow_inactive))
 
131
 
 
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
 
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
    """
 
144
    return _get_exercise_attempts(store, user, exercise, as_of,
 
145
        allow_inactive).first()
 
146
 
 
147
def save_exercise(store, user, exercise, text, date):
 
148
    """Save an exercise for a user.
 
149
 
 
150
    Given a store, User, Exercise and text and date, save the text to the
 
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,
 
155
                ivle.database.ExerciseSave.exercise_id == exercise.id).one()
 
156
    if saved is None:
 
157
        saved = ivle.database.ExerciseSave(user=user, exercise=exercise)
 
158
        store.add(saved)
 
159
 
 
160
    saved.date = date
 
161
    saved.text = text
 
162
 
 
163
def calculate_score(store, user, worksheet):
 
164
    """
 
165
    Given a storm.store, User, Exercise and Worksheet, calculates a score for
 
166
    the user on the given worksheet.
 
167
    Returns a 4-tuple of ints, consisting of:
 
168
    (No. mandatory exercises completed,
 
169
     Total no. mandatory exercises,
 
170
     No. optional exercises completed,
 
171
     Total no. optional exercises)
 
172
    """
 
173
    mand_done = 0
 
174
    mand_total = 0
 
175
    opt_done = 0
 
176
    opt_total = 0
 
177
 
 
178
    # Get the student's pass/fail for each exercise in this worksheet
 
179
    for worksheet_exercise in worksheet.worksheet_exercises:
 
180
        exercise = worksheet_exercise.exercise
 
181
        optional = worksheet_exercise.optional
 
182
 
 
183
        done, _ = get_exercise_status(store, user, exercise)
 
184
        # done is a bool, whether this student has completed that problem
 
185
        if optional:
 
186
            opt_total += 1
 
187
            if done: opt_done += 1
 
188
        else:
 
189
            mand_total += 1
 
190
            if done: mand_done += 1
 
191
 
 
192
    return mand_done, mand_total, opt_done, opt_total