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