267 lines
11 KiB
PHP
267 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Modules\Siswa\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use DataTables;
|
|
use Modules\Seller\Entities\TicketModel;
|
|
use Modules\Seller\Entities\TicketResponseModel;
|
|
use Modules\Seller\Events\TicketCreated;
|
|
use App\Enums\GlobalEnum;
|
|
|
|
use App\Models\Siswa;
|
|
use App\Models\User;
|
|
use App\Models\Konselor;
|
|
use App\Models\LogActivites;
|
|
|
|
class TicketController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = TicketModel::where('user_id', auth()->user()->id)->orderBy('created_at', 'desc');
|
|
// Convert the Eloquent Collection to a regular PHP array
|
|
$data->each(function ($item, $key) {
|
|
$item->rowIndex = $key + 1;
|
|
});
|
|
|
|
return Datatables::of($data)
|
|
->addIndexColumn()
|
|
->addColumn('title-post', function($row){
|
|
$text = '
|
|
<p class="mb-0">' . $row->subject . '</p>
|
|
<p class="mb-0 small">Terakhir diperbarui pada ' . date_formatting($row->updated_at, 'timeago') . '</p>
|
|
';
|
|
return $text;
|
|
})
|
|
->addColumn('siswa', function($row){
|
|
return User::where('id', $row->konselor_id)->first()->name;
|
|
})
|
|
->addColumn('action', function($row){
|
|
$view = route('ticket.view', ['id' => $row->id]);
|
|
$delete = route('ticket.delete', ['id' => $row->id]);
|
|
$btn = '
|
|
<a href="' . $view . '" class="btn btn-light btn-sm px-4"><i class="ki-outline ki-eye"></i></a>
|
|
<a data-url="' . $delete . '" href="#" class="btn btn-light btn-sm deleteContent px-4"><i class="ki-outline ki-trash"></i></a>
|
|
';
|
|
return $btn;
|
|
})
|
|
->addColumn('status', function($row){
|
|
if ($row->is_status == 1) {
|
|
return '<span class="mb-1 badge font-medium bg-light-dark text-dark">Pending</span>';
|
|
} elseif($row->is_status == 2) {
|
|
return '<span class="mb-1 badge font-medium bg-light-primary text-primary">User Membalas</span>';
|
|
} elseif($row->is_status == 3) {
|
|
return '<span class="mb-1 badge font-medium bg-light-info text-info">Konselor Membalas</span>';
|
|
} elseif($row->is_status == 4) {
|
|
return '<span class="mb-1 badge font-medium bg-light-danger text-danger">Ditutup</span>';
|
|
}
|
|
})
|
|
->addColumn('approval', function($row){
|
|
if ($row->is_approval_siswa == 3) {
|
|
return '<span class="mb-1 badge font-medium bg-light-dark text-dark">Wali Menunggu Persetujuan</span>';
|
|
} elseif($row->is_approval_siswa == 1) {
|
|
return '<span class="mb-1 badge font-medium bg-light-success text-success">Wali Disetujui</span>';
|
|
} elseif($row->is_approval_siswa == 4) {
|
|
return '<span class="mb-1 badge font-medium bg-light-danger text-danger">Persetujuan Ditolak</span>';
|
|
} else {
|
|
return '<span class="mb-1 badge font-medium bg-light-info text-info">-</span>';
|
|
}
|
|
})
|
|
->addColumn('priority', function($row){
|
|
if ($row->is_priority == GlobalEnum::isTicketPriorityNormal) {
|
|
return '<span class="mb-1 badge font-medium bg-light-info text-info">Normal</span>';
|
|
} elseif($row->is_priority == GlobalEnum::isTicketPriorityMedium) {
|
|
return '<span class="mb-1 badge font-medium bg-light-primary text-primary">Sedang</span>';
|
|
} elseif($row->is_priority == GlobalEnum::isTicketPriorityHigh) {
|
|
return '<span class="mb-1 badge font-medium bg-light-danger text-danger">Parah</span>';
|
|
}
|
|
})
|
|
->rawColumns(['title-post','action','status','priority','siswa', 'approval'])
|
|
->filter(function ($query) use ($request) {
|
|
if ($request->has('search')) {
|
|
$search = $request->get('search')['value'];
|
|
|
|
$filterCategory = explode('|', $search);
|
|
if($filterCategory[0] === 'status') {
|
|
if(!empty($filterCategory[1])) {
|
|
$query->where('is_status', 'LIKE', "%$filterCategory[1]%");
|
|
} else {
|
|
$query->get();
|
|
}
|
|
} elseif($filterCategory[0] === 'user') {
|
|
if(!empty($filterCategory[1])) {
|
|
$query->where('konselor_id', '=', User::where('name', 'like', "%$filterCategory[1]%")->first()->id);
|
|
} else {
|
|
$query->get();
|
|
}
|
|
}
|
|
}
|
|
})
|
|
->make(true);
|
|
}
|
|
|
|
$data = [
|
|
'subtitle' => 'Riwayat Bimbingan & Konseling',
|
|
'button' => true,
|
|
'module' => [
|
|
'url' => site_url('siswa', 'ticket/create'),
|
|
'name' => 'Buat Jadwal'
|
|
]
|
|
];
|
|
|
|
return view('siswa::ticket.index', compact('data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data = [
|
|
'subtitle' => 'Jadwal Bimbingan & Konseling',
|
|
];
|
|
|
|
$getListSiswa = User::where('level', GlobalEnum::isSiswa)->get();
|
|
return view('siswa::ticket.add', compact('data', 'getListSiswa'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'konselor_id' => 'required',
|
|
'subject' => 'required',
|
|
'is_priority' => 'required',
|
|
'type' => 'required'
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return redirect()->back()->with('swal', swal_alert('error', $validator->errors()->first()));
|
|
}
|
|
|
|
$input = $request->all();
|
|
$ticket = new TicketModel([
|
|
'id' => Str::uuid(),
|
|
'user_id' => user()->id,
|
|
'konselor_id' => $input['konselor_id'],
|
|
'subject' => $input['subject'] . ' #' . rand(111,999),
|
|
'is_priority' => $input['is_priority'],
|
|
'type' => $input['type'],
|
|
'senderAs' => 1,
|
|
'is_status' => 2,
|
|
'meet_at' => $input['meet_at'],
|
|
]);
|
|
|
|
$check = 0;
|
|
if($input['type'] == 1) {
|
|
$message = "Bapak/Ibu Konselor, Saya ingin melakukan bimbingan secara aplikasi";
|
|
$check = TicketModel::where('subject', $ticket->subject)->count();
|
|
$ticketResponse = new TicketResponseModel();
|
|
$ticketResponse->id = Str::uuid();
|
|
$ticketResponse->id_ticket = $ticket->id; // Isi dengan nilai yang sesuai
|
|
$ticketResponse->user_id = $ticket->user_id; // Isi dengan nilai yang sesuai
|
|
$ticketResponse->message = clean($message);
|
|
$ticketResponse->save();
|
|
}
|
|
|
|
$postTicket = $ticket->save();
|
|
|
|
if($check == 0) {
|
|
if($postTicket) {
|
|
return redirect()->to(site_url('siswa', 'ticket'))->with('swal', swal_alert('success', 'Anda berhasil membuka tiket baru.'));
|
|
} else {
|
|
return redirect()->back()->with('swal', swal_alert('error', 'Terjadi kesalahan saat membuka tiket.'));
|
|
}
|
|
} else {
|
|
return redirect()->back()->with('swal', swal_alert('error', 'Ditemukan data yang telah ada'));
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'message' => 'required',
|
|
]);
|
|
|
|
if($validator->fails()) {
|
|
return redirect()->back()->with('swal', swal_alert('error', $validator->errors()->first()))->withInput();
|
|
}
|
|
|
|
$input = $request->all();
|
|
$ticket = TicketModel::find($id);
|
|
if($ticket) {
|
|
$post = new TicketResponseModel([
|
|
'id' => Str::uuid(),
|
|
'id_ticket' => $id,
|
|
'user_id' => auth()->user()->id,
|
|
'message' => $input['message']
|
|
]);
|
|
|
|
$ticket->is_status = 2;
|
|
if($post->save() && $ticket->save()) {
|
|
return redirect()->route('siswa.ticket.detail', ['id' => $id])->with('swal', swal_alert('success', 'Anda berhasil membalas tiket.'));
|
|
} else {
|
|
return redirect()->route('siswa.ticket.detail', ['id' => $id])->with('swal', swal_alert('error', 'Terjadi kesalahan saat membalas tiket.'));
|
|
}
|
|
}
|
|
}
|
|
|
|
public function view($id)
|
|
{
|
|
$data = [
|
|
'subtitle' => TicketModel::where('id', $id)->first()->subject,
|
|
];
|
|
|
|
$detail = TicketModel::where('id', $id)->first();
|
|
$siswa = User::where('id', $detail->user_id)->first()->name;
|
|
$nis = Siswa::where('user_id', $detail->user_id)->first()->nis;
|
|
$ticketResponse = TicketResponseModel::where('id_ticket', $id)->orderBy('created_at', 'asc')->get();
|
|
return view('siswa::ticket.detail', compact('data', 'nis', 'ticketResponse', 'detail', 'siswa'));
|
|
}
|
|
|
|
public function accept()
|
|
{
|
|
$chat = TicketModel::find(segment(4));
|
|
if($chat)
|
|
{
|
|
$chat->is_approval_siswa = 1;
|
|
$chat->save();
|
|
|
|
return redirect()->back()->with('swal', swal_alert('success', 'Request berhasil disetujui.'));
|
|
} else {
|
|
return redirect()->back()->with('swal', swal_alert('error', 'Tidak ditemukan data yang dituju.'));
|
|
}
|
|
}
|
|
|
|
public function decline()
|
|
{
|
|
$chat = TicketModel::find(segment(4));
|
|
if($chat)
|
|
{
|
|
$chat->is_approval_siswa = 4;
|
|
$chat->save();
|
|
|
|
return redirect()->back()->with('swal', swal_alert('success', 'Request berhasil ditolak.'));
|
|
} else {
|
|
return redirect()->back()->with('swal', swal_alert('error', 'Tidak ditemukan data yang dituju.'));
|
|
}
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$ticket = TicketModel::find($id);
|
|
if($ticket) {
|
|
$detailReply = TicketResponseModel::where('id_ticket', $ticket->id)->get();
|
|
|
|
if($ticket->delete() && $detailReply->delete()) {
|
|
return redirect()->back()->with('swal', swal_alert('success', 'Anda berhasil menghapus tiket.'));
|
|
} else {
|
|
return redirect()->back()->with('swal', swal_alert('error', 'Terjadi kesalahan saat menghapus tiket.'));
|
|
}
|
|
}
|
|
}
|
|
}
|