def gen_q(qtid, student=0, exam=0, position=0): """ Given a qtemplate, will generate a question instance. If student and/or exam is supplied it will be assigned appropriately. If exam is supplied, position must also be supplied. Will return the ID of the created instance. """ # Pick a variation randomly version = DB.get_qt_version(qtid) numvars = DB.get_qt_num_variations(qtid, version) if numvars > 0: variation = random.randint(1, numvars) else: L.warn("No question variations (qtid=%d)" % qtid) Audit.audit( 3, student, qtid, "General", "Failed to generate question %s for %s, exam %s" % (qtid, student, exam)) return False q_id = gen_q_from_var(qtid, student, exam, position, version, variation) if not q_id: Audit.audit( 3, student, qtid, "General", "Failed to generate instance of %s for %s, exam %s" % (qtid, student, exam)) return q_id
def gen_q(qtid, student=0, exam=0, position=0, variation=None): """ Given a qtemplate, will generate a question instance. If student and/or exam is supplied it will be assigned appropriately. If exam is supplied, position must also be supplied. Will return the ID of the created instance. """ # Pick a variation randomly if not supplied version = DB.get_qt_version(qtid) numvars = DB.get_qt_num_variations(qtid, version) if variation is None and numvars > 0: variation = random.randint(1, numvars) if numvars == 0: L.warn("No question variations (qtid=%d)" % qtid) Audit.audit(3, student, qtid, "General", "Failed to generate question %s for %s, exam %s" % (qtid, student, exam)) return False q_id = gen_q_from_var(qtid, student, exam, position, version, variation) if not q_id: Audit.audit(3, student, qtid, "General", "Failed to generate instance of %s for %s, exam %s" % (qtid, student, exam)) return q_id
def qtlog_as_html(topic, qtid): """Show the most recent log errors for the given qtid. """ versionre = re.compile(r'version=(\d+),') variationre = re.compile(r'variation=(\d+),') priorityre = re.compile(r'priority=([^,]+),') facilityre = re.compile(r'facility=([^,]+),') messagere = re.compile(r'message=(.+)$', re.MULTILINE) out = "" name = DB.get_qt_name(qtid) out += "<h2>Log Entries for %s, topic %s</h2>" % (name, topic) out += "<p><i>These can be created from within __marker.py or __results.py by calling " out += "log(priority, mesg), for example:</i> " out += "<pre>log('error','User entered a value we can't parse.')</pre></p>" out += "<p><i>Typical priorities might be 'error', 'info', 'noise'</i></p>" out += "<table style='border: solid 1px black;' border='1'><tr><th>Time</th><th>Ver</th>" out += "<th>Variation</th><th>Pri</th><th>Fac</th><th>Message</th></tr>" entries = Audit.get_records_by_object(qtid, limit=100, offset=0) for entry in entries: try: version = versionre.findall(entry['message'])[0] except (IndexError, TypeError): version = '.' try: variation = variationre.findall(entry['message'])[0] except (IndexError, TypeError): variation = '.' try: priority = priorityre.findall(entry['message'])[0] except (IndexError, TypeError): priority = '.' try: facility = facilityre.findall(entry['message'])[0] except (IndexError, TypeError): facility = '.' try: message = messagere.findall(entry['message'])[0] except (IndexError, TypeError): message = '.' out += "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % ( entry['time'].strftime("%Y/%b/%d %H:%M:%S"), version, variation, priority, facility, message) out += "</table>" return out
def qtlog_as_html(topic, qtid): """Show the most recent log errors for the given qtid. """ versionre = re.compile(r'version=(\d+),') variationre = re.compile(r'variation=(\d+),') priorityre = re.compile(r'priority=([^,]+),') facilityre = re.compile(r'facility=([^,]+),') messagere = re.compile(r'message=(.+)$', re.MULTILINE) out = "" name = DB.get_qt_name(qtid) out += "<h2>Log Entries for %s, topic %s</h2>" % (name, topic) out += "<p><i>These can be created from within __marker.py or __results.py by calling " out += "log(priority, mesg), for example:</i> " out += "<pre>log('error','User entered a value we can't parse.')</pre></p>" out += "<p><i>Typical priorities might be 'error', 'info', 'noise'</i></p>" out += "<table style='border: solid 1px black;' border='1'><tr><th>Time</th><th>Ver</th>" out += "<th>Variation</th><th>Pri</th><th>Fac</th><th>Message</th></tr>" entries = Audit.get_records_by_object(qtid, limit=100, offset=0) for entry in entries: try: version = versionre.findall(entry['message'])[0] except (IndexError, TypeError): version = '.' try: variation = variationre.findall(entry['message'])[0] except (IndexError, TypeError): variation = '.' try: priority = priorityre.findall(entry['message'])[0] except (IndexError, TypeError): priority = '.' try: facility = facilityre.findall(entry['message'])[0] except (IndexError, TypeError): facility = '.' try: message = messagere.findall(entry['message'])[0] except (IndexError, TypeError): message = '.' out += "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (entry['time'].strftime("%Y/%b/%d %H:%M:%S"), version, variation, priority, facility, message) out += "</table>" return out