Ejemplo n.º 1
0
def get_q_att_fname(qid, name):
    """ Return (mimetype, filename) with the relevant filename.
        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":

        fname = DB.get_q_att_fname(qtid, name, variation, version)
        if fname:
            return DB.get_q_att_mimetype(qtid, name, variation, version), fname
        fname = DB.get_qt_att_fname(qtid, name, version)
        if fname:
            return DB.get_qt_att_mimetype(qtid, name, version), fname
    else:
        fname = DB.get_qt_att_fname(qtid, name, version)
        if fname:
            return DB.get_qt_att_mimetype(qtid, name, version), fname
        fname = DB.get_q_att_fname(qtid, name, variation, version)
        if fname:
            return DB.get_q_att_mimetype(qtid, name, variation, version), fname
    return None, None
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def render_q_html(q_id, readonly=False):
    """ Fetch the question html and get it ready for display - replacing
        links with appropriate targets and filling in form details."""
    try:
        q_id = int(q_id)
        assert q_id > 0
    except (ValueError, TypeError, AssertionError):
        log(WARN,
            "renderQuestionHTML(%s,%s) called with bad qid?" % (q_id, readonly))
    qt_id = DB.get_q_parent(q_id)
    try:
        qt_id = int(qt_id)
        assert qt_id > 0
    except (ValueError, TypeError, AssertionError):
        log(WARN,
            "renderQuestionHTML(%s,%s), getparent failed? " % (q_id, readonly))
    variation = DB.get_q_variation(q_id)
    version = DB.get_q_version(q_id)
    data = DB.get_q_att(qt_id, "qtemplate.html", variation, version)
    if not data:
        log(WARN,
            "Unable to retrieve qtemplate for q_id: %s" % q_id)
        return "QuestionError"
    try:
        out = unicode(data, "utf-8")
    except UnicodeDecodeError:
        try:
            out = unicode(DB.get_q_att(qt_id, "qtemplate.html", variation, version),
                          "latin-1")
        except UnicodeDecodeError, err:
            log(ERROR,
                "unicode error decoding qtemplate for q_id %s: %s" % (q_id, err))
            raise
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def get_q_att_fname(qid, name):
    """ Return (mimetype, filename) with the relevant filename.
        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":

        fname = DB.get_q_att_fname(qtid, name, variation, version)
        if fname:
            return DB.get_q_att_mimetype(qtid, name, variation, version), fname
        fname = DB.get_qt_att_fname(qtid, name, version)
        if fname:
            return DB.get_qt_att_mimetype(qtid, name, version), fname
    else:
        fname = DB.get_qt_att_fname(qtid, name, version)
        if fname:
            return DB.get_qt_att_mimetype(qtid, name, version), fname
        fname = DB.get_q_att_fname(qtid, name, variation, version)
        if fname:
            return DB.get_q_att_mimetype(qtid, name, variation, version), fname
    return None, None
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def render_q_html(q_id, readonly=False):
    """ Fetch the question html and get it ready for display - replacing
        links with appropriate targets and filling in form details."""
    try:
        q_id = int(q_id)
        assert q_id > 0
    except (ValueError, TypeError, AssertionError):
        L.warn("renderQuestionHTML(%s,%s) called with bad qid?" % (q_id, readonly))
    qt_id = DB.get_q_parent(q_id)
    try:
        qt_id = int(qt_id)
        assert qt_id > 0
    except (ValueError, TypeError, AssertionError):
        L.warn("renderQuestionHTML(%s,%s), getparent failed? " % (q_id, readonly))
    variation = DB.get_q_variation(q_id)
    version = DB.get_q_version(q_id)
    data = DB.get_q_att(qt_id, "qtemplate.html", variation, version)
    if not data:
        L.warn("Unable to retrieve qtemplate for q_id: %s" % q_id)
        return "QuestionError"
    try:
        out = unicode(data, "utf-8")
    except UnicodeDecodeError:
        try:
            out = unicode(DB.get_q_att(qt_id, "qtemplate.html", variation, version),
                          "latin-1")
        except UnicodeDecodeError as err:
            L.error("unicode error decoding qtemplate for q_id %s: %s" % (q_id, err))
            raise
    out = out.replace("This question is not verified yet, please report any error!", "")

    out = out.replace("ANS_", "Q_%d_ANS_" % (q_id,))
    out = out.replace("$IMAGES$",
                      "%s/att/qatt/%s/%s/%s/" %
                      (OaConfig.parentURL, qt_id, version, variation))
    out = out.replace("$APPLET$",
                      "%s/att/qatt/%s/%s/%s/" %
                      (OaConfig.parentURL, qt_id, version, variation))
    out = out.replace("$STATIC$",
                      "%s/att/qtatt/%s/%s/%s/" %
                      (OaConfig.parentURL, qt_id, version, variation))
    if readonly:
        out = out.replace("<INPUT ", "<INPUT READONLY ")
        out = out.replace("<SELECT ", "<SELECT DISABLED=DISABLED STYLE='color: black;'")
    guesses = DB.get_q_guesses(q_id)
    for guess in guesses.keys():
        # noinspection PyComparisonWithNone
        if guesses[guess] == None:  # If it's 0 we want to leave it alone
            guesses[guess] = ""
        if guesses[guess] == "None":
            guesses[guess] = ""
            # for each question
    if guesses:
        for ques in range(25, 0, -1):
            if ("G%d" % ques) in guesses:
                out = out.replace("VAL_%d" % ques, htmlesc(guesses["G%d" % ques]))
                for part in range(50, 0, -1):
                    if guesses["G%d" % ques] == "%s.0" % part or guesses["G%d" % ques] == "%s" % part:
                        out = out.replace("Oa_SEL_%d_%d" % (ques, part),
                                          "SELECTED")
                        out = out.replace("Oa_CHK_%d_%d" % (ques, part),
                                          "CHECKED")
                    else:
                        out = out.replace("Oa_SEL_%d_%d" % (ques, part),
                                          "")
                        out = out.replace("Oa_CHK_%d_%d" % (ques, part),
                                          "")
            else:
                out = out.replace("VAL_%d" % (ques,), "")
    for ques in range(25, 0, -1):
        out = out.replace("VAL_%d" % (ques,), "")
    return out
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
def render_q_html(q_id, readonly=False):
    """ Fetch the question html and get it ready for display - replacing
        links with appropriate targets and filling in form details."""
    try:
        q_id = int(q_id)
        assert q_id > 0
    except (ValueError, TypeError, AssertionError):
        L.warn("renderQuestionHTML(%s,%s) called with bad qid?" %
               (q_id, readonly))
    qt_id = DB.get_q_parent(q_id)
    try:
        qt_id = int(qt_id)
        assert qt_id > 0
    except (ValueError, TypeError, AssertionError):
        L.warn("renderQuestionHTML(%s,%s), getparent failed? " %
               (q_id, readonly))
    variation = DB.get_q_variation(q_id)
    version = DB.get_q_version(q_id)
    data = DB.get_q_att(qt_id, "qtemplate.html", variation, version)
    if not data:
        L.warn("Unable to retrieve qtemplate for q_id: %s" % q_id)
        return "QuestionError"
    try:
        out = unicode(data, "utf-8")
    except UnicodeDecodeError:
        try:
            out = unicode(
                DB.get_q_att(qt_id, "qtemplate.html", variation, version),
                "latin-1")
        except UnicodeDecodeError as err:
            L.error("unicode error decoding qtemplate for q_id %s: %s" %
                    (q_id, err))
            raise
    out = out.replace(
        "This question is not verified yet, please report any error!", "")

    out = out.replace("ANS_", "Q_%d_ANS_" % (q_id, ))
    out = out.replace(
        "$IMAGES$", "%s/att/qatt/%s/%s/%s/" %
        (OaConfig.parentURL, qt_id, version, variation))
    out = out.replace(
        "$APPLET$", "%s/att/qatt/%s/%s/%s/" %
        (OaConfig.parentURL, qt_id, version, variation))
    out = out.replace(
        "$STATIC$", "%s/att/qtatt/%s/%s/%s/" %
        (OaConfig.parentURL, qt_id, version, variation))
    if readonly:
        out = out.replace("<INPUT ", "<INPUT READONLY ")
        out = out.replace("<SELECT ",
                          "<SELECT DISABLED=DISABLED STYLE='color: black;'")
    guesses = DB.get_q_guesses(q_id)
    for guess in guesses.keys():
        # noinspection PyComparisonWithNone
        if guesses[guess] == None:  # If it's 0 we want to leave it alone
            guesses[guess] = ""
        if guesses[guess] == "None":
            guesses[guess] = ""
            # for each question
    if guesses:
        for ques in range(25, 0, -1):
            if ("G%d" % ques) in guesses:
                out = out.replace("VAL_%d" % ques,
                                  htmlesc(guesses["G%d" % ques]))
                for part in range(50, 0, -1):
                    if guesses["G%d" % ques] == "%s.0" % part or guesses[
                            "G%d" % ques] == "%s" % part:
                        out = out.replace("Oa_SEL_%d_%d" % (ques, part),
                                          "SELECTED")
                        out = out.replace("Oa_CHK_%d_%d" % (ques, part),
                                          "CHECKED")
                    else:
                        out = out.replace("Oa_SEL_%d_%d" % (ques, part), "")
                        out = out.replace("Oa_CHK_%d_%d" % (ques, part), "")
            else:
                out = out.replace("VAL_%d" % (ques, ), "")
    for ques in range(25, 0, -1):
        out = out.replace("VAL_%d" % (ques, ), "")
    return out