Ejemplo n.º 1
0
def mx_admin_add_quiz(quiz_id=None):
    edit = False; name = ''; length = '' 
    frm = MxForm()
    rqm = mx_request_method()
    postfail = False    
    frm.add_field('name', 'Name is required',\
                    lambda x: x != None and len(x) < 255)
    frm.add_field('length', 'Length is required',\
                    lambda x: x != None and x.isdigit() and int(x) < 100, int)

    if quiz_id != None:
        quiz_id, name, length = model.q_s_quiz(quiz_id)
        edit = True
    if rqm == 'POST':
        name = frm.get('name')
        length = frm.get('length')
        if frm.is_valid():
            sid = mx_get_cookie('session_id')
            model.q_i_session_value(sid, 'quiz_id', quiz_id)
            model.q_i_session_value(sid, 'q_n', 1)
            if edit:
                model.q_u_quiz(quiz_id, name, length) 
                model.q_i_session_value(sid, 'quiz_edit', 1)
                mx_redirect('/admin/edit_question')
            else:
                quiz_id = model.q_i_quiz(name, length)
                model.q_i_session_value(sid, 'quiz_edit', 0)
                mx_redirect('/admin/add_question')
        else:
            postfail = True
    if postfail or rqm == 'GET':
        return mx_template('admin_add_quiz_A.tpl', errors=frm.errors, name = name, length = length, quiz_id = quiz_id)
Ejemplo n.º 2
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)