Commit 8c7ae3b6 authored by Samarth Joshi's avatar Samarth Joshi

Adding timer functionality

parent 2b739610
No preview for this file type
......@@ -14,6 +14,12 @@ class quiz(models.Model):
quizInfo=models.CharField(max_length=100,default="No Info Available")
quizInstructor=models.CharField(max_length=100,default="No Name")
@property
def end_datetime( self ):
starttime = datetime.datetime.combine( self.date, self.startTime if self.startTime is not None else datetime.time.min )
endtime = starttime + datetime.timedelta(minutes=self.length)
return endtime
def __str__(self):
return str(self.quizId)
......
......@@ -112,7 +112,7 @@
.marksarea {
text-align: right;
}
#blankspace {
#timer {
font-size: 2em;
}
......
......@@ -89,10 +89,14 @@ function set_option( question_id, option_id ) {
}
}
function submit_form() {
function submit_form(force) {
var main_form = document.getElementById('main_form');
var x = confirm("Are you sure you want to submit?");
if(x) {
if(force==0) {
var x = confirm("Are you sure you want to submit?");
if(x) {
main_form.submit();
}
} else {
main_form.submit();
}
}
}
......@@ -5,7 +5,6 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,300;0,400;0,700;1,400&display=swap" rel="stylesheet">
<link href="{% static 'css/quiz_attempt_page.css' %}" rel="stylesheet">
</head>
<body>
<form id="main_form" method="POST" action="/result/">{% csrf_token %}
......@@ -23,7 +22,7 @@
</ul>
<section id="toolbar">
<div class="button_group">
<button class="quiz_btn submit" type="button" onclick="submit_form()"><strong>Submit</strong></button>
<button class="quiz_btn submit" type="button" onclick="submit_form(0)"><strong>Submit</strong></button>
</div>
<div class="button_group">
<button class="quiz_btn prev_ques" type="button" onclick="prev_question()"><strong>Previous</strong></button>
......@@ -39,9 +38,9 @@
<div id="questions_area_wrapper">
{% for i in questions%}
<div class="question">
<div class="rubrics_header">
<div class="rubrics_header question_header">
<p>Question <br/>{{ forloop.counter }} of {{ count }}</p>
<p id="blankspace"></p>
<p id="timer">00:00:00</p>
<p class="marksarea">Marks: {{ i.marks }}<br/>Negative: -{{ i.negative }}</p>
</div>
<!-- <h1 id ='quizId'>{{i.quizId}}<h1> -->
......@@ -68,5 +67,32 @@
<script src="{% static 'js/quiz_attempt_page.js' %}"></script>
<script>
var enddate = new Date({{ endtime|safe }});
var endtime = enddate.getTime();
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = endtime - now;
// Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("timer").innerHTML = hours + ":" + minutes + ":" + seconds;
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "EXPIRED";
submit_form(1);
}
}, 1000);
</script>
</body>
</html>
......@@ -27,6 +27,7 @@ from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.db.models import Count
import matplotlib.pyplot as plt, mpld3
import json
lst = []
answers = []
......@@ -36,21 +37,13 @@ negative=[]
def index(request):
obj = Questions.objects.get_queryset().order_by('questionId')
count = Questions.objects.all().count()
quizInstance = quiz.objects.get(pk=0)
obj = Questions.objects.filter(quizId=quizInstance).all().order_by('questionId')
endtime = quizInstance.end_datetime
endtime = json.dumps(endtime.isoformat())
count = len(obj)
print(obj)
""" paginator = Paginator(obj,1)
try:
page = int(request.GET.get('page','1'))
except:
page =1
try:
questions = paginator.page(page)
except Exception as e:
questions=paginator.page(paginator.num_pages) """
return render(request,'index.html',{'questions':obj,'count':count})
#quiz started #quiz completed.
return render(request,'index.html',{'questions':obj,'count':count, 'endtime': endtime})
@login_required
def result(request):
......@@ -76,30 +69,6 @@ def result(request):
return redirect('/view_submission/?q='+str(quizId))
else:
return HttpResponse("You have already submitted this quiz")
""" answer=list() #contains user responses
ans=list()
for req in 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=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
"""
......
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