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

1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
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
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
26
from storm.locals import And, Asc, Desc, Store
27
import genshi
28
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
29
import ivle.database
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
30
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
31
                          WorksheetExercise, Exercise
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
32
1080.1.59 by Matt Giuca
ivle.worksheet, ivle.database: Added/updated __all__.
33
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
34
           'get_exercise_attempts', 'get_exercise_attempt',
35
          ]
36
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
37
def get_exercise_status(store, user, worksheet_exercise, as_of=None):
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
38
    """Given a storm.store, User and Exercise, returns information about
39
    the user's performance on that problem.
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
40
    @param store: A storm.store
41
    @param user: A User.
42
    @param worksheet_exercise: An Exercise.
43
    @param as_of: Optional datetime. If supplied, gets the status as of as_of.
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
44
    Returns a tuple of:
45
        - A boolean, whether they have successfully passed this exercise.
46
        - An int, the number of attempts they have made up to and
47
          including the first successful attempt (or the total number of
48
          attempts, if not yet successful).
49
    """
50
    # A Storm expression denoting all active attempts by this user for this
51
    # exercise.
52
    is_relevant = ((ExerciseAttempt.user_id == user.id) &
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
53
            (ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
54
            (ExerciseAttempt.active == True))
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
55
    if as_of is not None:
56
        is_relevant &= ExerciseAttempt.date <= as_of
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
57
58
    # Get the first successful active attempt, or None if no success yet.
59
    # (For this user, for this exercise).
60
    first_success = store.find(ExerciseAttempt, is_relevant,
61
            ExerciseAttempt.complete == True
62
        ).order_by(Asc(ExerciseAttempt.date)).first()
63
64
    if first_success is not None:
65
        # Get the total number of active attempts up to and including the
66
        # first successful attempt.
67
        # (Subsequent attempts don't count, because the user had already
68
        # succeeded by then).
69
        num_attempts = store.find(ExerciseAttempt, is_relevant,
70
                ExerciseAttempt.date <= first_success.date).count()
71
    else:
72
        # User has not yet succeeded.
73
        # Get the total number of active attempts.
74
        num_attempts = store.find(ExerciseAttempt, is_relevant).count()
75
76
    return first_success is not None, num_attempts
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
77
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
78
def get_exercise_stored_text(store, user, worksheet_exercise):
79
    """Given a storm.store, User and WorksheetExercise, returns an
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
80
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
81
    this question (note that ExerciseAttempt is a subclass of ExerciseSave).
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
82
    Returns None if the user has not saved or made an attempt on this
83
    problem.
84
    If the user has both saved and submitted, it returns whichever was
85
    made last.
86
    """
87
88
    # Get the saved text, or None
89
    saved = store.find(ExerciseSave,
90
                ExerciseSave.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
91
                ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
92
93
    # Get the most recent attempt, or None
94
    attempt = store.find(ExerciseAttempt,
95
            ExerciseAttempt.user_id == user.id,
96
            ExerciseAttempt.active == True,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
97
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
98
        ).order_by(Asc(ExerciseAttempt.date)).last()
99
100
    # Pick the most recent of these two
101
    if saved is not None:
102
        if attempt is not None:
103
            return saved if saved.date > attempt.date else attempt
104
        else:
105
            return saved
106
    else:
107
        if attempt is not None:
108
            return attempt
109
        else:
110
            return None
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
111
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
112
def _get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
113
        allow_inactive=False):
114
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
115
    into a list."""
116
117
    # Get the most recent attempt before as_of, or None
118
    return store.find(ExerciseAttempt,
119
            ExerciseAttempt.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
120
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
121
            True if allow_inactive else ExerciseAttempt.active == True,
122
            True if as_of is None else ExerciseAttempt.date <= as_of,
123
        ).order_by(Desc(ExerciseAttempt.date))
124
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
125
def get_exercise_attempts(store, user, worksheet_exercise, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
126
        allow_inactive=False):
127
    """Given a storm.store, User and Exercise, returns a list of
128
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
129
    exercise, sorted from latest to earliest.
130
131
    as_of: Optional datetime.datetime object. If supplied, only returns
132
        attempts made before or at this time.
133
    allow_inactive: If True, will return disabled attempts.
134
    """
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
135
    return list(_get_exercise_attempts(store, user, worksheet_exercise, as_of,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
136
        allow_inactive))
137
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
138
def get_exercise_attempt(store, user, worksheet_exercise, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
139
        allow_inactive=False):
1099.1.198 by William Grant
Fix retrieval of old exercise attempts.
140
    """Given a storm.store, User and WorksheetExercise, returns an
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
141
    ivle.database.ExerciseAttempt object for the last submitted attempt for
142
    this question.
143
    Returns None if the user has not made an attempt on this
144
    problem.
145
146
    as_of: Optional datetime.datetime object. If supplied, only returns
147
        attempts made before or at this time.
148
    allow_inactive: If True, will return disabled attempts.
149
    """
1099.1.198 by William Grant
Fix retrieval of old exercise attempts.
150
    return _get_exercise_attempts(store, user, worksheet_exercise, as_of,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
151
        allow_inactive).first()
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
152
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
153
def save_exercise(store, user, worksheet_exercise, text, date):
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
154
    """Save an exercise for a user.
155
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
156
    Given a store, User, WorksheetExercise, text and date, save the text to the
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
157
    database. This will create the ExerciseSave if needed.
158
    """
159
    saved = store.find(ivle.database.ExerciseSave,
160
                ivle.database.ExerciseSave.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
161
                ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
162
                ).one()
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
163
    if saved is None:
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
164
        saved = ivle.database.ExerciseSave(user=user, 
165
                                        worksheet_exercise=worksheet_exercise)
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
166
        store.add(saved)
167
168
    saved.date = date
169
    saved.text = text
170
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
171
def calculate_score(store, user, worksheet, as_of=None):
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
172
    """
173
    Given a storm.store, User, Exercise and Worksheet, calculates a score for
174
    the user on the given worksheet.
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
175
    @param store: A storm.store
176
    @param user: A User.
177
    @param worksheet: A Worksheet.
178
    @param as_of: Optional datetime. If supplied, gets the score as of as_of.
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
179
    Returns a 4-tuple of ints, consisting of:
180
    (No. mandatory exercises completed,
181
     Total no. mandatory exercises,
182
     No. optional exercises completed,
183
     Total no. optional exercises)
184
    """
185
    mand_done = 0
186
    mand_total = 0
187
    opt_done = 0
188
    opt_total = 0
189
190
    # Get the student's pass/fail for each exercise in this worksheet
191
    for worksheet_exercise in worksheet.worksheet_exercises:
192
        exercise = worksheet_exercise.exercise
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
193
        worksheet = worksheet_exercise.worksheet
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
194
        optional = worksheet_exercise.optional
195
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
196
        done, _ = get_exercise_status(store, user, worksheet_exercise, as_of)
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
197
        # done is a bool, whether this student has completed that problem
198
        if optional:
199
            opt_total += 1
200
            if done: opt_done += 1
201
        else:
202
            mand_total += 1
203
            if done: mand_done += 1
204
205
    return mand_done, mand_total, opt_done, opt_total
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
206
1195.1.13 by Matt Giuca
ivle.worksheet.utils: Added calculate_mark, which is from the duplicated code
207
def calculate_mark(mand_done, mand_total):
208
    """Calculate a subject mark, given the result of all worksheets.
209
    @param mand_done: The total number of mandatory exercises completed by
210
        some student, across all worksheets.
211
    @param mand_total: The total number of mandatory exercises across all
212
        worksheets in the offering.
213
    @return: (percent, mark, mark_total)
214
        percent: The percentage of exercises the student has completed, as an
215
            integer between 0 and 100 inclusive.
216
        mark: The mark the student has received, based on the percentage.
217
        mark_total: The total number of marks available (currently hard-coded
218
            as 5).
219
    """
220
    # We want to display a students mark out of 5. However, they are
221
    # allowed to skip 1 in 5 questions and still get 'full marks'.
222
    # Hence we divide by 16, essentially making 16 percent worth
223
    # 1 star, and 80 or above worth 5.
1195.1.15 by Matt Giuca
ivle-marks, ivle.worksheet.utils: Fixed Divide-by-zero exception if there are
224
    if mand_total > 0:
225
        percent_int = (100 * mand_done) // mand_total
226
    else:
227
        # Avoid Div0, just give everyone 0 marks if there are none available
228
        percent_int = 0
1195.1.13 by Matt Giuca
ivle.worksheet.utils: Added calculate_mark, which is from the duplicated code
229
    # percent / 16, rounded down, with a maximum mark of 5
230
    max_mark = 5
231
    mark = min(percent_int // 16, max_mark)
232
    return (percent_int, mark, max_mark)
233
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
234
def update_exerciselist(worksheet):
235
    """Runs through the worksheetstream, generating the appropriate
236
    WorksheetExercises, and de-activating the old ones."""
237
    exercises = []
238
    # Turns the worksheet into an xml stream, and then finds all the 
239
    # exercise nodes in the stream.
1099.1.220 by Nick Chadwick
Merged from trunk
240
    worksheetdata = genshi.XML(worksheet.get_xml())
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
241
    for kind, data, pos in worksheetdata:
242
        if kind is genshi.core.START:
243
            # Data is a tuple of tag name and a list of name->value tuples
244
            if data[0] == 'exercise':
245
                src = ""
246
                optional = False
247
                for attr in data[1]:
248
                    if attr[0] == 'src':
249
                        src = attr[1]
250
                    if attr[0] == 'optional':
251
                        optional = attr[1] == 'true'
252
                if src != "":
253
                    exercises.append((src, optional))
254
    ex_num = 0
255
    # Set all current worksheet_exercises to be inactive
256
    db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise,
257
        WorksheetExercise.worksheet_id == worksheet.id)
258
    for worksheet_exercise in db_worksheet_exercises:
259
        worksheet_exercise.active = False
260
    
261
    for exerciseid, optional in exercises:
262
        worksheet_exercise = Store.of(worksheet).find(WorksheetExercise,
263
            WorksheetExercise.worksheet_id == worksheet.id,
264
            Exercise.id == WorksheetExercise.exercise_id,
265
            Exercise.id == exerciseid).one()
266
        if worksheet_exercise is None:
267
            exercise = Store.of(worksheet).find(Exercise,
268
                Exercise.id == exerciseid
269
            ).one()
270
            if exercise is None:
271
                raise NotFound()
272
            worksheet_exercise = WorksheetExercise()
273
            worksheet_exercise.worksheet_id = worksheet.id
274
            worksheet_exercise.exercise_id = exercise.id
275
            Store.of(worksheet).add(worksheet_exercise)
276
        worksheet_exercise.active = True
277
        worksheet_exercise.seq_no = ex_num
278
        worksheet_exercise.optional = optional
279