Ejemplo n.º 1
0
	def  get(self, request):
		data = {
			'deleted': False
		}
		answersR = repo.AnswerRepository(Answers)
		ans_id = request.GET.get('id', None)
		quiz_id = answersR.get_quiz_id(ans_id)
		quiz = repo.QuizRepository(Quiz)
		owner_id = quiz.get_owner_id(quiz_id)
		questions = repo.QuestionRepository(Questions)
		question_id = answersR.get_question_id(ans_id)
		question_type = questions.get_question_type(question_id)
		answer_points = answersR.get_answer_points(ans_id)
		is_done = questions.get_done_status(question_id)

		is_deleted = False
		if request.user.id == owner_id:
			answersR.answer_delete(ans_id)
			is_deleted = True
			data = {
				'deleted': True
		}
		if is_deleted:
			if question_type != 'grammar':
				if is_done == True:
					if answer_points != 0:
						questions.update_is_done(question_id, False)
			else:
				answersRepo = repo.AnswerRepository(Answers)
				answers_count = answersRepo.get_answers_count(question_id)
				if is_done == True and answers_count == 0:
					questions.update_is_done(question_id, False)

		return JsonResponse(data)
Ejemplo n.º 2
0
def construct_quiz_teacher(quiz_id):
	quesrtr = repo.QuestionRepository(Questions)
	ar = repo.AnswerRepository(Answers)

	questions = quesrtr.get_by_quiz_id(quiz_id)

	quiz = {}

	for question in questions:
		temp = {}
		temp[question.id] = {}
		temp[question.id]['qname'] = question.name
		temp[question.id]['qtype'] = question.qtype
		temp[question.id]['points'] = question.points
		temp[question.id]['description'] = question.description
		temp[question.id]['answers'] = {}

		if question.qtype != 'open':
			answers = ar.get_answer_points_and_name_by_question(question.id)
			for answer in answers:
				temp[question.id]['answers'][answer.id] = {}
				temp[question.id]['answers'][answer.id]['answer'] = answer.name
				temp[question.id]['answers'][answer.id]['points'] = answer.points
				temp[question.id]['answers'][answer.id]['correct'] = answer.correct
		quiz.update(temp)
	return quiz
Ejemplo n.º 3
0
def construct_quiz(quiz_id):
	quesrtr = repo.QuestionRepository(Questions)
	ar = repo.AnswerRepository(Answers)

	questions = quesrtr.get_by_quiz_id(quiz_id)
	#questions = list(questions)
	#random.shuffle(questions)
	quiz = {}

	for question in questions:
		temp = {}
		temp[question.id] = {}
		temp[question.id]['qname'] = question.name
		temp[question.id]['qtype'] = question.qtype
		temp[question.id]['answers'] = {}

		if question.qtype != 'open':
			answers = ar.get_answer_points_and_name_by_question(question.id)
			count_true = 0
			answers = list(answers)
			random.shuffle(answers)
			for answer in answers:
				temp[question.id]['answers'][answer.id] = {}
				temp[question.id]['answers'][answer.id]['answer'] = answer.name
				temp[question.id]['answers'][answer.id]['points'] = answer.points
				if answer.correct == True:
					count_true += 1
			temp[question.id]['cor_ans'] = count_true
		quiz.update(temp)				
	return quiz
Ejemplo n.º 4
0
def check_student_compare_answer(question_id, stud_answer):
	ar = repo.AnswerRepository(Answers)
	answers = ar.get_answer_points_and_name_by_question(question_id)
	for answer in answers:
		teacher_answer, student_answer = prepare_string(str(answer.name), stud_answer)
		distance = levenshtein_ratio_and_distance(teacher_answer, student_answer)
		if(distance == 0):
			return answer.points
	return 0
