Skip to content

edited Question #19

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 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
56a7503
Update views.py
BehroozMoniri Jul 29, 2022
8cc29a9
Add Question, Choice, Submission
BehroozMoniri Jul 29, 2022
1d7c851
Edited Choice
BehroozMoniri Jul 29, 2022
3233068
registered question & choice
BehroozMoniri Jul 29, 2022
5a0d578
edited Question
BehroozMoniri Jul 29, 2022
4dd0c0d
changed the form
BehroozMoniri Jul 29, 2022
94de0c7
Update models.py
BehroozMoniri Jul 30, 2022
ef4e38a
Update views.py
BehroozMoniri Jul 30, 2022
90e36db
Update views.py
BehroozMoniri Jul 30, 2022
a8cd58c
Update urls.py
BehroozMoniri Jul 30, 2022
2b09434
Update urls.py
BehroozMoniri Jul 30, 2022
c610af5
corrected spelling error
BehroozMoniri Jul 30, 2022
19f9a2b
corrected spelling error
BehroozMoniri Jul 30, 2022
8909a9c
Update admin.py
BehroozMoniri Jul 30, 2022
3794705
changed 20.0.4 to gunicorn==20.1.0
BehroozMoniri Jul 30, 2022
05629a1
Update urls.py
BehroozMoniri Jul 30, 2022
26ec4ab
Update views.py
BehroozMoniri Jul 30, 2022
5734cd2
Update views.py
BehroozMoniri Jul 30, 2022
bd06d23
Update urls.py
BehroozMoniri Jul 30, 2022
fcd52fd
Update urls.py
BehroozMoniri Jul 30, 2022
925b280
Update exam_result_bootstrap.html
BehroozMoniri Jul 30, 2022
630b122
Update course_detail_bootstrap.html
BehroozMoniri Jul 30, 2022
5aa67d1
Update urls.py
BehroozMoniri Jul 30, 2022
83e6432
Update urls.py
BehroozMoniri Jul 30, 2022
b43ffa2
Update views.py
BehroozMoniri Jul 31, 2022
afeeee3
Update exam_result_bootstrap.html
BehroozMoniri Jul 31, 2022
13a2837
Update exam_result_bootstrap.html
BehroozMoniri Jul 31, 2022
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
4 changes: 3 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, Submission

# <HINT> Register QuestionInline and ChoiceInline classes here

Expand Down Expand Up @@ -28,3 +28,5 @@ class LessonAdmin(admin.ModelAdmin):
admin.site.register(Lesson, LessonAdmin)
admin.site.register(Instructor)
admin.site.register(Learner)
admin.site.register(Question)
admin.site.register(Choice)
60 changes: 40 additions & 20 deletions onlinecourse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,40 +95,60 @@ class Enrollment(models.Model):
rating = models.FloatField(default=5.0)


# <HINT> Create a Question Model with:
# Used to persist question content for a course
# Has a One-To-Many (or Many-To-Many if you want to reuse questions) relationship with course
# Has a grade point for each question
# Has question content
# Other fields and methods you would like to design
#class Question(models.Model):
class Question(models.Model):
text = models.TextField(max_length=500, default="question is...", null=False)
grade = models.CharField(default="U",, null=True)
lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE)
course = models.ManyToMany(Course , on_delete=models.SET_NULL)
A = 'A'
B ='B'
C ='C'
D= 'D'
Choices = [(A , 'A'), (B, 'B'),(C, 'C'), (D, 'D') ]
correct_ans = models.CharField(choices=Choices, default=A, null=False)
# Foreign key to lesson
# question text
# question grade/mark

