Esempio n. 1
0
def mx_admin_add_yaml_quiz():
    import yaml
    frm = MxForm()
    frm.add_field('quiz_yaml', 'Valid YAML is required',\
                  lambda x: x != None)
    postfail = False
    rqm = mx_request_method()
    if rqm == 'POST':
        y = frm.get('quiz_yaml')
        if frm.is_valid():
            try:
                ymap = yaml.safe_load(y)
                quiz = ymap['quiz']
                name = quiz['name'].strip()
                questions = quiz['questions']
                quiz_id = model.q_i_quiz(name, len(questions))
                c = 1 
                for q in questions:
                    question_id = model.q_i_question(\
                            quiz_id, q['text'].strip(), c, q['value'])
                    answers = q['answers']
                    for a in answers:
                        model.q_i_answer(question_id, a['text'], a['correct'])
                    c += 1
                return mx_template('admin_success.tpl',\
                                        message='Quiz added successfully!')
            except (KeyError, yaml.YAMLError):
                postfail = True
        else:
            postfail = True
    if postfail or rqm == 'GET': 
        return mx_template('admin_add_yaml_quiz.tpl',\
                            failed=postfail, errors=frm.errors)
Esempio n. 2
0
def mx_import_yaml():
    global YAML_FILES

    model.G_db = sqlite3.connect("mixopterus.db")
    for yf in YAML_FILES:
        f = open(yf)
        y = f.read()
        if y != None:
            y = y.strip()
            ymap = yaml.safe_load(y)
            try:
                quiz = ymap["quiz"]
                name = quiz["name"].strip()
                questions = quiz["questions"]
                quiz_id = model.q_i_quiz(name, len(questions))
                c = 1
                for q in questions:
                    question_id = model.q_i_question(quiz_id, q["text"].strip(), c, q["value"])
                    answers = q["answers"]
                    for a in answers:
                        model.q_i_answer(question_id, a["text"], a["correct"])
                    c += 1
            except KeyError:
                print "Skipping malformed YAML file: %s" % (yf)
                continue
        f.close()
Esempio n. 3
0
def mx_admin_add_answer():
    postfail = False
    frm = MxForm()
    rqm = mx_request_method()
    sid = mx_get_cookie('session_id')
    n_answers = int(model.q_s_session_value(sid, 'n_answers')[3])
    question_id = int(model.q_s_session_value(sid, 'question_id')[3])
    quiz_id = int(model.q_s_session_value(sid, 'quiz_id')[3])
    edit = int(model.q_s_session_value(sid, 'quiz_edit')[3]) == 1
    q_n = int(model.q_s_session_value(sid, 'q_n')[3])
    q_text = model.q_s_session_value(sid, 'q_text')[3]
    
    q_answers = []
    if edit:
        q_answers = model.q_s_answers_for_question(question_id)

    if rqm == 'POST':
        def proc(t_i, c_i):
            t_s = 'text-%s' % str(t_i)
            c_s = 'correct-%s' % str(c_i)
            frm.add_field(t_s, 'Answer is required',\
                    lambda x: x != None and len(x) < 1024)
            frm.add_field(c_s, 'Correct is required',\
                    lambda x: x != None and x.isdigit() and int(x) < 2, int)
            return (frm.get(t_s), frm.get(c_s))
        st = range(1, n_answers + 1)
        fs = map(proc, st, st)
        if frm.is_valid():
            for tfs, cfs in fs:
                model.q_i_answer(question_id, tfs, cfs == 1)
            q_n = int(model.q_s_session_value(sid, 'q_n')[3])
            quiz_id = int(model.q_s_session_value(sid, 'quiz_id')[3])
            quiz_ln = model.q_s_quiz(quiz_id)[2]
            model.q_d_session_value(sid, 'q_text')
            model.q_d_session_value(sid, 'question_id')
            model.q_d_session_value(sid, 'n_answers')
            if (q_n + 1) > quiz_ln:
                mx_redirect('/admin/add_quiz_summary')
            else:
                model.q_u_session_value(sid, 'q_n', q_n + 1)
                mx_redirect('/admin/add_question')
        else:
            postfail = True
    if postfail or rqm == 'GET':
        quiz_name = model.q_s_quiz(quiz_id)[1]
        return mx_template('admin_add_quiz_C.tpl',\
                            quiz_name = quiz_name,
                            n_answers=n_answers,\
                            q_answers=q_answers, q_n=q_n,\
                            q_text=q_text, errors=frm.errors,\
                            edit=edit, values=frm.values)