1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Judite\Models\Shift; |
6
|
|
|
use App\Judite\Models\Course; |
7
|
|
|
use App\Judite\Models\Student; |
8
|
|
|
use App\Judite\Models\Enrollment; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use App\Http\Controllers\Controller; |
11
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
12
|
|
|
use App\Exceptions\InvalidFieldValueException; |
13
|
|
|
use App\Exceptions\InvalidImportFileException; |
14
|
|
|
use App\Http\Requests\Enrollment\ImportRequest; |
15
|
|
|
|
16
|
|
|
class EnrollmentController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Exports the list of students enrolled in each course. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Http\Response |
22
|
|
|
*/ |
23
|
|
|
public function export() |
24
|
|
|
{ |
25
|
|
|
// Get the list of students enrolled on each course |
26
|
|
|
$enrollments = DB::transaction(function () { |
27
|
|
|
$enrollments = Enrollment::with(['student', 'course']) |
28
|
|
|
->get() |
29
|
|
|
->sortByDesc('courses.name'); |
30
|
|
|
|
31
|
|
|
return $enrollments; |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
// Export to CSV |
35
|
|
|
$result = Excel::create('enrollments', function ($excel) use ($enrollments) { |
36
|
|
|
$excel->sheet('Enrollments', function ($sheet) use ($enrollments) { |
37
|
|
|
$sheet->loadView('enrollments.export', compact('enrollments')); |
38
|
|
|
}); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return $result->export('csv'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Store the enrollments imported in the request. |
46
|
|
|
* |
47
|
|
|
* @param \App\Http\Requests\Enrollment\ImportRequest $request |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\RedirectResponse |
50
|
|
|
*/ |
51
|
|
|
public function storeImport(ImportRequest $request) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$file = $request->enrollments; |
55
|
|
|
$path = $file->path(); |
56
|
|
|
|
57
|
|
|
Excel::load($path, function ($reader) { |
58
|
|
|
DB::transaction(function () use ($reader) { |
59
|
|
|
// Loop for all the rows of the table |
60
|
|
|
$reader->each(function ($row, $index) { |
61
|
|
|
$index += 2; // Skip header |
62
|
|
|
|
63
|
|
|
// Get the models of the given ids |
64
|
|
|
// Check if the given student number exists |
65
|
|
|
$student = Student::whereNumber($row->student_id)->first(); |
66
|
|
|
if ($student === null) { |
67
|
|
|
$exception = new InvalidFieldValueException(); |
68
|
|
|
$exception->setField('Student ID', $row->student_id, "The student {$row->student_id} does not exist. (at line {$index})"); |
69
|
|
|
throw $exception; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Check if the given course id exists |
73
|
|
|
$course = Course::find($row->course_id); |
74
|
|
|
if ($course === null) { |
75
|
|
|
$exception = new InvalidFieldValueException(); |
76
|
|
|
$exception->setField('Course ID', $row->course_id, "The course {$row->course_id} does not exist. (at line {$index})"); |
77
|
|
|
throw $exception; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Check if the given shift tag exists in the associated course |
81
|
|
|
if ($row->shift === null) { |
82
|
|
|
$exception = new InvalidFieldValueException(); |
83
|
|
|
$exception->setField('Shift', $row->shift, "The shift field is required on imported enrollments. (at line {$index})"); |
84
|
|
|
throw $exception; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$shift = $course->getShiftByTag($row->shift); |
88
|
|
|
if ($shift === null) { |
89
|
|
|
$shift = Shift::make(['tag' => $row->shift]); |
90
|
|
|
$course->addShift($shift); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Check if the enrollment exists |
94
|
|
|
$enrollment = Enrollment::where([ |
95
|
|
|
'course_id' => $course->id, |
96
|
|
|
'student_id' => $student->id, |
97
|
|
|
])->first(); |
98
|
|
|
if ($enrollment === null) { |
99
|
|
|
$exception = new InvalidImportFileException("The student {$row->student_id} is not enrolled in course {$row->course_id}. (at line {$index})"); |
100
|
|
|
throw $exception; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Add the shift to the enrollment |
104
|
|
|
$enrollment->shift()->associate($shift); |
105
|
|
|
$enrollment->save(); |
106
|
|
|
}); |
107
|
|
|
}); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
flash('The enrollments file was successfully imported.')->success(); |
111
|
|
|
} catch (InvalidImportFileException $e) { |
112
|
|
|
flash($e->getMessage())->error(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return redirect()->route('enrollments.import'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Show the form for importing enrollments. |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\View\View |
122
|
|
|
*/ |
123
|
|
|
public function import() |
124
|
|
|
{ |
125
|
|
|
return view('enrollments.import'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|