-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
766 lines (707 loc) · 27.9 KB
/
Copy pathbackend.py
File metadata and controls
766 lines (707 loc) · 27.9 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
import sqlite3
import hashlib
from sqlite3.dbapi2 import TimeFromTicks, connect
## Checks if given uid exists in table USERS
def checkUidExists(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM USERS WHERE UID = ?", (str(uid),))
if cursor.fetchone() == None:
print("No user with given uid found")
return False
else:
print("User found")
return True
def getUid(username):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT UID FROM USERS WHERE USERNAME = ?", (str(username),))
result = cursor.fetchone()
conn.close()
if result == None:
print("No user with given username found")
else:
print("Uid found")
return result[0]
## Checks if given classid exists in table CLASSES
def checkClassIdExists(classid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM CLASSES WHERE CLASSID = ?", (str(classid),))
if cursor.fetchone() == None:
print("No class with given classid found")
return False
else:
print("Class found")
return True
## Checks if given testid exists in table TESTS
def checkTestIdExists(testid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM TESTS WHERE TESTID = ?", (str(testid),))
if cursor.fetchone() == None:
print("No test with given testid found")
return False
else:
print("Test found")
return True
## Checks if given classid exists in table CLASSES
def checkTestNameExists(testname):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM TESTS WHERE NAME= ?", (str(testname),))
if cursor.fetchone() == None:
print("No test with given testname found")
return False
else:
print("Test found")
return True
## returns classid of given classname
def getClassID(classname):
conn = sqlite3.connect("GradingSystemDB.db")
try:
cursor = conn.execute("SELECT * FROM CLASSES WHERE NAME = ?", (str(classname),))
conn.close()
return cursor.fetchone()[0]
print("Retrieved classid for given classname")
except:
print("Given Classname does not exist in CLASSES")
## checks what role the given uid has
def checkUserRole(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT ROLE FROM USERS WHERE UID = ?", (str(uid),))
role = cursor.fetchone()[0]
return role
## returns list of dictionaries containing one user each
def fetchAllUsers():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute('''SELECT * FROM USERS;''')
responseList = []
for row in cursor:
tempdict = {
"UID": row[0],
"Forename": row[1],
"Surname": row[2],
"Role": row[3],
"Username": row[4],
}
responseList.append(tempdict)
conn.close()
print("Fetched ALL from STUDENTS")
return responseList
## adds user with given variables to table USERS
def addUserToDB(forename, surname, role, username, password):
pwhash = generatePWHash(password)
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("INSERT INTO USERS (FORENAME, SURNAME, ROLE, USERNAME, PWHASH) \
VALUES (?, ?, ?, ?, ?)", (str(forename), str(surname), str(role), str(username), str(pwhash),))
conn.commit()
conn.close()
print("Added user to USERS")
## Helper for addUserToDB: returns hexcode of hash of password given
def generatePWHash(password):
pwhash = hashlib.md5(password.encode()).hexdigest()
return pwhash
def getPWHash(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT PWHASH FROM USERS WHERE UID = ?", (str(uid),))
result = cursor.fetchone()
conn.close()
if result == None:
print("No user with given uid found")
else:
print("Uid found")
return result[0]
## changes given attribute to value for given uid
def changeUserAttribute(uid, attribute, value):
if checkUidExists(uid) == False:
return
if attribute == "Role":
print("Roll cannot be changed")
return
if attribute == "UID":
print("UID cannot be changed")
return
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("UPDATE USERS SET "+str(attribute)+"= ? WHERE UID = ?", (str(value), str(uid),))
conn.commit()
conn.close()
## removes entry of given uid from table USERS
## if role == Teacher, check if they are assigned to at least one subject, if not remove from USERS
## if role == Student, remove all tests of student, remove student from CLASSATTENDANCES, remove student from USERS
def removeUserFromDB(uid):
if checkUidExists(uid) == False:
return
conn = sqlite3.connect("GradingSystemDB.db")
role = checkUserRole(uid)
if role == "Teacher":
if checkTeacherAssigned(uid) == True:
conn.close()
print("Teacher is assigned to at least one Subject and cannot be removed")
else:
conn.execute("DELETE FROM USERS WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("Teacher removed from DB")
elif role == "Student":
purgeStudentTests(uid)
removeUserFromClassByUid(uid)
conn.execute("DELETE FROM USERS WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("Student and relevant tests and classattendance removed from DB")
else:
conn.execute("DELETE FROM USERS WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("User/Admin removed from DB")
## Helper for removeUserFromDB: check if given uid is a teacher and if so, check if they are assigned to at least one subject
def checkTeacherAssigned(uid):
conn = sqlite3.connect("GradingSystemDB.db")
role = checkUserRole(uid)
if role == "Teacher":
cursor = conn.execute("SELECT SUBJID FROM SUBJECTS WHERE UID = ?", (str(uid),))
if cursor.fetchone() == None:
print("Teacher Assigned: False")
conn.close()
return False
else:
print("Teacher Assigned: True")
conn.close()
return True
else:
print("User is not a Teacher")
conn.close()
return False
## Helper for removeUserFromDB: check if given uid is a student, if so remove their tests from TESTS
def purgeStudentTests(uid):
conn = sqlite3.connect('GradingSystemDB.db')
role = checkUserRole(uid)
if role == "Student":
conn.execute("DELETE FROM TESTS WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("Removed tests related to uid")
else:
conn.close()
print("Given uid doesn't belong to a Student")
## Fetches all entries from Classes, returns list of dictionaries
def fetchAllClasses():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute('''SELECT * FROM CLASSES;''')
responseList = []
for row in cursor:
tempdict = {
"ClassID": row[0],
"Name": row[1],
}
responseList.append(tempdict)
conn.close()
print("Fetched ALL from CLASSES")
return responseList
## Fetches all entries from Classes, returns list of dictionaries
def fetchAllClassAttendances():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute('''SELECT * FROM CLASSATTENDANCES;''')
responseList = []
for row in cursor:
tempdict = {
"ClassID": row[0],
"Uid": row[1],
}
responseList.append(tempdict)
conn.close()
print("Fetched ALL from CLASSATTENDANCES")
return responseList
## adds given class name to CLASSES
def addClassToDB(classname):
if checkClassNameTaken(classname) == False:
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("INSERT INTO CLASSES (NAME) \
VALUES (?)", (str(classname),))
conn.commit()
conn.close()
print("Added class to CLASSES")
else:
print("Class not added because classname already taken")
## Helper for addClassToDB: checks if given classname is already taken
def checkClassNameTaken(classname):
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute("SELECT * FROM CLASSES WHERE NAME = ?", (str(classname),))
if cursor.fetchone() == None:
print("Given Classname not taken")
return False
else:
print("Given Classname already taken")
return True
## changes attribute for given classid to value
def changeClassName(classid, name):
if checkClassIdExists(classid) == False:
return
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("UPDATE CLASSES SET NAME = ? WHERE CLASSID = ?", (str(name), str(classid)))
conn.commit()
conn.close()
print("Changed name to given value for given classid")
## adds entry of given uid to CLASSATTENDANCES for given classid
def addUserToClass(uid, classid):
if checkUidExists(uid) == False:
return
if checkClassIdExists(classid) == False:
return
if checkUserRole(uid) == "Student":
userassigned = checkUserAssignedClass(uid)
if userassigned != None:
removeUserFromClassByUid(uid)
conn = sqlite3.connect('GradingSystemDB.db')
conn.execute("INSERT INTO CLASSATTENDANCES (CLASSID, UID) \
VALUES (?, ?)", (str(classid), str(uid),))
conn.commit()
conn.close()
print("Added user to relevant class")
else:
print("Uid doesn't belong to a student")
## Helper for addUserToClass: Checks whether Student is already assigned to a class
def checkUserAssignedClass(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT CLASSID FROM CLASSATTENDANCES WHERE UID = ?", (str(uid),))
classid = cursor.fetchone()
conn.close()
if classid == None:
print("Student isn't assigned to any class")
return classid
else:
print("Student assigned to class, returning classid")
return classid[0]
## removes entry of given uid from CLASSATTENDANCES, archives subjects with tests associated with the user
def removeUserFromClassByUid(uid):
if checkUidExists(uid) == False:
return
if checkUserRole(uid) == "Student":
archiveSubjectsOfRemovedUser(uid)
conn = sqlite3.connect('GradingSystemDB.db')
conn.execute("DELETE FROM CLASSATTENDANCES WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("Student removed from relevant class")
else:
print("Uid doesn't belong to Student")
## Helper for removeUserFromClassByUid: checks if user is assigned to class, creates archived copy of subject with tests of user
def archiveSubjectsOfRemovedUser(uid):
subjids = getStudentSubjects(uid)
print(subjids)
for subjid in subjids:
if originalHasTestsAssigned(uid, subjid) == True:
copysubjid = createCopyOfSubject(subjid, uid)
redirectTests(uid, subjid, copysubjid)
## Helper for archiveSubjectsofRemovedUser: retrieves all relevant subjids for given uid
def getStudentSubjects(uid):
conn = sqlite3.connect("GradingSystemDB.db")
classid = checkUserAssignedClass(uid)
cursor = conn.execute("SELECT SUBJID FROM SUBJECTS WHERE CLASSID = ?", (str(classid),))
results = cursor.fetchall()
conn.close()
subjids = []
for result in results:
subjid = result[0]
subjids.append(subjid)
print("Retrieved relevant subjects for uid")
return subjids
## Helper of archiveSubjectsofRemovedUser: redirect tests for given uid to given subjid
def redirectTests(uid, subjid, copysubjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT TESTID FROM TESTS WHERE UID = ? AND SUBJID = ?", (str(uid), str(subjid)))
results = cursor.fetchall()
for result in results:
testid = result[0]
print("test id = " + str(testid))
conn.execute("UPDATE TESTS SET SUBJID = ? WHERE TESTID = ?", (str(copysubjid), str(testid)))
conn.commit()
conn.close()
print("Redirected Tests of given uid"+ str(uid) +"to given subjid" + str(subjid))
## Helper of archiveSubjetcsofRemovedUser: checks whether original subject had tests, returns list of testids
def originalHasTestsAssigned(uid, subjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT TESTID FROM TESTS WHERE SUBJID = ? AND UID = ?", (str(subjid), str(uid)))
results = cursor.fetchone()
# resultlist = []
# for result in results:
# resultlist.append(result[0])
if results != None:
print("original test assigned: True")
return True
else:
print("original test assigned: False")
return False
## Helper of archiveSubjectsofRemovedUser: create archived copy of given subjid's entry in SUBJECTS, returns subjid of said subject
def createCopyOfSubject(subjid, uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM SUBJECTS WHERE SUBJID = ?", (str(subjid),))
result = cursor.fetchall()
result = result[0]
name = result[1] + createUniqueSubjName(uid)
classid = result[2]
uid = result[3]
conn.execute("INSERT INTO SUBJECTS (NAME, CLASSID, UID, ARCHIVED) \
VALUES (?, ?, ?, ?)", (str(name), str(classid), str(uid), str(1)))
print("Created copy of given subject")
cursor = conn.execute("SELECT SUBJID FROM SUBJECTS WHERE NAME = ? AND ARCHIVED = 1", (str(name),))
result = cursor.fetchall()
conn.commit()
conn.close()
print("Retrieved subjid of created copy")
return result[0][0]
## helper of createCopyOfSubject: retrieves StudentName of given uid, generates unique subject name
def createUniqueSubjName(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT FORENAME, SURNAME FROM USERS WHERE UID = ?", (str(uid),))
result = cursor.fetchall()
namestring = result[0][0] + result[0][1] + " class: " + str(checkUserAssignedClass(uid))
print("Created unique namestring")
return namestring
## removes given classid from CLASSES
def removeClassFromDB(classid):
if checkClassIdExists == False:
return
removeUserFromClassByClassId(classid)
disposeOfSubject(None, classid)
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("DELETE FROM CLASSES WHERE CLASSID = ?", (str(classid),))
conn.commit()
conn.close()
print("Removed entry of given classid from CLASSES")
## Helper for removeClassFromDB: removes entries of uids associated with classid from CLASSATTENDANCES
def removeUserFromClassByClassId(classid):
for uid in getUidByClassId(classid):
# archiveSubjectsOfRemovedUser(uid)
conn = sqlite3.connect('GradingSystemDB.db')
conn.execute("DELETE FROM CLASSATTENDANCES WHERE UID = ?", (str(uid),))
conn.commit()
conn.close()
print("Student removed from relevant class")
## helper of removeUserFromClassByClassId: returns all uids associated with classid
def getUidByClassId(classid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT UID FROM CLASSATTENDANCES WHERE CLASSID = ?", (str(classid)))
results = cursor.fetchall()
uidlist = []
for result in results:
uid = result[0]
uidlist.append(uid)
conn.close()
print("Retrieved list of subjids associated with classid")
return uidlist
## returns all subjects as list of dictionaries
def fetchAllSubjects():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute('''SELECT * FROM SUBJECTS;''')
responseList = []
for row in cursor:
tempdict = {
"SubjID": row[0],
"Name": row[1],
"ClassID": row[2],
"UID": row[3],
"Archived": row[4]
}
responseList.append(tempdict)
conn.close()
print("Fetched ALL from Subjects")
return responseList
# adds subject with given parameters to SUBJECTS
def addSubjectToDB(name, classid, uid, archived):
if checkClassIdExists(classid) == False:
print("Subject could not be created: Class does not exist")
return
if checkUidExists(uid) == False:
print("Subject could not be created: User does not exist")
return
if checkSubjectNameTaken(name, classid) == True:
return
if checkUserRole(uid) != "Teacher":
print("Given User is not a Teacher")
return
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("INSERT INTO SUBJECTS (NAME, CLASSID, UID, ARCHIVED) \
VALUES (?, ?, ?, ?)", (str(name), str(classid), str(uid), str(archived)))
conn.commit()
conn.close()
print("Added subject to SUBJECTS")
## Helper for addClassToDB: checks if given subject name is already taken
def checkSubjectNameTaken(subjectname, classid):
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute("SELECT * FROM SUBJECTS WHERE NAME = ? AND CLASSID = ?", (str(subjectname), str(classid)))
if cursor.fetchone() == None:
print("Given Subjectname not taken for given class")
return False
else:
print("Given Subjectname already taken for given class")
return True
## changes attribute to value for given subjid
def changeSubjectAttribute(subjid, attribute, value):
if checkSubjIdExists(subjid) == False:
return
if checkSubjectArchived(subjid) == True:
print("No further changes can be made to archived subject")
return
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("UPDATE SUBJECTS SET "+str(attribute)+"= ? WHERE SUBJID = ?", (str(value), str(subjid),))
conn.commit()
conn.close()
print("Changed attribute to value for given subjid")
## Helper of changeSubjectAttribute: Checks if given subjid exists in table SUBJECTS
def checkSubjIdExists(subjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM SUBJECTS WHERE SUBJID = ?", (str(subjid),))
if cursor.fetchone() == None:
print("No subject with given subjid found")
conn.close()
return False
else:
print("Subject found")
conn.close()
return True
## Helper of changeSubjectAttribute: Checks if given subjid has archived flag enabled
def checkSubjectArchived(subjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT ARCHIVED FROM SUBJECTS WHERE SUBJID = ?", (str(subjid),))
result = cursor.fetchone()[0]
conn.close()
if result == 0:
print("Subject not archived")
return False
else:
print("Subject archived")
return True
## either removes or archives subject, or subjects if classid is given
def disposeOfSubject(subjid = None, classid = None):
if subjid != None and classid == None:
if checkSubjIdExists(subjid) == False:
return
removeOrArchiveSubjectFromDB(subjid)
elif subjid == None and classid != None:
subjids = getSubjIdByClassId(classid)
for subjid in subjids:
if checkSubjIdExists(subjid) == False:
continue
removeOrArchiveSubjectFromDB(subjid)
else:
print("Incorrect input, XOR required")
## Helper of disposeOfSubject: returns list of subjids for given classids
def getSubjIdByClassId(classid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT SUBJID FROM SUBJECTS WHERE CLASSID = ?", (str(classid)))
results = cursor.fetchall()
subjidlist = []
for result in results:
subjid = result[0]
subjidlist.append(subjid)
conn.close()
print("Retrieved list of subjids associated with classid")
return subjidlist
## Helper for disposeOfSubject: removes entry of given subjid from SUBJECTS
def removeOrArchiveSubjectFromDB(subjid):
if checkTestsAssigned(subjid) == False:
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("DELETE FROM SUBJECTS WHERE SUBJID = ?", (str(subjid),))
conn.commit()
conn.close()
print("Removed entry of given subjid from SUBJECTS")
if checkTestsAssigned(subjid) == True:
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("UPDATE SUBJECTS SET ARCHIVED = 1 WHERE SUBJID = ?", (str(subjid),))
conn.commit()
conn.close()
print("Subject archived")
# Helper for removeOrArchiveSubjectFromDB: checks if tests are assigned to given subject
def checkTestsAssigned(subjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT * FROM TESTS WHERE SUBJID = ?", (str(subjid),))
if cursor.fetchone() == None:
print("Tests Assigned: False")
conn.close()
return False
else:
print("Tests Assigned: True")
conn.close()
return True
## returns all subjects as list of dictionaries
def fetchAllTests():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute('''SELECT * FROM TESTS;''')
responseList = []
for row in cursor:
tempdict = {
"TestID": row[0],
"Name": row[1],
"Date": row[2],
"SubjId": row[3],
"Uid": row[4],
"Grade": row[5]
}
responseList.append(tempdict)
conn.close()
print("Fetched ALL from Subjects")
return responseList
## returns list of dictionaries containing user info as well as their average grades
def fetchAllPupilsAvgGrades():
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute("SELECT * FROM USERS WHERE ROLE = ?", ("Student",))
results = cursor.fetchall()
resultlist = []
print(results)
for row in results:
tempdict = {
"UID": row[0],
"Forename": row[1],
"Surname": row[2],
"Average Grades": fetchAvgGradesAllSubjects(row[0])
}
resultlist.append(tempdict)
conn.close()
print("Fetched ALL from Subjects")
return resultlist
# Helper of fetchAllPupilsAvgGrades: fetches all grades associated with given uid, returns average
def fetchAvgGradesAllSubjects(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT GRADE FROM TESTS WHERE UID = ?", (str(uid),))
results = cursor.fetchall()
conn.close()
gradesacc = 0
divisor = 0
for row in results:
gradesacc += row[0]
divisor += 1
try:
print("Average grade calculated")
return (gradesacc / divisor)
except:
print("Student has no grades")
return 0
# Adds Test with given values to TESTS
def addTestToDB(name, date, subjid, uid, grade):
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("INSERT INTO TESTS (NAME, DATE, SUBJID, UID, GRADE) \
VALUES (?, ?, ?, ?, ?)", (str(name), str(date), str(subjid), str(uid), str(grade)))
conn.commit()
conn.close()
print("Added test to TESTS")
## changes attribute to value for given testid
def changeTestAttributeByTestId(testid, attribute, value):
if checkTestIdExists(testid) == False:
return
conn = sqlite3.connect("GradingSystemDB.db")
conn.execute("UPDATE TESTS SET "+str(attribute)+"= ? WHERE TESTID = ?", (str(value), str(testid),))
conn.commit()
conn.close()
print("Changed attribute to value for given testid")
## returns users and their grades for given test name
def fetchAllPupilsAndGradesForTest(name):
conn = sqlite3.connect('GradingSystemDB.db')
cursor = conn.execute("SELECT UID, TESTID, GRADE FROM TESTS WHERE NAME = ?", (str(name),))
results = cursor.fetchall()
resultlist = []
for row in results:
tempdict = {
"UID": row[0],
"TESTID": row[1],
"Forename": getUserName(row[0])[0],
"Surname": getUserName(row[0])[1],
"Grade:": row[2]
}
resultlist.append(tempdict)
conn.close()
print("Fetched Users and grades for given test")
return resultlist
## Helper of fetchAllPupilsAndGradesForTest: returns forename, surname for given uid
def getUserName(uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT FORENAME, SURNAME FROM USERS WHERE UID = ?", (str(uid),))
results = cursor.fetchall()
return [results[0][0], results[0][1]]
## changes grade for given uid and testid in TESTS
def editGradeOfPupilInTest(uid, testid, grade):
if checkUidExists(uid) == False:
return
if checkTestIdExists(testid) == False:
return
conn = sqlite3.connect('GradingSystemDB.db')
conn.execute("UPDATE TESTS SET GRADE = ? WHERE UID = ? AND TESTID = ?", (str(grade), str(uid), str(testid)))
conn.commit()
conn.close()
print("Changed grade of given student for given test")
## removes all relevant entries of given test from TESTS
def removeTestFromDB(name):
conn = sqlite3.connect('GradingSystemDB.db')
conn.execute("DELETE FROM TESTS WHERE NAME = ?", (str(name),))
conn.commit()
conn.close()
print("Removed all relevant entries of given test from TESTS")
## Retrieves Average grades for all subjects of given uid
def fetchAllSubjectsAndAvgGradesOneStudent(uid):
if checkUidExists(uid) == False:
return
if checkUserRole(uid) != "Student":
print("Given user is not a Student, returning")
return
subjids = getStudentSubjects(uid)
resultlist = []
for subjid in subjids:
tempdict = {
"SubjID": subjid,
"Subject": getSubjectName(subjid),
"Average Grade": fetchAvgGradesOneSubjectOneStudent(subjid, uid)
}
resultlist.append(tempdict)
print("Retrieved Average grades for all subjects of given uid")
return resultlist
## Helper of fetchAllSubjectsAndAVgGradesOneStudent: retrieves subjectname for given subjid from SUBJECTS
def getSubjectName(subjid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT NAME FROM SUBJECTS WHERE SUBJID = ?", (str(subjid),))
result = cursor.fetchall()[0][0]
conn.close()
print("Fetched Subject name for given subjid")
return result
## Helper for fetchAllSubjectsAndAvgGradesOneStudent: calculates average grade for given subject for given student
def fetchAvgGradesOneSubjectOneStudent(subjid, uid):
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT GRADE FROM TESTS WHERE SUBJID = ? AND UID = ?", (str(subjid), str(uid)))
results = cursor.fetchall()
gradesacc = 0
divisor = 0
for row in results:
gradesacc += row[0]
divisor += 1
try:
print("Average grade calculated")
return(gradesacc / divisor)
except:
print("Student has no grades")
return 0
## Retrieves all tests and grades of given subjid for given uid
def fetchAllTestsAndGradesOneSubjectOneStudent(subjid, uid):
if checkUidExists(uid) == False:
return
if checkSubjIdExists(subjid) == False:
return
if checkUserRole(uid) != "Student":
print("Given user not a student, returning")
return
conn = sqlite3.connect("GradingSystemDB.db")
cursor = conn.execute("SELECT TESTID, NAME, GRADE FROM TESTS WHERE SUBJID = ? AND UID = ?", (str(subjid), str(uid)))
results = cursor.fetchall()
conn.close()
resultlist = []
for row in results:
tempdict = {
"TestID": row[0],
"Test": row[1],
"Grade": row[2]
}
resultlist.append(tempdict)
print("Retrieved all tests and grades of given subject for given student")
return resultlist
# removeUserFromDB(1)
# addUserToDB("Lucia", "Wuckert", "Student", "lumawu", "password")
# addUserToDB("Kenneth", "Otto", "Admin", "kotto", "password")
# addUserToClass(14, 1)
# addTestToDB("ubertest 1", "2020-01-01", 1, 14, 2.5)
# addTestToDB("ubertest 2", "2020-01-02", 1, 14, 5.0)
# addTestToDB("ubertest 3", "2020-01-03", 2, 14, 3.5)
# addTestToDB("ubertest 4", "2020-01-04", 2, 14, 1)