~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
1394.2.8 by William Grant
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().
26
import os.path
27
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
28
from storm.locals import And, Asc, Desc, Store
29
import genshi
30
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
31
import ivle.database
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
32
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
1689.1.15 by Matt Giuca
ivle.worksheet.utils: Added get_exercise_statistics function.
33
                          WorksheetExercise, Exercise, User
1394.2.8 by William Grant
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().
34
import ivle.webapp.tutorial.test
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
35
1384.1.10 by William Grant
Display a nice error message if a worksheet attempts to reference a non-existent exercise.
36
__all__ = ['ExerciseNotFound', 'get_exercise_status',
1689.1.15 by Matt Giuca
ivle.worksheet.utils: Added get_exercise_statistics function.
37
           'get_exercise_statistics',
1384.1.10 by William Grant
Display a nice error message if a worksheet attempts to reference a non-existent exercise.
38
           'get_exercise_stored_text', 'get_exercise_attempts',
1394.2.8 by William Grant
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().
39
           'get_exercise_attempt', 'test_exercise_submission',
1080.1.59 by Matt Giuca
ivle.worksheet, ivle.database: Added/updated __all__.
40
          ]
41
1384.1.10 by William Grant
Display a nice error message if a worksheet attempts to reference a non-existent exercise.
42
class ExerciseNotFound(Exception):
43
    pass
44
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
45
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
46
    """Given a storm.store, User and Exercise, returns information about
47
    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
48
    @param store: A storm.store
49
    @param user: A User.
50
    @param worksheet_exercise: An Exercise.
51
    @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
52
    Returns a tuple of:
53
        - A boolean, whether they have successfully passed this exercise.
54
        - An int, the number of attempts they have made up to and
55
          including the first successful attempt (or the total number of
56
          attempts, if not yet successful).
57
    """
58
    # A Storm expression denoting all active attempts by this user for this
59
    # exercise.
60
    is_relevant = ((ExerciseAttempt.user_id == user.id) &
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
61
            (ExerciseAttempt.ws_ex_id == worksheet_exercise.id) &
62
            (ExerciseAttempt.active == True))
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
63
    if as_of is not None:
64
        is_relevant &= ExerciseAttempt.date <= as_of
1080.1.56 by Matt Giuca
Added new module: ivle.worksheet. This will contain general functions for
65
66
    # Get the first successful active attempt, or None if no success yet.
67
    # (For this user, for this exercise).
68
    first_success = store.find(ExerciseAttempt, is_relevant,
69
            ExerciseAttempt.complete == True
70
        ).order_by(Asc(ExerciseAttempt.date)).first()
71
72
    if first_success is not None:
73
        # Get the total number of active attempts up to and including the
74
        # first successful attempt.
75
        # (Subsequent attempts don't count, because the user had already
76
        # succeeded by then).
77
        num_attempts = store.find(ExerciseAttempt, is_relevant,
78
                ExerciseAttempt.date <= first_success.date).count()
79
    else:
80
        # User has not yet succeeded.
81
        # Get the total number of active attempts.
82
        num_attempts = store.find(ExerciseAttempt, is_relevant).count()
83
84
    return first_success is not None, num_attempts
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
85
1689.1.15 by Matt Giuca
ivle.worksheet.utils: Added get_exercise_statistics function.
86
def get_exercise_statistics(store, worksheet_exercise):
87
    """Return statistics about an exercise (with respect to a given
88
    worksheet).
89
    (number of students completed, number of students attempted)."""
90
    # Count the set of Users whose ID matches an attempt in this worksheet
91
    num_completed = store.find(User, User.id == ExerciseAttempt.user_id,
92
        ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
93
        ExerciseAttempt.complete == True).config(distinct=True).count()
94
    num_attempted = store.find(User, User.id == ExerciseAttempt.user_id,
95
        ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
96
        ).config(distinct=True).count()
97
    return num_completed, num_attempted
98
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
99
def get_exercise_stored_text(store, user, worksheet_exercise):
100
    """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.
101
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
102
    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
103
    Returns None if the user has not saved or made an attempt on this
104
    problem.
105
    If the user has both saved and submitted, it returns whichever was
106
    made last.
107
    """
108
109
    # Get the saved text, or None
110
    saved = store.find(ExerciseSave,
111
                ExerciseSave.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
112
                ExerciseSave.ws_ex_id == worksheet_exercise.id).one()
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
113
114
    # Get the most recent attempt, or None
