1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\Nilai\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Bantenprov\Nilai\Facades\NilaiFacade; |
9
|
|
|
|
10
|
|
|
/* Models */ |
11
|
|
|
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Akademik; |
12
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
13
|
|
|
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai; |
14
|
|
|
use App\User; |
15
|
|
|
|
16
|
|
|
/* Etc */ |
17
|
|
|
use Validator; |
18
|
|
|
use Auth; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The AkademikController class. |
22
|
|
|
* |
23
|
|
|
* @package Bantenprov\Nilai |
24
|
|
|
* @author bantenprov <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class AkademikController extends Controller |
27
|
|
|
{ |
28
|
|
|
protected $akademik; |
29
|
|
|
protected $siswa; |
30
|
|
|
protected $user; |
31
|
|
|
protected $nilai; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new controller instance. |
35
|
|
|
* |
36
|
|
|
* @return void |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
$this->akademik = new Akademik; |
41
|
|
|
$this->siswa = new Siswa; |
42
|
|
|
$this->user = new User; |
43
|
|
|
$this->nilai = new Nilai; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Display a listing of the resource. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function index(Request $request) |
52
|
|
|
{ |
53
|
|
View Code Duplication |
if (request()->has('sort')) { |
|
|
|
|
54
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
55
|
|
|
|
56
|
|
|
$query = $this->akademik->orderBy($sortCol, $sortDir); |
|
|
|
|
57
|
|
|
} else { |
58
|
|
|
$query = $this->akademik->orderBy('id', 'asc'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
if ($request->exists('filter')) { |
|
|
|
|
62
|
|
|
$query->where(function($q) use($request) { |
63
|
|
|
$value = "%{$request->filter}%"; |
64
|
|
|
|
65
|
|
|
$q->where('nomor_un', 'like', $value); |
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
70
|
|
|
|
71
|
|
|
$response = $query->with(['siswa', 'user'])->paginate($perPage); |
72
|
|
|
|
73
|
|
|
return response()->json($response) |
74
|
|
|
->header('Access-Control-Allow-Origin', '*') |
75
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Show the form for creating a new resource. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\Http\Response |
82
|
|
|
*/ |
83
|
|
|
public function create() |
84
|
|
|
{ |
85
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
86
|
|
|
$akademik = $this->akademik->getAttributes(); |
87
|
|
|
$siswas = $this->siswa->getAttributes(); |
88
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
89
|
|
|
$users_special = $this->user->all(); |
90
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
91
|
|
|
$current_user = Auth::User(); |
92
|
|
|
|
93
|
|
|
foreach($siswas as $siswa){ |
94
|
|
|
array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
98
|
|
|
|
99
|
|
View Code Duplication |
if ($role_check) { |
|
|
|
|
100
|
|
|
$user_special = true; |
101
|
|
|
|
102
|
|
|
foreach($users_special as $user){ |
103
|
|
|
array_set($user, 'label', $user->name); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$users = $users_special; |
107
|
|
|
} else { |
108
|
|
|
$user_special = false; |
109
|
|
|
|
110
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
111
|
|
|
|
112
|
|
|
$users = $users_standar; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
array_set($current_user, 'label', $current_user->name); |
116
|
|
|
|
117
|
|
|
$response['akademik'] = $akademik; |
|
|
|
|
118
|
|
|
$response['siswas'] = $siswas; |
119
|
|
|
$response['users'] = $users; |
120
|
|
|
$response['user_special'] = $user_special; |
121
|
|
|
$response['current_user'] = $current_user; |
122
|
|
|
$response['error'] = false; |
123
|
|
|
$response['message'] = 'Success'; |
124
|
|
|
$response['status'] = true; |
125
|
|
|
|
126
|
|
|
return response()->json($response); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Store a newly created resource in storage. |
131
|
|
|
* |
132
|
|
|
* @param \Illuminate\Http\Request $request |
133
|
|
|
* @return \Illuminate\Http\Response |
134
|
|
|
*/ |
135
|
|
|
public function store(Request $request) |
136
|
|
|
{ |
137
|
|
|
$akademik = $this->akademik; |
138
|
|
|
|
139
|
|
|
$validator = Validator::make($request->all(), [ |
140
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->akademik->getTable()},nomor_un,NULL,id,deleted_at,NULL", |
141
|
|
|
'bahasa_indonesia' => 'required|numeric|min:0|max:100', |
142
|
|
|
'bahasa_inggris' => 'required|numeric|min:0|max:100', |
143
|
|
|
'matematika' => 'required|numeric|min:0|max:100', |
144
|
|
|
'ipa' => 'required|numeric|min:0|max:100', |
145
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
149
|
|
|
$error = true; |
150
|
|
|
$message = $validator->errors()->first(); |
151
|
|
|
} else { |
152
|
|
|
$akademik->nomor_un = $request->input('nomor_un'); |
|
|
|
|
153
|
|
|
$akademik->bahasa_indonesia = $request->input('bahasa_indonesia'); |
|
|
|
|
154
|
|
|
$akademik->bahasa_inggris = $request->input('bahasa_inggris'); |
|
|
|
|
155
|
|
|
$akademik->matematika = $request->input('matematika'); |
|
|
|
|
156
|
|
|
$akademik->ipa = $request->input('ipa'); |
|
|
|
|
157
|
|
|
$akademik->user_id = $request->input('user_id'); |
|
|
|
|
158
|
|
|
$akademik->save(); |
159
|
|
|
|
160
|
|
|
$error = false; |
161
|
|
|
$message = 'Success'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$response['akademik'] = $akademik; |
|
|
|
|
165
|
|
|
$response['error'] = $error; |
166
|
|
|
$response['message'] = $message; |
167
|
|
|
$response['status'] = true; |
168
|
|
|
|
169
|
|
|
return response()->json($response); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Display the specified resource. |
174
|
|
|
* |
175
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
176
|
|
|
* @return \Illuminate\Http\Response |
177
|
|
|
*/ |
178
|
|
|
public function show($id) |
179
|
|
|
{ |
180
|
|
|
$akademik = $this->akademik->with(['siswa', 'user'])->findOrFail($id); |
|
|
|
|
181
|
|
|
|
182
|
|
|
$response['akademik'] = $akademik; |
|
|
|
|
183
|
|
|
$response['error'] = false; |
184
|
|
|
$response['message'] = 'Success'; |
185
|
|
|
$response['status'] = true; |
186
|
|
|
|
187
|
|
|
return response()->json($response); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Show the form for editing the specified resource. |
192
|
|
|
* |
193
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
194
|
|
|
* @return \Illuminate\Http\Response |
195
|
|
|
*/ |
196
|
|
|
public function edit($id) |
197
|
|
|
{ |
198
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
199
|
|
|
$akademik = $this->akademik->with(['siswa', 'user'])->findOrFail($id); |
|
|
|
|
200
|
|
|
$siswas = $this->siswa->getAttributes(); |
201
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
202
|
|
|
$users_special = $this->user->all(); |
203
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
204
|
|
|
$current_user = Auth::User(); |
205
|
|
|
|
206
|
|
|
if ($akademik->siswa !== null) { |
207
|
|
|
array_set($akademik->siswa, 'label', $akademik->siswa->nomor_un.' - '.$akademik->siswa->nama_siswa); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
211
|
|
|
|
212
|
|
|
if ($akademik->user !== null) { |
213
|
|
|
array_set($akademik->user, 'label', $akademik->user->name); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
if ($role_check) { |
|
|
|
|
217
|
|
|
$user_special = true; |
218
|
|
|
|
219
|
|
|
foreach($users_special as $user){ |
220
|
|
|
array_set($user, 'label', $user->name); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$users = $users_special; |
224
|
|
|
} else { |
225
|
|
|
$user_special = false; |
226
|
|
|
|
227
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
228
|
|
|
|
229
|
|
|
$users = $users_standar; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
array_set($current_user, 'label', $current_user->name); |
233
|
|
|
|
234
|
|
|
$response['akademik'] = $akademik; |
|
|
|
|
235
|
|
|
$response['siswas'] = $siswas; |
236
|
|
|
$response['users'] = $users; |
237
|
|
|
$response['user_special'] = $user_special; |
238
|
|
|
$response['current_user'] = $current_user; |
239
|
|
|
$response['error'] = false; |
240
|
|
|
$response['message'] = 'Success'; |
241
|
|
|
$response['status'] = true; |
242
|
|
|
|
243
|
|
|
return response()->json($response); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Update the specified resource in storage. |
248
|
|
|
* |
249
|
|
|
* @param \Illuminate\Http\Request $request |
250
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
251
|
|
|
* @return \Illuminate\Http\Response |
252
|
|
|
*/ |
253
|
|
|
public function update(Request $request, $id) |
254
|
|
|
{ |
255
|
|
|
$akademik = $this->akademik->with(['siswa', 'user'])->findOrFail($id); |
|
|
|
|
256
|
|
|
|
257
|
|
|
$validator = Validator::make($request->all(), [ |
258
|
|
|
// 'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->akademik->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
|
|
|
|
259
|
|
|
'bahasa_indonesia' => 'required|numeric|min:0|max:100', |
260
|
|
|
'bahasa_inggris' => 'required|numeric|min:0|max:100', |
261
|
|
|
'matematika' => 'required|numeric|min:0|max:100', |
262
|
|
|
'ipa' => 'required|numeric|min:0|max:100', |
263
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
264
|
|
|
]); |
265
|
|
|
|
266
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
267
|
|
|
$error = true; |
268
|
|
|
$message = $validator->errors()->first(); |
269
|
|
|
} else { |
270
|
|
|
$akademik->nomor_un = $akademik->nomor_un; // $request->input('nomor_un'); |
|
|
|
|
271
|
|
|
$akademik->bahasa_indonesia = $request->input('bahasa_indonesia'); |
272
|
|
|
$akademik->bahasa_inggris = $request->input('bahasa_inggris'); |
273
|
|
|
$akademik->matematika = $request->input('matematika'); |
274
|
|
|
$akademik->ipa = $request->input('ipa'); |
275
|
|
|
$akademik->user_id = $request->input('user_id'); |
276
|
|
|
$akademik->save(); |
277
|
|
|
|
278
|
|
|
$error = false; |
279
|
|
|
$message = 'Success'; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$response['akademik'] = $akademik; |
|
|
|
|
283
|
|
|
$response['error'] = $error; |
284
|
|
|
$response['message'] = $message; |
285
|
|
|
$response['status'] = true; |
286
|
|
|
|
287
|
|
|
return response()->json($response); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Remove the specified resource from storage. |
292
|
|
|
* |
293
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
294
|
|
|
* @return \Illuminate\Http\Response |
295
|
|
|
*/ |
296
|
|
|
public function destroy($id) |
297
|
|
|
{ |
298
|
|
|
$akademik = $this->akademik->findOrFail($id); |
|
|
|
|
299
|
|
|
|
300
|
|
|
if ($akademik->delete()) { |
301
|
|
|
$response['message'] = 'Success'; |
|
|
|
|
302
|
|
|
$response['success'] = true; |
303
|
|
|
$response['status'] = true; |
304
|
|
|
} else { |
305
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
306
|
|
|
$response['success'] = false; |
307
|
|
|
$response['status'] = false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return json_encode($response); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.