def del_question(user_id, qbank_code, question_id): user = User.objects().with_id(user_id) old_question = Question.objects().with_id(question_id) qbank = Qbank.objects(qbank_code=qbank_code).first() old_qbank_code = qbank_code question_type = old_question['question_type'] question = old_question['question'] optiona = old_question['optiona'] optionb = old_question['optionb'] optionc = old_question['optionc'] optiond = old_question['optiond'] right_answer = old_question['right_answer'] deletedquestion = Deletedquestion( old_qbank_code=old_qbank_code, question_type=question_type, question=question, optiona=optiona, optionb=optionb, optionc=optionc, optiond=optiond, right_answer=right_answer, ) if question_type == "Dễ": qbank.update(pull__qbank_easy=old_question) elif question_type == "Trung bình": qbank.update(pull__qbank_medium=old_question) elif question_type == "Khó": qbank.update(pull__qbank_hard=old_question) deletedquestion.save() old_question.delete() user = User.objects().with_id(user_id) old_question = Question.objects().with_id(question_id) qbank = Qbank.objects(qbank_code=qbank_code).first() return render_template('update_qbank.html', user=user, qbank=qbank)
def update_question(user_id, qbank_code, question_id): user = User.objects().with_id(user_id) old_question = Question.objects().with_id(question_id) qbank = Qbank.objects(qbank_code=qbank_code).first() if request.method == 'GET': return render_template('update_question.html', question=old_question) if request.method == 'POST': form = request.form question_type = form['question_type'] question = form['question'] optiona = form['optiona'] optionb = form['optionb'] optionc = form['optionc'] optiond = form['optiond'] right_answer = form.get('right_answer') old_question.update( set__question_type=question_type, set__question=question, set__optiona=optiona, set__optionb=optionb, set__optionc=optionc, set__optiond=optiond, set__right_answer=right_answer, ) user = User.objects().with_id(user_id) old_question = Question.objects().with_id(question_id) qbank = Qbank.objects(qbank_code=qbank_code).first() return render_template('update_qbank.html', user=user, qbank=qbank)
def create_qbank(user_id): user = User.objects().with_id(user_id) qbanks = Qbank.objects() if request.method == 'GET': return render_template('qbank_code.html', error="") if request.method == 'POST': form = request.form qbank_code = form['qbank_code'].upper() qbank_list = [] for qb in qbanks: qbank_list.append(qb['qbank_code']) if qbank_code in qbank_list: error = "Question bank code is already existed, please choose another Question bank code" return render_template('qbank_code.html', error=error) else: new_qbank = Qbank( qbank_code=qbank_code, qbank_easy=[], qbank_medium=[], qbank_hard=[], ) new_qbank.save() user.update(push__qbanks=new_qbank) return redirect( url_for('qbank_code', user_id=user_id, qbank_code=qbank_code))
def save_qbank(user_id, qbank_code): user = User.objects().with_id(user_id) qbanks = user['qbanks'] qbank = Qbank.objects(qbank_code=qbank_code).first() user.update(pull__qbanks=qbank) user.update(push__qbanks=qbank) return render_template('qbanks.html', user=user, qbanks=qbanks)
def exam_teacher_info(user_id, exam_id): exam = Exam.objects().with_id(exam_id) print(exam['id']) if request.method == 'GET': return render_template('teacher_info.html') if request.method == 'POST': form = request.form teacher_id = form['teacher_id'] qbank_code = form['qbank_code'].upper() n_easy = int(form['n_easy']) n_medium = int(form['n_medium']) n_hard = int(form['n_hard']) time = int(form['time']) exam.update( set__teacher_id=teacher_id, set__qbank_code=qbank_code, set__n_easy=n_easy, set__n_medium=n_medium, set__n_hard=n_hard, set__time=time, ) exam.reload() print(exam['id']) qbank_code = exam['qbank_code'] print(qbank_code) qbank = Qbank.objects(qbank_code=qbank_code).first() qbank_easy = qbank['qbank_easy'] qbank_medium = qbank['qbank_medium'] qbank_hard = qbank['qbank_hard'] n_easy = exam['n_easy'] n_medium = exam['n_medium'] n_hard = exam['n_hard'] len_easy = len(qbank_easy) len_medium = len(qbank_medium) len_hard = len(qbank_hard) choose_easy = sample(range(len_easy), n_easy) choose_medium = sample(range(len_medium), n_medium) choose_hard = sample(range(len_hard), n_hard) exam_questions = [] for i in choose_easy: q = qbank_easy[i] exam_questions.append(q) for i in choose_medium: q = qbank_medium[i] exam_questions.append(q) for i in choose_hard: q = qbank_hard[i] exam_questions.append(q) exam.update(set__exam_questions=exam_questions) return redirect( url_for('exam_waiting', user_id=user_id, exam_id=exam_id))
def add_question(user_id, qbank_code): user = User.objects().with_id(user_id) qbanks = user['qbanks'] qbank = Qbank.objects(qbank_code=qbank_code).first() qlist = [] for q in qbank['qbank_easy']: qlist.append(q['question']) for q in qbank['qbank_medium']: qlist.append(q['question']) for q in qbank['qbank_hard']: qlist.append(q['question']) if request.method == 'GET': return render_template('qbank.html', user_id=user_id, qbank_code=qbank_code, error="") if request.method == 'POST': form = request.form question_type = form['question_type'] question = form['question'] optiona = form['optiona'] optionb = form['optionb'] optionc = form['optionc'] optiond = form['optiond'] right_answer = form.get('right_answer') if question in qlist: return render_template( 'qbank.html', user_id=user_id, qbank_code=qbank_code, error="Đã tồn tại câu hỏi, làm ơn nhập câu hỏi tiếp theo") else: new_question = Question( question_type=question_type, question=question, optiona=optiona, optionb=optionb, optionc=optionc, optiond=optiond, right_answer=right_answer, ) new_question.save() if question_type == "Dễ": qbank.update(push__qbank_easy=new_question) elif question_type == "Trung bình": qbank.update(push__qbank_medium=new_question) elif question_type == "Khó": qbank.update(push__qbank_hard=new_question) return redirect( url_for('qbank_code', user_id=user_id, qbank_code=qbank_code))
def delete_qbank(user_id, qbank_code): user = User.objects().with_id(user_id) old_qbank = Qbank.objects(qbank_code=qbank_code).first() deletedqbank = Deletedqbank( user_id=user_id, qbank_code=qbank_code, qbank_easy=old_qbank['qbank_easy'], qbank_medium=old_qbank['qbank_medium'], qbank_hard=old_qbank['qbank_hard'], ) deletedqbank.save() user.update(pull__qbanks=old_qbank) old_qbank.delete() user = User.objects().with_id(user_id) qbanks = user['qbanks'] return render_template('qbanks.html', user=user, qbanks=qbanks)
def exam_doing(user_id, exam_id): exam = Exam.objects().with_id(exam_id) qbank_code = exam['qbank_code'] time = exam['time'] qbank = Qbank.objects(qbank_code=qbank_code).first() exam_questions = exam['exam_questions'] if request.method == 'GET': return render_template('student_exam.html', exam_questions=exam_questions, time=time) if request.method == 'POST': form = request.form answer_list = [] for i in range(len(exam_questions)): i = i + 1 answer = form[str(i)] answer_list.append(answer) exam.update(set__answer=answer_list) return redirect(url_for('welcome', user_id=user_id))
def update_qbank(user_id, qbank_code): user = User.objects().with_id(user_id) qbank = Qbank.objects(qbank_code=qbank_code).first() return render_template('update_qbank.html', user=user, qbank=qbank)