Skip to content

flo #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

flo #32

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion onlinecourse/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
# <HINT> Import any new Models here
from .models import Course, Lesson, Instructor, Learner
from .models import Course, Lesson, Instructor, Learner, Question, Choice

# <HINT> Register QuestionInline and ChoiceInline classes here

Expand All @@ -9,6 +9,14 @@ class LessonInline(admin.StackedInline):
model = Lesson
extra = 5

class QuestionInline(admin.StackedInline):
model = Question
extra = 5

class ChoiceInline(admin.StackedInline):
model = Choice
extra = 5


# Register your models here.
class CourseAdmin(admin.ModelAdmin):
Expand All @@ -21,10 +29,14 @@ class CourseAdmin(admin.ModelAdmin):
class LessonAdmin(admin.ModelAdmin):
list_display = ['title']

class QuestionAdmin(admin.ModelAdmin):
inlines = [ ChoiceInline]


# <HINT> Register Question and Choice models here

admin.site.register(Course, CourseAdmin)
admin.site.register(Lesson, LessonAdmin)
admin.site.register(Instructor)
admin.site.register(Learner)
admin.site.register(Question, QuestionAdmin)
24 changes: 22 additions & 2 deletions onlinecourse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ class Enrollment(models.Model):
# else:
# return False

