From aab320d8867a15569ddd0da029f13ea5740f6518 Mon Sep 17 00:00:00 2001
From: prathamesh
Date: Wed, 16 Mar 2016 12:05:27 +0530
Subject: A moderator can download csv file for a given question paper.
csv includes the latest attempt for the quiz/questionpaper.
It contains user details and marks for the quiz.
---
yaksh/models.py | 17 ++++++++++++++
yaksh/templates/yaksh/monitor.html | 1 +
yaksh/urls.py | 2 ++
yaksh/views.py | 45 ++++++++++++++++++++++++++++++++++++++
4 files changed, 65 insertions(+)
(limited to 'yaksh')
diff --git a/yaksh/models.py b/yaksh/models.py
index 1bbd035..7bd0a9e 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -414,6 +414,23 @@ class AnswerPaperManager(models.Manager):
question_stats[question] = [0, questions[question.id]]
return question_stats
+ def get_answerpapers_for_quiz(self, questionpaper_id):
+ return self.filter(question_paper_id=questionpaper_id)
+
+ def _get_answerpapers_users(self, answerpapers):
+ return answerpapers.values_list('user', flat=True).distinct()
+
+ def get_latest_attempts(self, questionpaper_id):
+ papers = self.get_answerpapers_for_quiz(questionpaper_id)
+ users = self._get_answerpapers_users(papers)
+ latest_attempts = []
+ for user in users:
+ latest_attempts.append(self._get_latest_attempt(papers, user))
+ return latest_attempts
+
+ def _get_latest_attempt(self, answerpapers, user_id):
+ return answerpapers.filter(user_id=user_id).order_by('-attempt_number')[0]
+
###############################################################################
class AnswerPaper(models.Model):
diff --git a/yaksh/templates/yaksh/monitor.html b/yaksh/templates/yaksh/monitor.html
index cb14ba5..b81c9f2 100644
--- a/yaksh/templates/yaksh/monitor.html
+++ b/yaksh/templates/yaksh/monitor.html
@@ -40,6 +40,7 @@
{% if papers %}
Number of papers: {{ papers|length }}
Question Statisitics
+Download CSV
Name |
diff --git a/yaksh/urls.py b/yaksh/urls.py
index d74e244..be33051 100644
--- a/yaksh/urls.py
+++ b/yaksh/urls.py
@@ -65,6 +65,8 @@ urlpatterns += patterns('yaksh.views',
'show_statistics'),
url(r'^manage/statistics/question/(?P\d+)/(?P\d+)/$',
'show_statistics'),
+ url(r'^manage/monitor/download_csv/(?P\d+)/$',
+ 'download_csv'),
url(r'manage/courses/$', 'courses'),
url(r'manage/add_course/$', 'add_course'),
url(r'manage/course_detail/(?P\d+)/$', 'course_detail'),
diff --git a/yaksh/views.py b/yaksh/views.py
index 9cd6fdb..bdb86b9 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -5,6 +5,7 @@ import stat
from os.path import dirname, pardir, abspath, join, exists
import datetime
import collections
+import csv
from django.http import HttpResponse
from django.contrib.auth import login, logout, authenticate
from django.shortcuts import render_to_response, get_object_or_404, redirect
@@ -1551,6 +1552,50 @@ def user_data(request, username, questionpaper_id=None):
context_instance=RequestContext(request))
+@login_required
+def download_csv(request, questionpaper_id):
+ user = request.user
+ if not is_moderator(user):
+ raise Http404('You are not allowed to view this page!')
+ quiz = Quiz.objects.get(questionpaper=questionpaper_id)
+ if quiz.course.creator != user:
+ raise Http404('The question paper does not belong to your course')
+ papers = AnswerPaper.objects.get_latest_attempts(questionpaper_id)
+ if not papers:
+ return monitor(request, questionpaper_id)
+ response = HttpResponse(content_type='text/csv')
+ response['Content-Disposition'] = 'attachment; filename="{0}.csv"'.format(
+ (quiz.description).replace('.', ''))
+ writer = csv.writer(response)
+ header = [
+ 'name',
+ 'username',
+ 'roll_number',
+ 'institute',
+ 'marks_obtained',
+ 'total_marks',
+ 'percentage',
+ 'questions',
+ 'questions_answererd',
+ 'status'
+ ]
+ writer.writerow(header)
+ for paper in papers:
+ row = [
+ '{0} {1}'.format(paper.user.first_name, paper.user.last_name),
+ paper.user.username,
+ paper.user.profile.roll_number,
+ paper.user.profile.institute,
+ paper.marks_obtained,
+ paper.question_paper.total_marks,
+ paper.percent,
+ paper.questions, paper.questions_answered,
+ paper.status
+ ]
+ writer.writerow(row)
+ return response
+
+
@login_required
def grade_user(request, username, questionpaper_id=None):
"""Present an interface with which we can easily grade a user's papers
--
cgit