From fae780add6e0653d72e4e2c7dc25fdf1fd1486e5 Mon Sep 17 00:00:00 2001 From: ayishanishana21 Date: Thu, 5 Feb 2026 15:27:38 +0530 Subject: [PATCH] added download option in participant --- events/urls.py | 9 +++++ events/views.py | 34 +++++++++++++++++++ .../templates/test/test_participant.html | 6 ++++ 3 files changed, 49 insertions(+) diff --git a/events/urls.py b/events/urls.py index 2f6f85f40..900005797 100644 --- a/events/urls.py +++ b/events/urls.py @@ -78,6 +78,15 @@ url(r'^training/view/training-completion/(?P\d+)/$', view_training_completion, name="view_training_completion"), #url(r'^test/subscribe/(\d+)/(\d+)/$', test_student_subscribe', name='test_student_subscribe'), + + + url( + r'^test/(?P\d+)/participants/download/$', + download_test_participants_csv, + name='download_test_participants_csv' +), + + url(r'^test/(\d+)/participant/$', test_participant, name='test_participant'), url(r'^test/participant/certificate/(\d+)/(\d+)/$', test_participant_ceritificate, name='test_participant_ceritificate'), url(r'^test/participant/certificate/all/(\d+)/$', test_participant_ceritificate_all, name='test_participant_ceritificate_all'), diff --git a/events/views.py b/events/views.py index c509dbd04..392c62d37 100644 --- a/events/views.py +++ b/events/views.py @@ -2307,6 +2307,40 @@ def test_participant(request, tid=None): # can_download_certificate = 1 context = {'collection' : test_mdlusers, 'test' : t, 'can_download_certificate':can_download_certificate} return render(request, 'events/templates/test/test_participant.html', context) + +@login_required +def download_test_participants_csv(request, test_id): + user = request.user + try: + test = Test.objects.get(id=test_id) + except Test.DoesNotExist: + raise PermissionDenied() + if not (user == test.organiser.user or user == test.invigilator.user or user.groups.filter( name__in=["Event Manager", "Resource Person"] ).exists()): + raise PermissionDenied() + + if test.status == 4: + participants = TestAttendance.objects.filter(test_id=test.id,status__gte=2) + else: + participants = TestAttendance.objects.filter(test_id=test.id) + participants = participants.order_by('id') + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = ('attachment; filename="{}_participants.csv"'.format(test.foss.foss)) + + writer = csv.writer(response) + writer.writerow(['First Name','Last Name','Email ID','Score (%)']) + + for ta in participants: + firstname = ta.mdluser_firstname or '' + lastname = ta.mdluser_lastname or '' + mdluser = MdlUser.objects.filter(id=ta.mdluser_id).first() + email = mdluser.email if mdluser and mdluser.email else '' + grade = MdlQuizGrades.objects.filter(quiz=ta.mdlquiz_id,userid=ta.mdluser_id).first() + score = round(grade.grade, 2) if grade else 'NA' + writer.writerow([firstname,lastname,email,score]) + + return response + + def test_participant_ceritificate(request, wid, participant_id): #response = HttpResponse(content_type='application/pdf') diff --git a/static/events/templates/test/test_participant.html b/static/events/templates/test/test_participant.html index 8c72cfea0..6de28e771 100644 --- a/static/events/templates/test/test_participant.html +++ b/static/events/templates/test/test_participant.html @@ -38,6 +38,12 @@