diff options
-rw-r--r-- | yaksh/models.py | 16 | ||||
-rw-r--r-- | yaksh/templates/yaksh/courses.html | 1 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 31 |
4 files changed, 49 insertions, 1 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index ad61872..31038f1 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -221,6 +221,13 @@ class Course(models.Model): success = False return success + def get_only_students(self): + teachers = list(self.teachers.all().values_list("id", flat=True)) + teachers.append(self.creator.id) + students = self.students.exclude(id__in=teachers) + return students + + def __str__(self): return self.name @@ -900,6 +907,15 @@ class AnswerPaperManager(models.Manager): data['questionpaperid'] = questionpaper_id return data + def get_user_best_of_attempts_marks(self, quiz, user_id): + best_attempt = 0.0 + papers = self.filter(question_paper__quiz=quiz, + user=user_id).values("marks_obtained") + if papers: + best_attempt = max([marks["marks_obtained"] for marks in papers]) + return best_attempt + + ############################################################################### class AnswerPaper(models.Model): diff --git a/yaksh/templates/yaksh/courses.html b/yaksh/templates/yaksh/courses.html index ecd3a13..b7cc2e1 100644 --- a/yaksh/templates/yaksh/courses.html +++ b/yaksh/templates/yaksh/courses.html @@ -79,6 +79,7 @@ </div> <br/> <button class="btn btn-primary pull-right"type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/addquiz/{{course.id}}/");'>Add New Quiz</button> + <p><a href="{{URL_ROOT}}/exam/manage/courses/download_course_csv/{{course.id}}">Download CSV</a></p> </div> </div> <br><br> diff --git a/yaksh/urls.py b/yaksh/urls.py index bdf14ea..629f8f5 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -113,4 +113,6 @@ urlpatterns += [ views.regrade, name='regrade'), url(r'^manage/(?P<mode>[\w\-]+)/(?P<quiz_id>\d+)/$', views.test_quiz), url(r'^manage/create_demo_course/$', views.create_demo_course), + url(r'^manage/courses/download_course_csv/(?P<course_id>\d+)/$', + views.download_course_csv), ] diff --git a/yaksh/views.py b/yaksh/views.py index 4be2fc1..41539b4 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -1001,7 +1001,6 @@ def download_csv(request, questionpaper_id): writer.writerow(row) return response - @login_required def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): """Present an interface with which we can easily grade a user's papers @@ -1282,3 +1281,33 @@ def regrade(request, course_id, question_id=None, answerpaper_id=None, questionp answerpaper = get_object_or_404(AnswerPaper, pk=answerpaper_id) details.append(answerpaper.regrade(question_id)) return grader(request, extra_context={'details': details}) + +@login_required +def download_course_csv(request, course_id): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + course = get_object_or_404(Course,pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('The question paper does not belong to your course') + students = course.get_only_students().values("id", "first_name", "last_name") + quizzes = Quiz.objects.filter(course=course) + for student in students: + total_course_marks = 0.0 + for quiz in quizzes: + quiz_best_marks = AnswerPaper.objects.get_user_best_of_attempts_marks\ + (quiz, student["id"]) + total_course_marks += quiz_best_marks + student["{}".format(quiz.description)] = quiz_best_marks + student["total"] = total_course_marks + + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{0}.csv"'.format( + (course.name).lower().replace('.', '')) + header = ['first_name', 'last_name']+[quiz.description for quiz in quizzes]\ + + ['total'] + writer = csv.DictWriter(response,fieldnames=header, extrasaction='ignore') + writer.writeheader() + for student in students: + writer.writerow(student) + return response |