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 all 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
15 changes: 10 additions & 5 deletions onlinecourse/admin.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
from django.contrib import admin
# <HINT> Import any new Models here
from .models import Course, Lesson, Instructor, Learner
from .models import Question, Choice, Course, Lesson, Instructor, Learner

# <HINT> Register QuestionInline and ChoiceInline classes here


class LessonInline(admin.StackedInline):
model = Lesson
extra = 5


# Register your models here.
class CourseAdmin(admin.ModelAdmin):
inlines = [LessonInline]
list_display = ('name', 'pub_date')
list_filter = ['pub_date']
search_fields = ['name', 'description']

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

class LessonAdmin(admin.ModelAdmin):
list_display = ['title']

class QuestionAdmin(admin.ModelAdmin):
inlines = [ChoiceInline]
fields = ('question_text', 'grade', 'lesson_id')

# <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)
admin.site.register(Choice)
52 changes: 31 additions & 21 deletions onlinecourse/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Instructor(models.Model):
on_delete=models.CASCADE,
)
full_time = models.BooleanField(default=True)
total_learners = models.IntegerField()
total_learners = models.IntegerField(default=0)

def __str__(self):
return self.user.username
Expand Down Expand Up @@ -57,7 +57,7 @@ class Course(models.Model):
name = models.CharField(null=False, max_length=30, default='online course')
image = models.ImageField(upload_to='course_images/')
description = models.CharField(max_length=1000)
pub_date = models.DateField(null=True)
pub_date = models.DateField(auto_now_add=True)
instructors = models.ManyToManyField(Instructor)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Enrollment')
total_enrollment = models.IntegerField(default=0)
Expand All @@ -73,7 +73,7 @@ class Lesson(models.Model):
title = models.CharField(max_length=200, default="title")
order = models.IntegerField(default=0)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
content = models.TextField()
content = models.TextField(max_length=200, default="Programming...")


# Enrollment model
Expand All @@ -90,8 +90,8 @@ class Enrollment(models.Model):
]
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
date_enrolled = models.DateField(default=now)
mode = models.CharField(max_length=5, choices=COURSE_MODES, default=AUDIT)
date_enrolled = models.DateField(auto_now=True)
mode = models.CharField(max_length=50, choices=COURSE_MODES, default=AUDIT)
rating = models.FloatField(default=5.0)


Expand All @@ -101,34 +101,44 @@ class Enrollment(models.Model):
# 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):
# Foreign key to lesson
lesson_id = models.ForeignKey(Lesson, on_delete=models.CASCADE)
# question text
question_text = models.TextField(max_length=1000 ,default='Question is')
# question grade/mark

grade = models.IntegerField()
# <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
def __str__(self):
return self.question_text

# <HINT> Create a Choice Model with:
# Used to persist choice content for a question
# One-To-Many (or Many-To-Many if you want to reuse choices) relationship with Question
# Choice content
# 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_id = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.TextField(max_length=1000)
is_correct = models.BooleanField(default=False)
def __str__(self):
return self.choice_text
# <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.CASCADE)
chocies = models.ManyToManyField(Choice)
# user = models.ManyToManyField(Learner)
# course = models.ManyToManyField(Course)
#total =
# Other fields and methods you would like to design
103 changes: 57 additions & 46 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 All @@ -41,62 +41,73 @@

<!-- Page content -->
<div class="container-fluid">
<h2>{{ course.name }}</h2>
<div class="card-columns-vertical">
{% for lesson in course.lesson_set.all %}
<div class="card mt-1">
<div class="card-header"><h5>Lesson {{lesson.order|add:1}}: {{lesson.title}}</h5></div>
<div class="card-body">{{lesson.content}}</div>
</div>
{% endfor %}
</div>
<h2>{{ course.name }}</h2>
<div class="card-columns-vertical">
{% for lesson in course.lesson_set.all %}
<div class="card mt-1">
<div class="card-header"><h5>Lesson {{lesson.order|add:1}}: {{lesson.title}}</h5></div>
<div class="card-body">{{lesson.content}}</div>
</div>
{% endfor %}
</div>
<!-- Task: show questions and choices -->
<!-- <HINT> Use Bootstrap Collapse to hide exam first, more details could be found here
https://www.w3schools.com/bootstrap4/bootstrap_collapse.asp-->

<!--

A collapse example here:
<div id="exam" class="collapse">
Click to expand elements within the collapse div
</div>

-->

<br>
<center><button data-toggle="collapse" data-target="#exam">Examination</button></center>
<br>
<div id="exam" class="collapse">
{% if user.is_authenticated %}
<!-- <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 -->


