-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengineDB.php
341 lines (298 loc) · 11.6 KB
/
engineDB.php
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
<?php
class EngineDB {
private $servername = "localhost";
private $username = "root";
private $password = "";
private $dbname = "enginedb";
private $db;
function connect() {
$this->db = new mysqli(
$this->servername,
$this->username,
$this->password,
$this->dbname
);
if ($this->db->connect_error) {
die("Connection failed: " . $this->db->connect_error);
}
}
function disconnect() {
if (!$this->db) {
echo "No connection to close.";
return;
}
if ($this->db->close()) {
echo "Connection closed.";
} else {
echo "Error closing connection.";
}
}
function getDB() {
return $this->db;
}
function checkUserExists($email) {
$sql = "SELECT * FROM entity_user WHERE email = '$email'";
$result = $this->db->query($sql);
if ($result->num_rows > 0) {
return true;
} else {
return false;
}
}
function getUser($email) {
$query = "SELECT `id_user` AS `ID_USER`, `email` AS `EMAIL`, `password` AS `PASSWORD`, `is_admin` AS `IS_ADMIN` FROM `entity_user` WHERE `email` = ?";
$stmt = $this->db->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
return $user;
}
function addUser($email, $password, $isAdmin) {
$sql = "INSERT INTO entity_user (email, password, is_admin) VALUES (?, ?, ?)";
$stmt = $this->db->prepare($sql);
if ($stmt === false) {
// Handle error
return false;
}
// Bind the parameters
$stmt->bind_param("ssi", $email, $password, $isAdmin);
// Execute the statement
if ($stmt->execute()) {
$stmt->close();
return true;
} else {
$stmt->close();
return false;
}
}
function updateUser($email, $password) {
$sql = "UPDATE entity_user SET password = '$password' WHERE email = '$email'";
if ($this->db->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
function deleteUserById($user_id) {
// Delete from the entity_user table
$sql = "DELETE FROM entity_user WHERE id_user = $user_id";
if ($this->db->query($sql) === TRUE) {
// Now delete from the xref_survey_question_answer_user table
$sql = "DELETE FROM xref_survey_question_answer_user WHERE id_user = $user_id";
if ($this->db->query($sql) === TRUE) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function deleteUserByEmail($email) {
$sql = "DELETE FROM entity_user WHERE email = '$email'";
if ($this->db->query($sql) === TRUE) {
return true;
} else {
return false;
}
}
function getAllUsers() {
$sql = "SELECT * FROM entity_users";
$result = $this->db->query($sql);
if ($result->num_rows > 0) {
$users = array();
while ($row = $result->fetch_assoc()) {
$users[] = $row;
}
return $users;
} else {
return null;
}
}
function deleteSurveyById($survey_id) {
// Start a transaction to ensure atomicity
$this->db->begin_transaction();
try {
// Delete entries from xref_survey_question_answer_user
$sql = "DELETE FROM xref_survey_question_answer_user WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
throw new Exception("Failed to delete from xref_survey_question_answer_user");
}
$stmt->close();
// Get all questions related to the survey
$sql = "SELECT id_question FROM xref_survey_question WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
$stmt->execute();
$result = $stmt->get_result();
$questions = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Prepare the statement for getting answers
$sql = "SELECT id_answer FROM xref_question_answer WHERE id_question = ?";
$stmt_get_answers = $this->db->prepare($sql);
// Prepare the statement for deleting answers
$sql_delete_answer = "DELETE FROM entity_answer WHERE id_answer = ?";
$stmt_delete_answer = $this->db->prepare($sql_delete_answer);
// Loop through each question
foreach ($questions as $question) {
$question_id = $question['id_question'];
// Get all answers related to the question
$stmt_get_answers->bind_param("i", $question_id);
$stmt_get_answers->execute();
$result = $stmt_get_answers->get_result();
$answers = $result->fetch_all(MYSQLI_ASSOC);
// Delete each answer
foreach ($answers as $answer) {
$answer_id = $answer['id_answer'];
$stmt_delete_answer->bind_param("i", $answer_id);
if (!$stmt_delete_answer->execute()) {
throw new Exception("Failed to delete from entity_answer");
}
}
}
// Close prepared statements for answers
$stmt_get_answers->close();
$stmt_delete_answer->close();
// Delete cross references in the xref_question_answer table for these questions
$sql = "DELETE FROM xref_question_answer WHERE id_question = ?";
$stmt = $this->db->prepare($sql);
foreach ($questions as $question) {
$question_id = $question['id_question'];
$stmt->bind_param("i", $question_id);
if (!$stmt->execute()) {
throw new Exception("Failed to delete from xref_question_answer");
}
}
$stmt->close();
// Delete the questions in the entity_question table
$sql = "DELETE FROM entity_question WHERE id_question = ?";
$stmt = $this->db->prepare($sql);
foreach ($questions as $question) {
$question_id = $question['id_question'];
$stmt->bind_param("i", $question_id);
if (!$stmt->execute()) {
throw new Exception("Failed to delete from entity_question");
}
}
$stmt->close();
// Delete cross references in the xref_survey_question table
$sql = "DELETE FROM xref_survey_question WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
throw new Exception("Failed to delete from xref_survey_question");
}
$stmt->close();
// Finally, delete the survey
$sql = "DELETE FROM entity_survey WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
throw new Exception("Failed to delete from entity_survey");
}
$stmt->close();
// Commit the transaction
$this->db->commit();
return true;
} catch (Exception $e) {
// Rollback the transaction if an error occurs
$this->db->rollback();
return false;
}
}
function none($survey_id) {
// Need to first delete all the answers for the survey
// Also delete the cross references in the xref_question_answer table
// Then delete the questions for those answers
// Also delete the cross references in the xref_survey_question table
// Then delete the survey
// Then delete the entries in teh xref table for survey_question_answer_user
// Delete entries from xref_survey_question_answer_user
$sql = "DELETE FROM xref_survey_question_answer_user WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
// Get all questions related to the survey
$sql = "SELECT id_question FROM xref_survey_question WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
$stmt->execute();
$result = $stmt->get_result();
$questions = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Get all the answers related to each of the questions in the survey
$sql = "SELECT id_answer FROM xref_question_answer WHERE id_question = ?";
foreach ($questions as $question) {
$question_id = $question['id_question'];
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $question_id);
$stmt->execute();
$result = $stmt->get_result();
$answers = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
// Delete the answers
$sql = "DELETE FROM entity_answer WHERE id_answer = ?";
foreach ($answers as $answer) {
$answer_id = $answer['id_answer'];
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $answer_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
}
}
// Delete cross references in the xref_question_answer table for these questions
foreach ($questions as $question) {
$question_id = $question['id_question'];
$sql = "DELETE FROM xref_question_answer WHERE id_question = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $question_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
}
// Delete the questions in the entity_question table
$sql = "DELETE FROM entity_question WHERE id_question = ?";
foreach ($questions as $question) {
$question_id = $question['id_question'];
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $question_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
}
// Delete cross references in the xref_survey_question table
$sql = "DELETE FROM xref_survey_question WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
// Finally, delete the survey
$sql = "DELETE FROM entity_survey WHERE id_survey = ?";
$stmt = $this->db->prepare($sql);
$stmt->bind_param("i", $survey_id);
if (!$stmt->execute()) {
$stmt->close();
return false;
}
$stmt->close();
return true;
}
};