-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSubjectController.php
More file actions
534 lines (449 loc) · 19 KB
/
SubjectController.php
File metadata and controls
534 lines (449 loc) · 19 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
namespace App\Http\Controllers;
use App\Models\Child;
use App\Models\Subject;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class SubjectController extends Controller
{
// No constructor needed - using Eloquent relationships
/**
* Display a listing of subjects.
*/
public function index(Request $request)
{
try {
if (! auth()->check()) {
return redirect()->route('login')->with('error', 'Please log in to continue.');
}
// Get all children for this user using Eloquent relationships
$user = auth()->user();
$children = $user->children()->orderBy('name')->get();
// Determine selected child - default to first child if none selected
/** @var \App\Models\Child|null $firstChild */
$firstChild = $children->first();
$selectedChildId = $request->get('child_id', $firstChild?->id);
$selectedChild = null;
if ($selectedChildId) {
/** @var \App\Models\Child|null $selectedChild */
$selectedChild = $children->firstWhere('id', (int) $selectedChildId);
}
// Get subjects for the selected child or show empty state
if ($selectedChild) {
$subjects = $selectedChild->subjects()->withCount('units')->orderBy('name')->get();
} else {
$subjects = collect([]);
}
$showQuickStart = $selectedChild && $subjects->isEmpty();
if ($request->expectsJson() || $request->header('HX-Request')) {
return view('subjects.partials.subjects-list', compact('subjects', 'showQuickStart', 'selectedChild'));
}
return view('subjects.index', compact('subjects', 'showQuickStart', 'children', 'selectedChild'));
} catch (\Exception $e) {
Log::error('Error fetching subjects: '.$e->getMessage());
if ($request->expectsJson() || $request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Error loading subjects. Please try again.').'</div>', 500);
}
return back()->with('error', 'Unable to load subjects. Please try again.');
}
}
/**
* Show the form for creating a new subject.
*/
public function create(Request $request)
{
$colors = Subject::getColorOptions();
$childId = $request->get('child_id');
if ($request->header('HX-Request')) {
return view('subjects.partials.create-form', compact('colors', 'childId'));
}
return view('subjects.create', compact('colors', 'childId'));
}
/**
* Store a newly created subject in storage.
*/
public function store(Request $request)
{
try {
if (! auth()->check()) {
return response('Unauthorized', 401);
}
$validated = $request->validate([
'name' => 'required|string|max:255',
'color' => 'required|string',
'child_id' => 'required|integer|exists:children,id',
]);
// Validate color is in allowed options
$allowedColors = array_keys(Subject::getColorOptions());
if (! in_array($validated['color'], $allowedColors)) {
return back()->withErrors(['color' => 'Invalid color selected.']);
}
// Verify child belongs to the current user using relationships
/** @var \App\Models\Child|null $child */
$child = auth()->user()->children()->find($validated['child_id']);
if (! $child) {
return response('Child not found or access denied', 403);
}
$subject = new Subject([
'name' => $validated['name'],
'color' => $validated['color'],
'user_id' => auth()->id(),
'child_id' => $validated['child_id'],
]);
if ($subject->save()) {
if ($request->header('HX-Request')) {
// Return updated subjects list for the specific child using relationships
$subjects = $child->subjects()->orderBy('name')->get();
$showQuickStart = false;
$selectedChild = $child;
return view('subjects.partials.subjects-list', compact('subjects', 'showQuickStart', 'selectedChild'));
}
return redirect()->route('subjects.index')->with('success', 'Subject created successfully.');
} else {
throw new \Exception('Failed to save subject');
}
} catch (\Exception $e) {
Log::error('Error creating subject: '.$e->getMessage());
if ($request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Error creating subject. Please try again.').'</div>', 500);
}
return back()->withErrors(['error' => 'Unable to create subject. Please try again.']);
}
}
/**
* Display the specified subject.
*/
public function show(Request $request, string $id)
{
try {
if (! auth()->check()) {
return redirect()->route('login');
}
$user = auth()->user();
$subject = $user->children()
->with('subjects.units')
->get()
->pluck('subjects')
->flatten()
->where('id', $id)
->first();
if (! $subject) {
return redirect()->route('subjects.index')->with('error', 'Subject not found.');
}
$units = $subject->units;
if ($request->header('HX-Request')) {
return view((string) 'subjects.partials.subject-details', compact('subject', 'units'));
}
return view((string) 'subjects.show', compact('subject', 'units'));
} catch (\Exception $e) {
Log::error('Error fetching subject: '.$e->getMessage());
return redirect()->route('subjects.index')->with('error', 'Unable to load subject. Please try again.');
}
}
/**
* Show the form for editing the specified subject.
*/
public function edit(Request $request, string $id)
{
try {
if (! auth()->check()) {
return response('Unauthorized', 401);
}
$subject = Subject::where('id', $id)
->where('user_id', auth()->id())
->first();
if (! $subject) {
return response('Subject not found', 404);
}
$colors = Subject::getColorOptions();
if ($request->header('HX-Request')) {
return view('subjects.partials.edit-form', compact('subject', 'colors'));
}
return view('subjects.edit', compact('subject', 'colors'));
} catch (\Exception $e) {
Log::error('Error loading subject for edit: '.$e->getMessage());
return response('Unable to load subject for editing.', 500);
}
}
/**
* Update the specified subject in storage.
*/
public function update(Request $request, string $id)
{
try {
if (! auth()->check()) {
return response('Unauthorized', 401);
}
$subject = Subject::where('id', $id)
->where('user_id', auth()->id())
->first();
if (! $subject) {
return response('Subject not found', 404);
}
$validated = $request->validate([
'name' => 'required|string|max:255',
'color' => 'required|string',
]);
// Validate color is in allowed options
$allowedColors = array_keys(Subject::getColorOptions());
if (! in_array($validated['color'], $allowedColors)) {
return back()->withErrors(['color' => 'Invalid color selected.']);
}
$subject->name = $validated['name'];
$subject->color = $validated['color'];
if ($subject->save()) {
if ($request->header('HX-Request')) {
// Return updated subjects list using child relationship
/** @var \App\Models\Child $child */
$child = $subject->child;
$subjects = $child->subjects()->orderBy('name')->get();
$showQuickStart = false;
$selectedChild = $child;
return view('subjects.partials.subjects-list', compact('subjects', 'showQuickStart', 'selectedChild'));
}
return redirect()->route('subjects.index')->with('success', 'Subject updated successfully.');
} else {
throw new \Exception('Failed to update subject');
}
} catch (\Exception $e) {
Log::error('Error updating subject: '.$e->getMessage());
if ($request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Error updating subject. Please try again.').'</div>', 500);
}
return back()->withErrors(['error' => 'Unable to update subject. Please try again.']);
}
}
/**
* Remove the specified subject from storage.
*/
public function destroy(Request $request, string $id)
{
try {
if (! auth()->check()) {
return response('Unauthorized', 401);
}
$subject = Subject::where('id', $id)
->where('user_id', auth()->id())
->first();
if (! $subject) {
return response('Subject not found', 404);
}
// Check if subject has units - prevent deletion if it has units
$unitCount = $subject->units()->count();
if ($unitCount > 0) {
if ($request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Cannot delete subject with existing units. Please delete all units first.').'</div>', 400);
}
return back()->withErrors(['error' => 'Cannot delete subject with existing units. Please delete all units first.']);
}
if ($subject->delete()) {
if ($request->header('HX-Request')) {
// Return updated subjects list using child relationship
/** @var \App\Models\Child $child */
$child = $subject->child;
$subjects = $child->subjects()->orderBy('name')->get();
$showQuickStart = false;
$selectedChild = $child;
return view('subjects.partials.subjects-list', compact('subjects', 'showQuickStart', 'selectedChild'));
}
return redirect()->route('subjects.index')->with('success', 'Subject deleted successfully.');
} else {
throw new \Exception('Failed to delete subject');
}
} catch (\Exception $e) {
Log::error('Error deleting subject: '.$e->getMessage());
if ($request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Error deleting subject. Please try again.').'</div>', 500);
}
return back()->withErrors(['error' => 'Unable to delete subject. Please try again.']);
}
}
/**
* Show the quick start form for creating multiple subjects.
*/
public function quickStartForm(Request $request)
{
if (! auth()->check()) {
return response('Unauthorized', 401);
}
// Get child_id from request
$childId = $request->get('child_id');
if (! $childId) {
return response('Child ID is required', 400);
}
// Verify child belongs to the current user using relationships
/** @var \App\Models\Child|null $child */
$child = auth()->user()->children()->find($childId);
if (! $child) {
return response('Child not found or access denied', 403);
}
// Get subject templates for all grade levels
$templates = $this->getSubjectTemplates();
if ($request->header('HX-Request')) {
return view('subjects.partials.quick-start-modal', compact('templates', 'child'));
}
return response('This endpoint only supports HTMX requests', 400);
}
/**
* Store multiple subjects from quick start.
*/
public function quickStartStore(Request $request)
{
try {
if (! auth()->check()) {
return response('Unauthorized', 401);
}
$validated = $request->validate([
'grade_level' => 'required|in:elementary,middle,high',
'subjects' => 'required|array|min:1',
'subjects.*' => 'string|max:255',
'custom_subjects' => 'nullable|array',
'custom_subjects.*' => 'nullable|string|max:255',
'child_id' => 'required|integer|exists:children,id',
]);
// Verify child belongs to the current user using relationships
/** @var \App\Models\Child|null $child */
$child = auth()->user()->children()->find($validated['child_id']);
if (! $child) {
return response('Child not found or access denied', 403);
}
$createdCount = 0;
$colorOptions = array_keys(Subject::getColorOptions());
$colorIndex = 0;
// Create selected standard subjects
foreach ($validated['subjects'] as $subjectName) {
$subject = new Subject([
'name' => $subjectName,
'color' => $this->getSubjectColor($subjectName, $colorOptions, $colorIndex),
'user_id' => auth()->id(),
'child_id' => $validated['child_id'],
]);
if ($subject->save()) {
$createdCount++;
$colorIndex++;
}
}
// Create custom subjects if any
if (! empty($validated['custom_subjects'])) {
foreach ($validated['custom_subjects'] as $customName) {
if (trim($customName) !== '') {
$subject = new Subject([
'name' => trim($customName),
'color' => $colorOptions[$colorIndex % count($colorOptions)],
'user_id' => auth()->id(),
'child_id' => $validated['child_id'],
]);
if ($subject->save()) {
$createdCount++;
$colorIndex++;
}
}
}
}
if ($request->header('HX-Request')) {
// Return updated subjects list for the specific child using relationships
$subjects = $child->subjects()->orderBy('name')->get();
$showQuickStart = false;
$selectedChild = $child;
return view('subjects.partials.subjects-list', compact('subjects', 'showQuickStart', 'selectedChild'))
->with('success', __(':count subjects created successfully', ['count' => $createdCount]));
}
return redirect()->route('subjects.index')
->with('success', __(':count subjects created successfully', ['count' => $createdCount]));
} catch (\Exception $e) {
Log::error('Error in quick start: '.$e->getMessage());
if ($request->header('HX-Request')) {
return response('<div class="text-red-500">'.__('Error creating subjects. Please try again.').'</div>', 500);
}
return back()->with('error', 'Unable to create subjects. Please try again.');
}
}
/**
* Get subject color based on subject type.
*/
private function getSubjectColor(string $subjectName, array $colorOptions, int &$index): string
{
// Map subjects to specific colors for consistency
$colorMap = [
'Mathematics' => '#3B82F6', // Blue
'Science' => '#10B981', // Green
'Biology' => '#10B981',
'Chemistry' => '#10B981',
'Physics' => '#10B981',
'Life Science' => '#10B981',
'Earth Science' => '#10B981',
'Physical Science' => '#10B981',
'Reading/Language Arts' => '#8B5CF6', // Purple
'English Language Arts' => '#8B5CF6',
'Social Studies' => '#F97316', // Orange
'History' => '#F97316',
'World History' => '#F97316',
'U.S. History' => '#F97316',
'Art' => '#EC4899', // Pink
'Music' => '#EC4899',
'Physical Education' => '#EF4444', // Red
'Health' => '#EF4444',
'Computer Science' => '#6B7280', // Gray
'Foreign Language' => '#14B8A6', // Teal
'World Language' => '#14B8A6',
];
// Check if we have a specific color for this subject
foreach ($colorMap as $key => $color) {
if (stripos($subjectName, $key) !== false) {
return $color;
}
}
// Return next available color from the options
return $colorOptions[$index % count($colorOptions)];
}
/**
* Get subject templates by grade level.
*/
private function getSubjectTemplates(): array
{
return [
'elementary' => [
__('reading_language_arts'),
__('mathematics'),
__('science'),
__('social_studies'),
__('art'),
__('music'),
__('physical_education'),
],
'middle' => [
__('english_language_arts'),
__('mathematics'),
__('life_science'),
__('earth_science'),
__('physical_science'),
__('social_studies'),
__('world_history'),
__('physical_education'),
__('world_language'),
__('computer_science'),
__('art'),
__('music'),
__('health'),
],
'high' => [
__('english_language_arts'),
__('algebra'),
__('geometry'),
__('calculus'),
__('biology'),
__('chemistry'),
__('physics'),
__('world_history'),
__('us_history'),
__('foreign_language'),
__('computer_science'),
__('economics'),
__('psychology'),
__('art'),
__('physical_education'),
],
];
}
}