示例#1
0
def show(qaire_id):
    qaires = Questionnaire.select().where(Questionnaire.public_id == qaire_id)
    ret = {}

    for qaire in qaires:
        category = Category.select().join(Questionnaire).where(Questionnaire.id == qaire.id)
        questions = Question.select().join(Questionnaire).where(Questionnaire.id == qaire.id)

        catname = ''

        for cat in category:
            catname = cat.name
            break

        ret = {'id': qaire.id, 'public_id': qaire.public_id, 'name': qaire.name, 'category': catname, 'questions' : [], 'category_name': category.get().name}

        for qion in questions:
            qtype = qtype2str(qion.typ)
            ret['questions'].append({'id': qion.id, 'type': qion.typ, 'description': qion.description})

            if qtype == 'single' or qtype == 'multi':
                ret['questions'][-1]['options'] = []
                options = Option.select().join(Question).where(Question.id == qion.id)

                for opt in options:
                    ret['questions'][-1]['options'].append({'id': opt.id, 'text': opt.text})

    if not ret:
        ret = response_error('not_found', False)

    return json.dumps(ret)
示例#2
0
文件: question.py 项目: vojto/riddle
def new_question():
    user = auth.get_logged_in_user()

    description = request.form['description']
    typ = request.form['type']
    presented = request.form.get('presented', False)
    public_id = request.form['public_id']
    options = request.form.getlist('options[]')

    try:
        # Find questionnaire
        qaire = Questionnaire.select().join(Category).where(Category.teacher == user).where(Questionnaire.public_id == public_id).get()

        # Create question
        if qtype2str(typ) is None:
            return response_error('unknown_question_type')
        question = Question.create(description=description, typ=typ, presented=presented, questionnaire=qaire)

        # Create option
        for option_text in options:
            option = Option.create(question=question, text=option_text)

        ret = response_success(False)
        ret['question_id'] = question.id
        return json.dumps(ret)

    except Questionnaire.DoesNotExist:
        return response_error('public_id_not_found')
示例#3
0
文件: misc.py 项目: vojto/riddle
def show_questions(qaire_id):
    user = auth.get_logged_in_user()
    qaires = Questionnaire.select().join(Category).where(Questionnaire.public_id == qaire_id).where(Category.teacher == user)

    ret = {}

    for qaire in qaires:
        category = Category.select().join(Questionnaire).where(Questionnaire.id == qaire.id)
        questions = Question.select().join(Questionnaire).where(Questionnaire.id == qaire.id)

        catname = ''

        for cat in category:
            catname = cat.name
            break

        ret = {'id': qaire.id, 'public_id': qaire.public_id, 'name': qaire.name, 'category': catname, 'questions' : []}

        for qion in questions:
            qtype = qtype2str(qion.typ)
            ret['questions'].append({'id': qion.id, 'type': qion.typ, 'description': qion.description, 'presented': qion.presented})

            if qtype == 'single' or qtype == 'multi':
                ret['questions'][-1]['options'] = []
                options = Option.select().join(Question).where(Question.id == qion.id)

                for opt in options:
                    ret['questions'][-1]['options'].append({'id': opt.id, 'text': opt.text})

    return json.dumps(ret)
示例#4
0
def results_options():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)
    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        if strtype == 'single' or strtype == 'multi':
            opts = Option.select().where(
                Option.question == qion).annotate(Answer)

            for opt in opts:
                ret['question_answers'].append({
                    'option_id': opt.id,
                    'option_text': opt.text,
                    'answers': opt.count
                })
        else:
            return response_error('wrong_question_type')

        return json.dumps(ret)

    return response_error('question_not_found')
示例#5
0
def results_texts():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        answers = Answer.select(Answer, Student).where(Answer.question == qion)

        for answer in answers:
            if answer.text:
                ret['question_answers'].append({
                    'text': answer.text,
                    'student': answer.student.name,
                    'id': answer.id
                })

        return json.dumps(ret)

    return response_error('question_not_found')
示例#6
0
def new_question():
    user = auth.get_logged_in_user()

    description = request.form['description']
    typ = request.form['type']
    presented = request.form.get('presented', False)
    public_id = request.form['public_id']
    options = request.form.getlist('options[]')

    try:
        # Find questionnaire
        qaire = Questionnaire.select().join(Category).where(
            Category.teacher == user).where(
                Questionnaire.public_id == public_id).get()

        # Create question
        if qtype2str(typ) is None:
            return response_error('unknown_question_type')
        question = Question.create(description=description,
                                   typ=typ,
                                   presented=presented,
                                   questionnaire=qaire)

        # Create option
        for option_text in options:
            option = Option.create(question=question, text=option_text)

        ret = response_success(False)
        ret['question_id'] = question.id
        return json.dumps(ret)

    except Questionnaire.DoesNotExist:
        return response_error('public_id_not_found')
示例#7
0
def submit_answer():
    student = get_current_student()
    question_id = request.form['question_id']

    qions = Question.select().where(Question.id == question_id)

    for qion in qions:
        qion_type = qtype2str(qion.typ)

        text_answer = request.form.get('text_answer')
        if text_answer and text_answer != '':
            Answer.create(text=text_answer, question=qion, student=student)

        option_ids = request.form.getlist('option_ids[]')
        if len(option_ids) > 0:
            if qion_type == 'single':
                option_ids = option_ids[:1]

            final_opts = []

            for oid in option_ids:
                opts = Option.select().where(Option.question == qion).where(Option.id == oid)
                for opt in opts:
                    final_opts.append(opt)
                    break
                else:
                    return response_error('wrong_option')

            for opt in final_opts:
                    Answer.create(option=opt, question=qion, student=student)

        return response_success()

    return response_error('question_not_found')
