|
1 | 1 | from django.db import models |
| 2 | +from django.contrib.auth.models import User |
| 3 | +# from backend.apps.uploads.models import UploadRecord - This will be revised and corrected |
| 4 | +from django.core.exceptions import ValidationError |
| 5 | +from django.core.validators import MinValueValidator |
2 | 6 |
|
3 | | -# Create your models here. |
| 7 | + |
| 8 | +class Assignment(models.Model): # Assignments: Represents assignments given to individual students. |
| 9 | + ASSIGNMENT_TYPES = [ |
| 10 | + ('EX', 'Exercises'), |
| 11 | + ('HW', 'Homework'), |
| 12 | + ('QZ', 'Quiz'), |
| 13 | + ('TT', 'Tests'), |
| 14 | + # Can add more if needed... |
| 15 | + ('EC', 'Extra Credit') |
| 16 | + ] |
| 17 | + # Fields |
| 18 | + title = models.CharField(max_length=200) |
| 19 | + description = models.TextField(blank=True) |
| 20 | + assignment_type = models.CharField(max_length=2, choices=ASSIGNMENT_TYPES) |
| 21 | + |
| 22 | + # Upload_record (ForeignKey to UploadRecord) - Currently not referenced correctly but will be when branch is updated |
| 23 | + # upload_record = models.ForeignKey(UploadRecord, on_delete=models.SET_NULL, null=True, blank=True) |
| 24 | + |
| 25 | + # Student (ForeignKey to User as there's no model exclusively for student) |
| 26 | + student = models.ForeignKey(User, on_delete=models.CASCADE, related_name="assignments", null=True, blank=True) |
| 27 | + # ^ Currently student is temporarily allowed to be nullable. Can be manually updated in a later time. |
| 28 | + |
| 29 | + deadline = models.DateTimeField(null=True, blank=True) # deadline is optional for extra credit/optional assignments |
| 30 | + |
| 31 | + def __str__(self): |
| 32 | + return self.title |
| 33 | + |
| 34 | + |
| 35 | +class Quiz(models.Model): # Quizzes: Represents quizzes linked to assignments. |
| 36 | + class Meta: |
| 37 | + verbose_name = 'Quiz' |
| 38 | + verbose_name_plural = 'Quizzes' |
| 39 | + # Fields |
| 40 | + assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE) |
| 41 | + time_limit = models.PositiveIntegerField(null=True, blank=True, help_text="Time limit for the quiz (in minutes)") |
| 42 | + num_of_questions = models.PositiveIntegerField() |
| 43 | + attempts = models.IntegerField(validators=[MinValueValidator(1)]) # Ensures that at least 1 attempt is required |
| 44 | + is_active = models.BooleanField(default=True) |
| 45 | + |
| 46 | + def __str__(self): |
| 47 | + return f"Quiz for {self.assignment.title}" |
| 48 | + |
| 49 | + |
| 50 | +class Question(models.Model): # Questions: Represents individual questions within a quiz. |
| 51 | + QUESTION_TYPES = [ |
| 52 | + ('MC', 'Multiple Choice'), |
| 53 | + ('SA', 'Short Answer'), |
| 54 | + ] |
| 55 | + # Fields |
| 56 | + quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE) |
| 57 | + question_type = models.CharField(max_length=2, choices=QUESTION_TYPES) |
| 58 | + order_of_question = models.PositiveIntegerField() |
| 59 | + question_text = models.TextField() |
| 60 | + # New field to store points/score for the question |
| 61 | + points = models.PositiveIntegerField(default=1, help_text="Points assigned to this question") |
| 62 | + |
| 63 | + def __str__(self): |
| 64 | + quiz_title = self.quiz.assignment.title if self.quiz and self.quiz.assignment else "Unknown Quiz" |
| 65 | + return f"Question {self.order_of_question} - {quiz_title}" |
| 66 | + |
| 67 | + |
| 68 | +class Choice(models.Model): # Choices: Store the possible answer choices for multiple choice questions. |
| 69 | + # Fields |
| 70 | + question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name="choices") |
| 71 | + choice_text = models.CharField(max_length=255) |
| 72 | + is_correct = models.BooleanField(default=False) |
| 73 | + |
| 74 | + def __str__(self): |
| 75 | + return f"Choice: {self.choice_text} ({'Correct' if self.is_correct else 'Incorrect'})" |
| 76 | + |
| 77 | + |
| 78 | +class Solution(models.Model): # Solutions: Represents correct answers to questions. |
| 79 | + # Fields |
| 80 | + question = models.ForeignKey(Question, on_delete=models.CASCADE) |
| 81 | + choices = models.ManyToManyField(Choice, blank=True) # for multiple choice questions |
| 82 | + short_answer_text = models.TextField(blank=True, null=True) # for short answer questions |
| 83 | + |
| 84 | + # New validation that ensures MC questions have at least one correct answer |
| 85 | + def clean(self): |
| 86 | + if self.question.question_type == 'MC' and not self.choices.filter(is_correct=True).exists(): |
| 87 | + raise ValidationError("A multiple-choice question must have at least one correct answer.") |
| 88 | + |
| 89 | + def __str__(self): |
| 90 | + if self.question.question_type == 'MC': |
| 91 | + correct_choices = ", ".join(choice.choice_text for choice in self.choices.all()) |
| 92 | + return f"Solution for Question {self.question.id}: {correct_choices}" |
| 93 | + elif self.question.question_type == 'SA': |
| 94 | + return f"Solution for Question {self.question.id}: {self.short_answer_text[:30]}" |
| 95 | + else: |
| 96 | + return f"Solution for Question {self.question.id}: (No Answer Set)" |
0 commit comments