-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
282 lines (254 loc) · 10.9 KB
/
models.py
File metadata and controls
282 lines (254 loc) · 10.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
import sqlite3
from datetime import datetime
DATABASE = 'lms.db'
def get_db():
"""Kết nối database"""
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def init_db():
"""Khởi tạo database với các bảng cần thiết"""
conn = get_db()
cursor = conn.cursor()
# Bảng users
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
full_name TEXT NOT NULL,
role TEXT NOT NULL CHECK(role IN ('admin', 'teacher', 'student')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Bảng courses
cursor.execute('''
CREATE TABLE IF NOT EXISTS courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
description TEXT,
teacher_id INTEGER,
color TEXT DEFAULT '#ec4899',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (teacher_id) REFERENCES users(id)
)
''')
# Bảng course_enrollments (sinh viên tham gia khóa học)
cursor.execute('''
CREATE TABLE IF NOT EXISTS course_enrollments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id INTEGER NOT NULL,
student_id INTEGER NOT NULL,
enrolled_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id),
FOREIGN KEY (student_id) REFERENCES users(id),
UNIQUE(course_id, student_id)
)
''')
# Bảng materials (học liệu)
cursor.execute('''
CREATE TABLE IF NOT EXISTS materials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id INTEGER NOT NULL,
title TEXT NOT NULL,
description TEXT,
file_path TEXT,
file_name TEXT,
uploaded_by INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id),
FOREIGN KEY (uploaded_by) REFERENCES users(id)
)
''')
# Bảng announcements (thông báo)
cursor.execute('''
CREATE TABLE IF NOT EXISTS announcements (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
author_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id)
)
''')
# Bảng forum_posts (diễn đàn)
cursor.execute('''
CREATE TABLE IF NOT EXISTS forum_posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
author_id INTEGER NOT NULL,
likes INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id)
)
''')
# Bảng forum_likes (lượt thích bài viết)
cursor.execute('''
CREATE TABLE IF NOT EXISTS forum_likes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES forum_posts(id),
FOREIGN KEY (user_id) REFERENCES users(id),
UNIQUE(post_id, user_id)
)
''')
# Bảng quizzes (bài kiểm tra)
cursor.execute('''
CREATE TABLE IF NOT EXISTS quizzes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_id INTEGER NOT NULL,
title TEXT NOT NULL,
description TEXT,
duration INTEGER DEFAULT 30,
max_score INTEGER DEFAULT 100,
show_results INTEGER DEFAULT 1,
show_correct_answers INTEGER DEFAULT 0,
results_published INTEGER DEFAULT 0,
created_by INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (course_id) REFERENCES courses(id),
FOREIGN KEY (created_by) REFERENCES users(id)
)
''')
# Thêm cột results_published nếu bảng đã tồn tại
try:
cursor.execute('ALTER TABLE quizzes ADD COLUMN results_published INTEGER DEFAULT 0')
except:
pass # Cột đã tồn tại
# Thêm cột show_correct_answers nếu bảng đã tồn tại
try:
cursor.execute('ALTER TABLE quizzes ADD COLUMN show_correct_answers INTEGER DEFAULT 0')
except:
pass # Cột đã tồn tại
# Thêm cột is_visible nếu bảng đã tồn tại
try:
cursor.execute('ALTER TABLE quizzes ADD COLUMN is_visible INTEGER DEFAULT 1')
except:
pass # Cột đã tồn tại
# Bảng quiz_questions (câu hỏi)
cursor.execute('''
CREATE TABLE IF NOT EXISTS quiz_questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
quiz_id INTEGER NOT NULL,
question_text TEXT NOT NULL,
question_type TEXT NOT NULL CHECK(question_type IN ('multiple_choice', 'essay')),
points INTEGER DEFAULT 10,
order_num INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (quiz_id) REFERENCES quizzes(id)
)
''')
# Bảng quiz_choices (đáp án trắc nghiệm)
cursor.execute('''
CREATE TABLE IF NOT EXISTS quiz_choices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question_id INTEGER NOT NULL,
choice_text TEXT NOT NULL,
is_correct INTEGER DEFAULT 0,
FOREIGN KEY (question_id) REFERENCES quiz_questions(id)
)
''')
# Bảng quiz_submissions (bài nộp)
cursor.execute('''
CREATE TABLE IF NOT EXISTS quiz_submissions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
quiz_id INTEGER NOT NULL,
student_id INTEGER NOT NULL,
score REAL DEFAULT 0,
status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'graded')),
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (quiz_id) REFERENCES quizzes(id),
FOREIGN KEY (student_id) REFERENCES users(id),
UNIQUE(quiz_id, student_id)
)
''')
# Bảng quiz_answers (câu trả lời)
cursor.execute('''
CREATE TABLE IF NOT EXISTS quiz_answers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
submission_id INTEGER NOT NULL,
question_id INTEGER NOT NULL,
choice_id INTEGER,
essay_answer TEXT,
points_earned REAL DEFAULT 0,
FOREIGN KEY (submission_id) REFERENCES quiz_submissions(id),
FOREIGN KEY (question_id) REFERENCES quiz_questions(id),
FOREIGN KEY (choice_id) REFERENCES quiz_choices(id)
)
''')
conn.commit()
# Thêm dữ liệu mẫu
cursor.execute("SELECT COUNT(*) FROM users")
if cursor.fetchone()[0] == 0:
# Tạo tài khoản mẫu
users_data = [
('admin', 'admin123', 'Quản trị viên', 'admin'),
('teacher1', 'teacher123', 'Nguyễn Văn A', 'teacher'),
('teacher2', 'teacher123', 'Trần Thị B', 'teacher'),
('student1', 'student123', 'Bùi Minh Sơn', 'student'),
('student2', 'student123', 'Nguyễn Thái Dương', 'student'),
]
cursor.executemany(
'INSERT INTO users (username, password, full_name, role) VALUES (?, ?, ?, ?)',
users_data
)
# Tạo khóa học mẫu
courses_data = [
('HIS1001_40', 'Lịch sử Đảng Cộng sản Việt Nam', 'Khóa học về lịch sử Đảng CSVN', 2, '#ec4899'),
('INT2210_44', 'Cấu trúc dữ liệu và giải thuật', 'Khóa học về CTDL>', 2, '#14b8a6'),
('INT3310_40', 'Quản trị mạng', 'Khóa học về quản trị mạng máy tính', 2, '#ec4899'),
('POL1001_42', 'Tư tưởng Hồ Chí Minh', 'Khóa học về tư tưởng HCM', 2, '#14b8a6'),
('INT3318E_40', 'Các thiết bị mạng và môi trường truyền', 'Khóa học về thiết bị mạng', 2, '#ec4899'),
('SPO1001_40', 'Golf', 'Khóa học Golf', 2, '#14b8a6'),
]
cursor.executemany(
'INSERT INTO courses (code, name, description, teacher_id, color) VALUES (?, ?, ?, ?, ?)',
courses_data
)
# Đăng ký sinh viên vào khóa học
enrollments_data = [
(1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4),
(1, 5), (2, 5), (3, 5),
]
cursor.executemany(
'INSERT INTO course_enrollments (course_id, student_id) VALUES (?, ?)',
enrollments_data
)
# Tạo học liệu mẫu
materials_data = [
(5, 'Bài thực hành trên lab 404', 'Ngày hôm nay thầy có việc bận nên các em lên phòng lab 404 để thực hành', '6.2.4_Packet_Tracer_-_Configure_EtherChannel.pka', '6.2.4_Packet_Tracer_-_Configure_EtherChannel.pka', 2),
(5, '6.4.1 Packet Tracer - Implement Etherchannel', 'Hôm nay lớp sẽ làm bài thực hành 6.4.1 trên phòng máy', 'CCNAv7_SRWE_Skills_Assessment.pka', 'CCNAv7_SRWE_Skills_Assessment.pka', 2),
(5, 'Test', 'test', 'Automobile_data.csv', 'Automobile_data.csv', 2),
]
cursor.executemany(
'INSERT INTO materials (course_id, title, description, file_name, file_path, uploaded_by) VALUES (?, ?, ?, ?, ?, ?)',
materials_data
)
# Tạo thông báo mẫu
announcements_data = [
('Hôm nay nghỉ nhé các bé', 'Hôm nay nghỉ nhé các bé', 4),
('TALKSHOW EXPLORE TECHNOLOGY WITH VIETCOMBANK NGÀY 06/12/2022', 'Thời gian chương trình: 8h00 - 11h00 ngày 06/12/2022 Địa điểm: Tầng 1, Hội trường SunWah, 144 Xuân Thủy, Cầu Giấy, Hà Nội', 4),
]
cursor.executemany(
'INSERT INTO announcements (title, content, author_id) VALUES (?, ?, ?)',
announcements_data
)
# Tạo bài viết diễn đàn mẫu
forum_data = [
('Câu hỏi về diễn đàn', 'Diễn đàn này được sử dụng cho các sinh viên trên toàn khóa ?', 4, 0),
('TestPost2', 'TestPost2', 4, 0),
('TestPost', 'TestPost', 5, 0),
]
cursor.executemany(
'INSERT INTO forum_posts (title, content, author_id, likes) VALUES (?, ?, ?, ?)',
forum_data
)
conn.commit()
conn.close()
if __name__ == '__main__':
init_db()
print("Database initialized successfully!")