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

« back to all changes in this revision

Viewing changes to ivle/worksheet.py

  • Committer: mattgiuca
  • Date: 2008-02-24 22:09:16 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:553
Added new app: Settings (UI for userservice).
    (Added app dir, media dir with empty JS file, icon).
apps/__init__.py: Added error message.
dispatch/__init__.py: Added error message.
dispatch/html.py: Added link to Settings at the top
    (a special app).
    Fixed HTML icons (correct size link).
    Catch keyerror on icon which would otherwise be thrown all
    the way up on certain errors.

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
 
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
29
 
 
30
 
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
31
 
           'get_exercise_attempts', 'get_exercise_attempt',
32
 
          ]
33
 
 
34
 
def get_exercise_status(store, user, worksheet_exercise):
35
 
    """Given a storm.store, User and Exercise, returns information about
36
 
    the user's performance on that problem.
37
 
    Returns a tuple of:
38
 
        - A boolean, whether they have successfully passed this exercise.
39
 
        - An int, the number of attempts they have made up to and
40
 
          including the first successful attempt (or the total number of
41
 
          attempts, if not yet successful).
42
 
    """
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.ws_ex_id == worksheet_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, worksheet_exercise):
70
 
    """Given a storm.store, User and WorksheetExercise, 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
 
 
79
 
    # Get the saved text, or None
80
 
    saved = store.find(ExerciseSave,
81
 
                ExerciseSave.user_id == user.id,
82
 
                ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
83
 
 
84
 
    # Get the most recent attempt, or None
85
 
    attempt = store.find(ExerciseAttempt,
86
 
            ExerciseAttempt.user_id == user.id,
87
 
            ExerciseAttempt.active == True,
88
 
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id
89
 
        ).order_by(Asc(ExerciseAttempt.date)).last()
90
 
 
91
 
    # Pick the most recent of these two
92
 
    if saved is not None:
93
 
        if attempt is not None:
94
 
            return saved if saved.date > attempt.date else attempt
95
 
        else:
96
 
            return saved
97
 
    else:
98
 
        if attempt is not None:
99
 
            return attempt
100
 
        else:
101
 
            return None
102
 
 
103
 
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
104
 
        allow_inactive=False):
105
 
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
106
 
    into a list."""
107
 
 
108
 
    # Get the most recent attempt before as_of, or None
109
 
    return store.find(ExerciseAttempt,
110
 
            ExerciseAttempt.user_id == user.id,
111
 
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
112
 
            True if allow_inactive else ExerciseAttempt.active == True,
113
 
            True if as_of is None else ExerciseAttempt.date <= as_of,
114
 
        ).order_by(Desc(ExerciseAttempt.date))
115
 
 
116
 
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
117
 
        allow_inactive=False):
118
 
    """Given a storm.store, User and Exercise, returns a list of
119
 
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
120
 
    exercise, sorted from latest to earliest.
121
 
 
122
 
    as_of: Optional datetime.datetime object. If supplied, only returns
123
 
        attempts made before or at this time.
124
 
    allow_inactive: If True, will return disabled attempts.
125
 
    """
126
 
    return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
127
 
        allow_inactive))
128
 
 
129
 
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
130
 
        allow_inactive=False):
131
 
    """Given a storm.store, User and WorksheetExercise, returns an
132
 
    ivle.database.ExerciseAttempt object for the last submitted attempt for
133
 
    this question.
134
 
    Returns None if the user has not made an attempt on this
135
 
    problem.
136
 
 
137
 
    as_of: Optional datetime.datetime object. If supplied, only returns
138
 
        attempts made before or at this time.
139
 
    allow_inactive: If True, will return disabled attempts.
140
 
    """
141
 
    return _get_exercise_attempts(store, user, worksheet_exercise, as_of,
142
 
        allow_inactive).first()
143
 
 
144
 
def save_exercise(store, user, worksheet_exercise, text, date):
145
 
    """Save an exercise for a user.
146
 
 
147
 
    Given a store, User, WorksheetExercise, text and date, save the text to the
148
 
    database. This will create the ExerciseSave if needed.
149
 
    """
150
 
    saved = store.find(ivle.database.ExerciseSave,
151
 
                ivle.database.ExerciseSave.user_id == user.id,
152
 
                ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
153
 
                ).one()
154
 
    if saved is None:
155
 
        saved = ivle.database.ExerciseSave(user=user, 
156
 
                                        worksheet_exercise=worksheet_exercise)
157
 
        store.add(saved)
158
 
 
159
 
    saved.date = date
160
 
    saved.text = text
161
 
 
162
 
def calculate_score(store, user, worksheet):
163
 
    """
164
 
    Given a storm.store, User, Exercise and Worksheet, calculates a score for
165
 
    the user on the given worksheet.
166
 
    Returns a 4-tuple of ints, consisting of:
167
 
    (No. mandatory exercises completed,
168
 
     Total no. mandatory exercises,
169
 
     No. optional exercises completed,
170
 
     Total no. optional exercises)
171
 
    """
172
 
    mand_done = 0
173
 
    mand_total = 0
174
 
    opt_done = 0
175
 
    opt_total = 0
176
 
 
177
 
    # Get the student's pass/fail for each exercise in this worksheet
178
 
    for worksheet_exercise in worksheet.worksheet_exercises:
179
 
        exercise = worksheet_exercise.exercise
180
 
        worksheet = worksheet_exercise.worksheet
181
 
        optional = worksheet_exercise.optional
182
 
 
183
 
        done, _ = get_exercise_status(store, user, worksheet_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