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): ...@@ -14,6 +14,12 @@ class quiz(models.Model):
quizInfo=models.CharField(max_length=100,default="No Info Available") quizInfo=models.CharField(max_length=100,default="No Info Available")
quizInstructor=models.CharField(max_length=100,default="No Name") 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): def __str__(self):
return str(self.quizId) return str(self.quizId)
......
...@@ -112,7 +112,7 @@ ...@@ -112,7 +112,7 @@
.marksarea { .marksarea {
text-align: right; text-align: right;
} }
#blankspace { #timer {
font-size: 2em; font-size: 2em;
} }
......
...@@ -89,10 +89,14 @@ function set_option( question_id, option_id ) { ...@@ -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 main_form = document.getElementById('main_form');
if(force==0) {
var x = confirm("Are you sure you want to submit?"); var x = confirm("Are you sure you want to submit?");
if(x) { if(x) {
main_form.submit(); main_form.submit();
} }
} else {
main_form.submit();
}
} }
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <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="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"> <link href="{% static 'css/quiz_attempt_page.css' %}" rel="stylesheet">
</head> </head>
<body> <body>
<form id="main_form" method="POST" action="/result/">{% csrf_token %} <form id="main_form" method="POST" action="/result/">{% csrf_token %}
...@@ -23,7 +22,7 @@ ...@@ -23,7 +22,7 @@
</ul> </ul>
<section id="toolbar"> <section id="toolbar">
<div class="button_group"> <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>
<div class="button_group"> <div class="button_group">
<button class="quiz_btn prev_ques" type="button" onclick="prev_question()"><strong>Previous</strong></button> <button class="quiz_btn prev_ques" type="button" onclick="prev_question()"><strong>Previous</strong></button>
...@@ -39,9 +38,9 @@ ...@@ -39,9 +38,9 @@
<div id="questions_area_wrapper"> <div id="questions_area_wrapper">
{% for i in questions%} {% for i in questions%}
<div class="question"> <div class="question">
<div class="rubrics_header"> <div class="rubrics_header question_header">
<p>Question <br/>{{ forloop.counter }} of {{ count }}</p> <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> <p class="marksarea">Marks: {{ i.marks }}<br/>Negative: -{{ i.negative }}</p>
</div> </div>
<!-- <h1 id ='quizId'>{{i.quizId}}<h1> --> <!-- <h1 id ='quizId'>{{i.quizId}}<h1> -->
...@@ -68,5 +67,32 @@ ...@@ -68,5 +67,32 @@
<script src="{% static 'js/quiz_attempt_page.js' %}"></script> <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> </body>
</html> </html>
...@@ -27,6 +27,7 @@ from django.contrib.auth import login ...@@ -27,6 +27,7 @@ from django.contrib.auth import login
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.db.models import Count from django.db.models import Count
import matplotlib.pyplot as plt, mpld3 import matplotlib.pyplot as plt, mpld3
import json
lst = [] lst = []
answers = [] answers = []
...@@ -36,21 +37,13 @@ negative=[] ...@@ -36,21 +37,13 @@ negative=[]
def index(request): def index(request):
obj = Questions.objects.get_queryset().order_by('questionId') quizInstance = quiz.objects.get(pk=0)
count = Questions.objects.all().count() obj = Questions.objects.filter(quizId=quizInstance).all().order_by('questionId')
endtime = quizInstance.end_datetime
endtime = json.dumps(endtime.isoformat())
count = len(obj)
print(obj) print(obj)
""" paginator = Paginator(obj,1) return render(request,'index.html',{'questions':obj,'count':count, 'endtime': endtime})
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.
@login_required @login_required
def result(request): def result(request):
...@@ -77,30 +70,6 @@ def result(request): ...@@ -77,30 +70,6 @@ def result(request):
else: else:
return HttpResponse("You have already submitted this quiz") 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