115
    attempt = store.find(ExerciseAttempt,
116
            ExerciseAttempt.user_id == user.id,
117
            ExerciseAttempt.active == True,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
118
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id
1080.1.57 by Matt Giuca
ivle.worksheet: Added get_exercise_stored_text, ported from
119
        ).order_by(Asc(ExerciseAttempt.date)).last()
120
121
    # Pick the most recent of these two
122
    if saved is not None:
123
        if attempt is not None:
124
            return saved if saved.date > attempt.date else attempt
125
        else:
126
            return saved
127
    else:
128
        if attempt is not None:
129
            return attempt
130
        else:
131
            return None
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
132
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
133
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.
134
        allow_inactive=False):
135
    """Same as get_exercise_attempts, but doesn't convert Storm's iterator
136
    into a list."""
137
138
    # Get the most recent attempt before as_of, or None
139
    return store.find(ExerciseAttempt,
140
            ExerciseAttempt.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
141
            ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
1080.1.58 by Matt Giuca
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
142
            True if allow_inactive else ExerciseAttempt.active == True,
143
            True if as_of is None else ExerciseAttempt.date <= as_of,
144
        ).order_by(Desc(ExerciseAttempt.date))
145
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
146
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.
147
        allow_inactive=False):
148
    """Given a storm.store, User and Exercise, returns a list of
149
    ivle.database.ExerciseAttempt objects, one for each attempt made for the
150
    exercise, sorted from latest to earliest.
151
152
    as_of: Optional datetime.datetime object. If supplied, only returns
153
        attempts made before or at this time.
154
    allow_inactive: If True, will return disabled attempts.
155
    """
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
156
    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.
157
        allow_inactive))
158
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
159
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.
160
        allow_inactive=False):
1099.1.198 by William Grant
Fix retrieval of old exercise attempts.
161
    """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.
162
    ivle.database.ExerciseAttempt object for the last submitted attempt for
163
    this question.
164
    Returns None if the user has not made an attempt on this
165
    problem.
166
167
    as_of: Optional datetime.datetime object. If supplied, only returns
168
        attempts made before or at this time.
169
    allow_inactive: If True, will return disabled attempts.
170
    """
1099.1.198 by William Grant
Fix retrieval of old exercise attempts.
171
    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.
172
        allow_inactive).first()
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
173
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
174
def save_exercise(store, user, worksheet_exercise, text, date):
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
175
    """Save an exercise for a user.
176
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
177
    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.
178
    database. This will create the ExerciseSave if needed.
179
    """
180
    saved = store.find(ivle.database.ExerciseSave,
181
                ivle.database.ExerciseSave.user_id == user.id,
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
182
                ivle.database.ExerciseSave.ws_ex_id == worksheet_exercise.id
1099.1.150 by Nick Chadwick
Modified worksheets to properly link attempts to worksheets and
183
                ).one()
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
184
    if saved is None:
1099.1.180 by Nick Chadwick
This commit changes the tutorial service, which now almost exclusively
185
        saved = ivle.database.ExerciseSave(user=user, 
186
                                        worksheet_exercise=worksheet_exercise)
1080.1.88 by William Grant
ivle.worksheet: Add a save_exercise function.
187
        store.add(saved)
188
189
    saved.date = date
190
    saved.text = text
191
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
192
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
193
    """
194
    Given a storm.store, User, Exercise and Worksheet, calculates a score for
195
    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
196
    @param store: A storm.store
197
    @param user: A User.
198
    @param worksheet: A Worksheet.
199
    @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
200
    Returns a 4-tuple of ints, consisting of:
201
    (No. mandatory exercises completed,
202
     Total no. mandatory exercises,
203
     No. optional exercises completed,
204
     Total no. optional exercises)
205
    """
206
    mand_done = 0
207
    mand_total = 0
208
    opt_done = 0
209
    opt_total = 0
210
211
    # Get the student's pass/fail for each exercise in this worksheet
212
    for worksheet_exercise in worksheet.worksheet_exercises:
213
        exercise = worksheet_exercise.exercise
1099.4.3 by Nick Chadwick
Updated the tutorial service, to now allow users to edit worksheets
214
        worksheet = worksheet_exercise.worksheet
1080.1.60 by Matt Giuca
ivle.worksheet: Added calculate_score. This is a nice clean Storm port of
215
        optional = worksheet_exercise.optional
216
1195.1.18 by Matt Giuca
ivle.worksheet.utils: Can now calculate exercise and worksheet marks as of a
217
        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
