1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Judite\Models\Group; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use App\Judite\Models\Course; |
10
|
|
|
use App\Judite\Models\User; |
11
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
12
|
|
|
use Illuminate\Support\Facades\Mail; |
13
|
|
|
use App\Judite\Models\Student; |
14
|
|
|
|
15
|
|
|
class GroupController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Create a new controller instance. |
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->middleware('auth'); |
23
|
|
|
#$this->middleware('can.student'); |
24
|
|
|
#$this->middleware('can.group')->only(['create', 'store']); |
25
|
|
|
$this->middleware('student.verified'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Display a listing of the resource. |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\View\View |
32
|
|
|
*/ |
33
|
|
|
public function index() |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
$student = Auth::student(); |
37
|
|
|
|
38
|
|
|
$pendingGroups = DB::transaction(function () use ($student) { |
39
|
|
|
return $student->pendingGroups()->get(); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$confirmedGroups = DB::transaction(function () use ($student) { |
43
|
|
|
return $student->confirmedGroups()->get(); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
return view('groups.index', compact('student', 'pendingGroups', 'confirmedGroups')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function adminIndex() |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
$groups = DB::table('groups')->get(); |
53
|
|
|
|
54
|
|
|
$courses = DB::transaction(function () { |
55
|
|
|
return Course::orderedList()->get(); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
return view('groups.admin', compact('groups', 'courses')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function create() |
62
|
|
|
{ |
63
|
|
|
$student = Auth::student(); |
64
|
|
|
|
65
|
|
|
$courses = $student->getCoursesWithoutGroup(); |
66
|
|
|
|
67
|
|
|
return view('groups.create', compact('courses')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
|
public function store(Request $request) { |
74
|
|
|
|
75
|
|
|
$atributtes = $request->validate([ |
76
|
|
|
'email' => 'required', |
77
|
|
|
'course_id' => 'required' |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
DB::transaction(function () use ($atributtes){ |
81
|
|
|
$creator = Auth::student(); |
82
|
|
|
$course = Course::findOrFail($atributtes['course_id']); |
83
|
|
|
|
84
|
|
|
$group = new Group(); |
85
|
|
|
$course->addGroup($group); |
86
|
|
|
$group->refresh(); |
87
|
|
|
|
88
|
|
|
$group->addMember($creator); |
89
|
|
|
$creator->confirmGroup($group); |
90
|
|
|
|
91
|
|
|
$user = User::where('email', $atributtes['email'])->first(); |
92
|
|
|
$studentToAdd = $user->student()->first(); |
93
|
|
|
$group->addMember($studentToAdd); |
94
|
|
|
|
95
|
|
|
return $group; |
96
|
|
|
|
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
flash()->success('New group created successfully!'); |
100
|
|
|
|
101
|
|
|
return redirect()->route('groups.index'); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function edit(Group $group) { |
106
|
|
|
return view('groups.edit', compact('group')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function sendInvite(Group $group, Request $request) { |
110
|
|
|
$atributtes = $request->validate([ |
111
|
|
|
'email' => 'required', |
112
|
|
|
]); |
113
|
|
|
$user = User::where('email', $atributtes['email'])->first(); |
114
|
|
|
$studentToAdd = $user->student()->first(); |
115
|
|
|
$group->addMember($studentToAdd); |
116
|
|
|
|
117
|
|
|
flash()->success('Invite sent successfully!'); |
118
|
|
|
return redirect()->route('groups.index'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function update(Group $group) { |
122
|
|
|
$student = Auth::student(); |
123
|
|
|
$student->confirmGroup($group); |
124
|
|
|
flash()->success('Invite accepted successfully!'); |
125
|
|
|
return redirect()->route('groups.index'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function destroy(Group $group) { |
129
|
|
|
$student = Auth::student(); |
130
|
|
|
$student->declineGroup($group); |
131
|
|
|
return redirect()->route('groups.index'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function leave(Group $group) { |
135
|
|
|
$student = Auth::student(); |
136
|
|
|
$student->leaveGroup($group); |
137
|
|
|
return redirect()->route('groups.index'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Exports the list of groups in each course. |
142
|
|
|
* |
143
|
|
|
* @return \Illuminate\Http\Response |
144
|
|
|
*/ |
145
|
|
|
public function export() |
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
$groups = DB::transaction(function () { |
149
|
|
|
$groups = Group::with('course','students') |
150
|
|
|
->get() |
151
|
|
|
->sortByDesc('courses.name'); |
152
|
|
|
|
153
|
|
|
return $groups; |
154
|
|
|
}); |
155
|
|
|
|
156
|
|
|
$filename = "movies.json"; |
157
|
|
|
$handle = fopen($filename, 'w+'); |
158
|
|
|
fputs($handle, $groups->toJson(JSON_PRETTY_PRINT)); |
|
|
|
|
159
|
|
|
fclose($handle); |
|
|
|
|
160
|
|
|
$headers = array('Content-type'=> 'application/json'); |
161
|
|
|
return response()->download($filename,'groups.json',$headers); |
|
|
|
|
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|