-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.sql
More file actions
212 lines (190 loc) · 9.29 KB
/
Copy pathinit.sql
File metadata and controls
212 lines (190 loc) · 9.29 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
-- Stimma Database Initialization
-- Character set and collation for Swedish
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;
USE stimma;
-- Tabellstruktur `categories`
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` text DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`icon` varchar(255) DEFAULT NULL,
`sort_order` int(11) DEFAULT 0,
`status` enum('active','inactive') DEFAULT 'active',
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `users`
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(150) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`verification_token` varchar(64) DEFAULT NULL,
`verified_at` datetime DEFAULT NULL,
`last_login_at` datetime DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`is_admin` tinyint(1) DEFAULT 0,
`is_editor` tinyint(1) DEFAULT 0,
`role` enum('student','teacher','admin','super_admin') DEFAULT 'student',
`preferences` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`preferences`)),
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `courses`
CREATE TABLE IF NOT EXISTS `courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`description` text DEFAULT NULL,
`difficulty_level` enum('beginner','intermediate','advanced') DEFAULT 'beginner',
`duration_minutes` int(11) DEFAULT 0,
`prerequisites` text DEFAULT NULL,
`tags` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`tags`)),
`image_url` varchar(255) DEFAULT NULL,
`status` enum('active','inactive') DEFAULT 'active',
`sort_order` int(11) DEFAULT 0,
`featured` tinyint(1) DEFAULT 0,
`author_id` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
KEY `courses_ibfk_1` (`category_id`),
KEY `courses_ibfk_2` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `lessons`
CREATE TABLE IF NOT EXISTS `lessons` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`estimated_duration` int(11) DEFAULT 5,
`image_url` varchar(255) DEFAULT NULL,
`video_url` varchar(255) DEFAULT NULL,
`video_position` enum('top','bottom') NOT NULL DEFAULT 'bottom',
`content` text DEFAULT NULL,
`resource_links` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`resource_links`)),
`tags` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`tags`)),
`status` enum('active','inactive') DEFAULT 'active',
`sort_order` int(11) DEFAULT 0,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`ai_instruction` text DEFAULT NULL,
`ai_prompt` text DEFAULT NULL,
`quiz_question` text DEFAULT NULL,
`quiz_answer1` text DEFAULT NULL,
`quiz_answer2` text DEFAULT NULL,
`quiz_answer3` text DEFAULT NULL,
`quiz_correct_answer` tinyint(4) DEFAULT NULL,
`author_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`),
KEY `lessons_ibfk_2` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `logs`
CREATE TABLE IF NOT EXISTS `logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`message` text NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
KEY `idx_email` (`email`),
KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `progress`
CREATE TABLE IF NOT EXISTS `progress` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`lesson_id` int(11) DEFAULT NULL,
`status` enum('not_started','completed') DEFAULT 'not_started',
`started_at` timestamp NULL DEFAULT NULL,
`completion_time` int(11) DEFAULT NULL,
`attempts` int(11) DEFAULT 1,
`score` int(11) DEFAULT 0,
`updated_at` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `resources`
CREATE TABLE IF NOT EXISTS `resources` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`description` text DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`file_path` varchar(255) DEFAULT NULL,
`type` enum('link','file','embed') DEFAULT 'link',
`lesson_id` int(11) DEFAULT NULL,
`course_id` int(11) DEFAULT NULL,
`author_id` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
KEY `lesson_id` (`lesson_id`),
KEY `course_id` (`course_id`),
KEY `author_id` (`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `course_editors`
CREATE TABLE IF NOT EXISTS `course_editors` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL,
`email` varchar(150) NOT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`created_by` varchar(150) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_course_editor` (`course_id`,`email`),
KEY `idx_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `user_progress`
CREATE TABLE IF NOT EXISTS `user_progress` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`lesson_id` int(11) NOT NULL,
`status` enum('not_started','in_progress','completed') NOT NULL DEFAULT 'not_started',
`last_accessed` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`created_at` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `user_lesson` (`user_id`,`lesson_id`),
KEY `lesson_id` (`lesson_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Tabellstruktur `activity_log`
CREATE TABLE IF NOT EXISTS `activity_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_email` varchar(150) NOT NULL,
`action` varchar(255) NOT NULL,
`details` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_swedish_ci;
-- Skapa relationer/främmande nycklar
ALTER TABLE `categories`
ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL;
ALTER TABLE `courses`
ADD CONSTRAINT `courses_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL,
ADD CONSTRAINT `courses_ibfk_2` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE SET NULL;
ALTER TABLE `lessons`
ADD CONSTRAINT `lessons_ibfk_1` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `lessons_ibfk_2` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE SET NULL;
ALTER TABLE `resources`
ADD CONSTRAINT `resources_ibfk_1` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `resources_ibfk_2` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `resources_ibfk_3` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE SET NULL;
ALTER TABLE `course_editors`
ADD CONSTRAINT `course_editors_ibfk_1` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE;
ALTER TABLE `user_progress`
ADD CONSTRAINT `user_progress_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `user_progress_ibfk_2` FOREIGN KEY (`lesson_id`) REFERENCES `lessons` (`id`) ON DELETE CASCADE;
-- Skapa en admin-användare
INSERT INTO `users` (`email`, `name`, `is_admin`, `is_editor`, `role`, `verified_at`)
VALUES ('admin@sambruk.se', 'Admin', 1, 1, 'admin', NOW())
ON DUPLICATE KEY UPDATE is_admin = 1, is_editor = 1;
-- Skapa en exempelkategori
INSERT INTO `categories` (`name`, `description`, `status`)
VALUES ('Allmänt', 'Allmänna kurser', 'active');
-- Skapa en exempelkurs
INSERT INTO `courses` (`title`, `description`, `difficulty_level`, `status`, `featured`, `author_id`, `category_id`)
VALUES ('Välkommen till Stimma', 'En introduktionskurs till Stimma-plattformen', 'beginner', 'active', 1, 1, 1);
-- Skapa en exempellektion
INSERT INTO `lessons` (`course_id`, `title`, `content`, `status`, `sort_order`, `author_id`)
VALUES (1, 'Kom igång med Stimma', '<p>Välkommen till Stimma! Detta är en nanolearning-plattform som hjälper dig att lära dig i små steg.</p><p>Stimma erbjuder:</p><ul><li>Korta, fokuserade lektioner</li><li>AI-stöd för lärande</li><li>Quiz för att testa dina kunskaper</li><li>Spårning av dina framsteg</li></ul>', 'active', 1, 1);