@@ -72,32 +72,52 @@ def survey():
7272 form = SurveyForm ()
7373
7474 if form .validate_on_submit ():
75- video_platforms = ',' .join (form .video_platforms .data ) if form .video_platforms .data else ''
76- lesson_quality = ',' .join (form .lesson_quality .data ) if form .lesson_quality .data else ''
77-
78- response = Survey (
79- user_id = current_user .id ,
80- class_section = form .class_section .data ,
81- study_time = form .study_time .data ,
82- interest_level = form .interest_level .data ,
83- confidence = form .confidence .data ,
84- memory_method = form .memory_method .data ,
85- online_learning = form .online_learning .data ,
86- hardest_subject = form .hardest_subject .data ,
87- favorite_subject = form .favorite_subject .data ,
88- social_time = form .social_time .data ,
89- video_platforms = video_platforms ,
90- video_helpful = form .video_helpful .data ,
91- lesson_quality = lesson_quality ,
92- ideal_length = form .ideal_length .data ,
93- videos_for_tests = form .videos_for_tests .data ,
94- review_before_class = form .review_before_class .data
95- )
96- db .session .add (response )
97- db .session .commit ()
98- flash ('Thank you for completing the survey.' , 'success' )
99- # Redirect to recommendations page instead of back to survey
100- return redirect (url_for ('student.recommendations' , survey_id = response .id ))
75+ try :
76+ # Handle checkbox fields more robustly
77+ video_platforms = []
78+ if form .video_platforms .data :
79+ video_platforms = form .video_platforms .data
80+ video_platforms_str = ',' .join (video_platforms ) if video_platforms else ''
81+
82+ lesson_quality = []
83+ if form .lesson_quality .data :
84+ lesson_quality = form .lesson_quality .data
85+ lesson_quality_str = ',' .join (lesson_quality ) if lesson_quality else ''
86+
87+ response = Survey (
88+ user_id = current_user .id ,
89+ class_section = form .class_section .data ,
90+ study_time = form .study_time .data ,
91+ interest_level = form .interest_level .data ,
92+ confidence = form .confidence .data ,
93+ memory_method = form .memory_method .data ,
94+ online_learning = form .online_learning .data ,
95+ hardest_subject = form .hardest_subject .data ,
96+ favorite_subject = form .favorite_subject .data ,
97+ social_time = form .social_time .data ,
98+ video_platforms = video_platforms_str ,
99+ video_helpful = form .video_helpful .data ,
100+ lesson_quality = lesson_quality_str ,
101+ ideal_length = form .ideal_length .data ,
102+ videos_for_tests = form .videos_for_tests .data ,
103+ review_before_class = form .review_before_class .data
104+ )
105+ db .session .add (response )
106+ db .session .commit ()
107+ flash ('Thank you for completing the survey.' , 'success' )
108+
109+ # Redirect to recommendations page instead of back to survey
110+ return redirect (url_for ('student.recommendations' , survey_id = response .id ))
111+
112+ except Exception as e :
113+ print (f"Error saving survey: { e } " )
114+ db .session .rollback ()
115+ flash ('There was an error saving your survey. Please try again.' , 'danger' )
116+ return redirect (url_for ('student.survey' ))
117+ else :
118+ # Show validation errors if any
119+ if form .errors :
120+ flash ('Please fill in all required fields correctly.' , 'warning' )
101121
102122 return render_template ('survey.html' , form = form , current_user = current_user )
103123
@@ -107,42 +127,47 @@ def dashboard():
107127 if current_user .role != 'student' :
108128 return "Access denied" , 403
109129
110- # Motivational videos (existing logic)
111- videos = MotivationalVideo .query .all ()
112- video_data = []
113- for video in videos :
114- ratings = [r .rating for r in video .ratings ]
115- avg_rating = round (sum (ratings ) / len (ratings ), 1 ) if ratings else None
116- my_rating = next ((r .rating for r in video .ratings if r .student_id == current_user .id ), None )
117- video_data .append ({
118- 'video' : video ,
119- 'average_rating' : avg_rating ,
120- 'my_rating' : my_rating
121- })
122-
123- # --- NEW: Get recommendations for the current student ---
124- # Get the latest survey for the current user
125- survey = Survey .query .filter_by (user_id = current_user .id ).order_by (Survey .id .desc ()).first ()
126- recommended_videos = []
127- if survey :
128- # Get all available videos (submitted + motivational)
129- submitted_videos = VideoSubmission .query .all ()
130- motivational_videos = MotivationalVideo .query .all ()
131- all_videos = list (submitted_videos ) + list (motivational_videos )
132-
133- # Initialize recommender if needed
134- try :
135- initialize_recommender ()
136- recommended_videos = recommender .get_recommendations (survey , all_videos , top_k = 5 )
137- except Exception as e :
138- print (f"Error getting recommendations for dashboard: { e } " )
139- recommended_videos = []
140-
141- return render_template (
142- 'student_dashboard.html' ,
143- videos = video_data ,
144- recommended_videos = recommended_videos
145- )
130+ try :
131+ # Motivational videos (existing logic)
132+ videos = MotivationalVideo .query .all ()
133+ video_data = []
134+ for video in videos :
135+ ratings = [r .rating for r in video .ratings ]
136+ avg_rating = round (sum (ratings ) / len (ratings ), 1 ) if ratings else None
137+ my_rating = next ((r .rating for r in video .ratings if r .student_id == current_user .id ), None )
138+ video_data .append ({
139+ 'video' : video ,
140+ 'average_rating' : avg_rating ,
141+ 'my_rating' : my_rating
142+ })
143+
144+ # --- NEW: Get recommendations for the current student ---
145+ # Get the latest survey for the current user
146+ survey = Survey .query .filter_by (user_id = current_user .id ).order_by (Survey .id .desc ()).first ()
147+ recommended_videos = []
148+ if survey :
149+ # Get all available videos (submitted + motivational)
150+ submitted_videos = VideoSubmission .query .all ()
151+ motivational_videos = MotivationalVideo .query .all ()
152+ all_videos = list (submitted_videos ) + list (motivational_videos )
153+
154+ # Initialize recommender if needed
155+ try :
156+ initialize_recommender ()
157+ recommended_videos = recommender .get_recommendations (survey , all_videos , top_k = 5 )
158+ except Exception as e :
159+ print (f"Error getting recommendations for dashboard: { e } " )
160+ recommended_videos = []
161+
162+ return render_template (
163+ 'student_dashboard.html' ,
164+ videos = video_data ,
165+ recommended_videos = recommended_videos
166+ )
167+ except Exception as e :
168+ print (f"Error in dashboard route: { e } " )
169+ flash ('There was an error loading the dashboard. Please try again.' , 'warning' )
170+ return render_template ('student_dashboard.html' , videos = [], recommended_videos = [])
146171
147172@student_bp .route ('/submit' , methods = ['GET' , 'POST' ])
148173@login_required
@@ -336,50 +361,58 @@ def recommendations(survey_id):
336361 if current_user .role != 'student' :
337362 return "Access denied" , 403
338363
339- # Get the survey data
340- survey = Survey .query .get_or_404 (survey_id )
341-
342- # Verify the survey belongs to the current user
343- if survey .user_id != current_user .id :
344- abort (403 )
364+ try :
365+ # Get the survey data
366+ survey = Survey .query .get_or_404 (survey_id )
367+
368+ # Verify the survey belongs to the current user
369+ if survey .user_id != current_user .id :
370+ abort (403 )
345371
346- # Get all available videos (both submitted and motivational)
347- submitted_videos = VideoSubmission .query .all ()
348- motivational_videos = MotivationalVideo .query .all ()
349-
350- # Combine all videos
351- all_videos = list (submitted_videos ) + list (motivational_videos )
372+ # Get all available videos (both submitted and motivational)
373+ submitted_videos = VideoSubmission .query .all ()
374+ motivational_videos = MotivationalVideo .query .all ()
375+
376+ # Combine all videos
377+ all_videos = list (submitted_videos ) + list (motivational_videos )
352378
353- if not all_videos :
354- # If no videos are available, return empty recommendations
355- return render_template ('recommendations.html' , recommendations = [], survey = survey )
379+ if not all_videos :
380+ # If no videos are available, return empty recommendations
381+ return render_template ('recommendations.html' , recommendations = [], survey = survey )
356382
357- # Initialize the recommender (load existing model or train new one)
358- try :
359- initialize_recommender ()
360- except Exception as e :
361- print (f"Error initializing recommender: { e } " )
362- # Continue with fallback method
363-
364- # Get AI-powered recommendations
365- try :
366- recommendations = recommender .get_recommendations (survey , all_videos , top_k = 5 )
367- except Exception as e :
368- print (f"Error getting recommendations: { e } " )
383+ # Initialize the recommender (load existing model or train new one)
384+ try :
385+ initialize_recommender ()
386+ except Exception as e :
387+ print (f"Error initializing recommender: { e } " )
388+ # Continue with fallback method
389+
390+ # Get AI-powered recommendations with timeout
369391 recommendations = []
370-
371- # Add model information for transparency
372- model_info = {
373- 'model_type' : 'Logistic Regression' ,
374- 'features_used' : len (recommender .feature_names ),
375- 'is_trained' : recommender .model is not None
376- }
377-
378- return render_template ('recommendations.html' ,
379- recommendations = recommendations ,
380- survey = survey ,
381- model_info = model_info )
382-
392+ try :
393+ # Use a simpler approach without signal.SIGALRM (which doesn't work on Windows)
394+ recommendations = recommender .get_recommendations (survey , all_videos , top_k = 5 )
395+
396+ except Exception as e :
397+ print (f"Error getting recommendations: { e } " )
398+ # No fallback recommendations - just return empty list
399+ recommendations = []
400+
401+ # Add model information for transparency
402+ model_info = {
403+ 'model_type' : 'Logistic Regression' ,
404+ 'features_used' : len (recommender .feature_names ),
405+ 'is_trained' : recommender .model is not None
406+ }
407+
408+ return render_template ('recommendations.html' ,
409+ recommendations = recommendations ,
410+ survey = survey ,
411+ model_info = model_info )
412+ except Exception as e :
413+ print (f"Error in recommendations route: { e } " )
414+ flash ('There was an error generating recommendations. Please try again.' , 'warning' )
415+ return redirect (url_for ('student.survey' ))
383416
384417from ..teacher .models import MotivationalVideo , VideoRating
385418
0 commit comments