~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 11:09:50 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119110950-a40040jv6clxrvej
tutorial: Replaced call to ivle.db.create_worksheet with local code (roughly
    the same algorithm) which uses Worksheet and Exercise.
    This expands the code a lot, but it's all relevant to the tutorial system,
    so it belongs here, and still shorter than db.create_worksheet.
ivle.database: Added new supporting methods for Worksheet and Exercise.
ivle.db: Removed create_worksheet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
655
655
 
656
656
    # WORKSHEET/PROBLEM ASSOCIATION AND MARKS CALCULATION
657
657
 
658
 
    def create_worksheet(self, subject, worksheet, problems=None,
659
 
        assessable=None):
660
 
        """
661
 
        Inserts or updates rows in the worksheet and worksheet_problems
662
 
        tables, to create a worksheet in the database.
663
 
        This atomically performs all operations. If the worksheet is already
664
 
        in the DB, removes it and all its associated problems and rebuilds.
665
 
        Sets the timestamp to the current time.
666
 
 
667
 
        problems is a collection of pairs. The first element of the pair is
668
 
        the problem identifier ("identifier" column of the problem table). The
669
 
        second element is an optional boolean, "optional". This can be omitted
670
 
        (so it's a 1-tuple), and then it will default to False.
671
 
 
672
 
        Problems and assessable are optional, and if omitted, will not change
673
 
        the existing data. If the worksheet does not yet exist, and assessable
674
 
        is omitted, it defaults to False.
675
 
 
676
 
        Note: As with get_problem_problemid, if a problem name is not in the
677
 
        DB, it will be added to the problem table.
678
 
        """
679
 
        self.start_transaction()
680
 
        try:
681
 
            # Use the current time as the "mtime" field
682
 
            mtime = time.localtime()
683
 
            try:
684
 
                # Get the worksheetid
685
 
                r = self.get_single(
686
 
                    {"subject": subject, "identifier": worksheet},
687
 
                    "worksheet", ["worksheetid"], ["subject", "identifier"])
688
 
                worksheetid = r["worksheetid"]
689
 
 
690
 
                # Delete any problems which might exist, if problems is
691
 
                # supplied. If it isn't, keep the existing ones.
692
 
                if problems is not None:
693
 
                    query = ("DELETE FROM worksheet_problem "
694
 
                        "WHERE worksheetid = %d;" % worksheetid)
695
 
                    self.db.query(query)
696
 
                # Update the row with the new details
697
 
                if assessable is None:
698
 
                    query = ("UPDATE worksheet "
699
 
                        "SET mtime = %s WHERE worksheetid = %d;"
700
 
                        % (_escape(mtime), worksheetid))
701
 
                else:
702
 
                    query = ("UPDATE worksheet "
703
 
                        "SET assessable = %s, mtime = %s "
704
 
                        "WHERE worksheetid = %d;"
705
 
                        % (_escape(assessable), _escape(mtime), worksheetid))
706
 
                self.db.query(query)
707
 
            except DBException:
708
 
                # Assume the worksheet is not in the DB
709
 
                # If assessable is not supplied, default to False.
710
 
                if assessable is None:
711
 
                    assessable = False
712
 
                # Create the worksheet row
713
 
                query = ("INSERT INTO worksheet "
714
 
                    "(subject, identifier, assessable, mtime) "
715
 
                    "VALUES (%s, %s, %s, %s);"""
716
 
                    % (_escape(subject), _escape(worksheet),
717
 
                    _escape(assessable), _escape(mtime)))
718
 
                self.db.query(query)
719
 
                # Now get the worksheetid again - should succeed
720
 
                r = self.get_single(
721
 
                    {"subject": subject, "identifier": worksheet},
722
 
                    "worksheet", ["worksheetid"], ["subject", "identifier"])
723
 
                worksheetid = r["worksheetid"]
724
 
 
725
 
            # Now insert each problem into the worksheet_problem table
726
 
            if problems is not None:
727
 
                for problem in problems:
728
 
                    if isinstance(problem, tuple):
729
 
                        prob_identifier = problem[0]
730
 
                        try:
731
 
                            optional = problem[1]
732
 
                        except IndexError:
733
 
                            optional = False
734
 
                    else:
735
 
                        prob_identifier = problem
736
 
                        optional = False
737
 
                    problemid = self.get_problem_problemid(prob_identifier)
738
 
                    query = ("INSERT INTO worksheet_problem "
739
 
                        "(worksheetid, problemid, optional) "
740
 
                        "VALUES (%d, %d, %s);"
741
 
                        % (worksheetid, problemid, _escape(optional)))
742
 
                    self.db.query(query)
743
 
 
744
 
            self.commit()
745
 
        except:
746
 
            self.rollback()
747
 
            raise
748
 
 
749
658
    def calculate_score_worksheet(self, login, subject, worksheet):
750
659
        """
751
660
        Calculates the score for a user on a given worksheet.