-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
332 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Carbon\Carbon; | ||
use App\Models\User; | ||
use App\Models\Course; | ||
use Illuminate\Http\Request; | ||
use App\Models\PresenceStatus; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class TeacherController extends Controller | ||
{ | ||
public function getData($search=null, $status=null, $school=null, $department=null, $sort=null) | ||
{ | ||
$isManager = auth()->user()->hasRole('manager'); | ||
$isKaprog = auth()->user()->hasRole('kepala program'); | ||
$isKabeng = auth()->user()->hasRole('kepala bengkel'); | ||
$isTeacher = auth()->user()->hasRole('teacher'); | ||
$isMentor = auth()->user()->hasRole('mentor'); | ||
|
||
$users = User::whereRelation('roles', 'name', 'teacher'); | ||
if ($search) { | ||
$users = $users->where('name', 'like', '%' . $search . '%') | ||
->orWhere('email', 'like', '%' . $search . '%'); | ||
} | ||
if ($status) { | ||
$users = $users->where('status', $status); | ||
} | ||
if ($school) { | ||
$users = $users->whereHas('schools', function($query) use ($school) { | ||
$query->where('school_id', $school); | ||
}); | ||
} | ||
if ($department) { | ||
$users = $users->whereHas('departments', function($query) use ($department) { | ||
$query->where('department_id', $department); | ||
}); | ||
} | ||
if ($isManager) { | ||
$users = $users->whereHas('schools', function($query) { | ||
$query->where('school_id', auth()->user()->schools()->first()->id); | ||
}); | ||
} | ||
if ($isKaprog || $isKabeng) { | ||
$users = $users->kabeng(); | ||
} | ||
if ($isTeacher) { | ||
$users = $users->teacher(); | ||
} | ||
if ($isMentor) { | ||
$users = $users->mentor(auth()->user()->companies()->first()->id); | ||
} | ||
if ($sort) { | ||
if ($sort[0] == '-') { | ||
$sort = substr($sort, 1); | ||
$sortType = 'desc'; | ||
} else { | ||
$sortType = 'asc'; | ||
} | ||
|
||
$users = $users->orderBy($sort, $sortType); | ||
} | ||
$users = $users->paginate(10); | ||
|
||
if ($users->count() > 0) { | ||
$context = [ | ||
'status' => true, | ||
'message' => 'Data guru ditemukan', | ||
'teachers' => $users, | ||
'pagination' => $users->links()->render(), | ||
'search' => $search, | ||
'statusData' => $status, | ||
'school' => $school, | ||
'department' => $department, | ||
'sort' => $sort, | ||
]; | ||
} else { | ||
$context = [ | ||
'status' => false, | ||
'message' => 'Data guru tidak ditemukan', | ||
'teachers' => [], | ||
'pagination' => $users->links()->render(), | ||
'search' => $search, | ||
'statusData' => $status, | ||
'school' => $school, | ||
'department' => $department, | ||
'sort' => $sort, | ||
]; | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Request $request) | ||
{ | ||
$search = $request->query('search'); | ||
$status = $request->query('status'); | ||
$school = $request->query('school'); | ||
$department = $request->query('department'); | ||
$sort = $request->query('sort'); | ||
$sort = $sort ? $sort : 'name'; | ||
|
||
$context = $this->getData($search, $status, $school, $department, $sort); | ||
|
||
return $context['status'] | ||
? view('teachers.index', $context) | ||
: view('teachers.index', $context)->with('error', $context['message']); | ||
} | ||
|
||
public function edit(Request $request, $id) | ||
{ | ||
$id = decrypt($id); | ||
|
||
|
||
$user = User::whereRelation('roles', 'name', 'teacher') | ||
->where('id', $id) | ||
->first(); | ||
|
||
$modelIds = DB::table('model_has_roles') | ||
->where('role_id', 6) | ||
->pluck('model_id'); | ||
$students = User::whereIn('id', $modelIds)->get(); | ||
$selectedStudentIds = DB::table('group_user') | ||
->where('teacher_id', $id) | ||
->pluck('user_id') | ||
->toArray(); | ||
|
||
if ($user) { | ||
|
||
$context = [ | ||
'status' => true, | ||
'message' => 'Data guru ditemukan', | ||
'teacher' => $user, | ||
'students' => $students, | ||
'selectedStudentIds' => $selectedStudentIds | ||
]; | ||
} else { | ||
$context = [ | ||
'status' => false, | ||
'message' => 'Data guru tidak ditemukan', | ||
'teacher' => null, | ||
'students' => null, | ||
'selectedStudentIds' => null | ||
]; | ||
} | ||
|
||
return view('teachers.edit', $context); | ||
} | ||
|
||
public function update(Request $request, $id) | ||
{ | ||
$id = decrypt($id); | ||
|
||
$request->validate([ | ||
'name' => 'required|string|max:255', | ||
'student_ids' => 'nullable|array' | ||
]); | ||
|
||
$user = User::whereRelation('roles', 'name', 'teacher') | ||
->where('id', $id) | ||
->first(); | ||
|
||
if ($user) { | ||
$user->update([ | ||
'name' => $request->name, | ||
]); | ||
if ($request->student_ids) { | ||
DB::table('group_user')->where('teacher_id', $id)->delete(); | ||
foreach ($request->student_ids as $studentId) { | ||
DB::table('group_user')->insert([ | ||
'teacher_id' => $user->id, | ||
'user_id' => $studentId, | ||
]); | ||
} | ||
} | ||
|
||
return redirect()->route('teachers.index')->with('success', 'Data guru berhasil diperbarui'); | ||
} else { | ||
return redirect()->route('teachers.index')->with('error', 'Data guru tidak ditemukan'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@extends('layouts.dashboard') | ||
|
||
@section('dashboard-content') | ||
<x-form.form formTitle="Edit Guru" formMethod="POST" spoofMethod="PUT" | ||
formAction="{{ route('teachers.update', ['id' => encrypt($teacher->id)]) }}"> | ||
@error(['name', 'course_id', 'skills', 'company', 'start_date', 'end_date', 'extend']) | ||
<div class="alert alert-dark text-white help-block">{{ $message }}</div> | ||
@enderror | ||
<x-slot:formBody> | ||
@if (auth()->user()->can('user-edit')) | ||
<x-form.input-base label="Nama" id="input-name" type="text" name="name" required | ||
value="{{ $teacher->name }}" /> | ||
<div id="student-wrapper" class="form-group"> | ||
<label class="form-label">Siswa Bimbingan</label> | ||
<select class="form-select" style="width: 100%" name="student_ids[]" id="input-student" multiple="multiple"> | ||
@foreach ($students as $student) | ||
<option value="{{ $student->id }}" {{ in_array($student->id, $selectedStudentIds) ? 'selected' : '' }}>{{ $student->name }} - {{ $student->courses()->first()?->name }}</option> | ||
@endforeach | ||
</select> | ||
</div> | ||
@else | ||
<x-form.input-base readonly label="Nama" id="input-name" type="text" name="name" required | ||
value="{{ $teacher->name }}" /> | ||
@endif | ||
</x-slot:formBody> | ||
</x-form.form> | ||
@endsection | ||
|
||
|
||
@push('scripts') | ||
<script type="module"> | ||
$(document).ready(function() { | ||
$("#input-student").select2(); | ||
}); | ||
</script> | ||
@endpush |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{{-- @php | ||
dd($teachers->toArray()); | ||
@endphp --}} | ||
|
||
@extends('layouts.dashboard') | ||
|
||
@section('dashboard-content') | ||
<x-table pageName="Data Guru" routeCreate="{{ route('users.create') }}" :pagination="$teachers" :tableData="$teachers"> | ||
|
||
<x-slot:thead> | ||
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 w-15"> | ||
Kelola | ||
</th> | ||
<th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 w-15"> | ||
Nama | ||
</th> | ||
</x-slot:thead> | ||
|
||
<x-slot:tbody> | ||
@foreach ($teachers as $student) | ||
<tr> | ||
<td class="text-center"> | ||
<a href="{{ route('teachers.edit', encrypt($student->id)) }}" class="btn btn-info text-xs" | ||
data-bs-toggle="tooltip" data-bs-placement="bottom" title="Edit"> | ||
<i class="bi bi-pencil-square"></i></a> | ||
</td> | ||
<td class="text-sm">{{ $student->name }}</td> | ||
</tr> | ||
@endforeach | ||
</x-slot:tbody> | ||
</x-table> | ||
@endsection |
Oops, something went wrong.