Ejemplo n.º 5
0
def add_answer(request, qpk):
	# request should be ajax and method should be POST.
	if request.is_ajax and request.method == "POST":
		# get the form data
		form = AnswerForm(request.POST)
		# save the data and after fetch the object in instance
		if form.is_valid():

			instance = form.save(commit=False)
			answers = repo.AnswerRepository(Answers)
			sum_answers_points = answers.sum_all_question_answers_points(qpk)
			questions = repo.QuestionRepository(Questions)
			question_type = questions.get_question_type(qpk)

			if question_type != 'grammar':
				question_points = questions.get_question_points(qpk)
				form_points_data = form.cleaned_data["points"]
				instance.points = round(form_points_data, 1)
				free_points = question_points - sum_answers_points
				free_ponts = round(free_points, 1)
				instance.question_id = qpk

				left_points = round(free_points - form_points_data, 1)
				instance.points = round(form_points_data, 1)
				if form_points_data < free_points or left_points == 0.0:
					instance.save()
					messages.success(request, 'Success!')
				else:
					instance.points = 0
					instance.save()
					messages.error(request, 'You put more points than you have left. Available points: %f'%free_points)
				# serialize in new friend object in json
				ser_instance = serializers.serialize('json', [ instance, ])

				if left_points == 0:
					questions.update_is_done(qpk, True)
			else:
				instance.points = 0
				instance.question_id = qpk
				instance.save()
				questions.update_is_done(qpk, True)
				messages.success(request, 'Success!')
				ser_instance = serializers.serialize('json', [ instance, ])


			# send to client side.
			return JsonResponse({"instance": ser_instance}, status=200)
		else:
			# some form errors occured.
			return JsonResponse({"error": form.errors}, status=400)

	# some error occured
	return JsonResponse({"error": ""}, status=400)
Ejemplo n.º 6
0
def update_answer(request, pk, template_name='teachers/update/item_edit_form.html'):
	answer= get_object_or_404(Answers, pk=pk)
	answersR = repo.AnswerRepository(Answers)
	quiz_id = answersR.get_quiz_id(pk)
	quiz = repo.QuizRepository(Quiz)
	owner_id = quiz.get_owner_id(quiz_id)

	if request.user.id == owner_id:
		form = AnswerForm(request.POST or None, instance=answer)

		if form.is_valid():	
			instance = form.save(commit=False)
			form_points_data = form.cleaned_data["points"]
			answer_points = answersR.get_answer_points(pk)

			are_points_changed = round(answer_points - form_points_data, 1)
			if are_points_changed != 0:
				questions = repo.QuestionRepository(Questions)
				question_id = answersR.get_question_id(pk)
				sum_answers_points = answersR.sum_all_question_answers_points(question_id)
				question_points = answersR.get_question_points(question_id)
				answer_points = answersR.get_answer_points(pk)
				free_points = question_points - sum_answers_points + answer_points
				free_points = round(free_points, 1)

				instance.points =  round(form_points_data, 1)
				is_done = questions.get_done_status(question_id)

				difference = round(free_points - form_points_data, 1)
				
				if difference >= 0:
					if(difference == 0):
						if is_done == False: 
							questions.update_is_done(question_id, True)
					else:
						if is_done == True:
							questions.update_is_done(question_id, False)
					messages.success(request, 'Success!')
				else:
					instance.points = answer_points
					messages.error(request, 'You put more points than you have left. Available points: %f'%free_points)
			instance.save()
			return HttpResponse(render_to_string('teachers/update/item_edit_form_success.html'))
		return render(request, template_name, {'form':form, 'id': pk})
	raise Http404
Ejemplo n.º 7
0
def construct_main(quiz_id):
	quesrtr = repo.QuestionRepository(Questions)
	ar = repo.AnswerRepository(Answers)

	questions = quesrtr.get_id_and_type(quiz_id)

	quiz = {}

	for question in questions:
		temp = {}
		temp[question.id] = {}
		temp[question.id]['qtype'] = question.qtype
		temp[question.id]['answers'] = {}

		if question.qtype == 'multiple' or  question.qtype == 'single':
			answers = ar.get_answer_points_by_question(question.id)
			for answer in answers:
				temp[question.id]['answers'][answer.id] = answer.points
			
		quiz.update(temp)
	return quiz  
