11from django .contrib .auth .decorators import login_required
22from django .shortcuts import get_object_or_404 , redirect , render
33from home .decorators import profile_required
4+ from home .models import UserProfile
45
56from .forms import QuizForm
67from .models import Question , QuizProgress , Topic , UserAnswer
@@ -113,6 +114,7 @@ def quiz_question(request, topic_id):
113114
114115def 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