Beispiel #1
0
def mx_admin_add_question():
    sid = mx_get_cookie('session_id')
    q_n = model.q_s_session_value(sid, 'q_n')[3]
    quiz_id = model.q_s_session_value(sid, 'quiz_id')[3]
    edit = int(model.q_s_session_value(sid, 'quiz_edit')[3]) == 1
    postfail = False
    q_text = ''; n_answers = ''; q_value = ''
    question_id = 0
    frm = MxForm()
    frm.add_field('text', 'Question is required',\
                    lambda x: x != None and len(x) < 1024)
    frm.add_field('n_answers', 'Number of answers is required',\
                    lambda x: x != None and int(x) < 256, int)
    frm.add_field('value', 'Value is required',\
                    lambda x: x != None and int(x) < 100, int)
    if edit:
        q_id, q_text, n_answers, q_value, q_image, qq_quiz_id\
            = model.q_s_quiz_question(quiz_id, q_n)

    rqm = mx_request_method()
    if rqm == 'POST':
        q_text = frm.get('text')
        n_answers = frm.get('n_answers')
        q_value = frm.get('value')
        if frm.is_valid():
            model.q_i_session_value(sid, 'n_answers', n_answers)
            model.q_i_session_value(sid, 'q_text', q_text)
            if edit:
                model.q_u_question(\
                    question_id, q_text, q_n, q_value, None)
                model.q_i_session_value(sid, 'question_id',\
                                        question_id)
                mx_redirect('/admin/edit_answer')
            else:
                question_id = model.q_i_question(\
                                quiz_id, q_text, q_n, q_value, None) 
                model.q_i_session_value(sid, 'question_id',\
                                        question_id)
                mx_redirect('/admin/add_answer')
        else:
            postfail = True
    if postfail or rqm == 'GET':
        quiz_name = model.q_s_quiz(quiz_id)[1]
        return mx_template('admin_add_quiz_B.tpl',\
                            quiz_name=quiz_name, q_n=q_n,\
                            errors=frm.errors,\
                            q_text=q_text,\
                            n_answers=n_answers,\
                            q_value=q_value,
                            edit=edit)
Beispiel #2
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)
Beispiel #3
0
def mx_admin_add_quiz_summary():
    sid = mx_get_cookie('session_id')
    quiz_id = int(model.q_s_session_value(sid, 'quiz_id')[3])
    quiz_summary = model.q_s_quiz_summary(quiz_id)
    return mx_template('admin_add_quiz_D.tpl', quiz_summary=quiz_summary)