Add exam generation functionality with customizable name and filters
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
<ul class="dropdown-menu" aria-labelledby="sbasQuestionsDropdown">
|
<ul class="dropdown-menu" aria-labelledby="sbasQuestionsDropdown">
|
||||||
<li><a class="dropdown-item" href="{% url 'sbas:question_view' %}"><i class="bi bi-list-ul"></i> All Questions</a></li>
|
<li><a class="dropdown-item" href="{% url 'sbas:question_view' %}"><i class="bi bi-list-ul"></i> All Questions</a></li>
|
||||||
<li><a class="dropdown-item" href="{% url 'sbas:question_review_start' %}"><i class="bi bi-bookmarks-fill"></i> Review Questions</a></li>
|
<li><a class="dropdown-item" href="{% url 'sbas:question_review_start' %}"><i class="bi bi-bookmarks-fill"></i> Review Questions</a></li>
|
||||||
|
<li><a class="dropdown-item" href="{% url 'sbas:generate_exam' %}"><i class="bi bi-journal-plus"></i> Generate Exam</a></li>
|
||||||
<!-- Additional items can be added here -->
|
<!-- Additional items can be added here -->
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
{% extends 'sbas/base.html' %}
|
||||||
|
|
||||||
|
{% block title %}Generate Exam{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container mt-3">
|
||||||
|
<h2>Generate Personal SBA Exam</h2>
|
||||||
|
<p>Choose filters and provide a name for your exam. The created exam will be active and available for you to take.</p>
|
||||||
|
|
||||||
|
<form method="post" action="{% url 'sbas:exam_create_from_filters_with_name' %}">
|
||||||
|
{% csrf_token %}
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="exam-name" class="form-label">Exam name</label>
|
||||||
|
<input type="text" class="form-control" id="exam-name" name="name" placeholder="My personal SBA exam">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_category" class="form-label">Category</label>
|
||||||
|
<select id="id_category" name="category" class="form-select">
|
||||||
|
<option value="">Any</option>
|
||||||
|
{% for c in categories %}
|
||||||
|
<option value="{{ c.id }}">{{ c.category }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<p class="small text-muted">Only questions with an "Accepted" review will be used to build the exam.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="create-count" class="form-label">Number of questions (0 = all)</label>
|
||||||
|
<input type="number" id="create-count" name="count" class="form-control" value="0" min="0">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<input class="form-check-input" type="checkbox" value="on" id="exclude-answered" name="exclude_answered">
|
||||||
|
<label class="form-check-label" for="exclude-answered">
|
||||||
|
Exclude questions I have already answered in other exams
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">Generate Exam</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@@ -59,37 +59,6 @@
|
|||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<hr/>
|
|
||||||
|
|
||||||
<div class="mt-3">
|
|
||||||
<h5>Create your personal exam</h5>
|
|
||||||
<p>Generate a personal SBA exam from the filtered question bank. Optionally limit the number of questions.</p>
|
|
||||||
|
|
||||||
<form id="create-exam-form" method="post" action="{% url 'sbas:exam_create_from_filters' %}">
|
|
||||||
{% csrf_token %}
|
|
||||||
<input type="hidden" name="category" id="create-category" />
|
|
||||||
<input type="hidden" name="status" id="create-status" />
|
|
||||||
<div class="mb-2">
|
|
||||||
<label for="create-count" class="form-label">Number of questions (0 = all)</label>
|
|
||||||
<input type="number" name="count" id="create-count" class="form-control" min="0" value="0" />
|
|
||||||
</div>
|
|
||||||
<button id="create-exam-btn" type="button" class="btn btn-success">Create my exam</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
(function(){
|
|
||||||
const createBtn = document.getElementById('create-exam-btn');
|
|
||||||
createBtn.addEventListener('click', function(){
|
|
||||||
const categoryEl = document.getElementById('id_category');
|
|
||||||
const statusEl = document.getElementById('id_status');
|
|
||||||
const createCat = document.getElementById('create-category');
|
|
||||||
const createStatus = document.getElementById('create-status');
|
|
||||||
if (categoryEl) createCat.value = categoryEl.value || '';
|
|
||||||
if (statusEl) createStatus.value = statusEl.value || '';
|
|
||||||
document.getElementById('create-exam-form').submit();
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
</script>
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
+9
-3
@@ -99,10 +99,16 @@ urlpatterns = [
|
|||||||
views.import_llm_confirm,
|
views.import_llm_confirm,
|
||||||
name="import_llm_confirm",
|
name="import_llm_confirm",
|
||||||
),
|
),
|
||||||
|
|
||||||
path(
|
path(
|
||||||
"exam/create_from_filters/",
|
"exam/create_from_filters_with_name/",
|
||||||
views.create_personal_exam,
|
views.create_personal_exam_with_name,
|
||||||
name="exam_create_from_filters",
|
name="exam_create_from_filters_with_name",
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"question/generate/",
|
||||||
|
views.generate_exam,
|
||||||
|
name="generate_exam",
|
||||||
),
|
),
|
||||||
#path(
|
#path(
|
||||||
# "exam/<int:pk>/scores/<int:cid>/<str:passcode>/",
|
# "exam/<int:pk>/scores/<int:cid>/<str:passcode>/",
|
||||||
|
|||||||
+34
-4
@@ -383,12 +383,12 @@ class QuestionClone(QuestionCreateBase):
|
|||||||
class UserAnswerView(LoginRequiredMixin, DetailView):
|
class UserAnswerView(LoginRequiredMixin, DetailView):
|
||||||
model = UserAnswer
|
model = UserAnswer
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def create_personal_exam(request):
|
def create_personal_exam_with_name(request):
|
||||||
"""Create a personal SBA exam from question filters and redirect to start page.
|
"""Create a personal SBA exam from filters, using a provided name.
|
||||||
|
|
||||||
POST parameters expected:
|
POST parameters expected:
|
||||||
|
- name (optional): exam name to use
|
||||||
- category (optional)
|
- category (optional)
|
||||||
- status (optional)
|
- status (optional)
|
||||||
- count (optional): number of questions to include (0 or missing = all)
|
- count (optional): number of questions to include (0 or missing = all)
|
||||||
@@ -396,8 +396,13 @@ def create_personal_exam(request):
|
|||||||
if request.method != "POST":
|
if request.method != "POST":
|
||||||
return HttpResponseBadRequest("POST required")
|
return HttpResponseBadRequest("POST required")
|
||||||
|
|
||||||
|
name = request.POST.get("name") or None
|
||||||
|
# reuse existing create_personal_exam filtering logic but allow custom name
|
||||||
category = request.POST.get("category") or None
|
category = request.POST.get("category") or None
|
||||||
status = request.POST.get("status") or "ANY"
|
# For generated personal exams we only include accepted questions
|
||||||
|
status = QuestionReview.StatusChoices.ACCEPTED
|
||||||
|
# Optionally exclude questions the current user has already answered
|
||||||
|
exclude_answered = request.POST.get("exclude_answered") in ("on", "true", "True", "1")
|
||||||
try:
|
try:
|
||||||
count = int(request.POST.get("count") or 0)
|
count = int(request.POST.get("count") or 0)
|
||||||
if count < 0:
|
if count < 0:
|
||||||
@@ -413,6 +418,14 @@ def create_personal_exam(request):
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# If requested, remove questions the current user has already answered
|
||||||
|
if exclude_answered:
|
||||||
|
try:
|
||||||
|
answered_qs = UserAnswer.objects.filter(user=request.user).values_list("question_id", flat=True)
|
||||||
|
qs = qs.exclude(pk__in=list(answered_qs))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# Permission filter: non-checkers see open_access or their own questions
|
# Permission filter: non-checkers see open_access or their own questions
|
||||||
try:
|
try:
|
||||||
if not request.user.groups.filter(name="sbas_checker").exists():
|
if not request.user.groups.filter(name="sbas_checker").exists():
|
||||||
@@ -459,7 +472,9 @@ def create_personal_exam(request):
|
|||||||
matches = matches[:count]
|
matches = matches[:count]
|
||||||
|
|
||||||
# Create the Exam
|
# Create the Exam
|
||||||
|
if name is None or name.strip() == "":
|
||||||
name = f"{request.user.get_full_name() or request.user.username} - Personal SBA Exam"
|
name = f"{request.user.get_full_name() or request.user.username} - Personal SBA Exam"
|
||||||
|
|
||||||
exam = Exam(name=name, active=True, candidates_only=True, exam_mode=True)
|
exam = Exam(name=name, active=True, candidates_only=True, exam_mode=True)
|
||||||
exam.save()
|
exam.save()
|
||||||
# Add author and allow this user to take the exam
|
# Add author and allow this user to take the exam
|
||||||
@@ -481,6 +496,21 @@ def create_personal_exam(request):
|
|||||||
return redirect("sbas:exam_start", pk=exam.pk)
|
return redirect("sbas:exam_start", pk=exam.pk)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def generate_exam(request):
|
||||||
|
"""Render a small form allowing the user to name and generate a personal exam from filters."""
|
||||||
|
# Prepare categories and status options for the template
|
||||||
|
categories = Category.objects.all().order_by("category")
|
||||||
|
|
||||||
|
# Build status options: ANY, UNREVIEWED, plus choices from QuestionReview
|
||||||
|
status_options = [("ANY", "Any")]
|
||||||
|
status_options.append(("UNREVIEWED", "Unreviewed"))
|
||||||
|
for v, label in QuestionReview.StatusChoices.choices:
|
||||||
|
status_options.append((v, label))
|
||||||
|
|
||||||
|
return render(request, "sbas/generate_exam.html", {"categories": categories, "status_options": status_options})
|
||||||
|
|
||||||
|
|
||||||
class UserAnswerTableView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
class UserAnswerTableView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
||||||
model = UserAnswer
|
model = UserAnswer
|
||||||
table_class = UserAnswerTable
|
table_class = UserAnswerTable
|
||||||
|
|||||||
Reference in New Issue
Block a user