-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
1542 lines (1259 loc) · 53 KB
/
Copy pathapp.py
File metadata and controls
1542 lines (1259 loc) · 53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Response
from io import BytesIO
import openpyxl
from flask import Flask, render_template, request, redirect, url_for, session, flash
from datetime import datetime
from collections import defaultdict
import os
import psycopg2
app = Flask(__name__)
app.secret_key = "your_secret_key"
TEACHER_SUPERKEY="1"
STUDENT_DOMAIN = "@student.annauniv.edu"
TEACHER_DOMAIN = "@faculty.annauniv.edu"
def get_db_connection():
database_url = os.environ.get('DATABASE_URL')
if database_url:
return psycopg2.connect(
database_url,
sslmode='require'
)
else:
return psycopg2.connect(
host="localhost",
database="quizdb",
user="postgres",
password="4321"
)
@app.route('/', methods=['GET', 'POST'])
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email', '')
password = request.form.get('password', '')
conn = get_db_connection()
cur = conn.cursor()
cur.execute('SELECT * FROM users WHERE email=%s AND passwordhash=%s', (email, password))
user = cur.fetchone()
cur.close()
conn.close()
if user:
session['user_id'] = user[0]
session['role'] = user[4]
if user[4] == 'student':
return redirect(url_for('student_dashboard'))
elif user[4] == 'teacher':
# approved column can be None (pending), False (rejected), True (approved)
if user[6] is None:
flash("Teacher account status pending. Please wait for admin approval.", "info")
return render_template('login.html', form_data=request.form)
elif user[6] is False:
flash("Teacher account rejected by admin.", "error")
return render_template('login.html', form_data=request.form)
elif user[6] is True:
return redirect(url_for('teacher_dashboard'))
else: # For admin users and others
return redirect(url_for('admin_dashboard'))
else:
flash("Invalid email or password.", "error")
return render_template('login.html', form_data=request.form)
# GET request rendering login page with empty form
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form.get('name')
email = request.form.get('email')
password = request.form.get('password')
role = request.form.get('role')
classy = request.form.get('class')
teacher_key = request.form.get('teacher_key', '')
# Basic input validations
if not username or not email or not password or not role or not classy:
flash("Please fill out all the required fields.", "error")
return render_template('register.html', form_data=request.form)
if role == "admin":
flash("Online admin registration is not allowed. Please contact an existing admin.", "error")
return render_template('register.html', form_data=request.form)
if role == "teacher":
if not email.endswith(TEACHER_DOMAIN):
flash(f"Teachers must register using a {TEACHER_DOMAIN} email.", "error")
return render_template('register.html', form_data=request.form)
if teacher_key != TEACHER_SUPERKEY:
flash("Invalid teacher registration code.", "error")
return render_template('register.html', form_data=request.form)
if role == "student":
if not email.endswith(STUDENT_DOMAIN):
flash(f"Students must register using a {STUDENT_DOMAIN} email.", "error")
return render_template('register.html', form_data=request.form)
conn = get_db_connection()
cur = conn.cursor()
# Check duplicate email
cur.execute("SELECT * FROM users WHERE email = %s", (email,))
existing_user = cur.fetchone()
if existing_user:
# If user is a rejected teacher, reset approval to pending with new password
if existing_user[4] == 'teacher' and existing_user[6] == False:
cur.execute("UPDATE users SET approved = NULL, passwordhash = %s WHERE email = %s", (password, email))
conn.commit()
cur.close()
conn.close()
flash("Your previous rejection was reset, please wait for admin approval.", "info")
return redirect(url_for('login'))
else:
cur.close()
conn.close()
flash("Email already registered. Please login or use another email.", "error")
return render_template('register.html', form_data=request.form)
if role == 'teacher':
approved = None # Pending approval by default for teachers
else:
approved = True # Auto-approved for others (students/admin)
# Insert user
cur.execute(
'INSERT INTO users (name, email, passwordhash, role, class, approved) VALUES (%s, %s, %s, %s, %s, %s)',
(username, email, password, role, classy, approved)
)
conn.commit()
cur.close()
conn.close()
flash("Registered successfully! Please login.", "success")
return redirect(url_for('login'))
# On GET, show page with no pre-filled data
return render_template('register.html')
@app.route('/student_dashboard', methods=['GET', 'POST'])
def student_dashboard():
user_id = session.get('user_id')
if not user_id or session.get('role') != 'student':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Handle enrollment request FIRST (POST)
if request.method == 'POST':
class_to_enroll = request.form.get('class_to_enroll')
# Find status of the latest enrollment request for this class
cur.execute('''
SELECT approved FROM enrollments
WHERE studentid=%s AND class=%s
ORDER BY requested_at DESC LIMIT 1
''', (user_id, class_to_enroll))
latest = cur.fetchone()
if latest:
if latest[0] is None:
flash(f"You already have a pending enrollment request for '{class_to_enroll}'.", "warning")
elif latest[0] is True:
flash(f"You are already enrolled in '{class_to_enroll}'.", "info")
else: # latest[0] == False (rejected), allow to reapply
cur.execute('''
INSERT INTO enrollments (studentid, class, approved, requested_at)
VALUES (%s, %s, NULL, NOW())
''', (user_id, class_to_enroll))
conn.commit()
flash(f"Enrollment request reapplied for '{class_to_enroll}'.", "success")
else:
cur.execute('''
INSERT INTO enrollments (studentid, class, approved, requested_at)
VALUES (%s, %s, NULL, NOW())
''', (user_id, class_to_enroll))
conn.commit()
flash(f"Enrollment request for '{class_to_enroll}' submitted!", "success")
cur.close()
conn.close()
return redirect(url_for('student_dashboard'))
# GET REQUEST - Fetch all data
# Get all available classes
cur.execute("""
SELECT tc.class, tc.subject, u.name AS teacher_name
FROM teacher_classes tc
JOIN users u ON tc.teacherid = u.userid
""")
available_classes = cur.fetchall()
# Get student info
cur.execute('SELECT name, email, role FROM users WHERE userid=%s', (user_id,))
student = cur.fetchone()
# Get all classes student is enrolled in (for filtering quizzes)
cur.execute('SELECT class FROM enrollments WHERE studentid=%s AND approved=TRUE', (user_id,))
student_classes = [row[0] for row in cur.fetchall()]
# Get all requests for this student (approved, pending, rejected)
cur.execute('SELECT class, approved, requested_at FROM enrollments WHERE studentid=%s ORDER BY requested_at DESC', (user_id,))
rows = cur.fetchall()
# Get recent attempts with id, title, score
cur.execute('''
SELECT q.title, a.score, a.attemptid FROM attempts a
JOIN quizzes q ON a.quizid = q.quizid
WHERE a.studentid=%s AND a.score IS NOT NULL
ORDER BY a.endtime DESC LIMIT 5
''', (user_id,))
recent_results = cur.fetchall()
# Get attempt IDs which already have feedback submitted by this student
cur.execute('''
SELECT attemptid FROM feedback WHERE studentid = %s
''', (user_id,))
feedback_attempt_rows = cur.fetchall()
feedback_attempt_ids = {row[0] for row in feedback_attempt_rows}
# Latest status per class
status_by_class = defaultdict(list)
for cls, approved, req_time in rows:
status_by_class[cls].append((req_time, approved))
pending_enrollment_classes = []
rejected_enrollment_classes = []
for cls, statlist in status_by_class.items():
# sort by request time DESC so latest first
statlist.sort(reverse=True)
latest_approved = statlist[0][1]
if latest_approved is None:
pending_enrollment_classes.append(cls)
elif latest_approved is False:
rejected_enrollment_classes.append(cls)
# else skip (approved classes aren't shown)
# don't show approved classes in registration cards
# Get enrolled classes with details for "My Classes" section
cur.execute("""
SELECT DISTINCT e.class, tc.subject
FROM enrollments e
LEFT JOIN teacher_classes tc ON e.class = tc.class
WHERE e.studentid=%s AND e.approved=TRUE
ORDER BY e.class
""", (user_id,))
enrolled_classes = cur.fetchall()
enrollment_dict = {cls: True for cls, _ in enrolled_classes}
# Completed quizzes
cur.execute('SELECT COUNT(*) FROM attempts WHERE studentid=%s AND score IS NOT NULL', (user_id,))
completed_quizzes = cur.fetchone()[0]
# Average score
cur.execute('SELECT AVG(score) FROM attempts WHERE studentid=%s AND score IS NOT NULL', (user_id,))
avg_score = cur.fetchone()[0] or 0
# Pending quizzes
if student_classes:
format_strings = ','.join(['%s'] * len(student_classes))
cur.execute(f'''
SELECT COUNT(*) FROM quizzes AS q
WHERE q.class IN ({format_strings})
AND q.isdraft = FALSE
AND q.quizid NOT IN (
SELECT quizid FROM attempts WHERE studentid=%s AND score IS NOT NULL
)
''', (*student_classes, user_id))
pending_quizzes = cur.fetchone()[0]
else:
pending_quizzes = 0
# Best score
cur.execute('SELECT MAX(score) FROM attempts WHERE studentid=%s AND score IS NOT NULL', (user_id,))
best_score = cur.fetchone()[0] or 0
# Upcoming quizzes
upcoming_quizzes = []
if student_classes:
cur.execute(f'''
SELECT quizid, title, availablefrom, availableto, class FROM quizzes
WHERE class IN ({format_strings})
AND isdraft = FALSE
AND quizid NOT IN (
SELECT quizid FROM attempts WHERE studentid=%s AND score IS NOT NULL
)
ORDER BY availablefrom ASC
''', (*student_classes, user_id))
upcoming_quizzes = cur.fetchall()
# Recent results
cur.execute('''
SELECT q.title, a.score, a.attemptid FROM attempts a
JOIN quizzes q ON a.quizid = q.quizid
WHERE a.studentid=%s AND a.score IS NOT NULL
ORDER BY a.endtime DESC LIMIT 5
''', (user_id,))
recent_results = cur.fetchall()
cur.close()
conn.close()
return render_template(
'student_dashboard.html',
name=student[0],
email=student[1],
role=student[2],
available_classes=available_classes,
student_classes=student_classes,
enrolled_classes=enrolled_classes,
completed_quizzes=completed_quizzes,
avg_score=avg_score,
pending_quizzes=pending_quizzes,
best_score=best_score,
upcoming_quizzes=upcoming_quizzes,
recent_results=recent_results,
enrollment_dict=enrollment_dict,
rejected_enrollment_classes=rejected_enrollment_classes,
feedback_attempt_ids=feedback_attempt_ids,
pending_enrollment_classes=pending_enrollment_classes
)
@app.route('/teacher_dashboard')
def teacher_dashboard():
user_id = session.get('user_id')
if not user_id or session.get('role') != 'teacher':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Get teacher details with class
cur.execute('SELECT name, email, role, class FROM users WHERE userid=%s', (user_id,))
teacher = cur.fetchone()
# Extract teacher info - handle if class is missing
teacher_name = teacher[0]
teacher_email = teacher[1]
teacher_role = teacher[2]
classy = teacher[3] if len(teacher) > 3 and teacher[3] else "Not Assigned"
# Get teacher's managed classes and subjects
cur.execute('SELECT class, subject FROM teacher_classes WHERE teacherid=%s', (user_id,))
managed_classes = cur.fetchall()
# Extract class list for queries
managed_class_list = [cls[0] for cls in managed_classes]
# Total quizzes created by teacher
cur.execute('SELECT COUNT(*) FROM quizzes WHERE createdby=%s', (user_id,))
total_quizzes = cur.fetchone()[0]
# Active quizzes (not draft and available now)
cur.execute("""
SELECT COUNT(*) FROM quizzes
WHERE createdby=%s AND isdraft=FALSE AND availableto >= NOW()
""", (user_id,))
active_quizzes = cur.fetchone()[0]
cur.execute("""
SELECT quizid, title FROM quizzes WHERE createdby = %s
""", (user_id,))
quiz_ids_titles = cur.fetchall() # [(quizid, title), ...]
# Initialize grouped results
grouped_attempts = {title: [] for _, title in quiz_ids_titles}
# Fetch all attempts for quizzes created by this teacher
cur.execute("""
SELECT q.quizid, q.title, u.name, u.email, a.score, a.endtime
FROM attempts a
JOIN quizzes q ON a.quizid = q.quizid
JOIN users u ON a.studentid = u.userid
WHERE q.createdby = %s AND a.score IS NOT NULL
ORDER BY q.title, a.endtime DESC
""", (user_id,))
for quizid, title, student_name, student_email, score, endtime in cur.fetchall():
grouped_attempts[title].append({
"student_name": student_name,
"student_email": student_email,
"score": score,
"endtime": endtime
})
# Total students in teacher's classes
if managed_class_list:
cur.execute("""
SELECT COUNT(*) FROM users
WHERE class = ANY(%s) AND role='student'
""", (managed_class_list,))
total_students = cur.fetchone()[0]
else:
total_students = 0
# Average score across all student attempts in teacher's quizzes
cur.execute("""
SELECT AVG(a.score)
FROM attempts a
JOIN quizzes q ON a.quizid = q.quizid
WHERE q.createdby = %s AND a.score IS NOT NULL
""", (user_id,))
avg_score = cur.fetchone()[0] or 0
# Assuming user_id is teacher's ID
'''cur.execute("""
SELECT f.feedbackid, f.feedback, f.comments, f.createdat,
u.name AS student_name, q.title AS quiz_title
FROM feedback f
JOIN users u ON f.studentid = u.userid
JOIN attempts a ON f.attemptid = a.attemptid
JOIN quizzes q ON a.quizid = q.quizid
WHERE f.teacherid = %s
ORDER BY f.createdat DESC
""", (user_id,))
feedbacks = cur.fetchall() '''
# Fetch feedback tuples
cur.execute("""
SELECT f.feedbackid, f.feedback, f.comments, f.createdat,
u.name AS student_name, q.title AS quiz_title
FROM feedback f
JOIN users u ON f.studentid = u.userid
JOIN attempts a ON f.attemptid = a.attemptid
JOIN quizzes q ON a.quizid = q.quizid
WHERE f.teacherid = %s
ORDER BY f.createdat DESC
""", (user_id,))
feedback_rows = cur.fetchall()
# Convert tuples to dicts to ease templating with attribute access
feedbacks = []
for row in feedback_rows:
feedbacks.append({
'feedbackid': row[0],
'feedback': row[1],
'comments': row[2],
'createdat': row[3],
'student_name': row[4],
'quiz_title': row[5],
})
# List of quizzes created (with explicit column selection)
cur.execute("""
SELECT quizid, title, description, availablefrom, availableto,
attemptlimit, isdraft, class
FROM quizzes
WHERE createdby=%s
ORDER BY availablefrom DESC
""", (user_id,))
quiz_list = cur.fetchall()
quiz_list = sorted(quiz_list, key=lambda q: q[0], reverse=True)
# Recent student attempts (latest 5)
cur.execute("""
SELECT u.name, q.title, a.score, a.endtime
FROM attempts a
JOIN quizzes q ON a.quizid = q.quizid
JOIN users u ON a.studentid = u.userid
WHERE q.createdby = %s AND a.score IS NOT NULL
ORDER BY a.endtime DESC
LIMIT 5
""", (user_id,))
recent_attempts = cur.fetchall()
# Before closing the cursor in your route
if managed_class_list:
cur.execute("""
SELECT COUNT(*) FROM enrollments
WHERE approved IS NULL AND class = ANY(%s)
""", (managed_class_list,))
num_pending_enrollments = cur.fetchone()[0]
else:
num_pending_enrollments = 0
cur.close()
conn.close()
return render_template(
'teacher_dashboard.html',
name=teacher_name,
email=teacher_email,
role=teacher_role,
classy=classy,
managed_classes=managed_classes,
total_quizzes=total_quizzes,
active_quizzes=active_quizzes,
total_students=total_students,
avg_score=round(avg_score, 2),
quiz_list=quiz_list,
num_pending_enrollments=num_pending_enrollments,
grouped_attempts=grouped_attempts,
feedbacks=feedbacks,
recent_attempts=recent_attempts
)
@app.route('/admin_dashboard', methods=['GET', 'POST'])
def admin_dashboard():
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != "admin":
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Get admin details
cur.execute('SELECT name, email FROM users WHERE userid=%s', (user_id,))
admin_row = cur.fetchone()
name = admin_row[0]
email = admin_row[1]
# Handle form POST actions: approve, reject, deactivate, export
if request.method == 'POST':
if 'approveteacher' in request.form:
try:
teacher_id = int(request.form.get('approveteacher'))
cur.execute("UPDATE users SET approved = TRUE WHERE userid = %s", (teacher_id,))
conn.commit()
flash("Teacher approved successfully.", "success")
except Exception as e:
conn.rollback()
flash(f"Could not approve teacher: {str(e)}", "error")
elif 'rejectteacher' in request.form:
try:
teacher_id = int(request.form.get('rejectteacher'))
cur.execute("UPDATE users SET approved = FALSE WHERE userid = %s", (teacher_id,))
conn.commit()
flash("Teacher rejected and deactivated.", "warning")
except Exception as e:
conn.rollback()
flash(f"Could not reject teacher: {str(e)}", "error")
elif 'deactivateuser' in request.form:
try:
user_id_to_deactivate = int(request.form.get('deactivateuser'))
cur.execute("UPDATE users SET approved = FALSE WHERE userid = %s", (user_id_to_deactivate,))
conn.commit()
flash("User deactivated successfully.", "success")
except Exception as e:
conn.rollback()
flash(f"Could not deactivate user: {str(e)}", "error")
elif 'export_report' in request.form:
cur.execute("SELECT * FROM quizzes")
quizzes = cur.fetchall()
# Add CSV export logic here as needed
flash("Quiz report exported.", "success")
# Retrieve users excluding rejected for main user management list
cur.execute("SELECT userid, name, email, role, approved FROM users WHERE approved IS NULL OR approved = TRUE ORDER BY role, name")
users = cur.fetchall()
users = [dict(id=u[0], name=u[1], email=u[2], role=u[3], approved=u[4]) for u in users]
# Retrieve rejected teachers separately
cur.execute("SELECT userid, name, email, role FROM users WHERE role='teacher' AND approved = FALSE ORDER BY name")
rejected_teachers = cur.fetchall()
rejected_teachers = [dict(id=rt[0], name=rt[1], email=rt[2], role=rt[3]) for rt in rejected_teachers]
# System-wide stats
cur.execute("SELECT COUNT(*) FROM quizzes")
quizzes_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(DISTINCT class) FROM users WHERE class IS NOT NULL")
classes_count = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM attempts WHERE endtime > (CURRENT_DATE - INTERVAL '7 days')")
attempts_count = cur.fetchone()[0]
cur.close()
conn.close()
return render_template('admin_dashboard.html',
users=users,
rejected_teachers=rejected_teachers,
name=name,
email=email,
quizzes=quizzes_count,
classes=classes_count,
attempts=attempts_count)
@app.route('/export_quizzes', methods=['POST'])
def export_quizzes():
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != "admin":
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
SELECT q.quizid, q.title, q.description, q.class, q.availablefrom, q.availableto, u.name AS teacher
FROM quizzes q
LEFT JOIN users u ON q.createdby = u.userid
""")
quizzes = cur.fetchall()
cur.close()
conn.close()
# Create an Excel workbook and worksheet
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Quizzes"
# Write header
ws.append(['Quiz ID', 'Title', 'Description', 'Class', 'Available From', 'Available To', 'Teacher'])
# Write data
for row in quizzes:
ws.append(row)
# Prepare Excel file in memory
output = BytesIO()
wb.save(output)
output.seek(0)
return Response(
output,
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": "attachment;filename=quiz_report.xlsx"}
)
@app.route('/request_enrollment', methods=['POST'])
def request_enrollment():
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != 'student':
return redirect(url_for('login'))
requested_class = request.form.get('class_to_enroll').strip()
conn = get_db_connection()
cur = conn.cursor()
# Check if enrollment request already sent
cur.execute(
"SELECT * FROM enrollments WHERE studentid=%s AND class=%s AND approved=FALSE",
(user_id, requested_class)
)
exists = cur.fetchone()
if exists:
flash("You already have a pending enrollment request for this class.", "warning")
else:
cur.execute(
"INSERT INTO enrollments (studentid, class, approved) VALUES (%s, %s, FALSE)",
(user_id, requested_class)
)
conn.commit()
flash(f"Enrollment request submitted for class {requested_class}.", "success")
cur.close()
conn.close()
return redirect(url_for('student_dashboard'))
@app.route('/pending_enrollments', methods=['GET', 'POST'])
def pending_enrollments():
"""
Handles displaying pending enrollment requests for classes managed by the teacher,
and processing 'approve' or 'reject' actions.
"""
user_id = session.get('user_id')
role = session.get('role')
# 1. Authorization Check
if not user_id or role != 'teacher':
flash("You must be logged in as a teacher to view this page.", "error")
return redirect(url_for('login'))
conn = None
cur = None
requests = [] # Initialize requests list for safety
try:
conn = get_db_connection()
cur = conn.cursor()
# Get all classes this teacher manages
cur.execute("SELECT class FROM teacher_classes WHERE teacherid = %s", (user_id,))
classes = [row[0] for row in cur.fetchall()]
# Redirect if no assigned classes
if not classes:
flash("You are not assigned to any classes.", "error")
# The finally block will handle closing the connection/cursor
return redirect(url_for('teacher_dashboard'))
if request.method == 'POST':
# Input validation for POST request
enrollment_id_str = request.form.get('enrollment_id')
action = request.form.get('action')
if not enrollment_id_str or not action:
flash("Invalid request. Missing enrollment ID or action.", "error")
return redirect(url_for('pending_enrollments'))
try:
enrollment_id = int(enrollment_id_str)
except ValueError:
flash("Invalid enrollment ID format.", "error")
return redirect(url_for('pending_enrollments'))
# Check if the enrollment request is for one of the teacher's classes before proceeding
# This is a critical security check to prevent one teacher approving enrollments for another's class.
cur.execute("""
SELECT 1
FROM enrollments e
JOIN teacher_classes tc ON e.class = tc.class
WHERE e.enrollmentid = %s AND tc.teacherid = %s
""", (enrollment_id, user_id))
if not cur.fetchone():
flash("Enrollment request not found or does not belong to your class.", "error")
return redirect(url_for('pending_enrollments'))
if action == 'approve':
cur.execute("UPDATE enrollments SET approved = TRUE WHERE enrollmentid = %s", (enrollment_id,))
conn.commit()
flash("Enrollment approved.", "success")
elif action == 'reject':
cur.execute("UPDATE enrollments SET approved = FALSE WHERE enrollmentid = %s", (enrollment_id,))
conn.commit()
flash("Enrollment rejected.", "warning")
else:
flash("Invalid action specified.", "error")
# After POST, redirect to avoid form resubmission
# The finally block will close the connection
return redirect(url_for('pending_enrollments'))
# GET request: Fetch pending enrollments only for classes this teacher manages
# We use classes = ANY(%s) which is a standard PostgreSQL array check syntax
# when passing a Python list of strings (`classes`) to psycopg2.
if classes:
cur.execute("""
SELECT e.enrollmentid, u.userid, u.name, u.email, e.class
FROM enrollments e
JOIN users u ON e.studentid = u.userid
WHERE e.class = ANY(%s) AND e.approved IS NULL
""", (classes,))
# 'classes' is the list of class names
requests = cur.fetchall()
# Return the template with the pending requests
return render_template('pending_enrollments.html', requests=requests, classes=classes)
except psycopg2.Error as e:
# Catch and handle database exceptions
print(f"Database Error in pending_enrollments: {e}")
flash("An unexpected database error occurred. Please try again.", "error")
return redirect(url_for('teacher_dashboard'))
finally:
# 4. Ensure cleanup: Close the cursor and connection, even if an error occurred.
if cur:
cur.close()
if conn:
conn.close()
# ... (rest of your app.py content)
@app.route('/create_class', methods=['GET', 'POST'])
def create_class():
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != 'teacher':
return redirect(url_for('login'))
if request.method == 'POST':
class_name = request.form.get('class_name').strip()
subject = request.form.get('subject').strip()
conn = get_db_connection()
cur = conn.cursor()
# Check if the class name already exists
cur.execute("SELECT * FROM teacher_classes WHERE class = %s", (class_name,))
if cur.fetchone():
flash(f"Class '{class_name}' already exists. Please choose a different name.", "error")
cur.close()
conn.close()
return render_template('create_class.html')
else:
# DO NOT include "id" in this insert
cur.execute(
"INSERT INTO teacher_classes (teacherid, class, subject) VALUES (%s, %s, %s)",
(user_id, class_name, subject)
)
conn.commit()
flash(f"Class '{class_name}' created successfully!", "success")
cur.close()
conn.close()
return redirect(url_for('teacher_dashboard'))
return render_template('create_class.html')
@app.route('/manage_questions/<int:quiz_id>', methods=['GET', 'POST'])
def manage_questions(quiz_id):
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != 'teacher':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Fetch quiz details
cur.execute("""
SELECT quizid, createdby, title, description, difficulty,
availablefrom, availableto, attemptlimit, isdraft, class
FROM quizzes
WHERE quizid=%s AND createdby=%s
""", (quiz_id, user_id))
quiz = cur.fetchone()
if not quiz:
flash("Quiz not found or you don't have permission.", "error")
cur.close()
conn.close()
return redirect(url_for('teacher_dashboard'))
availableto = quiz[6]
quiz_ended = availableto is not None and datetime.now() > availableto
if request.method == 'POST':
if quiz_ended:
flash("Cannot add questions after the quiz end date.", "error")
cur.close()
conn.close()
return redirect(url_for('manage_questions', quiz_id=quiz_id))
# Add question logic...
question_text = request.form.get('question_text')
option_a = request.form.get('option_a')
option_b = request.form.get('option_b')
option_c = request.form.get('option_c')
option_d = request.form.get('option_d')
correct_option = request.form.get('correct_option')
difficulty = request.form.get('difficulty')
cur.execute("""
INSERT INTO questions (quizid, questiontext, optiona, optionb, optionc, optiond, correctoption, difficulty)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (quiz_id, question_text, option_a, option_b, option_c, option_d, correct_option, difficulty))
conn.commit()
flash("Question added successfully!", "success")
cur.close()
conn.close()
return redirect(url_for('manage_questions', quiz_id=quiz_id))
# Fetch all questions for this quiz
cur.execute("SELECT * FROM questions WHERE quizid=%s ORDER BY questionid", (quiz_id,))
questions = cur.fetchall()
cur.close()
conn.close()
return render_template('questions.html', quiz=quiz, questions=questions, quiz_id=quiz_id, quiz_ended=quiz_ended)
@app.route('/delete_question/<int:question_id>')
def delete_question(question_id):
user_id = session.get('user_id')
if not user_id or session.get('role') != 'teacher':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Get quiz_id before deleting
cur.execute("SELECT quizid FROM questions WHERE questionid=%s", (question_id,))
result = cur.fetchone()
if result:
quiz_id = result[0]
cur.execute("DELETE FROM questions WHERE questionid=%s", (question_id,))
conn.commit()
flash("Question deleted successfully!", "success")
cur.close()
conn.close()
return redirect(url_for('manage_questions', quiz_id=quiz_id))
@app.route('/attempt_quiz/<int:quiz_id>')
def attempt_quiz(quiz_id):
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != 'student':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Get quiz details - SELECT specific columns in known order
cur.execute("""
SELECT quizid, createdby, title, description, difficulty,
availablefrom, availableto, attemptlimit, isdraft, class
FROM quizzes
WHERE quizid=%s
""", (quiz_id,))
quiz = cur.fetchone()
if not quiz:
flash("Quiz not found.", "error")
return redirect(url_for('student_dashboard'))
# Now indices are:
# 0=quizid, 1=createdby, 2=title, 3=description, 4=difficulty,
# 5=availablefrom, 6=availableto, 7=attemptlimit, 8=isdraft, 9=class
# Check if student is enrolled in this class
cur.execute("SELECT 1 FROM enrollments WHERE studentid=%s AND class=%s AND approved=TRUE",
(user_id, quiz[9])) # quiz[9] is class
if not cur.fetchone():
flash("You are not enrolled in this class.", "error")
return redirect(url_for('student_dashboard'))
# Check number of attempts
cur.execute("SELECT COUNT(*) FROM attempts WHERE quizid=%s AND studentid=%s", (quiz_id, user_id))
attempt_count = cur.fetchone()[0]
cur.close()
conn.close()
return render_template('attempt_quiz.html', quiz=quiz, attempt_count=attempt_count, quiz_id=quiz_id)
# Check number of attempts
cur.close()
conn.close()
# Pass quiz tuple and other data to template
return render_template('attempt_quiz.html', quiz=quiz, attempt_count=attempt_count, quiz_id=quiz_id)
@app.route('/start_quiz/<int:quiz_id>')
def start_quiz(quiz_id):
user_id = session.get('user_id')
if not user_id or session.get('role') != 'student':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Create new attempt
cur.execute("""
INSERT INTO attempts (quizid, studentid, attemptno, starttime)
VALUES (%s, %s, (SELECT COALESCE(MAX(attemptno), 0) + 1 FROM attempts WHERE quizid=%s AND studentid=%s), NOW())
RETURNING attemptid
""", (quiz_id, user_id, quiz_id, user_id))
attempt_id = cur.fetchone()[0]
conn.commit()
cur.close()
conn.close()
# Redirect to take_quiz page with attempt_id
return redirect(url_for('take_quiz', attempt_id=attempt_id))
@app.route('/create_quiz', methods=['GET', 'POST'])
def create_quiz():
user_id = session.get('user_id')
role = session.get('role')
if not user_id or role != 'teacher':
return redirect(url_for('login'))
conn = get_db_connection()
cur = conn.cursor()
# Get teacher's classes
cur.execute("SELECT class, subject FROM teacher_classes WHERE teacherid=%s", (user_id,))
teacher_classes = cur.fetchall()