示例#8
0
文件: question.py 项目: vojto/riddle
def remove_question():
    user = auth.get_logged_in_user()
    question_id = request.form['id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        qion.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('question_not_found')
示例#9
0
def remove_question():
    user = auth.get_logged_in_user()
    question_id = request.form['id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        qion.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('question_not_found')
示例#10
0
def edit_question():
    user = auth.get_logged_in_user()

    question_id = request.form['id']
    description = request.form.get('description')
    typ = request.form.get('type')
    presented = request.form.get('presented')
    public_id = request.form.get('public_id')

    ret = {}

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        if description is not None:
            qion.description = description

        if typ:
            if not qtype2str(typ):
                return response_error('unknown_question_type')

            qion.typ = typ

        if presented is not None:
            qion.presented = presented

        if public_id:
            qaires = Questionnaire.select().join(Category).where(
                Category.teacher == user).where(
                    Questionnaire.public_id == public_id)
            for qaire in qaires:
                qion.public_id = public_id
                break
            else:
                return response_error('public_id_not_found')

        qion.save()

        if typ and qtype2str(qion.typ) == 'text':
            # Delete options when changing type to 'text'.
            opts = Option.select().where(Option.question == qion)
            for opt in opts:
                opt.delete_instance(recursive=True, delete_nullable=True)

        return response_success()

    return response_error('question_not_found')
示例#11
0
def show_questions(qaire_id):
    user = auth.get_logged_in_user()
    qaires = Questionnaire.select().join(Category).where(
        Questionnaire.public_id == qaire_id).where(Category.teacher == user)

    ret = {}

    for qaire in qaires:
        category = Category.select().join(Questionnaire).where(
            Questionnaire.id == qaire.id)
        questions = Question.select().join(Questionnaire).where(
            Questionnaire.id == qaire.id)

        catname = ''

        for cat in category:
            catname = cat.name
            break

        ret = {
            'id': qaire.id,
            'public_id': qaire.public_id,
            'name': qaire.name,
            'category': catname,
            'questions': []
        }

        for qion in questions:
            qtype = qtype2str(qion.typ)
            ret['questions'].append({
                'id': qion.id,
                'type': qion.typ,
                'description': qion.description,
                'presented': qion.presented
            })

            if qtype == 'single' or qtype == 'multi':
                ret['questions'][-1]['options'] = []
                options = Option.select().join(Question).where(
                    Question.id == qion.id)

                for opt in options:
                    ret['questions'][-1]['options'].append({
                        'id': opt.id,
                        'text': opt.text
                    })

    return json.dumps(ret)
示例#12
0
文件: question.py 项目: vojto/riddle
def edit_question():
    user = auth.get_logged_in_user()

    question_id = request.form['id']
    description = request.form.get('description')
    typ = request.form.get('type')
    presented = request.form.get('presented')
    public_id = request.form.get('public_id')

    ret = {}

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        if description is not None:
            qion.description = description

        if typ:
            if not qtype2str(typ):
                return response_error('unknown_question_type')

            qion.typ = typ

        if presented is not None:
            qion.presented = presented

        if public_id:
            qaires = Questionnaire.select().join(Category).where(Category.teacher == user).where(Questionnaire.public_id == public_id)
            for qaire in qaires:
                qion.public_id = public_id
                break
            else:
                return response_error('public_id_not_found')


        qion.save()

        if typ and qtype2str(qion.typ) == 'text':
            # Delete options when changing type to 'text'.
            opts = Option.select().where(Option.question == qion)
            for opt in opts:
                opt.delete_instance(recursive=True, delete_nullable=True)

        return response_success()


    return response_error('question_not_found')
示例#13
0
def new_option():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']
    text = request.form['text']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        typ = qtype2str(qion.typ)
        if typ == 'single' or typ == 'multi':
            option = Option.create(text=text, question=qion)
            ret = response_success(False)
            ret['option_id'] = option.id
            return json.dumps(ret)
        else:
            return response_error('options_not_supported')

    return response_error('question_not_found')
示例#14
0
文件: misc.py 项目: vojto/riddle
def results_options():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)
    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        if strtype == 'single' or strtype == 'multi':
            opts = Option.select().where(Option.question == qion).annotate(Answer)

            for opt in opts:
                ret['question_answers'].append({'option_id': opt.id, 'option_text': opt.text, 'answers': opt.count})
        else:
            return response_error('wrong_question_type')

        return json.dumps(ret)

    return response_error('question_not_found')
示例#15
0
文件: misc.py 项目: vojto/riddle
def results_texts():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        answers = Answer.select(Answer, Student).where(Answer.question == qion)

        for answer in answers:
            if answer.text:
                ret['question_answers'].append({'text': answer.text, 'student': answer.student.name, 'id': answer.id})


        return json.dumps(ret)

    return response_error('question_not_found')
示例#16
0
文件: question.py 项目: vojto/riddle
def present_question(qid):
    question = Question.get(Question.id == qid)
    question.present()
    print 'presenting question', question
    return response_success()
示例#17
0
def present_question(qid):
    question = Question.get(Question.id == qid)
    question.present()
    print 'presenting question', question
    return response_success()