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