1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\Sktm\Http\Controllers; |
4
|
|
|
|
5
|
|
|
/* Require */ |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Bantenprov\Sktm\Facades\SktmFacade; |
10
|
|
|
|
11
|
|
|
/* Models */ |
12
|
|
|
use Bantenprov\Sktm\Models\Bantenprov\Sktm\Sktm; |
13
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
14
|
|
|
use Bantenprov\Sktm\Models\Bantenprov\Sktm\MasterSktm; |
15
|
|
|
use App\User; |
16
|
|
|
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai; |
17
|
|
|
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah; |
18
|
|
|
|
19
|
|
|
/* Etc */ |
20
|
|
|
use Validator; |
21
|
|
|
use Auth; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The SktmController class. |
25
|
|
|
* |
26
|
|
|
* @package Bantenprov\Sktm |
27
|
|
|
* @author bantenprov <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class SktmController extends Controller |
30
|
|
|
{ |
31
|
|
|
protected $sktm; |
32
|
|
|
protected $siswa; |
33
|
|
|
protected $master_sktm; |
34
|
|
|
protected $user; |
35
|
|
|
protected $nilai; |
36
|
|
|
protected $admin_sekolah; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new controller instance. |
40
|
|
|
* |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->sktm = new Sktm; |
46
|
|
|
$this->siswa = new Siswa; |
47
|
|
|
$this->master_sktm = new MasterSktm; |
48
|
|
|
$this->user = new User; |
49
|
|
|
$this->nilai = new Nilai; |
50
|
|
|
$this->admin_sekolah = new AdminSekolah; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Display a listing of the resource. |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\Http\Response |
57
|
|
|
*/ |
58
|
|
|
public function index(Request $request) |
59
|
|
|
{ |
60
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
61
|
|
|
|
62
|
|
|
if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){ |
63
|
|
|
$response = []; |
64
|
|
|
return response()->json($response) |
65
|
|
|
->header('Access-Control-Allow-Origin', '*') |
66
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (request()->has('sort')) { |
70
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
71
|
|
|
|
72
|
|
|
if($this->checkRole(['superadministrator'])){ |
73
|
|
|
$query = $this->sktm->orderBy($sortCol, $sortDir); |
74
|
|
|
}else{ |
75
|
|
|
$query = $this->sktm->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
if($this->checkRole(['superadministrator'])){ |
79
|
|
|
$query = $this->sktm->orderBy('id', 'asc'); |
80
|
|
|
}else{ |
81
|
|
|
$query = $this->sktm->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($request->exists('filter')) { |
86
|
|
|
if($this->checkRole(['superadministrator'])){ |
87
|
|
View Code Duplication |
$query->where(function($q) use($request) { |
|
|
|
|
88
|
|
|
$value = "%{$request->filter}%"; |
89
|
|
|
|
90
|
|
|
$q->where('sekolah_id', 'like', $value) |
91
|
|
|
->orWhere('admin_sekolah_id', 'like', $value); |
92
|
|
|
}); |
93
|
|
|
}else{ |
94
|
|
View Code Duplication |
$query->where(function($q) use($request, $admin_sekolah) { |
|
|
|
|
95
|
|
|
$value = "%{$request->filter}%"; |
96
|
|
|
|
97
|
|
|
$q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
105
|
|
|
|
106
|
|
|
$response = $query->with(['siswa', 'master_sktm', 'user'])->paginate($perPage); |
107
|
|
|
|
108
|
|
|
return response()->json($response) |
109
|
|
|
->header('Access-Control-Allow-Origin', '*') |
110
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Display a listing of the resource. |
115
|
|
|
* |
116
|
|
|
* @return \Illuminate\Http\Response |
117
|
|
|
*/ |
118
|
|
|
public function get() |
119
|
|
|
{ |
120
|
|
|
$sktms = $this->sktm->with(['master_sktm', 'user'])->get(); |
121
|
|
|
|
122
|
|
|
$response['sktms'] = $sktms; |
|
|
|
|
123
|
|
|
$response['error'] = false; |
124
|
|
|
$response['message'] = 'Success'; |
125
|
|
|
$response['status'] = true; |
126
|
|
|
|
127
|
|
|
return response()->json($response); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Show the form for creating a new resource. |
132
|
|
|
* |
133
|
|
|
* @return \Illuminate\Http\Response |
134
|
|
|
*/ |
135
|
|
|
public function create() |
136
|
|
|
{ |
137
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
138
|
|
|
$sktm = $this->sktm->getAttributes(); |
139
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
140
|
|
|
$users_special = $this->user->all(); |
141
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
142
|
|
|
$current_user = Auth::User(); |
143
|
|
|
|
144
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
145
|
|
|
|
146
|
|
|
if($this->checkRole(['superadministrator'])){ |
147
|
|
|
$siswas = $this->siswa->all(); |
148
|
|
|
}else{ |
149
|
|
|
$sekolah_id = $admin_sekolah->sekolah_id; |
150
|
|
|
$siswas = $this->siswa->where('sekolah_id', $sekolah_id)->get(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// dd($siswas); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
156
|
|
|
|
157
|
|
|
if ($role_check) { |
158
|
|
|
$user_special = true; |
159
|
|
|
|
160
|
|
|
foreach ($users_special as $user) { |
161
|
|
|
array_set($user, 'label', $user->name); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$users = $users_special; |
165
|
|
|
} else { |
166
|
|
|
$user_special = false; |
167
|
|
|
|
168
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
169
|
|
|
|
170
|
|
|
$users = $users_standar; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
array_set($current_user, 'label', $current_user->name); |
174
|
|
|
|
175
|
|
|
$response['sktm'] = $sktm; |
|
|
|
|
176
|
|
|
$response['siswa'] = $siswas; |
177
|
|
|
$response['users'] = $users; |
178
|
|
|
$response['user_special'] = $user_special; |
179
|
|
|
$response['current_user'] = $current_user; |
180
|
|
|
$response['error'] = false; |
181
|
|
|
$response['message'] = 'Success'; |
182
|
|
|
$response['status'] = true; |
183
|
|
|
|
184
|
|
|
return response()->json($response); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Store a newly created resource in storage. |
189
|
|
|
* |
190
|
|
|
* @param \Illuminate\Http\Request $request |
191
|
|
|
* @return \Illuminate\Http\Response |
192
|
|
|
*/ |
193
|
|
|
public function store(Request $request) |
194
|
|
|
{ |
195
|
|
|
$sktm = $this->sktm; |
196
|
|
|
|
197
|
|
|
$validator = Validator::make($request->all(), [ |
198
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->sktm->getTable()},nomor_un,NULL,id,deleted_at,NULL", |
199
|
|
|
'master_sktm_id' => "required|exists:{$this->master_sktm->getTable()},id", |
200
|
|
|
'no_sktm' => 'required|max:255', |
201
|
|
|
// 'nilai' => 'required|numeric|min:0|max:100', |
|
|
|
|
202
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
203
|
|
|
]); |
204
|
|
|
|
205
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
206
|
|
|
$error = true; |
207
|
|
|
$message = $validator->errors()->first(); |
208
|
|
|
} else { |
209
|
|
|
$sktm_master_sktm_id = $request->input('master_sktm_id'); |
210
|
|
|
$master_sktm = $this->master_sktm->findOrFail($sktm_master_sktm_id); |
211
|
|
|
|
212
|
|
|
$sktm->nomor_un = $request->input('nomor_un'); |
213
|
|
|
$sktm->master_sktm_id = $sktm_master_sktm_id; |
214
|
|
|
$sktm->no_sktm = $request->input('no_sktm'); |
215
|
|
|
$sktm->nilai = $master_sktm->nilai; |
216
|
|
|
$sktm->user_id = $request->input('user_id'); |
217
|
|
|
|
218
|
|
|
$nilai = $this->nilai->updateOrCreate( |
219
|
|
|
[ |
220
|
|
|
'nomor_un' => $sktm->nomor_un, |
221
|
|
|
], |
222
|
|
|
[ |
223
|
|
|
'sktm' => $sktm->nilai, |
224
|
|
|
'total' => null, |
225
|
|
|
'user_id' => $sktm->user_id, |
226
|
|
|
] |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
DB::beginTransaction(); |
230
|
|
|
|
231
|
|
|
if ($sktm->save() && $nilai->save()) |
232
|
|
|
{ |
233
|
|
|
DB::commit(); |
234
|
|
|
|
235
|
|
|
$error = false; |
236
|
|
|
$message = 'Success'; |
237
|
|
|
} else { |
238
|
|
|
DB::rollBack(); |
239
|
|
|
|
240
|
|
|
$error = true; |
241
|
|
|
$message = 'Failed'; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$response['sktm'] = $sktm; |
|
|
|
|
246
|
|
|
$response['error'] = $error; |
247
|
|
|
$response['message'] = $message; |
248
|
|
|
$response['status'] = true; |
249
|
|
|
|
250
|
|
|
return response()->json($response); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Display the specified resource. |
255
|
|
|
* |
256
|
|
|
* @param \App\Sktm $nilai |
|
|
|
|
257
|
|
|
* @return \Illuminate\Http\Response |
258
|
|
|
*/ |
259
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$sktm = $this->sktm->with(['siswa', 'master_sktm', 'user'])->findOrFail($id); |
262
|
|
|
|
263
|
|
|
$response['sktm'] = $sktm; |
|
|
|
|
264
|
|
|
$response['error'] = false; |
265
|
|
|
$response['message'] = 'Success'; |
266
|
|
|
$response['status'] = true; |
267
|
|
|
|
268
|
|
|
return response()->json($response); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Show the form for editing the specified resource. |
273
|
|
|
* |
274
|
|
|
* @param \App\Sktm $nilai |
|
|
|
|
275
|
|
|
* @return \Illuminate\Http\Response |
276
|
|
|
*/ |
277
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
$user_id = isset(Auth::User()->id) ? Auth::User()->id : null; |
280
|
|
|
$sktm = $this->sktm->with(['siswa', 'master_sktm', 'user'])->findOrFail($id); |
281
|
|
|
$users = $this->user->getAttributes(); |
|
|
|
|
282
|
|
|
$users_special = $this->user->all(); |
283
|
|
|
$users_standar = $this->user->findOrFail($user_id); |
284
|
|
|
$current_user = Auth::User(); |
285
|
|
|
|
286
|
|
|
if ($sktm->user !== null) { |
287
|
|
|
array_set($sktm->user, 'label', $sktm->user->name); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$role_check = Auth::User()->hasRole(['superadministrator','administrator']); |
291
|
|
|
|
292
|
|
|
if ($role_check) { |
293
|
|
|
$user_special = true; |
294
|
|
|
|
295
|
|
|
foreach ($users_special as $user) { |
296
|
|
|
array_set($user, 'label', $user->name); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$users = $users_special; |
300
|
|
|
} else { |
301
|
|
|
$user_special = false; |
302
|
|
|
|
303
|
|
|
array_set($users_standar, 'label', $users_standar->name); |
304
|
|
|
|
305
|
|
|
$users = $users_standar; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
array_set($current_user, 'label', $current_user->name); |
309
|
|
|
|
310
|
|
|
$response['sktm'] = $sktm; |
|
|
|
|
311
|
|
|
$response['users'] = $users; |
312
|
|
|
$response['user_special'] = $user_special; |
313
|
|
|
$response['current_user'] = $current_user; |
314
|
|
|
$response['error'] = false; |
315
|
|
|
$response['message'] = 'Success'; |
316
|
|
|
$response['status'] = true; |
317
|
|
|
|
318
|
|
|
return response()->json($response); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Update the specified resource in storage. |
323
|
|
|
* |
324
|
|
|
* @param \Illuminate\Http\Request $request |
325
|
|
|
* @param \App\Sktm $nilai |
|
|
|
|
326
|
|
|
* @return \Illuminate\Http\Response |
327
|
|
|
*/ |
328
|
|
|
public function update(Request $request, $id) |
329
|
|
|
{ |
330
|
|
|
$sktm = $this->sktm->with(['siswa', 'master_sktm', 'user'])->findOrFail($id); |
331
|
|
|
|
332
|
|
|
$validator = Validator::make($request->all(), [ |
333
|
|
|
// 'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->sktm->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
|
|
|
|
334
|
|
|
'master_sktm_id' => "required|exists:{$this->master_sktm->getTable()},id", |
335
|
|
|
'no_sktm' => 'required|max:255', |
336
|
|
|
// 'nilai' => 'required|numeric|min:0|max:100', |
|
|
|
|
337
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
338
|
|
|
]); |
339
|
|
|
|
340
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
341
|
|
|
$error = true; |
342
|
|
|
$message = $validator->errors()->first(); |
343
|
|
|
} else { |
344
|
|
|
$sktm_master_sktm_id = $request->input('master_sktm_id'); |
345
|
|
|
$master_sktm = $this->master_sktm->findOrFail($sktm_master_sktm_id); |
346
|
|
|
|
347
|
|
|
$sktm->nomor_un = $request->input('nomor_un'); |
348
|
|
|
$sktm->master_sktm_id = $sktm_master_sktm_id; |
349
|
|
|
$sktm->no_sktm = $request->input('no_sktm'); |
350
|
|
|
$sktm->nilai = $master_sktm->nilai; |
351
|
|
|
$sktm->user_id = $request->input('user_id'); |
352
|
|
|
|
353
|
|
|
$nilai = $this->nilai->updateOrCreate( |
354
|
|
|
[ |
355
|
|
|
'nomor_un' => $sktm->nomor_un, |
356
|
|
|
], |
357
|
|
|
[ |
358
|
|
|
'sktm' => $sktm->nilai, |
359
|
|
|
'total' => null, |
360
|
|
|
'user_id' => $sktm->user_id, |
361
|
|
|
] |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
DB::beginTransaction(); |
365
|
|
|
|
366
|
|
|
if ($sktm->save() && $nilai->save()) |
367
|
|
|
{ |
368
|
|
|
DB::commit(); |
369
|
|
|
|
370
|
|
|
$error = false; |
371
|
|
|
$message = 'Success'; |
372
|
|
|
} else { |
373
|
|
|
DB::rollBack(); |
374
|
|
|
|
375
|
|
|
$error = true; |
376
|
|
|
$message = 'Failed'; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$response['sktm'] = $sktm; |
|
|
|
|
381
|
|
|
$response['error'] = $error; |
382
|
|
|
$response['message'] = $message; |
383
|
|
|
$response['status'] = true; |
384
|
|
|
|
385
|
|
|
return response()->json($response); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Remove the specified resource from storage. |
390
|
|
|
* |
391
|
|
|
* @param \App\Sktm $nilai |
|
|
|
|
392
|
|
|
* @return \Illuminate\Http\Response |
393
|
|
|
*/ |
394
|
|
|
public function destroy($id) |
395
|
|
|
{ |
396
|
|
|
$sktm = $this->sktm->findOrFail($id); |
397
|
|
|
|
398
|
|
|
$nilai = $this->nilai->updateOrCreate( |
399
|
|
|
[ |
400
|
|
|
'nomor_un' => $sktm->nomor_un, |
401
|
|
|
], |
402
|
|
|
[ |
403
|
|
|
'sktm' => 0, |
404
|
|
|
'total' => null, |
405
|
|
|
'user_id' => $sktm->user_id, |
406
|
|
|
] |
407
|
|
|
); |
408
|
|
|
|
409
|
|
|
DB::beginTransaction(); |
410
|
|
|
|
411
|
|
|
if ($sktm->delete() && $nilai->save()) |
412
|
|
|
{ |
413
|
|
|
DB::commit(); |
414
|
|
|
|
415
|
|
|
$response['message'] = 'Success'; |
|
|
|
|
416
|
|
|
$response['success'] = true; |
417
|
|
|
} else { |
418
|
|
|
DB::rollBack(); |
419
|
|
|
|
420
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
421
|
|
|
$response['success'] = false; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
$response['status'] = true; |
425
|
|
|
|
426
|
|
|
return json_encode($response); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
protected function checkRole($role = array()) |
430
|
|
|
{ |
431
|
|
|
return Auth::user()->hasRole($role); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
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.