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

« back to all changes in this revision

Viewing changes to ivle/worksheet/utils.py

  • Committer: William Grant
  • Date: 2010-02-25 03:18:21 UTC
  • Revision ID: grantw@unimelb.edu.au-20100225031821-mi9a2tm5679fht4d
Shuffle things around so that req.user and req.store only construct when actually retrieved, and ensure they're not retrieved for media files. Saves 50ms of DB connection time per request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
import ivle.database
32
32
from ivle.database import ExerciseAttempt, ExerciseSave, Worksheet, \
33
 
                          WorksheetExercise, Exercise
 
33
                          WorksheetExercise, Exercise, User
34
34
import ivle.webapp.tutorial.test
35
35
 
36
36
__all__ = ['ExerciseNotFound', 'get_exercise_status',
 
37
           'get_exercise_statistics',
37
38
           'get_exercise_stored_text', 'get_exercise_attempts',
38
39
           'get_exercise_attempt', 'test_exercise_submission',
39
40
          ]
82
83
 
83
84
    return first_success is not None, num_attempts
84
85
 
 
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
 
85
99
def get_exercise_stored_text(store, user, worksheet_exercise):
86
100
    """Given a storm.store, User and WorksheetExercise, returns an
87
101
    ivle.database.ExerciseSave object for the last saved/submitted attempt for
317
331
    
318
332
    Do not confuse this with a worksheet in the database. This worksheet
319
333
    has extra information for use in the output, such as marks."""
320
 
    def __init__(self, id, name, assessable):
 
334
    def __init__(self, id, name, assessable, published):
321
335
        self.id = id
322
336
        self.name = name
323
337
        self.assessable = assessable
 
338
        self.published = published
324
339
        self.complete_class = ''
325
340
        self.optional_message = ''
326
341
        self.total = 0
331
346
 
332
347
 
333
348
# XXX: This really shouldn't be needed.
334
 
def create_list_of_fake_worksheets_and_stats(store, user, offering):
 
349
def create_list_of_fake_worksheets_and_stats(config, store, user, offering):
335
350
    """Take an offering's real worksheets, converting them into stats.
336
351
 
337
352
    The worksheet listing views expect special fake worksheet objects
345
360
    problems_total = 0
346
361
 
347
362
    # Offering.worksheets is ordered by the worksheets seq_no
348
 
    for worksheet in offering.worksheets:
 
363
    worksheets = offering.worksheets
 
364
 
 
365
    # Unless we can edit worksheets, hide unpublished ones.
 
366
    if 'edit_worksheets' not in offering.get_permissions(user, config):
 
367
        worksheets = worksheets.find(published=True)
 
368
 
 
369
    for worksheet in worksheets:
349
370
        new_worksheet = FakeWorksheetForMarks(
350
 
            worksheet.identifier, worksheet.name, worksheet.assessable)
 
371
            worksheet.identifier, worksheet.name, worksheet.assessable,
 
372
            worksheet.published)
351
373
        if new_worksheet.assessable:
352
374
            # Calculate the user's score for this worksheet
353
375
            mand_done, mand_total, opt_done, opt_total = (