218
        # done is a bool, whether this student has completed that problem
219
        if optional:
220
            opt_total += 1
221
            if done: opt_done += 1
222
        else:
223
            mand_total += 1
224
            if done: mand_done += 1
225
226
    return mand_done, mand_total, opt_done, opt_total
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
227
1195.1.13 by Matt Giuca
ivle.worksheet.utils: Added calculate_mark, which is from the duplicated code
228
def calculate_mark(mand_done, mand_total):
229
    """Calculate a subject mark, given the result of all worksheets.
230
    @param mand_done: The total number of mandatory exercises completed by
231
        some student, across all worksheets.
232
    @param mand_total: The total number of mandatory exercises across all
233
        worksheets in the offering.
234
    @return: (percent, mark, mark_total)
235
        percent: The percentage of exercises the student has completed, as an
236
            integer between 0 and 100 inclusive.
237
        mark: The mark the student has received, based on the percentage.
238
        mark_total: The total number of marks available (currently hard-coded
239
            as 5).
240
    """
241
    # We want to display a students mark out of 5. However, they are
242
    # allowed to skip 1 in 5 questions and still get 'full marks'.
243
    # Hence we divide by 16, essentially making 16 percent worth
244
    # 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
245
    if mand_total > 0:
246
        percent_int = (100 * mand_done) // mand_total
247
    else:
248
        # Avoid Div0, just give everyone 0 marks if there are none available
249
        percent_int = 0
1195.1.13 by Matt Giuca
ivle.worksheet.utils: Added calculate_mark, which is from the duplicated code
250
    # percent / 16, rounded down, with a maximum mark of 5
251
    max_mark = 5
252
    mark = min(percent_int // 16, max_mark)
253
    return (percent_int, mark, max_mark)
254
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
255
def update_exerciselist(worksheet):
256
    """Runs through the worksheetstream, generating the appropriate
257
    WorksheetExercises, and de-activating the old ones."""
258
    exercises = []
259
    # Turns the worksheet into an xml stream, and then finds all the 
260
    # exercise nodes in the stream.
1720.1.1 by William Grant
Abstract reStructuredText rendering into ivle.database.
261
    worksheetdata = genshi.XML(worksheet.data_xhtml)
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
262
    for kind, data, pos in worksheetdata:
263
        if kind is genshi.core.START:
264
            # Data is a tuple of tag name and a list of name->value tuples
265
            if data[0] == 'exercise':
266
                src = ""
267
                optional = False
268
                for attr in data[1]:
269
                    if attr[0] == 'src':
270
                        src = attr[1]
271
                    if attr[0] == 'optional':
272
                        optional = attr[1] == 'true'
273
                if src != "":
274
                    exercises.append((src, optional))
275
    ex_num = 0
276
    # Set all current worksheet_exercises to be inactive
277
    db_worksheet_exercises = Store.of(worksheet).find(WorksheetExercise,
278
        WorksheetExercise.worksheet_id == worksheet.id)
279
    for worksheet_exercise in db_worksheet_exercises:
280
        worksheet_exercise.active = False
281
    
282
    for exerciseid, optional in exercises:
283
        worksheet_exercise = Store.of(worksheet).find(WorksheetExercise,
284
            WorksheetExercise.worksheet_id == worksheet.id,
285
            Exercise.id == WorksheetExercise.exercise_id,
286
            Exercise.id == exerciseid).one()
287
        if worksheet_exercise is None:
288
            exercise = Store.of(worksheet).find(Exercise,
289
                Exercise.id == exerciseid
290
            ).one()
291
            if exercise is None:
1384.1.10 by William Grant
Display a nice error message if a worksheet attempts to reference a non-existent exercise.
292
                raise ExerciseNotFound(exerciseid)
1102 by William Grant
Move ivle.webapp.tutorial.service.generate_exerciselist() to ivle.worksheet,
293
            worksheet_exercise = WorksheetExercise()
294
            worksheet_exercise.worksheet_id = worksheet.id
295
            worksheet_exercise.exercise_id = exercise.id
296
            Store.of(worksheet).add(worksheet_exercise)
297
        worksheet_exercise.active = True
298
        worksheet_exercise.seq_no = ex_num
299
        worksheet_exercise.optional = optional
300
1394.2.8 by William Grant
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().
301
302
def test_exercise_submission(config, user, exercise, code):
303
    """Test the given code against an exercise.
304
305
    The code is run in a console process as the provided user.
306
    """
307
    # Start a console to run the tests on
308
    jail_path = os.path.join(config['paths']['jails']['mounts'],
309
                             user.login)
310
    working_dir = os.path.join("/home", user.login)
1773 by David Coles
console: Make sure HOME is set correctly (not always the same as CWD). Also use ivle.interpret.execute_raw rather than duplicating trapoline calling code in ivle.chat
311
    cons = ivle.console.Console(config, user, jail_path,
1394.2.8 by William Grant
Factor out console-based exercise testing into ivle.worksheet.test_exercise_submission().
312
                                working_dir)
313
314
    # Parse the file into a exercise object using the test suite
315
    exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
316
        exercise, cons)
