summaryrefslogtreecommitdiff
path: root/yaksh/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/views.py')
-rw-r--r--yaksh/views.py148
1 files changed, 99 insertions, 49 deletions
diff --git a/yaksh/views.py b/yaksh/views.py
index 1bb335c..0bf91eb 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -28,10 +28,12 @@ import re
# Local imports.
from yaksh.code_server import get_result as get_result_from_code_server
from yaksh.models import (
- Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, Profile,
- QuestionPaper, QuestionSet, Quiz, Question, TestCase, User,
- FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, LearningUnit,
- LearningModule, CourseStatus
+ Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, FloatTestCase,
+ HookTestCase, IntegerTestCase, McqTestCase, Profile,
+ QuestionPaper, QuestionSet, Quiz, Question, StandardTestCase,
+ StdIOBasedTestCase, StringTestCase, TestCase, User,
+ get_model_class, FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile,
+ LearningUnit, LearningModule, CourseStatus, question_types
)
from yaksh.forms import (
UserRegisterForm, UserLoginForm, QuizForm, QuestionForm,
@@ -154,26 +156,39 @@ def user_logout(request):
def quizlist_user(request, enrolled=None, msg=None):
"""Show All Quizzes that is available to logged-in user."""
user = request.user
+ courses_data = []
if request.method == "POST":
course_code = request.POST.get('course_code')
hidden_courses = Course.objects.get_hidden_courses(code=course_code)
- courses = hidden_courses if hidden_courses else None
+ courses = hidden_courses
title = 'Search'
elif enrolled is not None:
- courses = user.students.all()
+ courses = user.students.filter(is_trial=False).order_by('-id')
title = 'Enrolled Courses'
else:
courses = Course.objects.filter(
active=True, is_trial=False
).exclude(
~Q(requests=user), ~Q(rejected=user), hidden=True
- )
+ ).order_by('-id')
title = 'All Courses'
+ for course in courses:
+ if user in course.students.all():
+ _percent = course.get_completion_percent(user)
+ else:
+ _percent = None
+ courses_data.append(
+ {
+ 'data': course,
+ 'completion_percentage': _percent,
+ }
+ )
+
context = {
- 'user': user, 'courses': courses,
+ 'user': user, 'courses': courses_data,
'title': title, 'msg': msg
}
@@ -381,7 +396,7 @@ def prof_manage(request, msg=None):
if not is_moderator(user):
return my_redirect('/exam/')
courses = Course.objects.filter(Q(creator=user) | Q(teachers=user),
- is_trial=False)
+ is_trial=False).distinct()
trial_paper = AnswerPaper.objects.filter(
user=user, question_paper__quiz__is_trial=True,
course__is_trial=True
@@ -469,11 +484,19 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None,
# unit module prerequiste check
if learning_module.has_prerequisite():
- if not learning_module.is_prerequisite_passed(user, course):
+ if not learning_module.is_prerequisite_complete(user, course):
msg = "You have not completed the module previous to {0}".format(
learning_module.name)
return course_modules(request, course_id, msg)
+ if learning_module.check_prerequisite_passes:
+ if not learning_module.is_prerequisite_passed(user, course):
+ msg = (
+ "You have not successfully passed the module"
+ " previous to {0}".format(learning_module.name)
+ )
+ return course_modules(request, course_id, msg)
+
# is user enrolled in the course
if not course.is_enrolled(user):
msg = 'You are not enrolled in {0} course'.format(course.name)
@@ -499,7 +522,7 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None,
# prerequisite check and passing criteria for quiz
if learning_unit.has_prerequisite():
- if not learning_unit.is_prerequisite_passed(
+ if not learning_unit.is_prerequisite_complete(
user, learning_module, course):
msg = "You have not completed the previous Lesson/Quiz/Exercise"
if is_moderator(user):
@@ -513,23 +536,28 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None,
# if any previous attempt
last_attempt = AnswerPaper.objects.get_user_last_attempt(
quest_paper, user, course_id)
- if last_attempt and last_attempt.is_attempt_inprogress():
- return show_question(
- request, last_attempt.current_question(), last_attempt,
- course_id=course_id, module_id=module_id,
- previous_question=last_attempt.current_question()
- )
+
+ if last_attempt:
+ if last_attempt.is_attempt_inprogress():
+ return show_question(
+ request, last_attempt.current_question(), last_attempt,
+ course_id=course_id, module_id=module_id,
+ previous_question=last_attempt.current_question()
+ )
+ attempt_number = last_attempt.attempt_number + 1
+ else:
+ attempt_number = 1
+
# allowed to start
if not quest_paper.can_attempt_now(user, course_id)[0]:
msg = quest_paper.can_attempt_now(user, course_id)[1]
if is_moderator(user):
return prof_manage(request, msg=msg)
- return view_module(request, module_id=module_id, course_id=course_id,
- msg=msg)
- if not last_attempt:
- attempt_number = 1
- else:
- attempt_number = last_attempt.attempt_number + 1
+ return complete(
+ request, msg, last_attempt.attempt_number, quest_paper.id,
+ course_id=course_id, module_id=module_id
+ )
+
if attempt_num is None and not quest_paper.quiz.is_exercise:
context = {
'user': user,
@@ -627,7 +655,7 @@ def show_question(request, question, paper, error_message=None,
'can_skip': can_skip,
'delay_time': delay_time,
'quiz_type': quiz_type,
- 'all_modules': all_modules
+ 'all_modules': all_modules,
}
answers = paper.get_previous_answers(question)
if answers:
@@ -901,8 +929,10 @@ def complete(request, reason=None, attempt_num=None, questionpaper_id=None,
"""Show a page to inform user that the quiz has been completed."""
user = request.user
if questionpaper_id is None:
- message = reason or "An Unexpected Error occurred. Please contact your '\
- 'instructor/administrator.'"
+ message = (
+ reason or "An Unexpected Error occurred."
+ " Please contact your instructor/administrator."
+ )
context = {'message': message}
return my_render_to_response(request, 'yaksh/complete.html', context)
else:
@@ -2194,18 +2224,15 @@ def _read_user_csv(reader, course):
if users.exists():
user = users[0]
if remove.strip().lower() == 'true':
- if _remove_from_course(user, course):
- upload_details.append("{0} -- {1} -- User rejected".format(
- counter, user.username))
- continue
+ _remove_from_course(user, course)
+ upload_details.append("{0} -- {1} -- User rejected".format(
+ counter, user.username))
else:
- if _add_to_course(user, course):
- upload_details.append("{0} -- {1} -- User rejected".format(
- counter, user.username))
- if user not in course.get_enrolled():
- upload_details.append("{0} -- {1} not added to course".format(
- counter, user))
- continue
+ _add_to_course(user, course)
+ upload_details.append(
+ "{0} -- {1} -- User Added Successfully".format(
+ counter, user.username))
+ continue
user_defaults = {'email': email, 'first_name': first_name,
'last_name': last_name}
user, created = _create_or_update_user(username, password,
@@ -2255,13 +2282,15 @@ def _get_csv_values(row, fields):
def _remove_from_course(user, course):
if user in course.get_enrolled():
course.reject(True, user)
- return True
+ else:
+ course.rejected.add(user)
def _add_to_course(user, course):
if user in course.get_rejected():
course.enroll(True, user)
- return True
+ else:
+ course.students.add(user)
def _create_or_update_user(username, password, defaults):
@@ -2420,17 +2449,24 @@ def show_lesson(request, lesson_id, module_id, course_id):
msg = "{0} is not active".format(learn_unit.lesson.name)
return view_module(request, module_id, course_id, msg)
if learn_module.has_prerequisite():
- if not learn_module.is_prerequisite_passed(user, course):
+ if not learn_module.is_prerequisite_complete(user, course):
msg = "You have not completed the module previous to {0}".format(
learn_module.name)
return view_module(request, module_id, course_id, msg)
+ if learn_module.check_prerequisite_passes:
+ if not learn_module.is_prerequisite_passed(user, course):
+ msg = (
+ "You have not successfully passed the module"
+ " previous to {0}".format(learn_module.name)
+ )
+ return view_module(request, module_id, course_id, msg)
# update course status with current unit
_update_unit_status(course_id, user, learn_unit)
all_modules = course.get_learning_modules()
if learn_unit.has_prerequisite():
- if not learn_unit.is_prerequisite_passed(user, learn_module, course):
+ if not learn_unit.is_prerequisite_complete(user, learn_module, course):
msg = "You have not completed previous Lesson/Quiz/Exercise"
return view_module(request, learn_module.id, course_id, msg=msg)
context = {'lesson': learn_unit.lesson, 'user': user,
@@ -2692,13 +2728,20 @@ def design_course(request, course_id):
if remove_values:
course.learning_module.remove(*remove_values)
- if "Change_prerequisite" in request.POST:
+ if "change_prerequisite_completion" in request.POST:
unit_list = request.POST.getlist("check_prereq")
for unit in unit_list:
learning_module = course.learning_module.get(id=unit)
learning_module.toggle_check_prerequisite()
learning_module.save()
+ if "change_prerequisite_passing" in request.POST:
+ unit_list = request.POST.getlist("check_prereq_passes")
+ for unit in unit_list:
+ learning_module = course.learning_module.get(id=unit)
+ learning_module.toggle_check_prerequisite_passes()
+ learning_module.save()
+
added_learning_modules = course.get_learning_modules()
all_learning_modules = LearningModule.objects.filter(
creator=user, is_trial=False)
@@ -2730,11 +2773,19 @@ def view_module(request, module_id, course_id, msg=None):
return course_modules(request, course_id, msg)
all_modules = course.get_learning_modules()
if learning_module.has_prerequisite():
- if not learning_module.is_prerequisite_passed(user, course):
+ if not learning_module.is_prerequisite_complete(user, course):
msg = "You have not completed the module previous to {0}".format(
learning_module.name)
return course_modules(request, course_id, msg)
+ if learning_module.check_prerequisite_passes:
+ if not learning_module.is_prerequisite_passed(user, course):
+ msg = (
+ "You have not successfully passed the module"
+ " previous to {0}".format(learning_module.name)
+ )
+ return course_modules(request, course_id, msg)
+
learning_units = learning_module.get_learning_units()
context['learning_units'] = learning_units
context['learning_module'] = learning_module
@@ -2821,8 +2872,6 @@ def preview_questionpaper(request, questionpaper_id):
if not is_moderator(user):
raise Http404('You are not allowed to view this page!')
paper = QuestionPaper.objects.get(id=questionpaper_id)
- if not paper.quiz.creator == user:
- raise Http404('This questionpaper does not belong to you')
context = {
'questions': paper._get_questions_for_answerpaper(),
'paper': paper,
@@ -2891,10 +2940,11 @@ def download_course(request, course_id):
course_name = course.name.replace(" ", "_")
# Static files required for styling in html template
- static_files = {"js": ["bootstrap.js", "bootstrap.min.js",
- "jquery-1.9.1.min.js", "video.js"],
- "css": ["bootstrap.css", "bootstrap.min.css",
- "video-js.css", "offline.css"],
+ static_files = {"js": ["bootstrap.min.js",
+ "jquery-3.3.1.min.js", "video.js"],
+ "css": ["bootstrap.min.css",
+ "video-js.css", "offline.css",
+ "yakshcustom.css"],
"images": ["yaksh_banner.png"]}
zip_file = course.create_zip(current_dir, static_files)
zip_file.seek(0)