656
656
# WORKSHEET/PROBLEM ASSOCIATION AND MARKS CALCULATION
658
def create_worksheet(self, subject, worksheet, problems=None,
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.
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.
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.
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.
679
self.start_transaction()
681
# Use the current time as the "mtime" field
682
mtime = time.localtime()
684
# Get the worksheetid
686
{"subject": subject, "identifier": worksheet},
687
"worksheet", ["worksheetid"], ["subject", "identifier"])
688
worksheetid = r["worksheetid"]
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)
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))
702
query = ("UPDATE worksheet "
703
"SET assessable = %s, mtime = %s "
704
"WHERE worksheetid = %d;"
705
% (_escape(assessable), _escape(mtime), worksheetid))
708
# Assume the worksheet is not in the DB
709
# If assessable is not supplied, default to False.
710
if assessable is None:
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)))
719
# Now get the worksheetid again - should succeed
721
{"subject": subject, "identifier": worksheet},
722
"worksheet", ["worksheetid"], ["subject", "identifier"])
723
worksheetid = r["worksheetid"]
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]
731
optional = problem[1]
735
prob_identifier = problem
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)))
749
658
def calculate_score_worksheet(self, login, subject, worksheet):
751
660
Calculates the score for a user on a given worksheet.