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 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 render_mark_results_script(qtid, qid, marks, script): """Run the provided script to show the marking for the question. """ version = DB.get_q_version(qid) variation = DB.get_q_variation(qid) qvars = DB.get_qt_variation(qtid, variation, version) questionhtml = render_q_html(qid, readonly=True) reshtml = "" qvars["__builtins__"] = {'MyFuncs': OqeSmartmarkFuncs, 'withinTolerance': script_funcs.within_tolerance, 'math': math, 'round': round, 'float': float, 'log': script_funcs.result_log_fn(qid), 'dir': dir, 'abs': abs, 'None': None, 'True': True, 'False': False, 'questionHTML': questionhtml, 'int': int, 'resultsHTML': reshtml} qvars['markeroutput'] = marks guesses = [int(var[1:]) for var in marks.keys() if re.search(r"^G([0-9]+)$", var) > 0] answers = [int(var[1:]) for var in marks.keys() if re.search(r"^A([0-9]+)$", var) > 0] tolerances = [int(var[1:]) for var in marks.keys() if re.search(r"^T([0-9]+)$", var) > 0] scores = [int(var[1:]) for var in marks.keys() if re.search(r"^M([0-9]+)$", var) > 0] comments = [int(var[1:]) for var in marks.keys() if re.search(r"^C([0-9]+)$", var) > 0] qvars['guesses'] = {} qvars['answers'] = {} qvars['tolerances'] = {} qvars['scores'] = {} qvars['comments'] = {} for guess in guesses: qvars['guesses'][guess] = marks['G%d' % guess] for answer in answers: qvars['answers'][answer] = marks['A%d' % answer] for tolerance in tolerances: qvars['tolerances'][tolerance] = marks['T%d' % tolerance] for score in scores: qvars['scores'][score] = marks['M%d' % score] for comment in comments: qvars['comments'][comment] = marks['C%d' % comment] qvars['numparts'] = len(answers) qvars['parts'] = range(1, len(answers) + 1) try: exec (script, qvars) except BaseException: (etype, value, tb) = sys.exc_info() script_funcs.q_log(qid, "error", "__results.py", "Reverting to standard display: __results.py: %s" % ( traceback.format_exception(etype, value, tb)[-2:])) if 'resultsHTML' in qvars: if len(qvars['resultsHTML']) > 2: reshtml = qvars['resultsHTML'] for v in qvars.keys(): reshtml = reshtml.replace("<IMG SRC %s>" % v, '<IMG SRC="$OaQID$%s" />' % qvars[v]) reshtml = reshtml.replace("$OaQID$", "%d/" % qid) return reshtml script_funcs.q_log(qid, "error", "__results.py", "'resultsHTML' not set, using standard renderer.") return render_mark_results_standard(qid, marks)
def render_mark_results_script(qtid, qid, marks, script): """Run the provided script to show the marking for the question. """ version = DB.get_q_version(qid) variation = DB.get_q_variation(qid) qvars = DB.get_qt_variation(qtid, variation, version) questionhtml = render_q_html(qid, readonly=True) reshtml = "" qvars["__builtins__"] = { 'MyFuncs': OqeSmartmarkFuncs, 'withinTolerance': script_funcs.within_tolerance, 'math': math, 'round': round, 'float': float, 'log': script_funcs.result_log_fn(qid), 'dir': dir, 'abs': abs, 'None': None, 'True': True, 'False': False, 'questionHTML': questionhtml, 'int': int, 'resultsHTML': reshtml } qvars['markeroutput'] = marks guesses = [ int(var[1:]) for var in marks.keys() if re.search(r"^G([0-9]+)$", var) > 0 ] answers = [ int(var[1:]) for var in marks.keys() if re.search(r"^A([0-9]+)$", var) > 0 ] tolerances = [ int(var[1:]) for var in marks.keys() if re.search(r"^T([0-9]+)$", var) > 0 ] scores = [ int(var[1:]) for var in marks.keys() if re.search(r"^M([0-9]+)$", var) > 0 ] comments = [ int(var[1:]) for var in marks.keys() if re.search(r"^C([0-9]+)$", var) > 0 ] qvars['guesses'] = {} qvars['answers'] = {} qvars['tolerances'] = {} qvars['scores'] = {} qvars['comments'] = {} for guess in guesses: qvars['guesses'][guess] = marks['G%d' % guess] for answer in answers: qvars['answers'][answer] = marks['A%d' % answer] for tolerance in tolerances: qvars['tolerances'][tolerance] = marks['T%d' % tolerance] for score in scores: qvars['scores'][score] = marks['M%d' % score] for comment in comments: qvars['comments'][comment] = marks['C%d' % comment] qvars['numparts'] = len(answers) qvars['parts'] = range(1, len(answers) + 1) try: exec(script, qvars) except BaseException: (etype, value, tb) = sys.exc_info() script_funcs.q_log( qid, "error", "__results.py", "Reverting to standard display: __results.py: %s" % (traceback.format_exception(etype, value, tb)[-2:])) if 'resultsHTML' in qvars: if len(qvars['resultsHTML']) > 2: reshtml = qvars['resultsHTML'] for v in qvars.keys(): reshtml = reshtml.replace("<IMG SRC %s>" % v, '<IMG SRC="$OaQID$%s" />' % qvars[v]) reshtml = reshtml.replace("$OaQID$", "%d/" % qid) return reshtml script_funcs.q_log(qid, "error", "__results.py", "'resultsHTML' not set, using standard renderer.") return render_mark_results_standard(qid, marks)