317
318
    # Run the test cases. Get the result back as a JSONable object.
319
    # Return it.
320
    test_results = exercise_obj.run_tests(code)
321
322
    # Close the console
323
    cons.close()
324
325
    return test_results
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
326
327
328
class FakeWorksheetForMarks:
329
    """This class represents a worksheet and a particular students progress
330
    through it.
331
    
332
    Do not confuse this with a worksheet in the database. This worksheet
333
    has extra information for use in the output, such as marks."""
1695.1.7 by William Grant
Indicate unpublished worksheets on the offering index.
334
    def __init__(self, id, name, assessable, published):
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
335
        self.id = id
336
        self.name = name
337
        self.assessable = assessable
1695.1.7 by William Grant
Indicate unpublished worksheets on the offering index.
338
        self.published = published
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
339
        self.complete_class = ''
340
        self.optional_message = ''
341
        self.total = 0
342
        self.mand_done = 0
343
    def __repr__(self):
344
        return ("Worksheet(id=%s, name=%s, assessable=%s)"
345
                % (repr(self.id), repr(self.name), repr(self.assessable)))
346
347
348
# XXX: This really shouldn't be needed.
1739 by Matt Giuca
Offering view now respects worksheet_cutoff when displaying the student's mark. Currently also the individual worksheet completions stop at the cutoff. This can be changed, but is hard.
349
def create_list_of_fake_worksheets_and_stats(config, store, user, offering,
350
    as_of=None):
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
351
    """Take an offering's real worksheets, converting them into stats.
352
353
    The worksheet listing views expect special fake worksheet objects
354
    that contain counts of exercises, whether they've been completed,
355
    that sort of thing. A fake worksheet object is used to contain
356
    these values, because nobody has managed to refactor the need out
357
    yet.
358
    """
359
    new_worksheets = []
360
    problems_done = 0
361
    problems_total = 0
362
363
    # Offering.worksheets is ordered by the worksheets seq_no
1695.1.6 by William Grant
Hide unpublished worksheets if edit_worksheets is not held.
364
    worksheets = offering.worksheets
365
366
    # Unless we can edit worksheets, hide unpublished ones.
367
    if 'edit_worksheets' not in offering.get_permissions(user, config):
368
        worksheets = worksheets.find(published=True)
369
370
    for worksheet in worksheets:
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
371
        new_worksheet = FakeWorksheetForMarks(
1695.1.7 by William Grant
Indicate unpublished worksheets on the offering index.
372
            worksheet.identifier, worksheet.name, worksheet.assessable,
373
            worksheet.published)
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
374
        if new_worksheet.assessable:
375
            # Calculate the user's score for this worksheet
376
            mand_done, mand_total, opt_done, opt_total = (
1739 by Matt Giuca
Offering view now respects worksheet_cutoff when displaying the student's mark. Currently also the individual worksheet completions stop at the cutoff. This can be changed, but is hard.
377
                ivle.worksheet.utils.calculate_score(store, user, worksheet,
378
                                                     as_of=as_of))
1442.1.26 by William Grant
Factor the real DB worksheet -> fake mark worksheet translation into ivle.worksheet.util, so we can use it elsewhere.
379
            if opt_total > 0:
380
                optional_message = " (excluding optional exercises)"
381
            else:
382
                optional_message = ""
383
            if mand_done >= mand_total:
384
                new_worksheet.complete_class = "complete"
385
            elif mand_done > 0:
386
                new_worksheet.complete_class = "semicomplete"
387
            else:
388
                new_worksheet.complete_class = "incomplete"
389
            problems_done += mand_done
390
            problems_total += mand_total
391
            new_worksheet.mand_done = mand_done
392
            new_worksheet.total = mand_total
393
            new_worksheet.optional_message = optional_message
394
        new_worksheets.append(new_worksheet)
395
396
    return new_worksheets, problems_total, problems_done