Commit 25fb3df7 authored by Samarth Joshi's avatar Samarth Joshi

Adding submission view

parent f01dab74
......@@ -27,6 +27,8 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('',a.student,name="student"),
path('quiz/',a.index),
path('submissions/',a.submissions),
path('view_submission/',a.view_sub),
path('save_ans/',a.save_ans,name="saveans"),
path('save_cribs/',a.save_cribs,name="savecribs"),
path('result/',a.result,name="result"),
......
No preview for this file type
File added
# Generated by Django 3.1.2 on 2020-10-24 14:00
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('quiz', '0004_auto_20201024_1020'),
]
operations = [
migrations.AlterField(
model_name='submission',
name='studentId',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
from django.db import models
class quiz(models.Model):
quizId=models.AutoField(default=0, primary_key=True) #primary key
startTime=models.DateTimeField()
......@@ -11,6 +10,9 @@ class quiz(models.Model):
quizInfo=models.CharField(max_length=100,default="No Info Available")
quizInstructor=models.CharField(max_length=100,default="No Name")
def __str__(self):
return str(self.quizId)
class Questions(models.Model):
question = models.TextField()
option1 = models.CharField(max_length=100)
......@@ -25,11 +27,14 @@ class Questions(models.Model):
questionId=models.AutoField(primary_key=True,)
quizId=models.ForeignKey(quiz,to_field='quizId',on_delete=models.CASCADE)#unique quiz identification
def __str__(self):
return str(self.questionId)
class submission(models.Model):
questionId=models.ForeignKey(Questions,to_field='questionId',on_delete=models.CASCADE)
option=models.CharField(max_length=100)
studentId=models.CharField(max_length=100)
studentId=models.ForeignKey(to=User, on_delete=models.CASCADE)
quizId=models.ForeignKey(quiz,to_field='quizId',on_delete=models.CASCADE)#unique quiz identification
class cribs(models.Model):
......
......@@ -40,12 +40,12 @@
<!-- <h1 id ='quizId'>{{i.quizId}}<h1> -->
<h1 id ='questionId' hidden>{{i.questionId}} </h1>
<h2 class="actual_question">{{i.question}}</h2>
<input type="hidden" id="qq{{forloop.counter0}}" name="qq{{forloop.counter0}}" value="{{i.questionId}}">
<!--<input type="hidden" id="qq{{forloop.counter0}}" name="qq{{forloop.counter0}}" value="{{i.questionId}}">-->
<label class="op_box" onclick="set_option({{forloop.counter0}},0)"><span id="lbl_{{forloop.counter0}}_0"><strong>A</strong><span>{{i.option1}}</span></span></label>
<label class="op_box" onclick="set_option({{forloop.counter0}},1)"><span id="lbl_{{forloop.counter0}}_1"><strong>B</strong><span>{{i.option2}}</span></span></label>
<label class="op_box" onclick="set_option({{forloop.counter0}},2)"><span id="lbl_{{forloop.counter0}}_2"><strong>C</strong><span>{{i.option3}}</span></span></label>
<label class="op_box" onclick="set_option({{forloop.counter0}},3)"><span id="lbl_{{forloop.counter0}}_3"><strong>D</strong><span>{{i.option4}}</span></span></label>
<input type="hidden" id="q{{forloop.counter0}}" name="{{i.questionId}}" value="NA">
<input type="hidden" id="q{{forloop.counter0}}" name="question_{{i}}" value="NA">
<!-- <label ><input type="textarea" name="fame" id="cribs" value="">Cribs...</label> -->
......
......@@ -6,6 +6,7 @@
</head>
<body>
<center><h1><a>Welcome Student{{user}}</a></h1>
<br><h1><a href="/quiz/">Start Quiz</a></h1></center>
<br><h1><a href="/quiz/">Start Quiz</a></h1>
<br><h1><a href="/submissions/">View Previous Quizzes</a></h1></center>
</body>
</html>
<!DOCTYPE html>
<head>
{% load static %}
</head>
<body>
{% for submission in submissions %}
<a href="../view_submission?q={{ submission.quizId }}">{{ submission.quizId }}</a>
{%endfor%}
</body>
</html>
<!DOCTYPE html>
<head>
{% load static %}
</head>
<body>
{% for sub in submissions %}
test {{ sub.questionId.question }}
{%endfor%}
</body>
</html>
from django.shortcuts import render
from django.http import HttpResponse
from . models import Questions
from . models import quiz
from django.core.paginator import Paginator
from .forms import UploadFileForm
from django.http import HttpResponseRedirect
......@@ -13,6 +14,7 @@ from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.db.models import Count
lst = []
......@@ -25,6 +27,7 @@ negative=[]
def index(request):
obj = Questions.objects.get_queryset().order_by('questionId')
count = Questions.objects.all().count()
print(obj)
""" paginator = Paginator(obj,1)
try:
page = int(request.GET.get('page','1'))
......@@ -38,35 +41,46 @@ def index(request):
return render(request,'index.html',{'questions':obj,'count':count})
#quiz started #quiz completed.
@login_required
def result(request):
answer=list() #contains user responses
if request.user.is_authenticated and request.method =="POST":
quizId=int(request.POST['quizId'])
quizInstance = quiz.objects.get(pk=quizId)
prev_subm = submission.objects.all().filter(quizId=quizInstance, studentId=request.user)
if len(prev_subm)==0:
for questionno in request.POST:
if questionno.startswith('question_'):
questionId = int(questionno[9:])
questionInstance = Questions.objects.get(pk=questionId)
t=submission(questionId=questionInstance, option=request.POST[questionno], studentId=request.user, quizId=quizInstance)
t.save()
return HttpResponse("Successfully Submitted")
else:
return HttpResponse("You have already submitted this quiz")
""" answer=list() #contains user responses
ans=list()
for req in request.POST:
ans.append(req)
if(request.method =="POST"):
quizId=request.POST['quizId']
i=len(request.POST)
ans.append(req) """
""" i=len(request.POST)
j=2
score=0
while j<=i-2:
q=Questions.objects.all().filter(quizId=quizId,questionId=request.POST[ans[j-1]]).values()#fetch the given quiz answers marks and negative marking
print(request.POST[ans[j-1]])
if(len(q)>=1):
if(request.POST[ans[j]]==q[0]['answer']):
score+=q[0]['marks']
elif(request.POST[ans[j]]!='NA'):
score-=q[0]['negative']
print("Score: " +str(score))
answer.append(request.POST[ans[j]])
t=submission(questionId=request.POST[ans[j-1]],option=request.POST[ans[j]],studentId=6969,quizId=quizId)#studentid hardcoded
t=submission(questionId=Questions.objects.get(pk=int(request.POST[ans[j]])),option=request.POST[request.POST[ans[j]]],studentId=6969,quizId=quiz.objects.get(pk=int(quizId)))#studentid hardcoded
t.save()
else:
print(str(request.POST[ans[j-1]]))
j+=2
return HttpResponse(str(score))
"""
......@@ -120,7 +134,7 @@ def handle_uploaded_file(f):
rows=readCSV(destination)
for row in rows:
q=Questions(question=row[0],option1=row[1],option2=row[2],option3=row[3],option4=row[4],answer=row[5],type=row[6],marks=int(row[7]),negative=float(row[8]),explainations=row[9],quizCode=row[10],quizId='0')#hardcoded quizid
q=Questions(question=row[0],option1=row[1],option2=row[2],option3=row[3],option4=row[4],answer=row[5],type=row[6],marks=int(row[7]),negative=float(row[8]),explainations=row[9],quizCode=row[10])#hardcoded quizid
q.save()
......@@ -140,5 +154,20 @@ def sign_up(request):
@login_required
def submissions(request):
#prev_subm = submission.objects.all().filter(studentId=request.user)
prev_subm = submission.objects.all().filter(studentId=request.user).values('quizId').annotate(dcount=Count('quizId'))
return render(request,'submissions.html', { 'submissions' : prev_subm })
@login_required
def view_sub(request):
if request.method == "GET":
quizId = int(request.GET.get('q'))
quizInstance = quiz.objects.get(pk=quizId)
subs = submission.objects.filter(quizId=quizInstance, studentId=request.user).select_related().all()
for sub in subs:
print(sub.questionId.question)
return render(request,'view_submissions.html', { 'submissions' : subs })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment