-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
334 lines (284 loc) · 11.2 KB
/
utils.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
<?php
//connect to database
function connect_db() {
$file_path = "db_credentials.txt";
$file = fopen($file_path, "r");
fscanf($file, "%s %s %s %s", $server, $database, $username, $password);
fclose($file);
$conn = mysqli_connect($server, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
return -1;
}
return $conn;
}
//execute mySQL query
function run_query($query, $result_mode=MYSQLI_STORE_RESULT) {
$conn = connect_db();
//echo $query;
$result = mysqli_query($conn, $query, $result_mode);
mysqli_close($conn);
return $result;
}
//get event
function get_event($id) {
$query = "SELECT * FROM event WHERE id='$id'";
$result = run_query($query);
if (mysqli_num_rows($result) == 0) {
die ("Could not locate task");
} else {
$row = mysqli_fetch_assoc($result);
return $row;
}
}
//add event
function add_event($name, $start, $start_time, $end, $end_time, $location) {
$formatted_start_time = format_time($start_time);
$formatted_end_time = format_time($end_time);
$query = "INSERT INTO events (name, start_date, start_time, end_date, end_time, location) VALUES ('$name', DATE '$start', '$formatted_start_time', DATE '$end', '$formatted_end_time', '$location')";
run_query($query);
$gapsQuery = "DELETE * FROM gaps";
run_query($gapsQuery);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//delete event
function delete_event($id) {
$query = "DELETE FROM events WHERE id='$id'";
run_query($query);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//edit event
function edit_event($id, $fields, $values) {
$query = "UPDATE events SET ";
$i = 0;
while ($i < (count($fields))) {
$query .= $fields[$i] . ' = "' . $values[$i] . '", ';
$i++;
}
$query = substr($query, 0, -2);
$query .= " WHERE id='$id'";
run_query($query);
$gapsQuery = "DELETE * FROM gaps";
run_query($gapsQuery);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//get task
function get_task($id) {
$query = "SELECT * FROM tasks WHERE id='$id'";
$result = run_query($query);
if (mysqli_num_rows($result) == 0) {
die ("Could not locate task");
} else {
$row = mysqli_fetch_assoc($result);
return $row;
}
}
//add task
function add_task($name, $duration, $deadline, $location, $priority) {
$newduration = format_duration($duration);
$query = "INSERT INTO tasks (name, duration, deadline, location, priority) VALUES ('$name', '$newduration', DATE '$deadline', '$location', '$priority')";
run_query($query);
$gapsQuery = "DELETE * FROM gaps";
run_query($gapsQuery);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//delete task
function delete_task($id) {
$query = "DELETE FROM tasks WHERE id='$id'";
run_query($query);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//edit task
function edit_task($id, $fields, $values) {
$query = "UPDATE tasks SET ";
$i = 0;
while ($i < (count($fields))) {
$query .= "'" . $field[$i] . "' = '" . $values[$i] . "', ";
}
$query = substr($query, 0, -2);
$query .= " WHERE id='$id'";
run_query($query);
$gapsQuery = "DELETE * FROM gaps";
run_query($gapsQuery);
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
foreach( $datesToTest as $toTest ) {
populate_gaps( $toTest );
}
}
//get events for one day
function get_day_schedule($date) {
$query = "SELECT * FROM events WHERE start_date='$date' ORDER BY (start_time)";
$result = run_query($query);
if (mysqli_num_rows($result) == 0) {
return 0;
} else {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
//get all tasks
function get_all_tasks() {
$query = "SELECT * FROM tasks ORDER BY start_date ASC";
$result = run_query($query);
if (mysqli_num_rows($result) == 0) {
return 0;
} else {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
//get all tasks
function get_all_events_and_tasks() {
$query = "SELECT * FROM tasks ORDER BY start_date ASC";
$query2 = "SELECT * FROM events ORDER BY start_date ASC";
$result = run_query($query);
$result2 = run_query($query2);
if (mysqli_num_rows($result) == 0 && mysqli_num_rows($result2) == 0) {
return 0;
} else {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
} while ($row = $result2->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
function get_day_gaps($date) {
$query = "SELECT * FROM gaps WHERE start_date='$date'";
$result = run_query($query);
if (mysqli_num_rows($result) == 0) {
return 0;
} else {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
//populate time gaps for a given day
function populate_gaps($date) {
$events = get_day_schedule($date);
$gap_start = "0000";
foreach ($events as $event) {
$event_start_time = $event['start_time'];
$duration = get_duration($gap_start, $event_start_time);
$query = "INSERT INTO gaps (start_date, start_time, end_date, end_time, duration) VALUES ('$date', '$gap_start', '$date', '$event_start_time', '$duration')";
run_query($query);
$gap_start = $event['end_time'];
}
$duration1 = get_duration($gap_start, 2359);
$query1 = "INSERT INTO gaps (start_date, start_time, end_date, end_time, duration) VALUES ('$date', '$gap_start', '$date', '2359', '$duration1')";
run_query($query1);
}
//fill tasks in gaps
function suggest_tasks() {
$tasks = get_all_tasks();
$datesToTest = array('2018-09-16', '2018-09-17','2018-09-18','2018-09-19','2018-09-20','2018-09-21','2018-09-22');
$i = 0;
foreach ($tasks as $task) {
if ($task['locked'] == 0) {
if (get_day_gaps($datesToTest[$i]) != 0) {
fill_time_slot($datesToTest[0], $task['id']);
} else {
$i++;
}
if ($i > count($datesToTest)) {
return 0;
}
}
}
}
//fill an empty time slot
function fill_time_slot($date, $task_id) {
$task = get_task($task_id);
$duration = $task['duration'];
$query1 = "SELECT * FROM gaps WHERE duration >= '$duration' ORDER BY (duration - '$duration') LIMIT 1";
$result = run_query($query1);
if (mysqli_num_rows($result) == 0) {
die ("Could not locate empty time block");
}
$slot = mysqli_fetch_assoc($result);
if ($slot['duration'] < 0) {
die ("No time blocks long enough");
}
$task_name = $task['name'];
$task_location = $task['location'];
$slot_start_date = $slot['start_date'];
$slot_start_time = $slot['start_time'];
$slot_end_date = $slot['start_date'];
$slot_end_time = $slot['start_time'] + $task['duration'];
$query2 = "INSERT INTO events (name, start_date, start_time, end_date, end_time, location) VALUES ('$task_name', '$slot_start_date', '$slot_start_time', '$slot_end_date', '$slot_end_time', '$task_location') ";
run_query($query2);
$query3 = "UPDATE tasks SET start_date = '$slot_start_date', start_time = '$slot_start_time', end_date = '$slot_end_date', end_time = '$slot_end_time', locked=1 WHERE id = '$task_id'";
run_query($query3);
$task = get_task($task_id);
if ($task['duration'] == $slot['duration']) {
$slot_id = $slot['id'];
$query3 = "DELETE FROM slots WHERE id='$slot_id'";
run_query($query3);
} else {
$task_end_time = $task['end_time'];
$query3 = "UPDATE gaps SET start_time = '$task_end_time'";
run_query($query3);
}
}
//format time from (HH :,MM) to HHMM
function format_time( $time ) {
$timeArr = explode(',', $time);
$hours = str_replace(array(' ', ':'), '', $timeArr[0]);
$minutes = str_replace(array(' ', ':'), '', $timeArr[1]);
$timeInt = (intval($hours) * 100) + intval($minutes);
return $timeInt;
}
// get duration from start and end time
function get_duration( $start, $end ) {
$startMinutes = $start % 100;
$endMinutes = $end % 100;
$startHours = ($start - $startMinutes) / 100;
$endHours = ($end - $endMinutes) / 100;
$diffMinutes = (60 - $startMinutes) + $endMinutes;
$diffHours = $endHours - ($startHours + 1);
$duration = $diffMinutes + ($diffHours * 60);
return $duration;
}
// format string duration
function format_duration( $string ) {
$formatArr = explode(',', $string);
$formatArr[0] = str_replace(array('hours', 'minutes', ' ', ','), '',$formatArr[0]);
$formatArr[1] = str_replace(array('hours', 'minutes', ' ', ','), '',$formatArr[1]);
$timeInt = (intval($formatArr[0]) * 60) + intval($formatArr[1]);
return $timeInt;
}
// add time
function add_time( $t1, $t2 ) {
$t1Minutes = $t1 % 100;
$t2Minutes = $t2 % 100;
$t1Hours =(($t1 - $t1Minutes)/100);
$t2Hours =(($t2 - $t2Minutes)/100);
$sum = (($t1Hours + $t2Hours)*100) + ($t1Minutes + $t2Minutes);
return $sum;
}