1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bantenprov\Prestasi\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\Prestasi\Facades\PrestasiFacade; |
10
|
|
|
|
11
|
|
|
/* Models */ |
12
|
|
|
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\Prestasi; |
13
|
|
|
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\MasterPrestasi; |
14
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
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 PrestasiController class. |
25
|
|
|
* |
26
|
|
|
* @package Bantenprov\Prestasi |
27
|
|
|
* @author bantenprov <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class PrestasiController extends Controller |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Create a new controller instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
protected $prestasi; |
37
|
|
|
protected $siswa; |
38
|
|
|
protected $master_prestasi; |
39
|
|
|
protected $user; |
40
|
|
|
protected $nilai; |
41
|
|
|
protected $admin_sekolah; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->prestasi = new Prestasi; |
46
|
|
|
$this->siswa = new Siswa; |
47
|
|
|
$this->master_prestasi = new MasterPrestasi; |
48
|
|
|
$this->user = new User; |
49
|
|
|
$this->nilai = new Nilai; |
50
|
|
|
$this->admin_sekolah = new AdminSekolah; |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Display a listing of the resource. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
|
|
public function index(Request $request) |
60
|
|
|
{ |
61
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
62
|
|
|
|
63
|
|
|
if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){ |
64
|
|
|
$response = []; |
65
|
|
|
return response()->json($response) |
66
|
|
|
->header('Access-Control-Allow-Origin', '*') |
67
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (request()->has('sort')) { |
71
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
72
|
|
|
|
73
|
|
|
if($this->checkRole(['superadministrator'])){ |
74
|
|
|
$query = $this->prestasi->orderBy($sortCol, $sortDir); |
75
|
|
|
}else{ |
76
|
|
|
$query = $this->prestasi->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
if($this->checkRole(['superadministrator'])){ |
80
|
|
|
$query = $this->prestasi->orderBy('id', 'asc'); |
81
|
|
|
}else{ |
82
|
|
|
$query = $this->prestasi->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($request->exists('filter')) { |
87
|
|
|
if($this->checkRole(['superadministrator'])){ |
88
|
|
View Code Duplication |
$query->where(function($q) use($request) { |
|
|
|
|
89
|
|
|
$value = "%{$request->filter}%"; |
90
|
|
|
|
91
|
|
|
$q->where('sekolah_id', 'like', $value) |
92
|
|
|
->orWhere('admin_sekolah_id', 'like', $value); |
93
|
|
|
}); |
94
|
|
|
}else{ |
95
|
|
View Code Duplication |
$query->where(function($q) use($request, $admin_sekolah) { |
|
|
|
|
96
|
|
|
$value = "%{$request->filter}%"; |
97
|
|
|
|
98
|
|
|
$q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
105
|
|
|
$response = $query->with('user')->with('master_prestasi')->with('siswa')->paginate($perPage); |
106
|
|
|
|
107
|
|
|
return response()->json($response) |
108
|
|
|
->header('Access-Control-Allow-Origin', '*') |
109
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Show the form for creating a new resource. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Http\Response |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
public function create() |
119
|
|
|
{ |
120
|
|
|
$response = []; |
121
|
|
|
|
122
|
|
|
$master_prestasis = $this->master_prestasi->all(); |
123
|
|
|
$users_special = $this->user->all(); |
|
|
|
|
124
|
|
|
$users_standar = $this->user->find(\Auth::User()->id); |
|
|
|
|
125
|
|
|
$current_user = \Auth::User(); |
126
|
|
|
|
127
|
|
|
$admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first(); |
128
|
|
|
|
129
|
|
|
if($this->checkRole(['superadministrator'])){ |
130
|
|
|
$siswas = $this->siswa->all(); |
131
|
|
|
}else{ |
132
|
|
|
$sekolah_id = $admin_sekolah->sekolah_id; |
133
|
|
|
$siswas = $this->siswa->where('sekolah_id', $sekolah_id)->get(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
array_set($current_user, 'label', $current_user->name); |
137
|
|
|
|
138
|
|
|
$response['current_user'] = $current_user; |
139
|
|
|
|
140
|
|
|
foreach($master_prestasis as $master_prestasi){ |
141
|
|
|
if($master_prestasi->juara == 1){ |
142
|
|
|
$juara = "Juara 1"; |
143
|
|
|
}elseif($master_prestasi->juara == 2){ |
144
|
|
|
$juara = "Juara 2"; |
145
|
|
|
}elseif($master_prestasi->juara == 3){ |
146
|
|
|
$juara = "Juara 3"; |
147
|
|
|
}else{ |
148
|
|
|
$juara = "Juara Harapan 1"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if($master_prestasi->tingkat == 1){ |
152
|
|
|
$tingkat = "Tingkat Internasional"; |
153
|
|
|
}elseif($master_prestasi->tingkat == 2){ |
154
|
|
|
$tingkat = "Tingkat Nasional"; |
155
|
|
|
}elseif($master_prestasi->tingkat == 3){ |
156
|
|
|
$tingkat = "Tingkat Provinsi"; |
157
|
|
|
}else{ |
158
|
|
|
$tingkat = "Tingkat Kabupaten/Kota"; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
array_set($master_prestasi, 'label', "( ".$juara." ".$tingkat." ) - ".$master_prestasi->jenis_prestasi->nama); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
foreach($siswas as $siswa){ |
165
|
|
|
array_set($siswa, 'label', $siswa->nama_siswa); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$response['master_prestasi'] = $master_prestasis; |
169
|
|
|
$response['siswa'] = $siswas; |
170
|
|
|
$response['status'] = true; |
171
|
|
|
|
172
|
|
|
return response()->json($response); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Display the specified resource. |
177
|
|
|
* |
178
|
|
|
* @param \App\Prestasi $prestasi |
|
|
|
|
179
|
|
|
* @return \Illuminate\Http\Response |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function store(Request $request) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$prestasi = $this->prestasi; |
184
|
|
|
|
185
|
|
|
$validator = Validator::make($request->all(), [ |
186
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,NULL,id,deleted_at,NULL", |
187
|
|
|
'master_prestasi_id' => "required|exists:{$this->master_prestasi->getTable()},id", |
188
|
|
|
'nama_lomba' => 'required|max:255', |
189
|
|
|
// 'nilai' => 'required|numeric|min:0|max:100', |
|
|
|
|
190
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
191
|
|
|
]); |
192
|
|
|
|
193
|
|
|
if ($validator->fails()) { |
194
|
|
|
$error = true; |
|
|
|
|
195
|
|
|
$response['message'] = $validator->errors()->first(); |
|
|
|
|
196
|
|
|
} else { |
197
|
|
|
$prestasi_master_prestasi_id = $request->input('master_prestasi_id'); |
198
|
|
|
$master_prestasi = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id); |
199
|
|
|
|
200
|
|
|
$prestasi->nomor_un = $request->input('nomor_un'); |
201
|
|
|
$prestasi->master_prestasi_id = $prestasi_master_prestasi_id; |
202
|
|
|
$prestasi->nama_lomba = $request->input('nama_lomba'); |
203
|
|
|
$prestasi->nilai = $master_prestasi->nilai; |
204
|
|
|
$prestasi->user_id = $request->input('user_id'); |
205
|
|
|
|
206
|
|
|
$nilai = $this->nilai->updateOrCreate( |
207
|
|
|
[ |
208
|
|
|
'nomor_un' => $prestasi->nomor_un, |
209
|
|
|
], |
210
|
|
|
[ |
211
|
|
|
'prestasi' => $prestasi->nilai, |
212
|
|
|
'total' => null, |
213
|
|
|
'user_id' => $prestasi->user_id, |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
DB::beginTransaction(); |
218
|
|
|
|
219
|
|
|
if ($prestasi->save() && $nilai->save()) |
220
|
|
|
{ |
221
|
|
|
DB::commit(); |
222
|
|
|
|
223
|
|
|
$error = false; |
|
|
|
|
224
|
|
|
$response['message'] = 'success'; |
|
|
|
|
225
|
|
|
} else { |
226
|
|
|
DB::rollBack(); |
227
|
|
|
|
228
|
|
|
$error = true; |
|
|
|
|
229
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$response['status'] = true; |
234
|
|
|
|
235
|
|
|
return response()->json($response); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Store a newly created resource in storage. |
240
|
|
|
* |
241
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
242
|
|
|
* @return \Illuminate\Http\Response |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$prestasi = $this->prestasi->findOrFail($id); |
247
|
|
|
|
248
|
|
|
$response['user'] = $prestasi->user; |
|
|
|
|
249
|
|
|
$response['master_prestasi'] = $prestasi->master_prestasi; |
250
|
|
|
$response['siswa'] = $prestasi->siswa; |
251
|
|
|
$response['prestasi'] = $prestasi; |
252
|
|
|
$response['status'] = true; |
253
|
|
|
|
254
|
|
|
return response()->json($response); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Show the form for editing the specified resource. |
259
|
|
|
* |
260
|
|
|
* @param \App\Prestasi $prestasi |
261
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
262
|
|
|
*/ |
263
|
|
|
|
264
|
|
|
public function edit($id) |
265
|
|
|
{ |
266
|
|
|
$prestasi = $this->prestasi->findOrFail($id); |
267
|
|
|
|
268
|
|
|
array_set($prestasi->user, 'label', $prestasi->user->name); |
269
|
|
|
array_set($prestasi->master_prestasi, 'label', $prestasi->master_prestasi->juara); |
270
|
|
|
array_set($prestasi->siswa, 'label', $prestasi->siswa->nama_siswa); |
271
|
|
|
|
272
|
|
|
$response['master_prestasi'] = $prestasi->master_prestasi; |
|
|
|
|
273
|
|
|
$response['siswa'] = $prestasi->siswa; |
274
|
|
|
$response['prestasi'] = $prestasi; |
275
|
|
|
$response['user'] = $prestasi->user; |
276
|
|
|
$response['status'] = true; |
277
|
|
|
|
278
|
|
|
return response()->json($response); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Update the specified resource in storage. |
283
|
|
|
* |
284
|
|
|
* @param \Illuminate\Http\Request $request |
285
|
|
|
* @param \App\Prestasi $prestasi |
|
|
|
|
286
|
|
|
* @return \Illuminate\Http\Response |
287
|
|
|
*/ |
288
|
|
View Code Duplication |
public function update(Request $request, $id) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$prestasi = $this->prestasi; |
291
|
|
|
|
292
|
|
|
$validator = Validator::make($request->all(), [ |
293
|
|
|
'nomor_un' => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,{$id},id,deleted_at,NULL", |
294
|
|
|
'master_prestasi_id' => "required|exists:{$this->master_prestasi->getTable()},id", |
295
|
|
|
'nama_lomba' => 'required|max:255', |
296
|
|
|
// 'nilai' => 'required|numeric|min:0|max:100', |
|
|
|
|
297
|
|
|
'user_id' => "required|exists:{$this->user->getTable()},id", |
298
|
|
|
]); |
299
|
|
|
|
300
|
|
|
if ($validator->fails()) { |
301
|
|
|
$error = true; |
|
|
|
|
302
|
|
|
$response['message'] = $validator->errors()->first(); |
|
|
|
|
303
|
|
|
} else { |
304
|
|
|
$prestasi_master_prestasi_id = $request->input('master_prestasi_id'); |
305
|
|
|
$master_prestasi = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id); |
306
|
|
|
|
307
|
|
|
$prestasi->nomor_un = $request->input('nomor_un'); |
308
|
|
|
$prestasi->master_prestasi_id = $prestasi_master_prestasi_id; |
309
|
|
|
$prestasi->nama_lomba = $request->input('nama_lomba'); |
310
|
|
|
$prestasi->nilai = $master_prestasi->nilai; |
311
|
|
|
$prestasi->user_id = $request->input('user_id'); |
312
|
|
|
|
313
|
|
|
$nilai = $this->nilai->updateOrCreate( |
314
|
|
|
[ |
315
|
|
|
'nomor_un' => $prestasi->nomor_un, |
316
|
|
|
], |
317
|
|
|
[ |
318
|
|
|
'prestasi' => $prestasi->nilai, |
319
|
|
|
'total' => null, |
320
|
|
|
'user_id' => $prestasi->user_id, |
321
|
|
|
] |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
DB::beginTransaction(); |
325
|
|
|
|
326
|
|
|
if ($prestasi->save() && $nilai->save()) |
327
|
|
|
{ |
328
|
|
|
DB::commit(); |
329
|
|
|
|
330
|
|
|
$error = false; |
|
|
|
|
331
|
|
|
$response['message'] = 'success'; |
|
|
|
|
332
|
|
|
} else { |
333
|
|
|
DB::rollBack(); |
334
|
|
|
|
335
|
|
|
$error = true; |
|
|
|
|
336
|
|
|
$response['message'] = 'Failed'; |
|
|
|
|
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
$response['status'] = true; |
341
|
|
|
|
342
|
|
|
return response()->json($response); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Remove the specified resource from storage. |
347
|
|
|
* |
348
|
|
|
* @param \App\Prestasi $prestasi |
|
|
|
|
349
|
|
|
* @return \Illuminate\Http\Response |
350
|
|
|
*/ |
351
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
352
|
|
|
{ |
353
|
|
|
$prestasi = $this->prestasi->findOrFail($id); |
354
|
|
|
|
355
|
|
|
if ($prestasi->delete()) { |
356
|
|
|
$response['status'] = true; |
|
|
|
|
357
|
|
|
} else { |
358
|
|
|
$response['status'] = false; |
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return json_encode($response); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
protected function checkRole($role = array()) |
365
|
|
|
{ |
366
|
|
|
return Auth::user()->hasRole($role); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.