1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Judite\Models\Exchange; |
6
|
|
|
use App\Judite\Models\Enrollment; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use App\Events\ExchangeWasConfirmed; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use App\Http\Requests\Exchange\CreateRequest; |
11
|
|
|
use App\Exceptions\EnrollmentCannotBeExchangedException; |
12
|
|
|
use App\Exceptions\ExchangeEnrollmentsOnDifferentCoursesException; |
13
|
|
|
|
14
|
|
|
class EnrollmentExchangeController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Create a new controller instance. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
$this->middleware('can.student'); |
23
|
|
|
$this->middleware('student.verified'); |
24
|
|
|
$this->middleware('can.exchange'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Show the form for creating a new resource. |
29
|
|
|
* |
30
|
|
|
* @param int $id |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse |
33
|
|
|
*/ |
34
|
|
|
public function create($id) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$data = DB::transaction(function () use ($id) { |
38
|
|
|
$enrollment = Auth::student()->enrollments()->findOrFail($id); |
39
|
|
|
|
40
|
|
|
if (! $enrollment->availableForExchange()) { |
41
|
|
|
throw new \LogicException('The enrollment is not available for exchange.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$matchingEnrollments = Enrollment::similarEnrollments($enrollment) |
45
|
|
|
->orderByStudent() |
46
|
|
|
->get(); |
47
|
|
|
|
48
|
|
|
return compact('enrollment', 'matchingEnrollments'); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
$data['matchingEnrollments'] = $data['matchingEnrollments']->map(function ($item) { |
52
|
|
|
return [ |
53
|
|
|
'id' => $item->id, |
54
|
|
|
'_toString' => $item->present()->inlineToString(), |
55
|
|
|
]; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
return view('exchanges.create', $data); |
59
|
|
|
} catch (\LogicException $e) { |
60
|
|
|
flash($e->getMessage())->error(); |
61
|
|
|
|
62
|
|
|
return redirect()->route('dashboard'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Store a newly created resource in storage. |
68
|
|
|
* |
69
|
|
|
* @param int $id |
70
|
|
|
* @param \App\Http\Requests\Exchange\CreateRequest $request |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Http\RedirectResponse |
73
|
|
|
*/ |
74
|
|
|
public function store($id, CreateRequest $request) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
$exchange = DB::transaction(function () use ($id, $request) { |
78
|
|
|
$this->validate($request, [ |
79
|
|
|
'to_enrollment_id' => 'exists:enrollments,id', |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$fromEnrollment = Auth::student()->enrollments()->findOrFail($id); |
83
|
|
|
$toEnrollment = Enrollment::find($request->input('to_enrollment_id')); |
84
|
|
|
|
85
|
|
|
// Firstly check if the inverse exchange for the same enrollments |
86
|
|
|
// already exists. If the inverse record is found then we will |
87
|
|
|
// exchange and update both enrollments of this exchange. |
88
|
|
|
if ($exchange = Exchange::findMatchingExchange($fromEnrollment, $toEnrollment)) { |
89
|
|
|
return $exchange->perform(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Otherwise, we create a new exchange between both enrollments |
93
|
|
|
// so the user that owns the target enrollment can confirm the |
94
|
|
|
// exchange and allow the other user to enroll on the shift. |
95
|
|
|
$exchange = Exchange::make(); |
96
|
|
|
$exchange->setExchangeEnrollments($fromEnrollment, $toEnrollment); |
97
|
|
|
$exchange->save(); |
98
|
|
|
|
99
|
|
|
return $exchange; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$message = 'The exchange was successfully proposed.'; |
103
|
|
|
if ($exchange->isPerformed()) { |
104
|
|
|
$message = 'The exchanged was successfully confirmed, since it matched an existing one.'; |
105
|
|
|
event(new ExchangeWasConfirmed($exchange)); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
flash($message)->success(); |
109
|
|
|
} catch (EnrollmentCannotBeExchangedException | ExchangeEnrollmentsOnDifferentCoursesException $e) { |
110
|
|
|
flash($e->getMessage())->error(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return redirect()->route('dashboard'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|