155 lines
4.7 KiB
PHP
155 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StudentStoreRequest;
|
|
use App\Models\Audition;
|
|
use App\Models\AuditLogEntry;
|
|
use App\Models\Student;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use function abort;
|
|
use function redirect;
|
|
|
|
class StudentController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
if (! Auth::user()->school_id) {
|
|
return redirect()->route('dashboard');
|
|
}
|
|
$students = Auth::user()->students()->withCount('entries')->get();
|
|
$auditions = Audition::all();
|
|
|
|
$shirtSizes = Student::$shirtSizes;
|
|
|
|
return view('students.index',
|
|
['students' => $students, 'auditions' => $auditions, 'shirtSizes' => $shirtSizes]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StudentStoreRequest $request)
|
|
{
|
|
if ($request->user()->cannot('create', Student::class)) {
|
|
abort(403);
|
|
}
|
|
|
|
$student = Student::create([
|
|
'first_name' => $request['first_name'],
|
|
'last_name' => $request['last_name'],
|
|
'grade' => $request['grade'],
|
|
'school_id' => Auth::user()->school_id,
|
|
]);
|
|
if ($request['shirt_size'] !== 'none') {
|
|
$student->update(['optional_data->shirt_size' => $request['shirt_size']]);
|
|
}
|
|
|
|
return redirect('/students')->with('success', 'Student Created');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Request $request, Student $student)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Request $request, Student $student)
|
|
{
|
|
if ($request->user()->cannot('update', $student)) {
|
|
abort(403);
|
|
}
|
|
|
|
$shirtSizes = Student::$shirtSizes;
|
|
|
|
return view('students.edit', ['student' => $student, 'shirtSizes' => $shirtSizes]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Student $student)
|
|
{
|
|
|
|
if ($request->user()->cannot('update', $student)) {
|
|
abort(403);
|
|
}
|
|
request()->validate([
|
|
'first_name' => ['required'],
|
|
'last_name' => ['required'],
|
|
'grade' => ['required', 'integer'],
|
|
'shirt_size' => [
|
|
'nullable',
|
|
function ($attribute, $value, $fail) {
|
|
if (! array_key_exists($value, Student::$shirtSizes)) {
|
|
$fail("The selected $attribute is invalid.");
|
|
}
|
|
},
|
|
],
|
|
]);
|
|
|
|
if (Student::where('first_name', request('first_name'))
|
|
->where('last_name', request('last_name'))
|
|
->where('school_id', Auth::user()->school_id)
|
|
->where('id', '!=', $student->id)
|
|
->exists()) {
|
|
return redirect()->route('students.edit', $student)->with('error',
|
|
'A student with that name already exists at your school.');
|
|
}
|
|
|
|
$student->update([
|
|
'first_name' => request('first_name'),
|
|
'last_name' => request('last_name'),
|
|
'grade' => request('grade'),
|
|
]);
|
|
|
|
$student->update(['optional_data->shirt_size' => $request['shirt_size']]);
|
|
|
|
$message = 'Updated student #'.$student->id.'<br>Name: '.$student->full_name().'<br>Grade: '.$student->grade.'<br>School: '.$student->school->name;
|
|
AuditLogEntry::create([
|
|
'user' => auth()->user()->email,
|
|
'ip_address' => request()->ip(),
|
|
'message' => $message,
|
|
'affected' => [
|
|
'students' => [$student->id],
|
|
'schools' => [$student->school_id],
|
|
],
|
|
]);
|
|
|
|
return redirect('/students')->with('success', 'Student updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Request $request, Student $student)
|
|
{
|
|
if ($request->user()->cannot('delete', $student)) {
|
|
abort(403);
|
|
}
|
|
$message = 'Deleted student #'.$student->id.'<br>Name: '.$student->full_name().'<br>Grade: '.$student->grade.'<br>School: '.$student->school->name;
|
|
AuditLogEntry::create([
|
|
'user' => auth()->user()->email,
|
|
'ip_address' => request()->ip(),
|
|
'message' => $message,
|
|
'affected' => [
|
|
'students' => [$student->id],
|
|
'schools' => [$student->school_id],
|
|
],
|
|
]);
|
|
$student->delete();
|
|
|
|
return redirect(route('students.index'));
|
|
}
|
|
}
|