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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-03-15 11:28:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:704
db.py: Added get_problem_status, which returns a boolean, whether or not the
    problem has been completed, and the number of attempts made up to and
    including the first success, for a given problem for a given user.

Show diffs side-by-side

added added

removed removed

Lines of Context:
556
556
        else:
557
557
            return None
558
558
 
 
559
    def get_problem_status(self, login, exercisename, dry=False):
 
560
        """Given a login name and exercise name, returns information about the
 
561
        user's performance on that problem.
 
562
        Returns a tuple of:
 
563
            - A boolean, whether they have successfully passed this exercise.
 
564
            - An int, the number of attempts they have made up to and
 
565
              including the first successful attempt (or the total number of
 
566
              attempts, if not yet successful).
 
567
        """
 
568
        problemid = self.get_problem_problemid(exercisename)
 
569
        loginid = self.get_user_loginid(login)  # May raise a DBException
 
570
 
 
571
        # ASSUME that it is completed, get the total number of attempts up to
 
572
        # and including the first successful attempt.
 
573
        # (Get the date of the first successful attempt. Then count the number
 
574
        # of attempts made <= that date).
 
575
        # Will return an empty table if the problem has never been
 
576
        # successfully completed.
 
577
        query = """SELECT COUNT(*) FROM problem_attempt
 
578
    WHERE loginid = %d AND problemid = %d AND date <=
 
579
        (SELECT date FROM problem_attempt
 
580
            WHERE loginid = %d AND problemid = %d AND complete = TRUE
 
581
            ORDER BY date ASC
 
582
            LIMIT 1);""" % (loginid, problemid, loginid, problemid)
 
583
        if dry: return query
 
584
        result = self.db.query(query)
 
585
        count = int(result.getresult()[0][0])
 
586
        if count > 0:
 
587
            # The user has made at least 1 successful attempt.
 
588
            # Return True for success, and the number of attempts up to and
 
589
            # including the successful one.
 
590
            return (True, count)
 
591
        else:
 
592
            # Returned 0 rows - this indicates that the problem has not been
 
593
            # completed.
 
594
            # Return the total number of attempts, and False for success.
 
595
            query = """SELECT COUNT(*) FROM problem_attempt
 
596
    WHERE loginid = %d AND problemid = %d;""" % (loginid, problemid)
 
597
            result = self.db.query(query)
 
598
            count = int(result.getresult()[0][0])
 
599
            return (False, count)
 
600
 
559
601
    def close(self):
560
602
        """Close the DB connection. Do not call any other functions after
561
603
        this. (The behaviour of doing so is undefined).