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

« back to all changes in this revision

Viewing changes to ivle/worksheet/utils.py

  • Committer: David Coles
  • Date: 2010-06-01 13:50:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: coles.david@gmail.com-20100601135033-syup3gfzd5qrw22b
File Browser: Don't show actions2_file for anything but text editor and 
disable the "unsaved content" warning.

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
244
258
    exercises = []
245
259
    # Turns the worksheet into an xml stream, and then finds all the 
246
260
    # exercise nodes in the stream.
247
 
    worksheetdata = genshi.XML(worksheet.get_xml())
 
261
    worksheetdata = genshi.XML(worksheet.data_xhtml)
248
262
    for kind, data, pos in worksheetdata:
249
263
        if kind is genshi.core.START:
250
264
            # Data is a tuple of tag name and a list of name->value tuples
294
308
    jail_path = os.path.join(config['paths']['jails']['mounts'],
295
309
                             user.login)
296
310
    working_dir = os.path.join("/home", user.login)
297
 
    cons = ivle.console.Console(config, user.unixid, jail_path,
 
311
    cons = ivle.console.Console(config, user, jail_path,
298
312
                                working_dir)
299
313
 
300
314
    # Parse the file into a exercise object using the test suite
309
323
    cons.close()
310
324
 
311
325
    return test_results
 
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."""
 
334
    def __init__(self, id, name, assessable, published):
 
335
        self.id = id
 
336
        self.name = name
 
337
        self.assessable = assessable
 
338
        self.published = published
 
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.
 
349
def create_list_of_fake_worksheets_and_stats(config, store, user, offering,
 
350
    as_of=None):
 
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
 
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:
 
371
        new_worksheet = FakeWorksheetForMarks(
 
372
            worksheet.identifier, worksheet.name, worksheet.assessable,
 
373
            worksheet.published)
 
374
        if new_worksheet.assessable:
 
375
            # Calculate the user's score for this worksheet
 
376
            mand_done, mand_total, opt_done, opt_total = (
 
377
                ivle.worksheet.utils.calculate_score(store, user, worksheet,
 
378
                                                     as_of=as_of))
 
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