~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 05:02:13 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:695
db: Added function get_problem_attempt_last_text: returns the last submitted
    text.
    Reordered some arguments. (incl. tutorialservice).

Show diffs side-by-side

added added

removed removed

Lines of Context:
455
455
 
456
456
        return d['problemid']
457
457
 
458
 
    def insert_problem_attempt(self, exercisename, login, date, complete,
 
458
    def insert_problem_attempt(self, login, exercisename, date, complete,
459
459
        attempt, dry=False):
460
460
        """Inserts the details of a problem attempt into the database.
461
461
        exercisename: Name of the exercise. (identifier field of problem
483
483
            frozenset(['problemid','loginid','date','complete','attempt']),
484
484
            dry=dry)
485
485
 
 
486
    def get_problem_attempt_last_text(self, login, exercisename, dry=False):
 
487
        """Given a login name and exercise name, returns the text of the
 
488
        last submitted attempt for this question. Returns None if the user has
 
489
        not made an attempt on this problem.
 
490
 
 
491
        Note: Even if dry, will still physically call get_problem_problemid,
 
492
        which may mutate the DB, and get_user_loginid, which may fail.
 
493
        """
 
494
        problemid = self.get_problem_problemid(exercisename)
 
495
        loginid = self.get_user_loginid(login)  # May raise a DBException
 
496
        # "Get the single newest attempt made by this user for this problem"
 
497
        query = ("SELECT attempt FROM problem_attempt "
 
498
            "WHERE loginid = %d AND problemid = %d "
 
499
            "ORDER BY date DESC "
 
500
            "LIMIT 1;" % (loginid, problemid))
 
501
        if dry: return query
 
502
        result = self.db.query(query)
 
503
        if result.ntuples() == 1:
 
504
            # The user has made at least 1 attempt. Return the newest.
 
505
            return result.getresult()[0][0]
 
506
        else:
 
507
            return None
 
508
 
486
509
    def close(self):
487
510
        """Close the DB connection. Do not call any other functions after
488
511
        this. (The behaviour of doing so is undefined).