-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimetable.php
273 lines (240 loc) · 12.7 KB
/
timetable.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
<?php $title = "Create Timetable"; ?>
<?php include("templates\\head.php"); ?>
<!-- Page specific code goes here -->
<?php
if (empty($_SESSION['timetable'])) {
if(!isset($_SESSION['tmp_timetable'])) {
$_SESSION['tmp_timetable'] = [];
}
}
// CREATE PERIOD
if (isset($_GET['createPeriod'])) {
$_SESSION['err'] = '';
$period = new Period($_GET['subject'], $_GET['startTime'], $_GET['endTime'], $_GET['period'], $_GET['day'], $_GET['classroom'], $_GET['teacher']);
$_SESSION['tmp_timetable'][] = $period->asArray();
sortPeriods();
header("location: timetable.php");
}
// DELETE PERIOD
if (isset($_GET['deletePeriod'])) {
unset($_SESSION['tmp_timetable'][$_GET['period']]);
sortPeriods();
header("location: timetable.php");
}
// EDIT PERIOD
if (isset($_GET['editPeriod'])) {
$period = new Period($_GET['subject'], $_GET['startTime'], $_GET['endTime'], $_GET['period'], $_GET['day'], $_GET['classroom'], $_GET['teacher']);
$_SESSION['tmp_timetable'][$_GET['periodIndex']] = $period->asArray();
sortPeriods();
header("location: timetable.php");
}
// once all periods have been added and edited create timetable
if(isset($_GET["createTimetable"])) {
if (count($_SESSION['tmp_timetable']) == 60) { // are all periods in the timetable entered?
$_SESSION['timetable'] = $_SESSION['tmp_timetable'];
saveToFile();
} else {
$_SESSION['err'] = 'Please make sure to enter the correct amount of periods before submitting your timetable.';
}
header("location: timetable.php");
}
// funciton : getDay() - returns current school day out of 10
function getDay() {
// get term dates and current dates
$username = $_SESSION['username'];
$termDatesFile = new SplFileObject("data\\users\\$username\\termdates.csv", "r");
$termDatesFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
$termDates = explode(',',$termDatesFile->fgets());
$date = date("Y-m-d");
$week = date("W");
$day = date("w");
// get term, term start day (of timetable), and term start week (of the year)
if ($date >= $termDates[0] && $date <= $termDates[1]) {
$termStartDay = $termDates[3];
$termStartWeek = date("W", strtotime($termDates[0]));
} else if ($date >= $termDates[3] && $date <= $termDates[4]) {
$termStartDay = $termDates[5];
$termStartWeek = date("W", strtotime($termDates[3]));
} else if ($date >= $termDates[6] && $date <= $termDates[7]) {
$termStartDay = $termDates[8];
$termStartWeek = date("W", strtotime($termDates[6]));
} else if ($date >= $termDates[9] && $date <= $termDates[10]) {
$termStartDay = $termDates[11];
$termStartWeek = date("W", strtotime($termDates[9]));
}
// get week of term
$weekOfTerm = $week - $termStartWeek + 1;
// get week of timetable
switch ($termStartDay) {
case '1':
if ($weekOfTerm % 2 == 0) {
$timetableWeek = 2;
} else {
$timetableWeek = 1;
}
break;
case '6':
if ($weekOfTerm % 2 == 0) {
$timetableWeek = 1;
} else {
$timetableWeek = 2;
}
break;
}
// get day of timetable
switch ($timetableWeek) {
case '1':
$timetableDay = $day;
break;
case '2':
$timetableDay = $day + 5;
break;
}
return $timetableDay;
}
?>
<div class="timetable-container">
<!-- NEW TIMETABLE FORM -->
<?php
if (empty($_SESSION['timetable'])) {
echo '<div class="create-timetable">';
} else {
echo '<div class="create-timetable hidden">';
}
?>
<div class="instructions">
<h3 style="margin-bottom: 0.25em;">Create your timetable!</h3>
<p>In order to use the timetable functionality, you must first enter in all of your timetable details.</p>
<br>
<p>To do so, follow these steps:</p>
<br>
<ol>
<li>Enter subjects in order from period 1 day 1, through to period 6 day 10.</li>
<br>
<li>Enter the subject's name, the start and end time of the period, the period number, the day of the timetable (1 - 10), the classroom, and the teacher.</li>
<br>
<li>Once complete, edit any mistakes and then click 'Create Timetable'.</li>
</ol>
<div class="triangle"></div>
</div>
<div class="new-period">
<h3 style="margin-bottom: 0.25em;">Create Timetable:</h3>
<form method="get">
<input type="text" name="subject" id="subject" placeholder="Subject Name"><br>
<input type="text" name="startTime" placeholder="Start Time" id="startTime" onfocus="(this.type='time')"><br>
<input type="text" name="endTime" placeholder="End Time" id="endTime" onfocus="(this.type='time')"> <br>
<input type="number" name="period" id="period" min="1" max="6" placeholder="Period (1 - 6)"><br>
<input type="number" name="day" id="day" min="1" max="10" placeholder="Day (1 - 10)"><br>
<input type="text" name="classroom" id="classroom" pattern="([A-Z])\d\d\d" placeholder="Classroom (A001)"><br>
<input type="text" name="teacher" id="teacher" placeholder="Teacher"><br>
<button type="create" name="createPeriod" id="new-period">Add new Period</button>
</form>
</div>
<div class="output">
<div class="italic period">
<div class="italic data">subject</div>
<div class="italic data">start time</div>
<div class="italic data">end time</div>
<div class="italic data">period</div>
<div class="italic data">day</div>
<div class="italic data">classroom</div>
<div class="italic data">teacher</div>
<div class="italic data">edit</div>
<div class="italic data">delete</div>
</div>
<?php
foreach($_SESSION['tmp_timetable'] as $period) {
$periodIndex = array_search($period, $_SESSION['tmp_timetable']);
if (isset($_GET['edit']) && $periodIndex == $_GET['period']) {
echo '<form class="period" method="get">';
echo '<input class="data" type="text" name="subject" id="subject" value="'.$period[0].'">';
echo '<input class="data" type="time" name="startTime" id="startTime" value="'.$period[1].'">';
echo '<input class="data" type="time" name="endTime" id="endTime" value="'.$period[2].'">';
echo '<input class="data" type="number" name="period" id="period" min="1" max="6" value="'.$period[3].'">';
echo '<input class="data" type="number" name="day" id="day" min="1" max="10" value="'.$period[4].'">';
echo '<input class="data" type="text" name="classroom" pattern="([A-Z])\d\d\d" id="classroom" value="'.$period[5].'">';
echo '<input class="data" type="text" name="teacher" id="teacher" value="'.$period[6].'">';
echo '<input type="hidden" name="periodIndex" id="periodIndex" value="'.$periodIndex.'">';
echo '<button type="submit" name="editPeriod"><i class="fa-solid fa-circle-check fa-xl"></i></button>';
echo '</form>';
} else {
echo '<div class="period">';
echo '<div class="data">'.$period[0].'</div>';
echo '<div class="data">'.$period[1].'</div>';
echo '<div class="data">'.$period[2].'</div>';
echo '<div class="data">'.$period[3].'</div>';
echo '<div class="data">'.$period[4].'</div>';
echo '<div class="data">'.$period[5].'</div>';
echo '<div class="data">'.$period[6].'</div>';
echo '<div><a href="timetable.php?period='.$periodIndex.'&edit="><button><i class="data fa-solid fa-pen fa-xl"></i></button></a></div>';
echo '<div><a href="timetable.php?period='.$periodIndex.'&deletePeriod="><button><i class="data fa-solid fa-trash fa-xl"></i></button></a></div>';
echo '</div>';
}
}
if (!empty($_SESSION['err'])) {
echo '<div class="period">'.$_SESSION['err'].'</div>';
}
?>
<button id="create-timetable"><a href="timetable.php?createTimetable=">Create Timetable</a></button>
</div>
</div>
<!-- ACTUAL TIMETABLE -->
<?php
if (empty($_SESSION['timetable'])) {
echo '<div id="timetable" class="timetable hidden">';
} else {
echo '<div id="timetable" class="timetable">';
}
?>
<div class="container">
<h3>Week 1</h3>
<div class="week">
<?php
for ($i = 0; $i < 5; $i++) {
echo '<div class="col day'.($i + 1).'">';
echo '<p class="title">Day '.($i + 1).'</p>';
for ($j = 0; $j < 6; $j++) {
if (($_SESSION['timetable'][$j + 6 * $i][1] <= date("H:i") && date("H:i") <= $_SESSION['timetable'][$j + 6 * $i][2]) && $i == getDay() - 1) {
echo '<div class="row period'.($j + 1).' active">';
} else {
echo '<div class="row period'.($j + 1).'">';
}
echo '<h4>'.$_SESSION['timetable'][$j + 6 * $i][0].'</h4>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][1].' to '. $_SESSION['timetable'][$j + 6 * $i][2].'</p>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][5].'</p>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][6].'</p>';
echo '</div>';
}
echo '</div>';
}
?>
</div>
</div>
<div class="container">
<h3>Week 2</h3>
<div class="week">
<?php
for ($i = 5; $i < 10; $i++) {
echo '<div class="col day'.($i + 1).'">';
echo '<p class="title">Day '.($i + 1).'</p>';
for ($j = 0; $j < 6; $j++) {
if (($_SESSION['timetable'][$j + 6 * $i][1] <= date("H:i") && date("H:i") <= $_SESSION['timetable'][$j + 6 * $i][2]) && $i == getDay() - 1) {
echo '<div class="row period'.($j + 1).' active">';
} else {
echo '<div class="row period'.($j + 1).'">';
}
echo '<h4>'.$_SESSION['timetable'][$j + 6 * $i][0].'</h4>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][1].' to '. $_SESSION['timetable'][$j + 6 * $i][2].'</p>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][5].'</p>';
echo '<p>'.$_SESSION['timetable'][$j + 6 * $i][6].'</p>';
echo '</div>';
}
echo '</div>';
}
?>
</div>
</div>
</div>
</div>
<!-- End Page specific code -->
<?php include('templates\\foot.php'); ?>