-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
819 lines (689 loc) · 34 KB
/
admin.py
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
from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import sys
import string
import pandas as pd
import nltk
from nltk.corpus import stopwords
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import warnings
from collections import defaultdict
warnings.filterwarnings("ignore")
nltk.download("stopwords")
app = Flask(__name__)
app.secret_key = 'your_secret_key'
# Set the template folder
app.template_folder = 'templates'
# MySQL Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '' # Enter your MySQL password here
app.config['MYSQL_DB'] = 'teacher_part'
mysql = MySQL(app)
# Set English stopwords
EN_STOPWORDS = set(stopwords.words("english"))
# Preprocess text
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.sentiment import SentimentIntensityAnalyzer
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.naive_bayes import MultinomialNB
from sentence_transformers import SentenceTransformer
import language_tool_python
# nltk.download('punkt')
# nltk.download('wordnet')
# nltk.download('vader_lexicon')
def preprocess_text(text):
tokens = word_tokenize(text) # Tokenization
lemmatizer = WordNetLemmatizer()
lemmatized_tokens = [lemmatizer.lemmatize(token.lower()) for token in tokens] # Lemmatization
return lemmatized_tokens
# Exact Match function
def exact_match(expected_answer, student_answer):
return int(expected_answer == student_answer)
# Partial Match function
def partial_match(expected_answer, student_answer):
expected_tokens = preprocess_text(expected_answer)
student_tokens = preprocess_text(student_answer)
common_tokens = set(expected_tokens) & set(student_tokens)
match_percentage = len(common_tokens) / max(len(expected_tokens), len(student_tokens))
return match_percentage
# Cosine Similarity function
def cosine_similarity_score(expected_answer, student_answer):
vectorizer = TfidfVectorizer(tokenizer=preprocess_text)
tfidf_matrix = vectorizer.fit_transform([expected_answer, student_answer])
cosine_sim = cosine_similarity(tfidf_matrix[0], tfidf_matrix[1])[0][0]
return cosine_sim
# Sentiment Analysis function
def sentiment_analysis(text):
sia = SentimentIntensityAnalyzer()
sentiment_score = sia.polarity_scores(text)['compound']
return (sentiment_score + 1) / 2 # Normalize to range [0, 1]
# Function to calculate enhanced sentence match score using Semantic Similarity
def enhanced_sentence_match(expected_answer, student_answer):
model = SentenceTransformer('paraphrase-MiniLM-L6-v2') # Load pre-trained Sentence Transformer model
embeddings_expected = model.encode([expected_answer])
embeddings_student = model.encode([student_answer])
similarity = cosine_similarity([embeddings_expected.flatten()], [embeddings_student.flatten()])[0][0]
return similarity
# Function to calculate multinomial naive Bayes score
def multinomial_naive_bayes_score(expected_answer, student_answer):
# Convert answers to a list
answers = [expected_answer, student_answer]
# Initialize CountVectorizer
vectorizer = CountVectorizer(tokenizer=preprocess_text)
# Fit and transform the answers
X = vectorizer.fit_transform(answers)
# Labels
y = [0, 1] # 0 for expected_answer, 1 for student_answer
# Train Multinomial Naive Bayes classifier
clf = MultinomialNB()
clf.fit(X, y)
# Predict probabilities
probs = clf.predict_proba(X)
# Return the probability of the student's answer being correct
return probs[1][1] # Probability of the student's answer being class 1 (student_answer)
# Function to calculate weighted average score
def weighted_average_score(scores, weights):
weighted_sum = sum(score * weight for score, weight in zip(scores, weights))
total_weight = sum(weights)
return weighted_sum / total_weight
def semantic_similarity_score(expected_answer, student_answer):
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
embeddings_expected = model.encode([expected_answer])
embeddings_student = model.encode([student_answer])
similarity = cosine_similarity([embeddings_expected.flatten()], [embeddings_student.flatten()])[0][0]
return similarity
def coherence_score(expected_answer, student_answer):
len_expected = len(word_tokenize(expected_answer))
len_student = len(word_tokenize(student_answer))
coherence_score = min(len_expected, len_student) / max(len_expected, len_student)
return coherence_score
def relevance_score(expected_answer, student_answer):
expected_tokens = set(word_tokenize(expected_answer.lower()))
student_tokens = set(word_tokenize(student_answer.lower()))
common_tokens = expected_tokens.intersection(student_tokens)
relevance_score = len(common_tokens) / len(expected_tokens)
return relevance_score
def evaluate(expected, response):
if expected == response:
return 10
elif not response:
return 0
# Calculate scores
exact_match_score = exact_match(expected, response)
partial_match_score = partial_match(expected, response)
cosine_similarity_score_value = cosine_similarity_score(expected, response)
sentiment_score = sentiment_analysis(response)
enhanced_sentence_match_score = enhanced_sentence_match(expected, response)
multinomial_naive_bayes_score_value = multinomial_naive_bayes_score(expected, response)
# Additional evaluation criteria
semantic_similarity_value = semantic_similarity_score(expected, response)
coherence_value = coherence_score(expected, response)
relevance_value = relevance_score(expected, response)
# Weighted average calculation
scores = [exact_match_score, partial_match_score, cosine_similarity_score_value,
sentiment_score, enhanced_sentence_match_score, multinomial_naive_bayes_score_value,
semantic_similarity_value, coherence_value, relevance_value]
# Weights for evaluation criteria
weights = [0.15, 0.1, 0.1, 0.05, 0.1, 0.1, 0.1, 0.1, 0.1]
# Scale scores between 0 and 10
scaled_scores = [score * 10 for score in scores]
# Calculate final weighted average score
final_score = weighted_average_score(scaled_scores, weights)
rounded_score = round(final_score)
# Print individual scores
print("Exact Match Score:", exact_match_score)
print("Partial Match Score:", partial_match_score)
print("Cosine Similarity Score:", cosine_similarity_score_value)
print("Sentiment Score:", sentiment_score)
print("Enhanced Sentence Match Score:", enhanced_sentence_match_score)
print("Multinomial Naive Bayes Score:", multinomial_naive_bayes_score_value)
print("Semantic Similarity Score:", semantic_similarity_value)
print("Coherence Score:", coherence_value)
print("Relevance Score:", relevance_value)
marks = rounded_score # or use final_score if you want to return the exact value without rounding
return marks
# Admin login route
@app.route('/')
def index():
return render_template('Homepage.html')
@app.route('/admin/login', methods=['GET', 'POST'])
def admin_login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Admins WHERE username = %s AND password = %s", (username, password))
admin = cur.fetchone()
cur.close()
if admin:
session['admin_logged_in'] = True
return redirect(url_for('admin_home'))
else:
return render_template('adminlogin.html', error='Invalid username or password')
return render_template('adminlogin.html')
# Admin home route
@app.route('/admin/home')
def admin_home():
if 'admin_logged_in' in session:
return render_template('adminhome.html')
else:
return redirect(url_for('admin_login'))
# Admin students route
@app.route('/admin/students')
def admin_students():
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Students")
students = cur.fetchall()
cur.close()
return render_template('admin_students.html', students=students)
else:
return redirect(url_for('admin_login'))
# Add student route
@app.route('/admin/add_student', methods=['POST'])
def add_student():
if 'admin_logged_in' in session:
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO Students (username, password) VALUES (%s, %s)", (username, password))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_students'))
else:
return redirect(url_for('admin_login'))
# Update student route
@app.route('/admin/update_student/<int:student_id>', methods=['POST'])
def update_student(student_id):
if 'admin_logged_in' in session:
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("UPDATE Students SET username = %s, password = %s WHERE student_id = %s", (username, password, student_id))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_students'))
else:
return redirect(url_for('admin_login'))
# Delete student route
@app.route('/admin/delete_student/<int:student_id>', methods=['POST'])
def delete_student(student_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("DELETE FROM Students WHERE student_id = %s", (student_id,))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_students'))
else:
return redirect(url_for('admin_login'))
# View student scores route
# View student scores route
# View student scores route
@app.route('/admin/view_student_scores/<int:student_id>')
def view_student_scores(student_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
query = """
SELECT DISTINCT sa.answer_id, sa.test_id, t.test_name, q.question_text,
ea.answer_text AS expected_answer,
sa.answer_text AS student_answer, sa.score
FROM studentanswers sa
JOIN tests t ON sa.test_id = t.test_id
JOIN questions q ON sa.question_id = q.question_id
JOIN expectedanswers ea ON q.question_id = ea.question_id
WHERE sa.student_id = %s
ORDER BY sa.test_id, q.question_id;
"""
cur.execute(query, (student_id,))
scores = cur.fetchall()
cur.close()
# Convert the tuple of tuples to a list of dictionaries
scores = [{'answer_id': score[0], 'test_id': score[1], 'test_name': score[2],
'question_text': score[3], 'expected_answer': score[4],
'student_answer': score[5], 'score': score[6]} for score in scores]
return render_template('student_scores.html', scores=scores)
else:
return redirect(url_for('admin_login'))
@app.route('/admin/delete_student_score/<int:answer_id>', methods=['POST'])
def delete_student_score(answer_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
# Delete the score with the given answer_id
query = "DELETE FROM studentanswers WHERE answer_id = %s"
cur.execute(query, (answer_id,))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_students')) # Redirect to admin students page
else:
return redirect(url_for('admin_login'))
###############################################################
#############################Admin Teacher ####################
# Admin teachers route
@app.route('/admin/teachers')
def admin_teachers():
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Teachers")
teachers = cur.fetchall()
cur.close()
return render_template('admin_teachers.html', teachers=teachers)
else:
return redirect(url_for('admin_login'))
# Admin add teacher route
@app.route('/admin/add_teacher', methods=['GET', 'POST'])
def add_teacher():
if 'admin_logged_in' in session:
if request.method == 'POST':
username = request.form['username'] # Changed from 'name' to 'username'
password = request.form['password'] # Changed from 'email' to 'password'
cur = mysql.connection.cursor()
cur.execute("INSERT INTO Teachers (username, password) VALUES (%s, %s)", (username, password))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_teachers'))
else:
return render_template('add_teacher.html')
else:
return redirect(url_for('admin_login'))
@app.route('/admin/update_teacher/<int:teacher_id>', methods=['GET', 'POST'])
def update_teacher(teacher_id):
if 'admin_logged_in' in session:
if request.method == 'POST':
try:
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("UPDATE Teachers SET username = %s, password = %s WHERE teacher_id = %s", (username, password, teacher_id))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_teachers'))
except Exception as e:
print("Error updating teacher:", e)
# Handle error appropriately, such as displaying an error message to the user
else:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Teachers WHERE teacher_id = %s", (teacher_id,))
teacher = cur.fetchone()
cur.close()
if teacher:
return render_template('update_teacher.html', teacher=teacher, teacher_id=teacher_id)
else:
# Handle case where teacher with given ID is not found
return "Teacher not found"
else:
return redirect(url_for('admin_login'))
# Admin delete teacher route
@app.route('/admin/delete_teacher/<int:teacher_id>', methods=['POST'])
def delete_teacher(teacher_id):
if 'admin_logged_in' in session:
try:
cur = mysql.connection.cursor()
# Delete related records from teacherstudentrelationship table
cur.execute("DELETE FROM teacherstudentrelationship WHERE teacher_id = %s", (teacher_id,))
# Now, delete the teacher from the teachers table
cur.execute("DELETE FROM teachers WHERE teacher_id = %s", (teacher_id,))
mysql.connection.commit()
cur.close()
return redirect(url_for('admin_teachers'))
except Exception as e:
# Handle any exceptions
flash("An error occurred while deleting the teacher.")
print(e) # Print the exception for debugging purposes
return redirect(url_for('admin_teachers'))
else:
return redirect(url_for('admin_login'))
# Admin view teacher tests route
@app.route('/admin/view_teacher_tests/<int:teacher_id>')
def view_teacher_tests(teacher_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Tests WHERE teacher_id = %s", (teacher_id,))
tests = cur.fetchall()
cur.close()
return render_template('view_teacher_tests.html', tests=tests, teacher_id=teacher_id)
else:
return redirect(url_for('admin_login'))
# Admin view test questions route
@app.route('/admin/view_test_questions/<int:test_id>')
def view_test_questions(test_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Questions WHERE test_id = %s", (test_id,))
questions = cur.fetchall()
# Fetching expected answers for each question
question_answers = {}
for question in questions:
cur.execute("SELECT * FROM ExpectedAnswers WHERE question_id = %s", (question[0],))
answers = cur.fetchall()
question_answers[question[0]] = answers
cur.close()
# Pass test_id as teacher_id to the template
return render_template('view_test_questions.html', teacher_id=test_id, questions=questions, question_answers=question_answers)
else:
return redirect(url_for('admin_login'))
# Admin view question expected answers route
@app.route('/admin/view_question_answers/<int:question_id>')
def view_question_answers(question_id):
if 'admin_logged_in' in session:
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM ExpectedAnswers WHERE question_id = %s", (question_id,))
answers = cur.fetchall()
cur.close()
return render_template('view_question_answers.html', answers=answers)
else:
return redirect(url_for('admin_login'))
################################################
# Admin logout route
@app.route('/admin/logout')
def admin_logout():
session.pop('admin_logged_in', None)
return redirect(url_for('admin_login'))
################################################################################
###################################Teacher LOGIN######################
# Teacher login route
@app.route('/teacher_login', methods=['GET', 'POST'])
def teacher_login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Teachers WHERE username = %s AND password = %s", (username, password))
teacher = cur.fetchone()
cur.close()
if teacher:
session['teacher_logged_in'] = True
session['teacher_id'] = teacher[0] # Assuming teacher_id is the first column
return redirect(url_for('teacher_home'))
else:
return render_template('teacher_login.html', error='Invalid username or password')
return render_template('teacher_login.html')
# Teacher home route
@app.route('/teacher_home', methods=['GET', 'POST'])
def teacher_home():
if 'teacher_logged_in' in session:
if request.method == 'POST':
# Check if form was submitted for adding, updating, or deleting test name
if 'add_test_name' in request.form:
test_name = request.form['test_name']
# Add test name to the database
cur = mysql.connection.cursor()
cur.execute("INSERT INTO Tests (test_name, teacher_id) VALUES (%s, %s)", (test_name, session['teacher_id']))
mysql.connection.commit()
cur.close()
elif 'update_test_name' in request.form:
test_id = request.form['test_id']
updated_test_name = request.form['updated_test_name']
# Update test name in the database
cur = mysql.connection.cursor()
cur.execute("UPDATE Tests SET test_name = %s WHERE test_id = %s", (updated_test_name, test_id))
mysql.connection.commit()
cur.close()
elif 'delete_test_name' in request.form:
test_id = request.form['test_id']
try:
# Delete related student answers first
cur = mysql.connection.cursor()
cur.execute("DELETE FROM studentanswers WHERE test_id = %s", (test_id,))
mysql.connection.commit()
cur.close()
# Delete related expected answers
cur = mysql.connection.cursor()
cur.execute("DELETE FROM expectedanswers WHERE question_id IN (SELECT question_id FROM questions WHERE test_id = %s)", (test_id,))
mysql.connection.commit()
cur.close()
# Delete related questions
cur = mysql.connection.cursor()
cur.execute("DELETE FROM questions WHERE test_id = %s", (test_id,))
mysql.connection.commit()
cur.close()
# Now delete the test from the Tests table
cur = mysql.connection.cursor()
cur.execute("DELETE FROM tests WHERE test_id = %s", (test_id,))
mysql.connection.commit()
cur.close()
except Exception as e:
# Handle any exceptions
print("Error:", e)
# Fetch all tests for the current teacher
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Tests WHERE teacher_id = %s", (session['teacher_id'],))
tests = cur.fetchall()
cur.close()
return render_template('teacher_home.html', tests=tests)
else:
return redirect(url_for('teacher_login'))
# Teacher logout route
@app.route('/teacher_logout')
def teacher_logout():
session.pop('teacher_logged_in', None)
session.pop('teacher_id', None)
return redirect(url_for('teacher_login'))
######################teacherLOGOUT####################################
################################################################################
###############teacher FUNCTIONS ############################
@app.route('/teacher/view_test_questions/<int:test_id>', methods=['GET', 'POST'])
def view_teacher_test_questions(test_id):
if 'teacher_logged_in' in session:
if request.method == 'POST':
if 'add_question' in request.form:
question_text = request.form['question_text']
expected_answers = request.form.getlist('expected_answer')
cur = mysql.connection.cursor()
cur.execute("INSERT INTO Questions (question_text, test_id) VALUES (%s, %s)", (question_text, test_id))
question_id = cur.lastrowid
for answer in expected_answers:
cur.execute("INSERT INTO ExpectedAnswers (answer_text, question_id) VALUES (%s, %s)", (answer, question_id))
mysql.connection.commit()
cur.close()
elif 'delete_question' in request.form:
question_id = request.form['question_id']
cur = mysql.connection.cursor()
cur.execute("DELETE FROM ExpectedAnswers WHERE question_id = %s", (question_id,))
cur.execute("DELETE FROM Questions WHERE question_id = %s", (question_id,))
mysql.connection.commit()
cur.close()
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Questions WHERE test_id = %s", (test_id,))
questions = cur.fetchall()
question_answers = {}
for question in questions:
cur.execute("SELECT * FROM ExpectedAnswers WHERE question_id = %s", (question[0],))
answers = cur.fetchall()
question_answers[question[0]] = answers
cur.close()
return render_template('view_teacher_test_questions.html', teacher_id=test_id, questions=questions, question_answers=question_answers)
else:
return redirect(url_for('teacher_login'))
###### Teacher ( student marks section page) ################
@app.route('/teacher_view_score')
def teacher_view_score():
# Check if the user is logged in as a teacher
if 'teacher_logged_in' in session:
teacher_id = session['teacher_id']
# Fetch student answers and expected answers for the logged-in teacher's tests
cur = mysql.connection.cursor()
query = """
SELECT s.student_id, s.username AS student_username, t.test_name, q.question_text, ea.answer_text AS expected_answer, sa.answer_text AS student_answer, sa.score
FROM StudentAnswers sa
JOIN Students s ON sa.student_id = s.student_id
JOIN Tests t ON sa.test_id = t.test_id
JOIN Questions q ON sa.question_id = q.question_id
JOIN ExpectedAnswers ea ON q.question_id = ea.question_id
WHERE t.teacher_id = %s
"""
cur.execute(query, (teacher_id,))
results = cur.fetchall()
# Group the results by student_id and test_name
student_scores = defaultdict(lambda: {'student_username': None, 'tests': defaultdict(list)})
for result in results:
student_id, student_username, test_name, question_text, expected_answer, student_answer, score = result
student_scores[student_id]['student_username'] = student_username
student_scores[student_id]['tests'][test_name].append({
'question_text': question_text,
'expected_answer': expected_answer,
'student_answer': student_answer,
'score': score
})
return render_template('teacher_view_score.html', student_scores=student_scores)
else:
return redirect(url_for('teacher_login'))
##############################################################
######################## Student LOGIN #######################
@app.route('/student_login', methods=['GET', 'POST'])
def student_login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Students WHERE username = %s AND password = %s", (username, password))
student = cur.fetchone()
cur.close()
if student:
session['student_logged_in'] = True
session['student_id'] = student[0] # Assuming student_id is the first column
return redirect(url_for('student_home'))
else:
return render_template('student_login.html', error='Invalid username or password')
return render_template('student_login.html')
@app.route('/student_home')
def student_home():
if 'student_logged_in' in session:
return render_template('student_home.html')
else:
return redirect(url_for('student_login'))
@app.route('/student_logout')
def student_logout():
session.pop('student_logged_in', None)
session.pop('student_id', None)
return redirect(url_for('student_login'))
# Route for showing available tests and taking a test
@app.route('/student_take_test', methods=['GET', 'POST'])
def student_take_test():
if 'student_logged_in' in session:
if request.method == 'POST':
# Handle form submission (store student answers)
test_id = request.form.get('test_id') # Assuming you have a hidden input field for the test_id
student_id = session['student_id'] # Assuming you have stored student_id in the session
# Check if the student has already taken the test
if check_test_taken(student_id):
return redirect(url_for('student_view_score'))
# Loop through form data to retrieve answers for each question
for question_id, answer in request.form.items():
# Assuming input field names are in the format 'question_{question_id}'
if question_id.startswith('question_'):
question_id = int(question_id.split('_')[1])
# Store student answer in the StudentAnswers table
cur = mysql.connection.cursor()
cur.execute("INSERT INTO StudentAnswers (student_id, test_id, question_id, answer_text) VALUES (%s, %s, %s, %s)",
(student_id, test_id, question_id, answer))
mysql.connection.commit()
cur.close()
# Redirect the student after storing answers
return redirect(url_for('student_view_score'))
else:
# Fetch tests that the student has not taken yet
cur = mysql.connection.cursor()
cur.execute("""SELECT t.test_id, t.test_name
FROM Tests t
LEFT JOIN StudentAnswers sa ON t.test_id = sa.test_id AND sa.student_id = %s
WHERE sa.test_id IS NULL""", (session['student_id'],))
tests = cur.fetchall()
cur.close()
# Convert the list of tuples to a list of dictionaries
tests = [{'test_id': test[0], 'test_name': test[1]} for test in tests]
return render_template('student_take_test.html', tests=tests)
else:
return redirect(url_for('student_login'))
@app.route('/student_take_test/<int:test_id>', methods=['GET', 'POST'])
def student_take_test_questions(test_id):
if 'student_logged_in' in session:
if request.method == 'POST':
# Retrieve student ID from the session
student_id = session['student_id']
# Retrieve test ID from the route parameter
test_id = test_id
# Loop through form data to retrieve answers for each question
for question_id, answer in request.form.items():
# Assuming input field names are in the format 'question_{question_id}'
if question_id.startswith('question_'):
question_id = int(question_id.split('_')[1])
# Store student answer in the StudentAnswers table
cur = mysql.connection.cursor()
cur.execute("INSERT INTO StudentAnswers (student_id, test_id, question_id, answer_text) VALUES (%s, %s, %s, %s)",
(student_id, test_id, question_id, answer))
mysql.connection.commit()
cur.close()
# Redirect the student after storing answers
return redirect(url_for('student_home'))
else:
# Fetch test details and questions for the specified test from the database
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM Tests WHERE test_id = %s", (test_id,))
test = cur.fetchone()
cur.execute("SELECT * FROM Questions WHERE test_id = %s", (test_id,))
questions = cur.fetchall()
cur.close()
return render_template('student_take_test_questions.html', test=test, questions=questions, test_id=test_id)
else:
return redirect(url_for('student_login'))
@app.route('/student_view_score')
def student_view_score():
# Check if the user is logged in as a student
if 'student_logged_in' in session:
student_id = session['student_id']
# Fetch student answers, test names, questions, and expected answers for the logged-in student's tests
cur = mysql.connection.cursor()
query = """
SELECT t.test_id, t.test_name, q.question_text, ea.answer_text AS expected_answer, sa.answer_text AS student_answer
FROM StudentAnswers sa
JOIN Tests t ON sa.test_id = t.test_id
JOIN Questions q ON sa.question_id = q.question_id
JOIN ExpectedAnswers ea ON q.question_id = ea.question_id
WHERE sa.student_id = %s
"""
cur.execute(query, (student_id,))
results = cur.fetchall()
# Prepare the data to be displayed
student_scores = {}
for result in results:
test_id, test_name, question_text, expected_answer, student_answer = result
# Calculate score
score = evaluate(expected_answer, student_answer)
cur.execute("UPDATE studentanswers SET score = %s WHERE student_id = %s AND test_id = %s AND question_id IN (SELECT question_id FROM questions WHERE question_text = %s)", (score, student_id, test_id, question_text))
mysql.connection.commit()
# Check if test_id already exists in student_scores
if test_id not in student_scores:
student_scores[test_id] = {
'test_id': test_id,
'test_name': test_name,
'total_score': 0,
'max_score': 0,
'scores': []
}
student_scores[test_id]['scores'].append({
'question': question_text,
'expected_answer': expected_answer,
'student_answer': student_answer,
'score': score
})
score = evaluate(expected_answer, student_answer)
# Increment total score
student_scores[test_id]['total_score'] += score
# Increment max score
student_scores[test_id]['max_score'] += 10
# Format total score as "X / Y" for each test
for test_data in student_scores.values():
test_data['total_score'] = f"{test_data['total_score']} / {test_data['max_score']}"
return render_template('student_view_score.html', student_scores=student_scores.values())
else:
return redirect(url_for('student_login'))
###############################################
#####################algorithm#################
###########################################
if __name__ == '__main__':
app.run(debug=True)