Skip to content

Commit

Permalink
add teacher role
Browse files Browse the repository at this point in the history
  • Loading branch information
akechi17 committed Aug 10, 2024
1 parent ea499ae commit 7b2119f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 31 deletions.
26 changes: 24 additions & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Company;
use App\Models\Department;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
Expand Down Expand Up @@ -146,6 +147,10 @@ public function create()
->join('schools', 'departments.school_id', '=', 'schools.id')
->where('schools.id', $schoolId)
->get();
$modelIds = DB::table('model_has_roles')
->where('role_id', 6)
->pluck('model_id');
$students = User::whereIn('id', $modelIds)->get();
} 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 @@ -154,6 +159,10 @@ public function create()
$departments = Department::where('id', $departmentId)->pluck('name', 'id');
$courses = Course::where('department_id', $departmentId)->pluck('name', 'id');
$companies = Company::where('department_id', $departmentId)->get();
$modelIds = DB::table('model_has_roles')
->where('role_id', 6)
->pluck('model_id');
$students = User::whereIn('id', $modelIds)->get();
} else {
$roles = auth()->user()->hasRole('super-admin')
? Role::pluck('name', 'id')
Expand All @@ -163,9 +172,13 @@ public function create()
$departments = Department::pluck('name', 'id');
$courses = Course::pluck('name', 'id');
$companies = Company::all();
$modelIds = DB::table('model_has_roles')
->where('role_id', 6)
->pluck('model_id');
$students = User::whereIn('id', $modelIds)->get();
}

return view('users.create', compact('schools', 'departments', 'courses', 'roles', 'companies'));
return view('users.create', compact('schools', 'departments', 'courses', 'roles', 'companies', 'students'));
}

/**
Expand All @@ -184,7 +197,8 @@ public function store(Request $request)
'school_id' => 'nullable|exists:schools,id',
'department_id' => 'nullable|exists:departments,id',
'course_id' => 'nullable|exists:courses,id',
'company_id' => 'nullable|exists:companies,id'
'company_id' => 'nullable|exists:companies,id',
'student_ids' => 'nullable|array'
]);

try {
Expand All @@ -210,6 +224,14 @@ public function store(Request $request)
if ($request->company_id) {
$user->companies()->attach($request->company_id);
}
if ($request->student_ids) {
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', 'Berhasil menambahkan user baru');
Expand Down
48 changes: 27 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"font-awesome": "^4.7.0",
"iconify-icon": "^1.0.8",
"jquery": "^3.7.1",
"select2": "^4.1.0-rc.0",
"sweetalert": "^2.1.2",
"trix": "^2.0.7",
"vanilla-picker": "^2.12.2"
Expand Down
2 changes: 1 addition & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import "./bootstrap";
import "./bootstrap";
4 changes: 2 additions & 2 deletions resources/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ window.swal = swal;
import utils from "./utils";
window.utils = utils;

import $ from "jquery";
window.$ = $;
// import $ from "jquery";
// window.$ = $;

import "iconify-icon";

Expand Down
4 changes: 4 additions & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf_token" content="{{ csrf_token() }}" />
<link rel="icon" type="image/svg+xml" href={{ asset('/img/favicon.ico') }} />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<style>
.notification-dot {
height: 10px;
Expand All @@ -26,6 +27,9 @@
<div class="content">
@yield('content')
</div>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>

@stack('scripts')
</body>

Expand Down
34 changes: 29 additions & 5 deletions resources/views/users/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<x-form.input-password label="Password *" id="input-password" name="password" />
<x-form.input-password label="Ulangi Password *" id="input-confirm-password" name="confirm-password" />
<div class="form-group">
<label class="form-label">Role</label>
<label class="form-label">Role *</label>
<select class="form-select" name="role_id" id="input-role">
<option selected hidden value="">Pilih</option>
@foreach ($roles as $key => $value)
Expand All @@ -20,7 +20,7 @@
<input type="hidden" value="1" name="school_id">

<div id="department-wrapper" class="d-none">
<x-form.select label="Kompetensi Keahlian" id="input-department" name="department_id">
<x-form.select label="Kompetensi Keahlian *" id="input-department" name="department_id">
<option selected hidden>Pilih</option>
<x-slot:options>
@foreach ($departments as $key => $value)
Expand All @@ -31,7 +31,7 @@
</div>

<div id="course-wrapper" class="d-none">
<x-form.select label="Kelas" id="input-courses" name="course_id">
<x-form.select label="Kelas *" id="input-courses" name="course_id">
<option selected hidden>Pilih</option>
<x-slot:options>
@foreach ($courses as $key => $value)
Expand All @@ -42,7 +42,7 @@
</div>

<div id="company-wrapper" class="d-none">
<x-form.select label="IDUKA" id="input-companies" name="company_id">
<x-form.select label="IDUKA *" id="input-companies" name="company_id">
<option selected hidden>Pilih</option>
<x-slot:options>
@foreach ($companies as $company)
Expand All @@ -51,17 +51,28 @@
</x-slot:options>
</x-form.select>
</div>

<div id="student-wrapper" class="form-group d-none">
<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 }}">{{ $student->name }} - {{ $student->courses()->first()?->name }}</option>
@endforeach
</select>
</div>
</x-slot:formBody>
</x-form.form>
@endsection

@push('scripts')
<script>
<script type="module">
document.addEventListener('DOMContentLoaded', function() {
const roleSelect = document.getElementById('input-role');
const departmentWrapper = document.getElementById('department-wrapper');
const courseWrapper = document.getElementById('course-wrapper');
const companyWrapper = document.getElementById('company-wrapper');
const studentWrapper = document.getElementById('student-wrapper');
if (roleSelect && departmentWrapper) {
roleSelect.addEventListener('change', function() {
Expand All @@ -71,23 +82,36 @@
departmentWrapper.classList.remove('d-none');
courseWrapper.classList.add('d-none');
companyWrapper.classList.add('d-none');
studentWrapper.classList.add('d-none');
} else if (selectedRole === 'mentor') {
departmentWrapper.classList.add('d-none');
companyWrapper.classList.remove('d-none');
courseWrapper.classList.add('d-none');
studentWrapper.classList.add('d-none');
} else if (selectedRole === 'student') {
departmentWrapper.classList.add('d-none');
companyWrapper.classList.add('d-none');
courseWrapper.classList.remove('d-none');
studentWrapper.classList.add('d-none');
} else if (selectedRole === 'teacher') {
departmentWrapper.classList.add('d-none');
companyWrapper.classList.add('d-none');
courseWrapper.classList.add('d-none');
studentWrapper.classList.remove('d-none');
} else {
departmentWrapper.classList.add('d-none');
courseWrapper.classList.add('d-none');
companyWrapper.classList.add('d-none');
studentWrapper.classList.add('d-none');
}
});
} else {
console.error('Elements not found');
}
});
$(document).ready(function() {
$("#input-student").select2();
});
</script>
@endpush

0 comments on commit 7b2119f

Please sign in to comment.