class Question(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
title = models.CharField(max_length=550, default="question title")
text = models.CharField(max_length=550, default="question text")
grade = models.FloatField(default=5.0)

def is_get_score(self, selected_ids):
all_answers = self.choice_set.filter(is_correct=True).count()
selected_correct = self.choice_set.filter(is_correct=True, id__in=selected_ids).count()
if all_answers == selected_correct:
return True
else:
return False

# <HINT> Create a Choice Model with:
# Used to persist choice content for a question
Expand All @@ -123,12 +136,19 @@ class Enrollment(models.Model):
# Indicate if this choice of the question is a correct one or not
# Other fields and methods you would like to design
# class Choice(models.Model):
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
text = models.CharField(max_length=550, default="question text")
is_correct = models.BooleanField(default=False)

# <HINT> The submission model
# One enrollment could have multiple submission
# One submission could have multiple choices
# One choice could belong to multiple submissions
#class Submission(models.Model):
# enrollment = models.ForeignKey(Enrollment, on_delete=models.CASCADE)
# choices = models.ManyToManyField(Choice)
# Other fields and methods you would like to design
# chocies = models.ManyToManyField(Choice)
# Other fields and methods you would like to design
class Submission(models.Model):
enrollment = models.ForeignKey(Enrollment, on_delete=models.CASCADE)
choices = models.ManyToManyField(Choice)
34 changes: 32 additions & 2 deletions onlinecourse/templates/onlinecourse/course_detail_bootstrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Username" name="psw" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
Expand Down Expand Up @@ -62,7 +62,37 @@ <h2>{{ course.name }}</h2>
</div>

-->
<button data-toggle="collapse" data-target="#exam"
class="btn btn-info" role="button" style="width:100%">
Start Exam</button>

<div id="exam" class="collapse">
{% if user.is_authenticated %}
<form id="questionform" action="{% url 'onlinecourse:submit' course.id %}" method="post">
{% for question in course.question_set.all %}
<div class="card mt-1">
<div class="card-header">
<h5>{{question.title}}</h5>
</div>
{% csrf_token %}
<b>{{question.text}}</b><br/>
<div class="form-group">
{% for choice in question.choice_set.all %}
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" name="choice_{{choice.id}}"
class="form-check-input" id="{{choice.id}}"
value="{{choice.id}}">{{ choice.text }}
</label>
</div>
{% endfor %}
</div>
</div>
{% endfor %}
<input class="btn btn-success btn-block" type="submit" value="Submit">
</form>
{% endif %}
</div>
<!-- <HINT> If user is authenticated, show course exam with a list of question -->

<!-- <HINT> Each example will have many questions -->
Expand Down Expand Up @@ -99,4 +129,4 @@ <h2>{{ course.name }}</h2>
https://www.w3schools.com/bootstrap4/bootstrap_forms_inputs.asp-->
</div>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Username" name="psw" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
Expand Down Expand Up @@ -62,4 +62,4 @@ <h5 class="card-title">{{ course.name }}, <span class="text-success">
<p>No courses are available.</p>
{% endif %}
</body>
</html>
</html>
27 changes: 25 additions & 2 deletions onlinecourse/templates/onlinecourse/exam_result_bootstrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Username" name="psw" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
Expand All @@ -39,17 +39,40 @@
{% if grade > 80 %}
<div class="alert alert-success">
<!--HINT Display passed info -->
<b> Congratulations, {{user.name}} ! </b> You have passed the exam and completed
the course with score {{mark}} /{{total_mark}}
</div>
{% else %}
<div class="alert alert-danger">
<!--HINT Display failed info -->
<b> Failed ! </b>Sorry, {{user.name}} ! You have failed with score {{mark}} /{{total_mark}}
</div>
<a class="btn btn-link text-danger" href="{% url 'onlinecourse:course_details' course.id %}">Re-test</a>
{% endif %}
<div class="card-columns-vertical mt-1">
<h5 class="">Exam results</h5>
<!--HINT Display exam results-->
{% for question in course.question_set.all %}
<div class="card">
<div class="card-header border-light">
<h5>{{question.text}}</h5>
</div>
<div class="card-body">
{% for choice in question.choice_set.all %}
{% if choice.is_correct and choice in choices %}
<p class="card-text" style="color:green">Correct answer: {{choice.text}}</p>
{% elif choice.is_correct and choice not in choices %}
<p class="card-text" style="color:orange">Not selected: {{choice.text}}</p>
{% elif choice.is_correct == False and choice in choices %}
<p class="card-text" style="color:red">Wrong answer: {{choice.text}}</p>
{% else %}
<p class="card-text" style="color:black">{{choice.text}}</p>
{% endif %}
{% endfor %}
</div>
</div>
{% endfor %}
</div>
</div>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions onlinecourse/templates/onlinecourse/user_login_bootstrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Username" name="psw" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
Expand Down Expand Up @@ -56,4 +56,4 @@ <h1>Login</h1>
</div>
</form>

</body>
</body>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Username" name="psw" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
Expand Down Expand Up @@ -61,4 +61,4 @@ <h1>Sign Up</h1>
</div>
</form>

</body>
</body>
4 changes: 2 additions & 2 deletions onlinecourse/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
path('<int:course_id>/enroll/', views.enroll, name='enroll'),

# <HINT> Create a route for submit view

path('<int:course_id>/submit/', views.submit, name='submit'),
# <HINT> Create a route for show_exam_result view

path('course/<int:course_id>/submission/<int:submission_id>/result/', views.show_exam_result, name='show_exam_result'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
43 changes: 40 additions & 3 deletions onlinecourse/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.shortcuts import render
from django.http import HttpResponseRedirect
# <HINT> Import any new Models here
from .models import Course, Enrollment
from .models import Course, Enrollment, Question, Choice, Submission
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse
Expand Down Expand Up @@ -111,6 +111,19 @@ def enroll(request, course_id):
# Add each selected choice object to the submission object
# Redirect to show_exam_result with the submission id
#def submit(request, course_id):
def submit(request, course_id):
user = request.user
course = get_object_or_404(Course, pk=course_id)
enrollment = Enrollment.objects.get(user=user, course=course)
submission = Submission.objects.create(enrollment=enrollment)
answers = extract_answers(request)
submission.choices.set(answers)
submission.save()
return HttpResponseRedirect(reverse(
viewname='onlinecourse:show_exam_result',
args=(course.id, submission.id,)
))



# <HINT> A example method to collect the selected choices from the exam form from the request object
Expand All @@ -122,7 +135,14 @@ def enroll(request, course_id):
# choice_id = int(value)
# submitted_anwsers.append(choice_id)
# return submitted_anwsers

def extract_answers(request):
submitted_anwsers = []
for key in request.POST:
if key.startswith('choice'):
value = request.POST[key]
choice_id = int(value)
submitted_anwsers.append(choice_id)
return submitted_anwsers

# <HINT> Create an exam result view to check if learner passed exam and show their question results and result for each question,
# you may implement it based on the following logic:
Expand All @@ -131,6 +151,23 @@ def enroll(request, course_id):
# For each selected choice, check if it is a correct answer or not
# Calculate the total score
#def show_exam_result(request, course_id, submission_id):

def show_exam_result(request, course_id, submission_id):
course = get_object_or_404(Course, pk=course_id)
submission = get_object_or_404(Submission, pk=submission_id)
choices = submission.choices.all()
total_mark, mark = 0, 0
for question in course.question_set.all():
total_mark += question.grade
if question.is_get_score(choices):
mark += question.grade

return render(
request,
'onlinecourse/exam_result_bootstrap.html',
{"course":course, "choices":choices,"mark":mark,
"total_mark": total_mark,
"submission": submission,
"grade": int((mark / total_mark) * 100) }
)