<!-- <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
<form id="questionform" action="point to a submit view" method="post">
... for each question in the course ...
<div class="card mt-1">
<div class="card-header"><h5>{{ question.question_text}}</h5></div>
{% csrf_token %}
<div class="form-group">
... for each choice in the question ...
<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.choice_text }}
</label>
</div>
</div>
<!-- A choice submission form example-->
<form id="questionform" action="submit/" method="post">
<!--... for each question in the course ...-->
{% for lesson in course.lesson_set.all %}
{% for question in lesson.question_set.all %}
<div class="card mt-1">
<div class="card-header"><h5>{{ question.question_text}}</h5></div>
{% csrf_token %}
<div class="form-group" style='padding: 20px;'>
<!--... for each choice in the question ...-->
{% 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.choice_text }}
</label>
</div>
<input class="btn btn-success btn-block" type="submit" value="Submit">
</form> -->

{% endfor %}
</div>
</div>
{% endfor %}
{% endfor %}
<br>
<input class="btn btn-success btn-block" type="submit" value="Submit">
<br><br><br>
</form>
<!--Check here to see more details Bootstrap checkbox
https://www.w3schools.com/bootstrap4/bootstrap_forms_inputs.asp-->
https://www.w3schools.com/bootstrap4/bootstrap_forms_inputs.asp-->
{% else %}
<li>
<form class="form-inline" action="{% url 'onlinecourse:login' %}" method="post">
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<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>
</form>
</li>
{% endif %}
</div>
</div>
</body>
</html>
</html>
100 changes: 97 additions & 3 deletions onlinecourse/templates/onlinecourse/exam_result_bootstrap.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!-- <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -35,21 +35,115 @@
</div>
</nav>

<div class="container-fluid">
<div class="container-fluid" style='padding: 20px;'>
{% if grade > 80 %}
<div class="alert alert-success">
<!--HINT Display passed info -->
<p><b>Congratulations, {{user.first_name}}! </b>You have passed the exam and completed the course with score {{grade}} / 100</p>
</div>
{% else %}
<div class="alert alert-danger">
<!--HINT Display failed info -->
<p><b>Failed. </b>Sorry, {{user.first_name}}! You have failed the exam with score {{grade}} / 100</p>
</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 each question in the course ...-->
{% for lesson in course.lesson_set.all %}
{% for question in lesson.question_set.all %}
<div class="card mt-1">
<div class="card-header"><h5 style="font-weight:bold">{{ question.question_text}}</h5></div>
<div class="form-group">
<!--... for each choice in the question choice_{{choice.id}}...-->
{% for choice in question.choice_set.all %}
<div class="form-check">
{% if choice.id in selected %}
{% if choice.is_correct == True %}
<label style="color:#2ea94a;font-weight:bold">Correct answer: {{choice.choice_text}}</label>
{% else %}
<label style="color:red;font-weight:bold">Wrong answer: {{choice.choice_text}}</label>
{% endif %}
{% else %}
<label>{{choice.choice_text}}</label>
{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
</body>
</html> -->

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% load static %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="{% url 'onlinecourse:index' %}">Home</a>
</div>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li>
<a class="btn btn-link" href="#">{{ user.first_name }}({{ user.username }})</a>
<a class="btn btn-link" href="{% url 'onlinecourse:logout' %}">Logout</a>
</li>
{% else %}
<li>
<form class="form-inline" action="{% url 'onlinecourse:login' %}" method="post">
{% 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" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'onlinecourse:registration' %}">Sign Up</a>
</div>
</form>
</li>
{% endif %}
</ul>
</div>
</nav>

<div class="container-fluid">
{% if grade > 80 %}
<div class="alert alert-success">
<b>Congratulations, {{user.username}}!</b>You have passed the exam and completed the course with score {{grade}} / 100</div>
{% else %}
<div class="alert alert-danger"><b>Failed </b></v>Sorry, {{user.username}}! You have failed exam with score {{grade}} / 100</div>
<a class="btn btn-link text-danger" href="{% url 'onlinecourse:course_details' course.id %}">Re-test</a>
{% endif %}
<div class="card-columns-vertical">
<h5 class="">Exam results</h5>
{% for question in lesson.question_set.all %}
<h5 class = "card-header border rounded-top">{{question.question_content}}</h5>
<div class = "card-body border rounded-bottom align-top mt-1 pt-1">
{% for choice in question.choice_set.all %}
{% if choice.pk in selected_ids and choice.is_correct %}
<div class = "text-success h-25 align-top">Correct Answer: {{choice.choice_text}}</div>
{% elif choice.pk in selected_ids and not choice.is_correct %}
<div class = "text-danger h-25">Incorrect Answer: {{choice.choice_text}}</div>
{% elif choice.pk not in selected_ids and choice.is_correct %}
<div class = "text-warning h-25">Not selected: {{choice.choice_text}}</div>
{% else %}
<p>{{choice.choice_text}}</p>
{% endif %}
{% endfor %}
</div>
{% endfor %}
</div>
</div>
</body>
</html>
</html>
Loading