~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
26
from storm.locals import And, Asc, Desc
27
import ivle.database
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
28
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
29
1080.1.59 by Matt Giuca
ivle.worksheet, ivle.database: Added/updated __all__.
30
__all__ = ['get_exercise_status', 'get_exercise_stored_text',
31
           'get_exercise_attempts', 'get_exercise_attempt',
32
          ]
33
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
34
def get_exercise_status(store, user, exercise, worksheet):
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
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.exercise_id == exercise.id) &
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
47
                   (ExerciseAttempt.active == True) &
48
                   (ExerciseAttempt.worksheetid == worksheet.id))
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
49
50
    # Get the first successful active attempt, or None if no success yet.
51
    # (For this user, for this exercise).
52
    first_success = store.find(ExerciseAttempt, is_relevant,
53
            ExerciseAttempt.complete == True
54
        ).order_by(Asc(ExerciseAttempt.date)).first()
55
56
    if first_success is not None:
57
        # Get the total number of active attempts up to and including the
58
        # first successful attempt.
59
        # (Subsequent attempts don't count, because the user had already
60
        # succeeded by then).
61
        num_attempts = store.find(ExerciseAttempt, is_relevant,
62
                ExerciseAttempt.date <= first_success.date).count()
63
    else:
64
        # User has not yet succeeded.
65
        # Get the total number of active attempts.
66
        num_attempts = store.find(ExerciseAttempt, is_relevant).count()
67
68
    return first_success is not None, num_attempts
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
69
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
70
def get_exercise_stored_text(store, user, exercise, worksheet):
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
71
    """Given a storm.store, User and Exercise, returns an
72
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
73
    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
74
    Returns None if the user has not saved or made an attempt on this
75
    problem.
76
    If the user has both saved and submitted, it returns whichever was
77
    made last.
78
    """
79
80
    # Get the saved text, or None
81
    saved = store.find(ExerciseSave,
82
                ExerciseSave.user_id == user.id,
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
83
                ExerciseSave.exercise_id == exercise.id,
84
                ExerciseSave.worksheetid == worksheet.id).one()
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
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,
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
90
            ExerciseAttempt.worksheetid == worksheet.id,
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
91
            ExerciseAttempt.active == True,
92
        ).order_by(Asc(ExerciseAttempt.date)).last()
93
94
    # Pick the most recent of these two
95
    if saved is not None:
96
        if attempt is not None:
97
            return saved if saved.date > attempt.date else attempt
98
        else:
99
            return saved
100
    else:
101
        if attempt is not None:
102
            return attempt
103
        else:
104
            return None
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
105
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
106
def _get_exercise_attempts(store, user, exercise, worksheet, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
107
        allow_inactive=False):
108
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
109
    into a list."""
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,
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
115
            ExerciseAttempt.worksheetid == worksheet.id,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
116
            True if allow_inactive else ExerciseAttempt.active == True,
117
            True if as_of is None else ExerciseAttempt.date <= as_of,
118
        ).order_by(Desc(ExerciseAttempt.date))
119
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
120
def get_exercise_attempts(store, user, exercise, worksheet, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
121
        allow_inactive=False):
122
    """Given a storm.store, User and Exercise, returns a list of
123
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
124
    exercise, sorted from latest to earliest.
125
126
    as_of: Optional datetime.datetime object. If supplied, only returns
127
        attempts made before or at this time.
128
    allow_inactive: If True, will return disabled attempts.
129
    """
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
130
    return list(_get_exercise_attempts(store, user, exercise, worksheet, as_of,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
131
        allow_inactive))
132
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
133
def get_exercise_attempt(store, user, exercise, worksheet, as_of=None,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
134
        allow_inactive=False):
135
    """Given a storm.store, User and Exercise, returns an
136
    ivle.database.ExerciseAttempt object for the last submitted attempt for
137
    this question.
138
    Returns None if the user has not made an attempt on this
139
    problem.
140
141
    as_of: Optional datetime.datetime object. If supplied, only returns
142
        attempts made before or at this time.
143
    allow_inactive: If True, will return disabled attempts.
144
    """
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
145
    return _get_exercise_attempts(store, user, exercise, worksheet, as_of,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
146
        allow_inactive).first()
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
147
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
148
def save_exercise(store, user, exercise, worksheet, text, date):
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
149
    """Save an exercise for a user.
150
151
    Given a store, User, Exercise and text and date, save the text to the
152
    database. This will create the ExerciseSave if needed.
153
    """
154
    saved = store.find(ivle.database.ExerciseSave,
155
                ivle.database.ExerciseSave.user_id == user.id,
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
156
                ivle.database.ExerciseSave.exercise_id == exercise.id,
157
                ivle.database.ExerciseSave.worksheetid == worksheet.id
158
                ).one()
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
159
    if saved is None:
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
160
        saved = ivle.database.ExerciseSave(user=user, exercise=exercise, 
161
                                           worksheet=worksheet)
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
162
        store.add(saved)
163
164
    saved.date = date
165
    saved.text = text
166
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
167
def calculate_score(store, user, worksheet):
168
    """
169
    Given a storm.store, User, Exercise and Worksheet, calculates a score for
170
    the user on the given worksheet.
171
    Returns a 4-tuple of ints, consisting of:
172
    (No. mandatory exercises completed,
173
     Total no. mandatory exercises,
174
     No. optional exercises completed,
175
     Total no. optional exercises)
176
    """
177
    mand_done = 0
178
    mand_total = 0
179
    opt_done = 0
180
    opt_total = 0
181
182
    # Get the student's pass/fail for each exercise in this worksheet
183
    for worksheet_exercise in worksheet.worksheet_exercises:
184
        exercise = worksheet_exercise.exercise
185
        optional = worksheet_exercise.optional
186
187
        done, _ = get_exercise_status(store, user, exercise)
188
        # done is a bool, whether this student has completed that problem
189
        if optional:
190
            opt_total += 1
191
            if done: opt_done += 1
192
        else:
193
            mand_total += 1
194
            if done: mand_done += 1
195
196
    return mand_done, mand_total, opt_done, opt_total