598
600
count = int(result.getresult()[0][0])
599
601
return (False, count)
603
# WORKSHEET/PROBLEM ASSOCIATION AND MARKS CALCULATION
605
def get_worksheet_mtime(self, subject, worksheet, dry=False):
607
For a given subject/worksheet name, gets the time the worksheet was
608
last updated in the DB, if any.
609
This can be used to check if there is a newer version on disk.
610
Returns the timestamp as a time.struct_time, or None if the worksheet
611
is not found or has no stored mtime.
615
{"subject": subject, "identifier": worksheet},
616
"worksheet", ["mtime"], ["subject", "identifier"],
619
# Assume the worksheet is not in the DB
623
if r["mtime"] is None:
625
return time.strptime(r["mtime"], TIMESTAMP_FORMAT)
627
def create_worksheet(self, subject, worksheet, problems,
630
Inserts or updates rows in the worksheet and worksheet_problems
631
tables, to create a worksheet in the database.
632
This atomically performs all operations. If the worksheet is already
633
in the DB, removes it and all its associated problems and rebuilds.
634
Sets the timestamp to the current time.
636
problems is a collection of pairs. The first element of the pair is
637
the problem identifier ("identifier" column of the problem table). The
638
second element is an optional boolean, "optional". This can be omitted
639
(so it's a 1-tuple), and then it will default to False.
641
Note: As with get_problem_problemid, if a problem name is not in the
642
DB, it will be added to the problem table.
644
self.start_transaction()
646
# Use the current time as the "mtime" field
647
mtime = time.localtime()
649
# Get the worksheetid
651
{"subject": subject, "identifier": worksheet},
652
"worksheet", ["worksheetid"], ["subject", "identifier"])
653
worksheetid = r["worksheetid"]
655
# Delete any problems which might exist
656
query = ("DELETE FROM worksheet_problem "
657
"WHERE worksheetid = %d;" % worksheetid)
659
# Update the row with the new details
660
query = ("UPDATE worksheet "
661
"SET assessable = %s, mtime = %s "
662
"WHERE worksheetid = %d;"
663
% (_escape(assessable), _escape(mtime), worksheetid))
666
# Assume the worksheet is not in the DB
667
# Create the worksheet row
668
query = ("INSERT INTO worksheet "
669
"(subject, identifier, assessable, mtime) "
670
"VALUES (%s, %s, %s, %s);"""
671
% (_escape(subject), _escape(worksheet),
672
_escape(assessable), _escape(mtime)))
674
# Now get the worksheetid again - should succeed
676
{"subject": subject, "identifier": worksheet},
677
"worksheet", ["worksheetid"], ["subject", "identifier"])
678
worksheetid = r["worksheetid"]
680
# Now insert each problem into the worksheet_problem table
681
for problem in problems:
682
if isinstance(problem, tuple):
683
prob_identifier = problem[0]
685
optional = problem[1]
689
prob_identifier = problem
691
problemid = self.get_problem_problemid(prob_identifier)
692
query = ("INSERT INTO worksheet_problem "
693
"(worksheetid, problemid, optional) "
694
"VALUES (%d, %d, %s);"
695
% (worksheetid, problemid, _escape(optional)))
602
704
"""Close the DB connection. Do not call any other functions after
603
705
this. (The behaviour of doing so is undefined).