# <HINT> A sample model method to calculate if learner get the score of the question
#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
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:
class Choice(models.Model):
# Used to persist choice content for a question
# One-To-Many (or Many-To-Many if you want to reuse choices) relationship with Question
question = models.ForeignKey(Question, on_delete=models.CASCADE)
# Choice content
A = 'A'
B ='B'
C ='C'
D= 'D'
Choices = [(A , 'A'), (B, 'B'),(C, 'C'), (D, 'D') ]
answer = models.CharField(max_length=500, choices=Choices, default=A)
def is_correct(self, answer):
correct_ans = question.correct_ans
if answer == correct_ans:
return True
else:
return False
# 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):


# <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
class Submission(models.Model):
enrollment = models.ForeignKey(Enrollment, on_delete=models.PROTECT)
chocies = models.ManyToManyField(Choice)
user = models.ManyToManyField(Learner)
course = models.ManyToManyField(Course)
#total =
# Other fields and methods you would like to design
32 changes: 20 additions & 12 deletions onlinecourse/templates/onlinecourse/course_detail_bootstrap.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,34 @@ <h2>{{ course.name }}</h2>

<!--

A collapse example here:
A collapse example here:

Click to expand elements within the collapse div -->
<div id="exam" class="collapse">
Click to expand elements within the collapse div
</div>

-->

<!-- <HINT> If user is authenticated, show course exam with a list of question -->

<!-- <HINT> Each example will have many questions -->

<!-- <HINT> Each question will have many choices -->

{% if user.is_authenticated %}
<ul>
{% for question in question.all %}

<li>
<p> {{question }}</p>
<ul>
{% for choice in question.choices %}
<li>{{choice }}</p> <br>
<input type="check" name="choice_{{choice.id}}" id="{{choice.id}}" ...></li></ul>
</li>
</ul>


<!-- <HINT> Create a form to collect the selected choices for all questions -->
<!-- <HINT> For each question choice, you may create a checkbox input like
<input type="check" name="choice_{{choice.id}}" id="{{choice.id}}" ...>

-->

<!-- A choice submission form example
<!-- A choice submission form example -->
<form id="questionform" action="point to a submit view" method="post">
... for each question in the course ...
<div class="card mt-1">
Expand All @@ -93,10 +101,10 @@ <h2>{{ course.name }}</h2>
</div>
</div>
<input class="btn btn-success btn-block" type="submit" value="Submit">
</form> -->
</form>

<!--Check here to see more details Bootstrap checkbox
https://www.w3schools.com/bootstrap4/bootstrap_forms_inputs.asp-->
</div>
</body>
</html>
</html>
51 changes: 28 additions & 23 deletions onlinecourse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from django.views import generic
from django.contrib.auth import login, logout, authenticate
import logging
from .models import Submission, Learner, Choice, Question, Enrolement, Lesson, Course
# Get an instance of a logger
logger = logging.getLogger(__name__)
# Create your views here.


def registration_request(request):
context = {}
if request.method == 'GET':
Expand All @@ -38,7 +38,6 @@ def registration_request(request):
context['message'] = "User already exists."
return render(request, 'onlinecourse/user_registration_bootstrap.html', context)


def login_request(request):
context = {}
if request.method == "POST":
Expand Down Expand Up @@ -105,32 +104,38 @@ def enroll(request, course_id):

# <HINT> Create a submit view to create an exam submission record for a course enrollment,
# you may implement it based on following logic:
# Get user and course object, then get the associated enrollment object created when the user enrolled the course
# Create a submission object referring to the enrollment
# Collect the selected choices from exam form
# 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):
course = get_object_or_404(Course, pk=course_id)
user = request.user
Submission.objects.create(user=user, course=course )

# Get user and course object, then get the associated enrollment object
# created when the user enrolled the course
# Create a submission object referring to the enrollment
# Collect the selected choices from exam form
# Add each selected choice object to the submission object
# Redirect to show_exam_result with the submission id



# <HINT> A example method to collect the selected choices from the exam form from the request object
#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
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:
# Get course and submission based on their ids
# Get the selected choice ids from the submission record
# 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)

# Get course and submission based on their ids
# Get the selected choice ids from the submission record
# For each selected choice, check if it is a correct answer or not
# Calculate the total score