Ejemplo n.º 8
0
def AnswersView(request, qpk):
	form = AnswerForm()

	questions = repo.QuestionRepository(Questions)
	question = questions.get_by_id(qpk)
	owner_id = questions.get_owner_id(qpk)

	if question:
		qtype = question[0].qtype
	else:
		qtype = "NONE"

	grammar_points = []
	if question[0].qtype == 'grammar':
		grammar_points = questions.get_grammar_points(qpk)	
		
	if request.user.id == owner_id and qtype != 'open':
		answersR = repo.AnswerRepository(Answers)
		answers = answersR.get_by_id_ordered(qpk)
		quiz_is_active = questions.get_quiz_status(qpk)
		quiz_id = questions.get_quiz_id(qpk)
		return render(request, "teachers/lists/answers_list.html", {"form": form, "answers": answers, "question_id": qpk, "question": question, 'quiz_is_active': quiz_is_active, 'quiz_id': quiz_id, 'qtype': qtype, 'grammar_points': grammar_points})
	raise Http404
Ejemplo n.º 9
0
def construct_quiz_student_results(quiz_id, student_id):

	qsrr = repo.QuizSolveRecordRepository(QuizSolveRecord)
	solve_info_id = qsrr.get_solve_info_id(quiz_id, student_id)

	if solve_info_id != 0:
		sar = repo.StudentAnswersRepository(StudentAnswers)
		stud_answers = sar.get_student_answers(solve_info_id)
		quesrtr = repo.QuestionRepository(Questions)
		ar = repo.AnswerRepository(Answers)
		answers_id = [ sub['answer_id'] for sub in stud_answers ] 
		
		questions = quesrtr.get_by_quiz_id(quiz_id)

		soar = repo.StudentOpenAnswersRepository(StudentOpenAnswers)
		sgar = repo.StudentGrammarAnswersRepository(StudentGrammarAnswers)
		quiz = {}

		for question in questions:
			temp = {}
			temp[question.id] = {}
			temp[question.id]['qname'] = question.name
			temp[question.id]['qtype'] = question.qtype
			temp[question.id]['points'] = question.points
			temp[question.id]['description'] = question.description
			temp[question.id]['answers'] = {}


			if question.qtype != 'open' and question.qtype != 'compare' and question.qtype != 'grammar':
				answers = ar.get_answer_points_and_name_by_question(question.id)
				for answer in answers:
					temp[question.id]['answers'][answer.id] = {}
					temp[question.id]['answers'][answer.id]['answer'] = answer.name
					temp[question.id]['answers'][answer.id]['points'] = answer.points
					temp[question.id]['answers'][answer.id]['correct'] = answer.correct
					if answer.id in answers_id:
						temp[question.id]['answers'][answer.id]['is_answer'] = True
					else:
						temp[question.id]['answers'][answer.id]['is_answer'] = False
			else:
				if question.qtype != 'grammar':
					open_student_answers = soar.get_student_answers(solve_info_id, question.id)
					temp[question.id]['answers']['answer'] = ""
					temp[question.id]['answers']['points'] = 0
					if open_student_answers != -1:
						temp[question.id]['answers']['answer'] = open_student_answers['answer']
						temp[question.id]['answers']['points'] = open_student_answers['points']
				else:
					open_student_answers = sgar.get_student_answers(solve_info_id, question.id)
					temp[question.id]['answers']['answer'] = ""
					temp[question.id]['answers']['points'] = 0
					if open_student_answers != -1:
						temp[question.id]['answers']['answer'] = open_student_answers['answer']
						temp[question.id]['answers']['points'] = open_student_answers['points']
						temp[question.id]['answers']['result'] =  json.loads(open_student_answers['check_result'])
						temp[question.id]['answers']['corrected_sents'] =  open_student_answers['corrected_sent']
								

			quiz.update(temp)
		return quiz
	else:
		return -1
