-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_generator.php
More file actions
222 lines (187 loc) · 7.17 KB
/
schedule_generator.php
File metadata and controls
222 lines (187 loc) · 7.17 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
<?php
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
// Get input data
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
http_response_code(400);
echo json_encode(['error' => 'Invalid input data']);
exit;
}
try {
$result = generateSchedule($input);
echo json_encode($result);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => 'Schedule generation failed: ' . $e->getMessage()]);
}
function generateSchedule($data) {
$days = $data['days'] ?? [];
$dailyHours = $data['dailyHours'] ?? [];
$restPeriods = $data['restPeriods'] ?? [];
$subjects = $data['subjects'] ?? [];
$schedule = [];
$timeSlots = generateTimeSlots();
$stats = [
'totalScheduledHours' => 0,
'tasksScheduled' => 0,
'efficiency' => 0
];
// Initialize schedule structure
foreach ($days as $day) {
$schedule[$day] = [];
foreach ($timeSlots as $slot) {
$schedule[$day][$slot] = [];
}
}
// Add rest periods
foreach ($restPeriods as $period) {
$periodDays = $period['applyToAll'] ? $days : array_intersect($period['days'], $days);
foreach ($periodDays as $day) {
if (!isset($schedule[$day])) continue;
$restSlots = getTimeSlotsBetween($period['startTime'], $period['endTime'], $timeSlots);
foreach ($restSlots as $slot) {
if (isset($schedule[$day][$slot])) {
$schedule[$day][$slot][] = [
'type' => 'rest',
'name' => $period['name'],
'color' => '#f39c12'
];
}
}
}
}
// Sort subjects by priority (high first)
usort($subjects, function($a, $b) {
$priorityOrder = ['high' => 3, 'medium' => 2, 'low' => 1];
$aPriority = $priorityOrder[$a['priority'] ?? 'medium'] ?? 2;
$bPriority = $priorityOrder[$b['priority'] ?? 'medium'] ?? 2;
return $bPriority - $aPriority;
});
// Calculate total required minutes per subject
$subjectRequirements = [];
foreach ($subjects as $subject) {
$subjectId = $subject['id'] ?? uniqid();
$duration = $subject['duration'] ?? 0;
$availableDays = $subject['availableDays'] ?? [];
$subjectRequirements[$subjectId] = [
'requiredMinutes' => $duration * 60,
'scheduledMinutes' => 0,
'availableDays' => $availableDays
];
}
// Schedule subjects
foreach ($subjects as $subject) {
$subjectId = $subject['id'] ?? uniqid();
$subjectName = $subject['name'] ?? 'Unnamed Task';
$color = generateColor($subjectId);
$requiredMinutes = $subjectRequirements[$subjectId]['requiredMinutes'] ?? 0;
$scheduledMinutes = 0;
$availableDays = $subject['availableDays'] ?? [];
// Try to schedule this subject
foreach ($availableDays as $day) {
if ($scheduledMinutes >= $requiredMinutes) break;
if (!in_array($day, $days)) continue;
$availableSlots = findAvailableSlots($day, $subject, $schedule, $timeSlots);
foreach ($availableSlots as $slot) {
if ($scheduledMinutes >= $requiredMinutes) break;
// Check if this slot is available
if (isSlotAvailable($schedule[$day][$slot])) {
$schedule[$day][$slot][] = [
'type' => 'task',
'name' => $subjectName,
'subjectId' => $subjectId,
'color' => $color
];
$scheduledMinutes += 30; // Each slot is 30 minutes
$stats['totalScheduledHours'] += 0.5;
}
}
}
if ($scheduledMinutes > 0) {
$stats['tasksScheduled']++;
}
// Update efficiency calculation
if (isset($subjectRequirements[$subjectId])) {
$subjectRequirements[$subjectId]['scheduledMinutes'] = $scheduledMinutes;
}
}
// Calculate efficiency
$totalRequired = 0;
$totalScheduled = 0;
foreach ($subjectRequirements as $requirement) {
$totalRequired += $requirement['requiredMinutes'];
$totalScheduled += $requirement['scheduledMinutes'];
}
$stats['efficiency'] = $totalRequired > 0 ? round(($totalScheduled / $totalRequired) * 100) : 0;
$stats['totalScheduledHours'] = round($stats['totalScheduledHours'], 1);
return [
'success' => true,
'schedule' => $schedule,
'stats' => $stats
];
}
function isSlotAvailable($slotItems) {
foreach ($slotItems as $item) {
if (($item['type'] ?? '') === 'task' || ($item['type'] ?? '') === 'rest') {
return false;
}
}
return true;
}
function findAvailableSlots($day, $subject, $schedule, $timeSlots) {
$availableSlots = [];
$unavailableTimes = $subject['unavailableTimes'] ?? [];
foreach ($timeSlots as $slot) {
// Check if slot is in unavailable times
$isUnavailable = false;
foreach ($unavailableTimes as $unavailable) {
$unavailableDays = $unavailable['days'] ?? [];
$startTime = $unavailable['startTime'] ?? '00:00';
$endTime = $unavailable['endTime'] ?? '00:00';
if (in_array($day, $unavailableDays) &&
isTimeInRange($slot, $startTime, $endTime)) {
$isUnavailable = true;
break;
}
}
if ($isUnavailable) continue;
// Check if slot is already occupied
if (isset($schedule[$day][$slot]) && isSlotAvailable($schedule[$day][$slot])) {
$availableSlots[] = $slot;
}
}
return $availableSlots;
}
function generateTimeSlots() {
$slots = [];
for ($hour = 8; $hour <= 20; $hour++) {
for ($minute = 0; $minute < 60; $minute += 30) {
$slots[] = sprintf('%02d:%02d', $hour, $minute);
}
}
return $slots;
}
function getTimeSlotsBetween($start, $end, $timeSlots) {
return array_filter($timeSlots, function($slot) use ($start, $end) {
return $slot >= $start && $slot < $end;
});
}
function isTimeInRange($time, $start, $end) {
return $time >= $start && $time < $end;
}
function generateColor($seed) {
$colors = ['#3498db', '#2ecc71', '#9b59b6', '#e74c3c', '#1abc9c', '#34495e', '#f1c40f', '#e67e22'];
if (is_numeric($seed)) {
return $colors[$seed % count($colors)];
} else {
// Generate hash-based color for string seeds
$hash = md5($seed);
return '#' . substr($hash, 0, 6);
}
}
?>