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\Nilai; |
12
|
|
|
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa; |
13
|
|
|
use App\User; |
14
|
|
|
|
15
|
|
|
/* Etc */ |
16
|
|
|
use Validator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The NilaiController class. |
20
|
|
|
* |
21
|
|
|
* @package Bantenprov\Nilai |
22
|
|
|
* @author bantenprov <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class NilaiController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Create a new controller instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected $siswa; |
32
|
|
|
protected $nilai; |
33
|
|
|
protected $user; |
34
|
|
|
|
35
|
|
|
public function __construct(Nilai $nilai, Siswa $siswa, User $user) |
36
|
|
|
{ |
37
|
|
|
$this->nilai = $nilai; |
38
|
|
|
$this->siswa = $siswa; |
39
|
|
|
$this->user = $user; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Display a listing of the resource. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function index(Request $request) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if (request()->has('sort')) { |
50
|
|
|
list($sortCol, $sortDir) = explode('|', request()->sort); |
51
|
|
|
|
52
|
|
|
$query = $this->nilai->orderBy($sortCol, $sortDir); |
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
$query = $this->nilai->orderBy('id', 'asc'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($request->exists('filter')) { |
58
|
|
|
$query->where(function($q) use($request) { |
59
|
|
|
$value = "%{$request->filter}%"; |
60
|
|
|
$q->where('user_id', 'like', $value) |
61
|
|
|
->orWhere('id', 'like', $value); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$perPage = request()->has('per_page') ? (int) request()->per_page : null; |
66
|
|
|
$response = $query->with('user')->with('siswa')->paginate($perPage); |
67
|
|
|
|
68
|
|
|
return response()->json($response) |
69
|
|
|
->header('Access-Control-Allow-Origin', '*') |
70
|
|
|
->header('Access-Control-Allow-Methods', 'GET'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Show the form for creating a new resource. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\Response |
77
|
|
|
*/ |
78
|
|
|
public function create() |
79
|
|
|
{ |
80
|
|
|
$siswas = $this->siswa->all(); |
81
|
|
|
$users = $this->user->all(); |
82
|
|
|
|
83
|
|
|
foreach($users as $user){ |
84
|
|
|
array_set($user, 'label', $user->name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach($siswas as $siswa){ |
88
|
|
|
array_set($siswa, 'label', $siswa->nama_siswa); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$response['siswa'] = $siswas; |
|
|
|
|
92
|
|
|
$response['user'] = $users; |
93
|
|
|
$response['status'] = true; |
94
|
|
|
|
95
|
|
|
return response()->json($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Display the specified resource. |
100
|
|
|
* |
101
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
102
|
|
|
* @return \Illuminate\Http\Response |
103
|
|
|
*/ |
104
|
|
|
public function store(Request $request) |
105
|
|
|
{ |
106
|
|
|
$nilai = $this->nilai; |
107
|
|
|
|
108
|
|
|
$validator = Validator::make($request->all(), [ |
109
|
|
|
'user_id' => 'required|unique:nilais,user_id', |
110
|
|
|
'siswa_id' => 'required|unique:nilais,siswa_id', |
111
|
|
|
'akademik_id' => 'required|unique:nilais,akademik_id', |
112
|
|
|
'prestasi_id' => 'required|unique:nilais,prestasi_id', |
113
|
|
|
'zona_id' => 'required|unique:nilais,zona_id', |
114
|
|
|
'sktm_id' => 'required|unique:nilais,sktm_id' |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
View Code Duplication |
if($validator->fails()){ |
|
|
|
|
118
|
|
|
$check = $nilai->where('user_id',$request->user_id)->orWhere('siswa_id',$request->siswa_id)->whereNull('deleted_at')->count(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if ($check > 0) { |
121
|
|
|
$response['message'] = 'Failed ! Username, Nama Siswa, already exists'; |
|
|
|
|
122
|
|
|
} else { |
123
|
|
|
$nilai->user_id = $request->input('user_id'); |
|
|
|
|
124
|
|
|
$nilai->siswa_id = $request->input('siswa_id'); |
|
|
|
|
125
|
|
|
$nilai->akademik_id = $request->input('akademik_id'); |
|
|
|
|
126
|
|
|
$nilai->prestasi_id = $request->input('prestasi_id'); |
|
|
|
|
127
|
|
|
$nilai->zona_id = $request->input('zona_id'); |
|
|
|
|
128
|
|
|
$nilai->sktm_id = $request->input('sktm_id'); |
|
|
|
|
129
|
|
|
$nilai->save(); |
130
|
|
|
|
131
|
|
|
$response['message'] = 'success'; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} else { |
134
|
|
|
$nilai->user_id = $request->input('user_id'); |
|
|
|
|
135
|
|
|
$nilai->siswa_id = $request->input('siswa_id'); |
|
|
|
|
136
|
|
|
$nilai->akademik_id = $request->input('akademik_id'); |
|
|
|
|
137
|
|
|
$nilai->prestasi_id = $request->input('prestasi_id'); |
|
|
|
|
138
|
|
|
$nilai->zona_id = $request->input('zona_id'); |
|
|
|
|
139
|
|
|
$nilai->sktm_id = $request->input('sktm_id'); |
|
|
|
|
140
|
|
|
$nilai->save(); |
141
|
|
|
|
142
|
|
|
$response['message'] = 'success'; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$response['status'] = true; |
146
|
|
|
|
147
|
|
|
return response()->json($response); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Store a newly created resource in storage. |
152
|
|
|
* |
153
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
154
|
|
|
* @return \Illuminate\Http\Response |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$nilai = $this->nilai->findOrFail($id); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$response['user'] = $nilai->user; |
|
|
|
|
161
|
|
|
$response['nilai'] = $nilai; |
162
|
|
|
$response['siswa'] = $nilai->siswa; |
163
|
|
|
$response['status'] = true; |
164
|
|
|
|
165
|
|
|
return response()->json($response); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Show the form for editing the specified resource. |
170
|
|
|
* |
171
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
172
|
|
|
* @return \Illuminate\Http\Response |
173
|
|
|
*/ |
174
|
|
|
public function edit($id) |
175
|
|
|
{ |
176
|
|
|
$nilai = $this->nilai->findOrFail($id); |
|
|
|
|
177
|
|
|
|
178
|
|
|
array_set($nilai->user, 'label', $nilai->user->name); |
179
|
|
|
array_set($nilai->siswa, 'label', $nilai->siswa->nama_siswa); |
180
|
|
|
|
181
|
|
|
$response['nilai'] = $nilai; |
|
|
|
|
182
|
|
|
$response['siswa'] = $nilai->siswa; |
183
|
|
|
$response['user'] = $nilai->user; |
184
|
|
|
$response['status'] = true; |
185
|
|
|
|
186
|
|
|
return response()->json($response); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Update the specified resource in storage. |
191
|
|
|
* |
192
|
|
|
* @param \Illuminate\Http\Request $request |
193
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
194
|
|
|
* @return \Illuminate\Http\Response |
195
|
|
|
*/ |
196
|
|
|
public function update(Request $request, $id) |
197
|
|
|
{ |
198
|
|
|
$response = array(); |
199
|
|
|
$message = array(); |
200
|
|
|
|
201
|
|
|
$nilai = $this->nilai->findOrFail($id); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$validator = Validator::make($request->all(), [ |
204
|
|
|
'user_id' => 'required|unique:nilais,user_id', |
205
|
|
|
'siswa_id' => 'required|unique:nilais,siswa_id', |
206
|
|
|
'akademik_id' => 'required|unique:nilais,akademik_id', |
207
|
|
|
'prestasi_id' => 'required|unique:nilais,prestasi_id', |
208
|
|
|
'zona_id' => 'required|unique:nilais,zona_id', |
209
|
|
|
'sktm_id' => 'required|unique:nilais,sktm_id' |
210
|
|
|
|
211
|
|
|
]); |
212
|
|
|
|
213
|
|
View Code Duplication |
if ($validator->fails()) { |
|
|
|
|
214
|
|
|
|
215
|
|
|
foreach($validator->messages()->getMessages() as $key => $error){ |
216
|
|
|
foreach($error AS $error_get) { |
217
|
|
|
array_push($message, $error_get); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$check_user = $this->nilai->where('id','!=', $id)->where('user_id', $request->user_id); |
|
|
|
|
222
|
|
|
$check_siswa = $this->nilai->where('id','!=', $id)->where('siswa_id', $request->siswa_id); |
|
|
|
|
223
|
|
|
|
224
|
|
|
if($check_user->count() > 0 || $check_siswa->count() > 0 ){ |
225
|
|
|
$response['message'] = implode("\n",$message); |
226
|
|
|
} else { |
227
|
|
|
$nilai->user_id = $request->input('user_id'); |
228
|
|
|
$nilai->siswa_id = $request->input('siswa_id'); |
229
|
|
|
$nilai->akademik_id = $request->input('akademik_id'); |
230
|
|
|
$nilai->prestasi_id = $request->input('prestasi_id'); |
231
|
|
|
$nilai->zona_id = $request->input('zona_id'); |
232
|
|
|
$nilai->sktm_id = $request->input('sktm_id'); |
233
|
|
|
$nilai->save(); |
234
|
|
|
|
235
|
|
|
$response['message'] = 'success'; |
236
|
|
|
} |
237
|
|
|
} else { |
238
|
|
|
$nilai->user_id = $request->input('user_id'); |
239
|
|
|
$nilai->siswa_id = $request->input('siswa_id'); |
240
|
|
|
$nilai->akademik_id = $request->input('akademik_id'); |
241
|
|
|
$nilai->prestasi_id = $request->input('prestasi_id'); |
242
|
|
|
$nilai->zona_id = $request->input('zona_id'); |
243
|
|
|
$nilai->sktm_id = $request->input('sktm_id'); |
244
|
|
|
$nilai->save(); |
245
|
|
|
|
246
|
|
|
$response['message'] = 'success'; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$response['status'] = true; |
250
|
|
|
|
251
|
|
|
return response()->json($response); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Remove the specified resource from storage. |
256
|
|
|
* |
257
|
|
|
* @param \App\Nilai $nilai |
|
|
|
|
258
|
|
|
* @return \Illuminate\Http\Response |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$nilai = $this->nilai->findOrFail($id); |
|
|
|
|
263
|
|
|
|
264
|
|
|
if ($nilai->delete()) { |
265
|
|
|
$response['status'] = true; |
|
|
|
|
266
|
|
|
} else { |
267
|
|
|
$response['status'] = false; |
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return json_encode($response); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
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.