Skip to content

Commit

Permalink
role kepala bengkel permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
akechi17 committed Aug 14, 2024
1 parent 6a965fd commit b4053a8
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 29 deletions.
188 changes: 188 additions & 0 deletions app/Http/Controllers/TeacherController.php
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');
}
}
}
35 changes: 33 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ public function getData($search=null, $status=null, $school=null, $department=nu
if ($isManager) {
$users->manager(auth()->user()->schools()->first()->id);
}
if ($isKaprog || $isKabeng) {
if ($isKaprog) {
$users->kaprog(auth()->user()->departments()->first()->id);
}
if ($isKabeng) {
$users->whereHas('roles', function($query) {
$query->where('name', 'mentor');
});
}
if ($isTeacher) {
$users = $users->teacher();
}
Expand Down Expand Up @@ -307,6 +312,10 @@ public function edit($id)
->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();
} elseif ($isKaprog) {
$roles = Role::where('name', 'student')->orWhere('name', 'kepala program')->orWhere('name', 'mentor')->pluck('name', 'id');
$schoolId = auth()->user()->schools()->first()->id;
Expand All @@ -319,6 +328,10 @@ public function edit($id)
->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();
} elseif ($isKabeng) {
$roles = Role::where('name', 'mentor')->pluck('name', 'id');
$schoolId = auth()->user()->schools()->first()->id;
Expand All @@ -331,6 +344,10 @@ public function edit($id)
->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();
} else {
$roles = auth()->user()->hasRole('super-admin')
? Role::pluck('name', 'id')
Expand All @@ -344,9 +361,13 @@ public function edit($id)
->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();
}

return view('users.edit', compact('user', 'schools', 'departments', 'courses', 'roles', 'companies', 'students'));
return view('users.edit', compact('user', 'schools', 'departments', 'courses', 'roles', 'companies', 'students', 'selectedStudentIds'));
} catch (\Exception $e) {
return redirect()->route('users.index')
->with('error', 'User tidak ditemukan');
Expand Down Expand Up @@ -374,6 +395,7 @@ public function update(Request $request, $id)
'department_id' => 'nullable|exists:departments,id',
'course_id' => 'nullable|exists:courses,id',
'company_id' => 'nullable|exists:companies,id',
'student_ids' => 'nullable|array',
'status' => 'boolean',
]);

Expand All @@ -398,6 +420,15 @@ public function update(Request $request, $id)
if ($request->company_id) {
$user->companies()->sync($request->company_id);
}
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('users.index')
->with('success', 'User berhasil diubah');
Expand Down
7 changes: 7 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ public function scopeKaprog($query, $department_id)
});
}

public function scopeKabeng($query)
{
return $query->whereHas('roles', function ($query) {
$query->where('name', 'teacher');
});
}

public function scopeMentor($query, $company_id)
{
return $query->whereRelation('companies', 'id', $company_id)
Expand Down
9 changes: 9 additions & 0 deletions database/seeders/MenuSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public function run()
'parent_id' => null,
'permission_id' => Permission::where('name', 'student-list')->first()->id,
],
[
'name' => 'Guru',
'icon' => 'mdi:account-multiple',
'url' => 'teachers',
'status' => 1,
'order' => 11,
'parent_id' => null,
'permission_id' => Permission::where('name', 'teacher-list')->first()->id,
],
[
'name' => 'IDUKA',
'icon' => 'mdi:building',
Expand Down
2 changes: 2 additions & 0 deletions database/seeders/PermissionTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function run()

'student-list',

'teacher-list',

'journal-list',
'journal-create',
'journal-edit',
Expand Down
36 changes: 36 additions & 0 deletions resources/views/teachers/edit.blade.php
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
32 changes: 32 additions & 0 deletions resources/views/teachers/index.blade.php
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
Loading

0 comments on commit b4053a8

Please sign in to comment.