diff --git a/README.md b/README.md index f8826d30..c81530f7 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,37 @@ +## Online Course — Assessment Feature (My Contribution) -**General Notes** +This repository contains an online course application built with Django. I was responsible for designing and implementing the course assessment feature end-to-end: modeling exam data, rendering questions in the UI, capturing submissions, and evaluating results. -An `onlinecourse` app has already been provided in this repo upon which you will be adding a new assesement feature. +## What I built -- If you want to develop the final project on Theia hosted by [IBM Developer Skills Network](https://labs.cognitiveclass.ai/), you will need to create the same project structure on Theia workspace and save it everytime you close the browser -- Or you could develop the final project locally by setting up your own Python runtime and IDE -- Hints for the final project are left on source code files -- You may choose any cloud platform for deployment (default is IBM Cloud Foundry) -- Depends on your deployment, you may choose any SQL database Django supported such as SQLite3, PostgreSQL, and MySQL (default is SQLite3) +- Data model -**ER Diagram** -For your reference, we have prepared the ER diagram design for the new assesement feature. +- Question with a configurable grade -![Onlinecourse ER Diagram](https://github.com/ibm-developer-skills-network/final-cloud-app-with-database/blob/master/static/media/course_images/onlinecourse_app_er.png) +- Choice with is_correct flag (supports single and multiple correct answers) + +- Submission that stores selected choices per attempt (M2M → Choice) + +- Admin authoring + +- Created/registered admin forms so instructors can build exams: Course → Questions → Choices + +- Views & URLs + +- Course detail view that renders the exam form + +- Submission view that validates and persists selected choices + +- Result view that evaluates the attempt and displays per-question feedback + total score + +- Scoring logic + +## Tech stack + +Python 3.10+ + +Django 4.x/5.x + +SQLite + +Django Templates + Bootstrap for styling diff --git a/onlinecourse/admin.py b/onlinecourse/admin.py index ffd8a631..098ac854 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, Submission, Choice # Register QuestionInline and ChoiceInline classes here @@ -21,6 +21,17 @@ class CourseAdmin(admin.ModelAdmin): class LessonAdmin(admin.ModelAdmin): list_display = ['title'] +class ChoiceInline(admin.StackedInline): + model = Choice + extra = 2 + +class QuestionInline(admin.StackedInline): + model = Question + extra = 2 + +class QuestionAdmin(admin.ModelAdmin): + inlines = [ChoiceInline] + list_display = ['content'] # Register Question and Choice models here @@ -28,3 +39,6 @@ class LessonAdmin(admin.ModelAdmin): admin.site.register(Lesson, LessonAdmin) admin.site.register(Instructor) admin.site.register(Learner) +admin.site.register(Question, QuestionAdmin) +admin.site.register(Choice) +admin.site.register(Submission) diff --git a/onlinecourse/migrations/0001_initial.py b/onlinecourse/migrations/0001_initial.py new file mode 100644 index 00000000..4b1bc81a --- /dev/null +++ b/onlinecourse/migrations/0001_initial.py @@ -0,0 +1,78 @@ +# Generated by Django 4.2.3 on 2025-08-27 21:50 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Course', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(default='online course', max_length=30)), + ('image', models.ImageField(upload_to='course_images/')), + ('description', models.CharField(max_length=1000)), + ('pub_date', models.DateField(null=True)), + ('total_enrollment', models.IntegerField(default=0)), + ], + ), + migrations.CreateModel( + name='Lesson', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(default='title', max_length=200)), + ('order', models.IntegerField(default=0)), + ('content', models.TextField()), + ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onlinecourse.course')), + ], + ), + migrations.CreateModel( + name='Learner', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('occupation', models.CharField(choices=[('student', 'Student'), ('developer', 'Developer'), ('data_scientist', 'Data Scientist'), ('dba', 'Database Admin')], default='student', max_length=20)), + ('social_link', models.URLField()), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Instructor', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('full_time', models.BooleanField(default=True)), + ('total_learners', models.IntegerField()), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='Enrollment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date_enrolled', models.DateField(default=django.utils.timezone.now)), + ('mode', models.CharField(choices=[('audit', 'Audit'), ('honor', 'Honor'), ('BETA', 'BETA')], default='audit', max_length=5)), + ('rating', models.FloatField(default=5.0)), + ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onlinecourse.course')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddField( + model_name='course', + name='instructors', + field=models.ManyToManyField(to='onlinecourse.instructor'), + ), + migrations.AddField( + model_name='course', + name='users', + field=models.ManyToManyField(through='onlinecourse.Enrollment', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/onlinecourse/migrations/0002_choice_submission_question_choice_question.py b/onlinecourse/migrations/0002_choice_submission_question_choice_question.py new file mode 100644 index 00000000..b1521097 --- /dev/null +++ b/onlinecourse/migrations/0002_choice_submission_question_choice_question.py @@ -0,0 +1,44 @@ +# Generated by Django 4.2.3 on 2025-08-27 21:59 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('onlinecourse', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.CharField(max_length=50)), + ('is_correct', models.BooleanField(default=False)), + ], + ), + migrations.CreateModel( + name='Submission', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choices', models.ManyToManyField(to='onlinecourse.choice')), + ('enrollment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onlinecourse.enrollment')), + ], + ), + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.CharField(max_length=100)), + ('grade', models.FloatField(default=50)), + ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onlinecourse.course')), + ], + ), + migrations.AddField( + model_name='choice', + name='question', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='onlinecourse.question'), + ), + ] diff --git a/onlinecourse/models.py b/onlinecourse/models.py index f6fbcbb5..80010842 100644 --- a/onlinecourse/models.py +++ b/onlinecourse/models.py @@ -76,6 +76,26 @@ class Lesson(models.Model): content = models.TextField() +class Question(models.Model): + course = models.ForeignKey(Course, on_delete=models.CASCADE) + content = models.CharField(max_length=100) + grade = models.FloatField(default=50) + + def __str__(self): + return "Question: " + self.content + # method to calculate if the learner gets 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 + +class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + content = models.CharField(max_length=50) + is_correct = models.BooleanField(default=False) # Enrollment model # Once a user enrolled a class, an enrollment entry should be created between the user and course # And we could use the enrollment to track information such as exam submissions @@ -98,6 +118,6 @@ class Enrollment(models.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) +class Submission(models.Model): + enrollment = models.ForeignKey(Enrollment, on_delete=models.CASCADE) + choices = models.ManyToManyField(Choice) diff --git a/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html b/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html index fb48e939..8d5d297d 100644 --- a/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/course_detail_bootstrap.html @@ -51,6 +51,33 @@