Ejemplo n.º 10
0
def update_question(request, pk, template_name='teachers/update/update_question.html'):
	question= get_object_or_404(Questions, pk=pk)
	questions = repo.QuestionRepository(Questions)
	owner_id = questions.get_owner_id(pk)
	is_actve = questions.get_quiz_status(pk)

	if request.user.id == owner_id and not is_actve:
		lr = repo.LanguagesRepository(Languages)
		languages = get_lang_options_for_question(lr, pk)
		cur_question_points = questions.get_question_points(pk)
		answers = repo.AnswerRepository(Answers)
		sum_answers_points, exist = answers.sum_all_question_answers_points_if_exist(pk)
	
		question_ = questions.get_by_id(pk)
		if question_:
			qtype = question_[0].qtype
		else:
			qtype = "NONE"

		grammar_points = []
		if question_[0].qtype == 'grammar':
			grammar_points = questions.get_grammar_points(pk)

		form = QuestionForm(request.POST or None, instance=question)
		if form.is_valid():
			qtype = questions.get_question_type(pk)
			update_status = -1
			question = form.save(commit=False)

			form_points_data = form.cleaned_data["points"]
			if_points_changed = round(form_points_data - cur_question_points, 1)

			if if_points_changed != 0:
				same_points = False
				question.points = round(form_points_data, 1)
				quiz_id = questions.get_quiz_id(pk)
				sum_quiz_questions = questions.sum_all_quiz_questions_points(quiz_id)
				quiz_points = questions.get_quiz_points(pk)
				aval_points = quiz_points - sum_quiz_questions + cur_question_points
				aval_points = round(aval_points, 1)

				if (form_points_data > aval_points):
					question.points = cur_question_points
					same_points = True
					messages.error(request, 'Available course points: %f. '%aval_points)
				if (form_points_data < sum_answers_points):
					question.points = cur_question_points
					messages.error(request, 'Answer points in this question are: %f. '%sum_answers_points)
				is_done = questions.get_done_status(pk)
				
				if form_points_data > sum_answers_points and not same_points:
					if is_done == True and qtype != 'grammar':
						update_status = 0
						#questions.update_is_done(pk, False)
				else:
					if is_done == False and form_points_data == sum_answers_points:
						update_status = 1
						#questions.update_is_done(pk, True)
			
			update_type = form.cleaned_data["qtype"]
			if update_type == 'open' or update_type == 'compare' or update_type == 'grammar':
				if qtype != 'open' and qtype != 'compare':
					if not exist:
						update_status = 1
					else:
						question.qtype = qtype
						update_status = -1
						#messages.error(request, 'Can not change type of the question to open or compare. There are answers in this question!')
				
				if update_type == 'compare':
					if not exist:
						update_status = 0
				else:
					if update_type == 'open':
						if not exist:
							update_status = 1
						else:
							question.qtype = qtype
							update_status = -1
							messages.error(request, 'Can not change type of the question to open. There are answers in this question!')
				if update_type == 'grammar' and qtype != 'grammar':
					question.qtype = qtype
			else:
				if qtype == 'open' or qtype == 'compare':
					if not exist:
						if update_status == -1:
							update_status = -1
						else:
							update_status = 0 
					if qtype == 'open' and update_type != 'open':
						update_status = 0 
				else:
					if qtype == 'grammar':
						update_status = 1

			question.save()	
			if update_status == 1 or qtype == 'grammar':
				gramm_update_status = 1
				if qtype == 'grammar' and update_type != 'grammar':
					gqsr = repo.GrammarQuestionSanctionsRepository(GrammarQuestionSanctions)
					gqsr.delete_sanctions(pk)
					if update_type == 'open':
						questions.update_is_done(pk, True)
					else:
						questions.update_is_done(pk, False)
					gramm_update_status = 0
				else:
					if update_type == 'grammar' and qtype == 'grammar':
						lang_id = lr.get_id_by_abr(request.POST["lang"])
						if lang_id != -1:
							gqsr = repo.GrammarQuestionSanctionsRepository(GrammarQuestionSanctions)
							gqsr.update_sanctions(pk, request.POST["spelling_points"], request.POST["grammar_points"], request.POST["translate_points"], request.POST["order_points"], request.POST["ethalon_points"], lang_id)
							gramm_update_status = 0
					else:
						if update_type == 'grammar' and qtype != 'grammar':
							gramm_update_status = 0
							messages.error(request, 'Can not change type of the question to grammar!')
				if gramm_update_status:
					questions.update_is_done(pk, True)
		
			else:
				if update_status == 0:
					questions.update_is_done(pk, False)
			
			return HttpResponse(render_to_string('teachers/update/item_edit_form_success.html'))
		return render(request, template_name, {'form':form, 'id': pk, 'sum_answers': sum_answers_points, 'mistake_points': grammar_points, 'qtype': qtype, 'languages': languages})
	raise Http404