def mark_q(qid, answers): """ Mark the question according to the answers given in a dictionary and return the result in a dictionary: input: {"A1":"0.345", "A2":"fred", "A3":"-26" } return: {"M1": Mark One, "C1": Comment One, "M2": Mark Two..... } """ qtid = DB.get_q_parent(qid) version = DB.get_q_version(qid) variation = DB.get_q_variation(qid) qvars = DB.get_qt_variation(qtid, variation, version) if not qvars: qvars = {} L.warn("markQuestion(%s, %s) unable to retrieve variables." % (qid, answers)) qvars['OaQID'] = int(qid) marktype = DB.get_qt_marker(qtid) if marktype == 1: # standard marks = mark_q_standard(qvars, answers) else: # We want the latest version of the marker, so no version given markerscript = DB.get_qt_att(qtid, "__marker.py") if not markerscript: markerscript = DB.get_qt_att(qtid, "marker.py") L.info("'marker.py' should now be called '__marker.py' (qtid=%s)" % qtid) if not markerscript: L.info("Unable to retrieve marker script for smart marker question (qtid=%s)!" % qtid) marks = mark_q_standard(qvars, answers) else: marks = mark_q_script(qvars, markerscript, answers) return marks
def mark_q(qid, answers): """ Mark the question according to the answers given in a dictionary and return the result in a dictionary: input: {"A1":"0.345", "A2":"fred", "A3":"-26" } return: {"M1": Mark One, "C1": Comment One, "M2": Mark Two..... } """ qtid = DB.get_q_parent(qid) version = DB.get_q_version(qid) variation = DB.get_q_variation(qid) qvars = DB.get_qt_variation(qtid, variation, version) if not qvars: qvars = {} L.warn("markQuestion(%s, %s) unable to retrieve variables." % (qid, answers)) qvars['OaQID'] = int(qid) marktype = DB.get_qt_marker(qtid) if marktype == 1: # standard marks = mark_q_standard(qvars, answers) else: # We want the latest version of the marker, so no version given markerscript = DB.get_qt_att(qtid, "__marker.py") if not markerscript: markerscript = DB.get_qt_att(qtid, "marker.py") L.info("'marker.py' should now be called '__marker.py' (qtid=%s)" % qtid) if not markerscript: L.info( "Unable to retrieve marker script for smart marker question (qtid=%s)!" % qtid) marks = mark_q_standard(qvars, answers) else: marks = mark_q_script(qvars, markerscript, answers) return marks
def gen_q_from_var(qt_id, student, exam, position, version, variation): """ Generate a question given a specific variation. """ qvars = None q_id = DB.create_q(qt_id, DB.get_qt_name(qt_id), student, 1, variation, version, exam) try: q_id = int(q_id) assert (q_id > 0) except (ValueError, TypeError, AssertionError): L.error("OaDB.createQuestion(%s,...) FAILED" % qt_id) imageexists = DB.get_q_att_mimetype(qt_id, "image.gif", variation, version) if not imageexists: if not qvars: qvars = DB.get_qt_variation(qt_id, variation, version) qvars['Oasis_qid'] = q_id image = DB.get_qt_att(qt_id, "image.gif", version) if image: newimage = gen_q_image(qvars, image) DB.create_q_att(qt_id, variation, "image.gif", "image/gif", newimage, version) htmlexists = DB.get_q_att_mimetype(qt_id, "qtemplate.html", variation, version) if not htmlexists: if not qvars: qvars = DB.get_qt_variation(qt_id, variation, version) html = DB.get_qt_att(qt_id, "qtemplate.html", version) if html: qvars['Oasis_qid'] = q_id newhtml = gen_q_html(qvars, html) L.info("generating new qattach qtemplate.html for %s" % q_id) DB.create_q_att(qt_id, variation, "qtemplate.html", "application/oasis-html", newhtml, version) try: q_id = int(q_id) assert (q_id > 0) except (ValueError, TypeError, AssertionError): L.error("generateQuestionFromVar(%s,%s), can't find qid %s? " % (qt_id, student, q_id)) if exam >= 1: DB.add_exam_q(student, exam, q_id, position) return q_id
def render_mark_results(qid, marks): """Take the marking results and display something for the student that tells them what they got right and wrong. If the question has an attachment "_rendermarks.py", it will be called, otherwise a default HTML table will be returned. _rendermarks.py should set variable "resultsHTML" to contain a suitable string for putting in an HTML page. """ qtid = DB.get_q_parent(qid) renderscript = DB.get_qt_att(qtid, "__results.py") if not renderscript: resultshtml = render_mark_results_standard(qid, marks) else: resultshtml = render_mark_results_script(qtid, qid, marks, renderscript) return resultshtml
def get_q_att(qid, name): """ Return (mimetype, data) with the relevant attachment. If it's not found in question, look in questiontemplate. """ qtid = DB.get_q_parent(qid) variation = DB.get_q_variation(qid) version = DB.get_q_version(qid) # for the two biggies we hit the question first, # otherwise check the question template first if name == "image.gif" or name == "qtemplate.html": data = DB.get_q_att(qtid, name, variation, version) if data: return DB.get_q_att_mimetype(qtid, name, variation, version), data data = DB.get_qt_att(qtid, name, version) if data: return DB.get_qt_att_mimetype(qtid, name, version), data else: data = DB.get_qt_att(qtid, name, version) if data: return DB.get_qt_att_mimetype(qtid, name, version), data data = DB.get_q_att(qtid, name, variation, version) if data: return DB.get_q_att_mimetype(qtid, name, variation, version), data return None, None
def qts_to_zip(qt_ids, fname="oa_export", extra_info = None): """ Take a list of QTemplate IDs and return a binary string containing them as an .oaq file. (a zip file in special format) """ fname = os.path.basename(fname) qdir = tempfile.mkdtemp(prefix="oa") info = { 'oasis': { 'oa_version': "3.9.4", 'qt_version': '0.9', 'url': OaConfig.parentURL }, 'qtemplates': {}, 'extra': extra_info } arc = zipfile.ZipFile(os.path.join(qdir, "oasisqe.zip"), 'w', zipfile.ZIP_DEFLATED) for qt_id in qt_ids: qtemplate = DB.get_qtemplate(qt_id) qtdir = os.path.join(qdir, str(qt_id)) attachments = DB.get_qt_atts(qt_id) if not 'qtemplate.html' in attachments: attachments.append('qtemplate.html') if not 'datfile.txt' in attachments: attachments.append('datfile.txt') if not 'image.gif' in attachments: attachments.append('image.gif') os.mkdir(qtdir) os.mkdir(os.path.join(qtdir, "attach")) info["qtemplates"][qt_id] = {'qtemplate': qtemplate} info["qtemplates"][qt_id]["attachments"] = [] for name in attachments: if not name: name='UNKNOWN' mtype = DB.get_qt_att_mimetype(qt_id, name) if not mtype: mtype = "" data = DB.get_qt_att(qt_id, name) if not data: data = "" info["qtemplates"][qt_id]["attachments"].append([name, mtype, len(data)]) subdir = os.path.join(qtdir, "attach", name) outf = open(subdir, "wb") outf.write(data) outf.close() arc.write(subdir, os.path.join("%s" % qt_id, "attach", name), zipfile.ZIP_DEFLATED) infof = open(os.path.join(qdir, "info.json"), "wb") infof.write(json.dumps(info)) infof.close() arc.write(os.path.join(qdir, "info.json"), os.path.join("info.json"), zipfile.ZIP_DEFLATED) arc.close() readback = open(os.path.join(qdir, "oasisqe.zip"), "rb") data = readback.read() readback.close() shutil.rmtree(qdir) return data
def qts_to_zip(qt_ids, extra_info=None): """ Take a list of QTemplate IDs and return a binary string containing them as an .oaq file. (a zip file in special format) """ qdir = tempfile.mkdtemp(prefix="oa") info = { 'oasis': { 'oa_version': "3.9.4", 'qt_version': '0.9', 'url': OaConfig.parentURL }, 'qtemplates': {}, 'extra': extra_info } arc = zipfile.ZipFile(os.path.join(qdir, "oasisqe.zip"), 'w', zipfile.ZIP_DEFLATED) for qt_id in qt_ids: qtemplate = DB.get_qtemplate(qt_id) qtdir = os.path.join(qdir, str(qt_id)) attachments = DB.get_qt_atts(qt_id) if 'qtemplate.html' not in attachments: attachments.append('qtemplate.html') if 'datfile.txt' not in attachments: attachments.append('datfile.txt') if 'image.gif' not in attachments: attachments.append('image.gif') os.mkdir(qtdir) os.mkdir(os.path.join(qtdir, "attach")) info["qtemplates"][qt_id] = {'qtemplate': qtemplate} info["qtemplates"][qt_id]["attachments"] = [] for name in attachments: if not name: name = 'UNKNOWN' mtype = DB.get_qt_att_mimetype(qt_id, name) if not mtype: mtype = "" data = DB.get_qt_att(qt_id, name) if not data: data = "" info["qtemplates"][qt_id]["attachments"].append([name, mtype, len(data)]) subdir = os.path.join(qtdir, "attach", name) outf = open(subdir, "wb") outf.write(data) outf.close() arc.write(subdir, os.path.join("%s" % qt_id, "attach", name), zipfile.ZIP_DEFLATED) infof = open(os.path.join(qdir, "info.json"), "wb") infof.write(json.dumps(info)) infof.close() arc.write(os.path.join(qdir, "info.json"), os.path.join("info.json"), zipfile.ZIP_DEFLATED) arc.close() readback = open(os.path.join(qdir, "oasisqe.zip"), "rb") data = readback.read() readback.close() shutil.rmtree(qdir) return data