From 7eb83ca87aee850ed6f9730eadd5c2177dff160c Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:57:16 -0500 Subject: [PATCH 1/9] Update views.py --- onlinecourse/views.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/onlinecourse/views.py b/onlinecourse/views.py index 6567363ea..f5a74889c 100644 --- a/onlinecourse/views.py +++ b/onlinecourse/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render from django.http import HttpResponseRedirect # 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 @@ -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,) + )) + # A example method to collect the selected choices from the exam form from the request object @@ -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 # 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: @@ -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) } + ) From 98d519d8cbfe168accc5e424960bf5729ab74ada Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:57:43 -0500 Subject: [PATCH 2/9] Update urls.py --- onlinecourse/urls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinecourse/urls.py b/onlinecourse/urls.py index 2960e1045..026d13127 100644 --- a/onlinecourse/urls.py +++ b/onlinecourse/urls.py @@ -18,7 +18,7 @@ path('/enroll/', views.enroll, name='enroll'), # Create a route for submit view - + path('/submit/', views.submit, name='submit'), # Create a route for show_exam_result view - + path('course//submission//result/', views.show_exam_result, name='show_exam_result'), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) From 348277be1e3da71661dfc269f51191ddd71e2ed7 Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:58:28 -0500 Subject: [PATCH 3/9] Update models.py --- onlinecourse/models.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/onlinecourse/models.py b/onlinecourse/models.py index e5b540b16..0874e6c4b 100644 --- a/onlinecourse/models.py +++ b/onlinecourse/models.py @@ -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 # Create a Choice Model with: # Used to persist choice content for a question @@ -123,6 +136,10 @@ 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) # The submission model # One enrollment could have multiple submission @@ -130,5 +147,8 @@ class Enrollment(models.Model): # 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 \ No newline at end of file +# 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) From 624ce31919ddbec171821a23b6c24d7564bf492a Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:58:50 -0500 Subject: [PATCH 4/9] Update admin.py --- onlinecourse/admin.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/onlinecourse/admin.py b/onlinecourse/admin.py index ffd8a631d..9f5abac9e 100644 --- a/onlinecourse/admin.py +++ b/onlinecourse/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin # Import any new Models here -from .models import Course, Lesson, Instructor, Learner +from .models import Course, Lesson, Instructor, Learner, Question, Choice # Register QuestionInline and ChoiceInline classes here @@ -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): @@ -21,6 +29,9 @@ class CourseAdmin(admin.ModelAdmin): class LessonAdmin(admin.ModelAdmin): list_display = ['title'] +class QuestionAdmin(admin.ModelAdmin): + inlines = [ ChoiceInline] + # Register Question and Choice models here @@ -28,3 +39,4 @@ class LessonAdmin(admin.ModelAdmin): admin.site.register(Lesson, LessonAdmin) admin.site.register(Instructor) admin.site.register(Learner) +admin.site.register(Question, QuestionAdmin) From f8d2d9fb15ebb9b75ead882342972497c4885cca Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:11:52 -0500 Subject: [PATCH 5/9] Update course_detail_bootstrap.html --- .../onlinecourse/course_detail_bootstrap.html | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html b/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html index 7a22e1694..8dada80a0 100644 --- a/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html @@ -28,7 +28,7 @@ {% csrf_token %}
- + Sign Up
@@ -62,7 +62,37 @@

{{ course.name }}

--> + +
+ {% if user.is_authenticated %} +
+ {% for question in course.question_set.all %} +
+
+
{{question.title}}
+
+ {% csrf_token %} + {{question.text}}
+
+ {% for choice in question.choice_set.all %} +
+ +
+ {% endfor %} +
+
+ {% endfor %} + +
+ {% endif %} +
@@ -99,4 +129,4 @@

{{ course.name }}

https://www.w3schools.com/bootstrap4/bootstrap_forms_inputs.asp--> - \ No newline at end of file + From d4933e6b004c1d64bae926f97b36fdc5e58113ba Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:12:44 -0500 Subject: [PATCH 6/9] Update exam_result_bootstrap.html --- .../onlinecourse/exam_result_bootstrap.html | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html b/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html index 264728f5c..dfa4971dc 100644 --- a/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html @@ -24,7 +24,7 @@ {% csrf_token %}
- + Sign Up
@@ -39,17 +39,40 @@ {% if grade > 80 %}
+ Congratulations, {{user.name}} ! You have passed the exam and completed + the course with score {{mark}} /{{total_mark}}
{% else %}
+ Failed ! Sorry, {{user.name}} ! You have failed with score {{mark}} /{{total_mark}}
Re-test {% endif %}
Exam results
+ {% for question in course.question_set.all %} +
+
+
{{question.text}}
+
+
+ {% for choice in question.choice_set.all %} + {% if choice.is_correct and choice in choices %} +

Correct answer: {{choice.text}}

+ {% elif choice.is_correct and choice not in choices %} +

Not selected: {{choice.text}}

+ {% elif choice.is_correct == False and choice in choices %} +

Wrong answer: {{choice.text}}

+ {% else %} +

{{choice.text}}

+ {% endif %} + {% endfor %} +
+
+ {% endfor %}
- \ No newline at end of file + From 85a1e48aa35f41febeb9b7fdebf44e41837994c4 Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:13:08 -0500 Subject: [PATCH 7/9] Update course_list_bootstrap.html --- .../templates/onlinecourse/course_list_bootstrap.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinecourse/templates/onlinecourse/course_list_bootstrap.html b/onlinecourse/templates/onlinecourse/course_list_bootstrap.html index 34793b319..6f5b870b6 100644 --- a/onlinecourse/templates/onlinecourse/course_list_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/course_list_bootstrap.html @@ -25,7 +25,7 @@ {% csrf_token %}
- + Sign Up
@@ -62,4 +62,4 @@
{{ course.name }},

No courses are available.

{% endif %} - \ No newline at end of file + From 9ee71a3dfb5f54151009eedbd9223cf16d4d77bd Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:13:57 -0500 Subject: [PATCH 8/9] Update user_login_bootstrap.html --- onlinecourse/templates/onlinecourse/user_login_bootstrap.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinecourse/templates/onlinecourse/user_login_bootstrap.html b/onlinecourse/templates/onlinecourse/user_login_bootstrap.html index c58611c57..3df099c27 100644 --- a/onlinecourse/templates/onlinecourse/user_login_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/user_login_bootstrap.html @@ -25,7 +25,7 @@ {% csrf_token %}
- + Sign Up
@@ -56,4 +56,4 @@

Login

- \ No newline at end of file + From 191bfad2210ceec41c3926d016085bf9056c0f03 Mon Sep 17 00:00:00 2001 From: flo289 <125163793+flo289@users.noreply.github.com> Date: Mon, 13 Feb 2023 15:14:16 -0500 Subject: [PATCH 9/9] Update user_registration_bootstrap.html --- .../templates/onlinecourse/user_registration_bootstrap.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onlinecourse/templates/onlinecourse/user_registration_bootstrap.html b/onlinecourse/templates/onlinecourse/user_registration_bootstrap.html index 14dc39584..0c43389db 100644 --- a/onlinecourse/templates/onlinecourse/user_registration_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/user_registration_bootstrap.html @@ -25,7 +25,7 @@ {% csrf_token %}
- + Sign Up
@@ -61,4 +61,4 @@

Sign Up

- \ No newline at end of file +