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

« back to all changes in this revision

Viewing changes to ivle/db.py

  • Committer: Matt Giuca
  • Date: 2009-01-19 17:06:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119170624-3bowlh5rs7lf7eqz
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
tutorialservice: Updated to use these new functions rather than ivle.db. Also
    uses datetime and does its own dictionarification rather than relying on
    ivle.db to do it.
    NOTE: This is just for the above two functions, not the whole module
    (still reliant on ivle.db).
ivle.db: Removed get_problem_attempts and get_problem_attempt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
513
513
                frozenset(['date', 'text']),
514
514
                frozenset(['problemid', 'loginid']))
515
515
 
516
 
    def get_problem_attempts(self, login, exercisename, allow_inactive=True,
517
 
                             dry=False):
518
 
        """Given a login name and exercise name, returns a list of dicts, one
519
 
        for each attempt made for that exercise.
520
 
        Dicts are {'date': 'formatted_time', 'complete': bool}.
521
 
        Ordered with the newest first.
522
 
        
523
 
        Note: By default, returns de-activated problem attempts.
524
 
        If allow_inactive is False, will not return disabled attempts.
525
 
 
526
 
        Note: Even if dry, will still physically call get_problem_problemid,
527
 
        which may mutate the DB, and get_user_loginid, which may fail.
528
 
        """
529
 
        problemid = self.get_problem_problemid(exercisename)
530
 
        loginid = self.get_user_loginid(login)  # May raise a DBException
531
 
        andactive = '' if allow_inactive else ' AND active'
532
 
        query = """SELECT date, complete FROM problem_attempt
533
 
    WHERE loginid = %d AND problemid = %d%s
534
 
    ORDER BY date DESC;""" % (loginid, problemid, andactive)
535
 
        if dry: return query
536
 
        result = self.db.query(query).getresult()
537
 
        # Make into dicts (could use dictresult, but want to convert values)
538
 
        return [{'date': date, 'complete': _parse_boolean(complete)}
539
 
                for date, complete in result]
540
 
 
541
 
    def get_problem_attempt(self, login, exercisename, as_of,
542
 
        allow_inactive=True, dry=False):
543
 
        """Given a login name, exercise name, and struct_time, returns the
544
 
        text of the submitted attempt for this question as of that date.
545
 
        Returns None if the user had not made an attempt on this problem at
546
 
        that date.
547
 
        
548
 
        Note: By default, returns de-activated problem attempts.
549
 
        If allow_inactive is False, will not return disabled attempts.
550
 
 
551
 
        Note: Even if dry, will still physically call get_problem_problemid,
552
 
        which may mutate the DB, and get_user_loginid, which may fail.
553
 
        """
554
 
        problemid = self.get_problem_problemid(exercisename)
555
 
        loginid = self.get_user_loginid(login)  # May raise a DBException
556
 
        # Very similar to query in get_problem_stored_text, but without
557
 
        # looking in problem_save, and restricting to a certain date.
558
 
        andactive = '' if allow_inactive else ' AND active'
559
 
        query = """SELECT attempt FROM problem_attempt
560
 
    WHERE loginid = %d AND problemid = %d%s AND date <= %s
561
 
    ORDER BY date DESC
562
 
    LIMIT 1;""" % (loginid, problemid, andactive, _escape(as_of))
563
 
        if dry: return query
564
 
        result = self.db.query(query)
565
 
        if result.ntuples() == 1:
566
 
            # The user has made at least 1 attempt. Return the newest.
567
 
            return result.getresult()[0][0]
568
 
        else:
569
 
            return None
570
 
 
571
516
    def _get_problem_status(self, login, exercisename, dry=False):
572
517
        """Given a login name and exercise name, returns information about the
573
518
        user's performance on that problem.