Exemplo n.º 1
0
def mx_quiz_mark(quiz_id, n=None):
    p_sid = mx_get_cookie('player_session_id')
    frm = MxForm()
    frm.add_field('select_answer', 'Select answer required',\
                  lambda x: x != None and x.isdigit(), int)
    postfail = False
    rqm = mx_request_method()
    if rqm == 'POST':
        correct = 0; value = 0; answer_id = 0; question_id = 0; i_r = False
        a = frm.get('select_answer')
        if frm.is_valid():
            rqu = model.q_s_quiz(quiz_id)
            quiz_length = rqu[2]
            ra = model.q_s_answer(a)
            rq = model.q_s_quiz_question(quiz_id, n)
            answer_id = ra[0]
            question_id = rq[0]
            correct = ra[3]
            value = rq[3]
            aa = model.q_s_already_answered(p_sid, quiz_id, answer_id)
            if aa == []:
                model.q_i_result(quiz_id, question_id,\
                                 answer_id, p_sid, correct, value)
            if (n + 1) <= quiz_length:
                mx_redirect('/quiz/%d/%d' % (quiz_id, n + 1))
            else:
                mx_redirect('/quiz/%d/end' % (quiz_id))
        else:
            postfail = True

    if postfail or rqm == 'GET':
        r = model.q_s_quiz(quiz_id)
        if n == None or p_sid == None:
            sid = mx_make_session_id()
            model.q_i_session(sid)
            mx_set_cookie('player_session_id', sid, max_age=3600)
            return mx_template('quiz_start.tpl', name=r[1], quiz_id=r[0],\
                                errors=frm.errors)
        else:
            rqq = model.q_s_quiz_question(quiz_id, n)
            question = {'id':rqq[0],
                        'content':rqq[1],
                        'position':rqq[2],
                        'value':rqq[3],
                        'image':rqq[4]}
            ras = model.q_s_answers_for_question(question['id'])
            return mx_template('quiz_play.tpl',\
                                        question=question,\
                                        answers=ras, quiz_id=quiz_id,\
                                        errors=frm.errors)
Exemplo 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)