{{ course.name }}

{% endfor %} + {% if user.is_authenticated %} +
+ +
+
+ {% for question in course.question_set.all %} +
+
+
{{ question.content }}
+
+ {% csrf_token %} +
+ {% for choice in question.choice_set.all %} +
+ +
+ {% endfor %} +
+
+ {% endfor %} + +
+
+{% endif %} diff --git a/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html b/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html index 264728f5..24917045 100644 --- a/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html +++ b/onlinecourse/templates/onlinecourse/exam_result_bootstrap.html @@ -39,16 +39,37 @@ {% if grade > 80 %}
+ Congratulations, {{ user.first_name }}! You have passed the exam and completed the course with score {{ grade }}/100
{% else %}
+ Failed Sorry, {{ user.first_name }}! You have failed the exam with score {{ grade }}/100
Re-test {% endif %}
Exam results
- + {% for question in course.question_set.all %} +
+
{{ question.content }}
+
+ {% for choice in question.choice_set.all %} +
+ {% if choice.is_correct and choice in choices %} +
Correct answer: {{ choice.content }}
+ {% else %}{% if choice.is_correct and not choice in choices %} +
Not selected: {{ choice.content }}
+ {% else %}{% if not choice.is_correct and choice in choices %} +
Wrong answer: {{ choice.content }}
+ {% else %} +
{{ choice.content }}
+ {% endif %}{% endif %}{% endif %} +
+ {% endfor %} +
+
+ {% endfor %}
diff --git a/onlinecourse/urls.py b/onlinecourse/urls.py index 2960e104..48304e75 100644 --- a/onlinecourse/urls.py +++ b/onlinecourse/urls.py @@ -18,7 +18,9 @@ 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="exam_result"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/onlinecourse/views.py b/onlinecourse/views.py index 53eb02f0..b13f586e 100644 --- a/onlinecourse/views.py +++ b/onlinecourse/views.py @@ -1,18 +1,50 @@ from django.shortcuts import render from django.http import HttpResponseRedirect # Import any new Models here -from .models import Course, Enrollment from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, render, redirect from django.urls import reverse from django.views import generic from django.contrib.auth import login, logout, authenticate import logging +from .models import Course, Enrollment, Question, Choice, Submission # Get an instance of a logger logger = logging.getLogger(__name__) + # Create your views here. +def submit(request, course_id): + course = get_object_or_404(Course, pk=course_id) + user = request.user + enrollment = Enrollment.objects.get(user=user, course=course) + submission = Submission.objects.create(enrollment=enrollment) + choices = extract_answers(request) + submission.choices.set(choices) + submission_id = submission.id + return HttpResponseRedirect(reverse(viewname='onlinecourse:exam_result', args=(course_id, submission_id,))) + +def show_exam_result(request, course_id, submission_id): + context = {} + course = get_object_or_404(Course, pk=course_id) + submission = Submission.objects.get(id=submission_id) + choices = submission.choices.all() + + total_score = 0 + questions = course.question_set.all() # Assuming course has related questions + + for question in questions: + correct_choices = question.choice_set.filter(is_correct=True) # Get all correct choices for the question + selected_choices = choices.filter(question=question) # Get the user's selected choices for the question + + # Check if the selected choices are the same as the correct choices + if set(correct_choices) == set(selected_choices): + total_score += question.grade # Add the question's grade only if all correct answers are selected + context['course'] = course + context['grade'] = total_score + context['choices'] = choices + return render(request, 'onlinecourse/exam_result_bootstrap.html', context) + def registration_request(request): context = {} if request.method == 'GET':