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