|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Judite\Models\Course; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
|
|
9
|
|
|
class DashboardController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Create a new controller instance. |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->middleware('auth'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Show the dashboard. |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Illuminate\Http\Response |
|
23
|
|
|
*/ |
|
24
|
|
|
public function index() |
|
25
|
|
|
{ |
|
26
|
|
|
return Auth::user()->isAdmin() |
|
|
|
|
|
|
27
|
|
|
? $this->adminDashboard() |
|
28
|
|
|
: $this->studentDashboard(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the admin's dashboard. |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Http\Response |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
protected function adminDashboard() |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$courses = DB::transaction(function () { |
|
39
|
|
|
return Course::withCount('enrollments') |
|
40
|
|
|
->orderedList() |
|
41
|
|
|
->get(); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
$courses = $courses->groupBy(function ($course) { |
|
45
|
|
|
return $course->present()->getOrdinalYear(); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
return view('admin.dashboard', compact('courses')); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the student's dashboard. |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Illuminate\Http\Response |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function studentDashboard() |
|
57
|
|
|
{ |
|
58
|
|
|
$data = DB::transaction(function () { |
|
59
|
|
|
$data['enrollments'] = student()->enrollments() |
|
|
|
|
|
|
60
|
|
|
->withCount('exchangesAsSource') |
|
61
|
|
|
->orderByCourse() |
|
62
|
|
|
->get(); |
|
63
|
|
|
$data['requestedExchanges'] = student()->requestedExchanges()->get(); |
|
64
|
|
|
$data['proposedExchanges'] = student()->proposedExchanges()->get(); |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
// Group all enrollments by the year of their associated course, so |
|
70
|
|
|
// the enrollments listing is organized by year. This will allow |
|
71
|
|
|
// a better experience, since it matches the official order. |
|
72
|
|
|
$data['enrollments'] = $data['enrollments']->groupBy(function ($enrollment) { |
|
73
|
|
|
return $enrollment->course->present()->getOrdinalYear(); |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
return view('dashboard', $data); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|