-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse.py
More file actions
110 lines (81 loc) · 3.33 KB
/
course.py
File metadata and controls
110 lines (81 loc) · 3.33 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
from database import connect_to_database
import traceback
def buy_course(student_id, course_id, amount):
db = connect_to_database()
cursor = db.cursor()
try:
# Check if the student has already bought the course
check_query = "SELECT * FROM progress WHERE student_id = %s AND course_id = %s"
cursor.execute(check_query, (student_id, course_id))
existing_progress = cursor.fetchone()
if existing_progress:
return f"Student has already bought this course."
# If not, add progress and update payment table
progress_query = "INSERT INTO progress (student_id, course_id, percentage_completed) VALUES (%s, %s, 0)"
cursor.execute(progress_query, (student_id, course_id))
payment_query = "INSERT INTO payment (student_id, amount, payment_date) VALUES (%s, %s, NOW())"
cursor.execute(payment_query, (student_id, amount))
db.commit()
return f"Course bought successfully!"
except Exception as e:
db.rollback()
return f"Error buying course: {str(e)}"
finally:
db.close()
def get_available_courses():
db = connect_to_database()
cursor = db.cursor()
try:
# Fetch all available courses
query = "SELECT * FROM course"
cursor.execute(query)
courses = cursor.fetchall()
return courses
except Exception as e:
return []
finally:
db.close()
def get_courses_by_domain(teacher_id):
db = connect_to_database()
cursor = db.cursor()
try:
# Fetch domain_id based on teacher_id
query_domain = "SELECT domain_id FROM teacher WHERE teacher_id = %s"
cursor.execute(query_domain, (teacher_id,))
domain_id = cursor.fetchone()[0]
# Fetch courses based on domain_id
query_courses = "SELECT * FROM course WHERE domain_id = %s"
cursor.execute(query_courses, (domain_id,))
courses = cursor.fetchall()
return courses
except Exception as e:
return []
finally:
db.close()
def delete_course(course_id, teacher_id):
db = connect_to_database()
cursor = db.cursor()
try:
# Fetch the domain_id associated with the teacher
query_domain = "SELECT domain_id FROM teacher WHERE teacher_id = %s"
cursor.execute(query_domain, (teacher_id,))
domain_id = cursor.fetchone()
if not domain_id:
return "Teacher not found."
# Check if the course is associated with the teacher's domain
check_association_query = "SELECT * FROM course WHERE course_id = %s AND domain_id = %s"
cursor.execute(check_association_query, (course_id, domain_id[0]))
association_exists = cursor.fetchone()
if association_exists:
# Delete the course from the course table
delete_course_query = "DELETE FROM course WHERE course_id = %s"
cursor.execute(delete_course_query, (course_id,))
db.commit()
return "Course deleted successfully!"
else:
return "Course is not associated with the teacher's domain."
except Exception as e:
db.rollback()
return f"Error deleting course: {str(e)}"
finally:
db.close()