Skip to content

Commit 6ab60c5

Browse files
Fix 40 (#114)
* quiz module now grants xp according to user score and topic difficulty
1 parent 042aae5 commit 6ab60c5

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Algolyzer/quiz/templates/quiz/results.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<span id="counter" style="--value:${counter};"></span>
1515
</span>
1616
</div>
17-
<div class="stat-desc lg:text-2xl">52% above average</div>
17+
<div class="stat-desc lg:text-2xl text-accent">Experience earned: +{{ xp }}</div>
1818
</div>
1919
</div>
2020
</div>
@@ -64,7 +64,6 @@
6464

6565
{% block scripts %}
6666
<script>
67-
{% comment %} Confetti Pop Script {% endcomment %}
6867
document.addEventListener("DOMContentLoaded", function () {
6968
var duration = 3 * 1000; // Run for 3 seconds
7069
var end = Date.now() + duration;
@@ -89,7 +88,7 @@
8988
randomConfetti(); // Start the explosion!
9089
});
9190

92-
{% comment %} Score Incrementor Script {% endcomment %}
91+
9392
function animateCounter(targetValue, speed = 100) {
9493
let currentValue = 0;
9594
const counterElement = document.getElementById("counter");

Algolyzer/quiz/views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib.auth.decorators import login_required
22
from django.shortcuts import get_object_or_404, redirect, render
33
from home.decorators import profile_required
4+
from home.models import UserProfile
45

56
from .forms import QuizForm
67
from .models import Question, QuizProgress, Topic, UserAnswer
@@ -113,6 +114,7 @@ def quiz_question(request, topic_id):
113114

114115
def quiz_results(request, topic_id):
115116
topic = get_object_or_404(Topic, id=topic_id)
117+
# print(topic)
116118
quiz_progress = get_object_or_404(QuizProgress, topic=topic, user=request.user)
117119
if not quiz_progress.completed:
118120
return redirect("quiz_question", topic_id=topic.id)
@@ -129,12 +131,27 @@ def quiz_results(request, topic_id):
129131

130132
score = answers.filter(is_correct=True).count()
131133
total = Question.objects.filter(topic=topic).count()
134+
XP_REWARDS = {
135+
"Easy": 2,
136+
"Medium": 5,
137+
"Hard": 10,
138+
"Veteran": 20,
139+
}
140+
# Add XP to the user's profile based on the topic difficulty
141+
user_profile = UserProfile.objects.get(user=request.user)
142+
print(topic.difficulty)
143+
xp_to_add = (
144+
XP_REWARDS.get(topic.difficulty, 0) * score
145+
) # Default to 0 if difficulty is not found
146+
print(xp_to_add * score)
147+
user_profile.add_xp(xp_to_add)
132148

133149
context = {
134150
"topic": topic,
135151
"score": score,
136152
"total": total,
137153
"answers": answers,
154+
"xp": xp_to_add,
138155
}
139156

140157
return render(request, "quiz/results.html", context=context)

0 commit comments

Comments
 (0)