304 lines
12 KiB
PHP
304 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\instructor;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Lesson;
|
|
use App\Models\ProjectSubmission;
|
|
use App\Models\Watch_history;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class ProjectController extends Controller
|
|
{
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => 'required',
|
|
'section' => 'required|numeric',
|
|
'course_id' => 'required'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect()->back()->withErrors($validator)->withInput();
|
|
}
|
|
|
|
$data['title'] = $request->title;
|
|
$data['course_id'] = $request->course_id;
|
|
$data['section_id'] = $request->section;
|
|
$data['description'] = $request->description;
|
|
$data['attachment'] = $request->attachment;
|
|
$data['summary'] = $request->summary;
|
|
|
|
|
|
Lesson::where('id', $id)->update($data);
|
|
|
|
Session::flash('success', get_phrase('Project has been updated.'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
|
|
$maxSort = Lesson::where('section_id', $request->section)
|
|
->max('sort');
|
|
|
|
$data['user_id'] = auth()->user()->id;
|
|
$data['sort'] = $maxSort + 1;
|
|
$data['title'] = $request->title;
|
|
$data['course_id'] = $request->course_id;
|
|
$data['section_id'] = $request->section;
|
|
$data['attachment'] = $request->attachment;
|
|
$data['description'] = $request->description;
|
|
$data['summary'] = $request->summary;
|
|
$data['lesson_type'] = 'project';
|
|
$data['status'] = 1;
|
|
|
|
Lesson::insert($data);
|
|
|
|
Session::flash('success', get_phrase('Project has been created.'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
public function getSubmissions($id)
|
|
{
|
|
$participants = ProjectSubmission::join('users', 'project_submissions.user_id', '=', 'users.id')
|
|
->where('project_submissions.lesson_id', $id)
|
|
->where('project_submissions.status', 0)
|
|
->select('users.name', 'users.id')
|
|
->distinct()
|
|
->get();
|
|
|
|
return view('instructor.project.grading.index', ['participants' => $participants, 'lesson_id' => $id]);
|
|
}
|
|
|
|
public function getPreview(Request $request)
|
|
{
|
|
// Validasi input
|
|
$request->validate([
|
|
'lesson_id' => 'required|integer',
|
|
'user_id' => 'required|integer'
|
|
]);
|
|
|
|
// Cari submission terbaru
|
|
$submission = ProjectSubmission::where('lesson_id', $request->lesson_id)
|
|
->where('user_id', $request->user_id)
|
|
->latest()
|
|
->first();
|
|
// dd($submission); // <-- INI HARUS DIHAPUS ATAU DI-KOMEN
|
|
|
|
// Tentukan label komentar
|
|
$commentLabel = (!empty($submission) && !empty($submission->comment))
|
|
? "Revisi Sebelumnya"
|
|
: "Masukkan Revisi Anda";
|
|
|
|
// Return view preview
|
|
return view('instructor.project.grading.preview', compact('submission', 'commentLabel'));
|
|
}
|
|
|
|
public function getParticipantSubmission(Request $request)
|
|
{
|
|
$submissions = ProjectSubmission::where('lesson_id', $request->quizId)
|
|
->where('user_id', $request->participant)
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
$html = '<option value="">' . get_phrase('Select a submission') . '</option>';
|
|
|
|
if($submissions->count() > 0){
|
|
foreach ($submissions as $submission) {
|
|
$date = date('d M Y, H:i', strtotime($submission->created_at));
|
|
|
|
$statusLabel = '';
|
|
if($submission->status == 1) $statusLabel = ' (Lulus)';
|
|
elseif($submission->status == 2) $statusLabel = ' (Revisi)';
|
|
else $statusLabel = ' (Pending)';
|
|
|
|
$html .= '<option value="' . $submission->id . '">' . $date . $statusLabel . '</option>';
|
|
}
|
|
} else {
|
|
$html .= '<option value="" disabled>' . get_phrase('No submission found') . '</option>';
|
|
}
|
|
|
|
return response()->json($html);
|
|
}
|
|
|
|
public function updateSubmission(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'status' => 'required|in:1,2',
|
|
'comment' => 'required_if:status,2', // Wajib isi jika status 2 (Revisi)
|
|
]);
|
|
|
|
// Menggunakan Model findOrFail
|
|
$submission = ProjectSubmission::findOrFail($id);
|
|
|
|
$submission->status = $request->status;
|
|
$submission->reviewed_by = auth()->id();
|
|
$submission->reviewed_at = now();
|
|
|
|
if ($request->status == 1) {
|
|
// Jika Diterima (1), Comment jadi NULL (sesuai request)
|
|
$submission->comment = null;
|
|
} else {
|
|
// Jika Perlu Perbaikan (2), Simpan Comment
|
|
$submission->comment = $request->comment;
|
|
}
|
|
|
|
$submission->save();
|
|
|
|
// **LOGIKA WATCH HISTORY - DIPERBAIKI berdasarkan referensi**
|
|
|
|
$lesson = Lesson::findOrFail($submission->lesson_id);
|
|
|
|
$student_id = $submission->user_id;
|
|
$course_id = $lesson->course_id;
|
|
$lesson_id = $submission->lesson_id;
|
|
|
|
// Ambil semua lesson ID untuk course ini
|
|
$total_lesson = Lesson::where('course_id', $course_id)
|
|
->pluck('id')
|
|
->map(function($id) {
|
|
return (int)$id;
|
|
})
|
|
->toArray();
|
|
|
|
// Cari watch_history untuk student ini
|
|
$watch_history = Watch_history::where('course_id', $course_id)
|
|
->where('student_id', $student_id)
|
|
->first();
|
|
|
|
// **LOGIKA UTAMA berdasarkan referensi set_watch_history**
|
|
if ($watch_history) {
|
|
// Parse data lama dengan handling yang robust
|
|
$completed_lessons = [];
|
|
|
|
if (!empty($watch_history->completed_lesson)) {
|
|
$decoded = json_decode($watch_history->completed_lesson, true);
|
|
|
|
if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
|
|
// Konversi semua nilai ke integer
|
|
$completed_lessons = array_map('intval', $decoded);
|
|
} else {
|
|
// Fallback jika format tidak valid
|
|
$completed_lesson_data = trim($watch_history->completed_lesson, '[]"\' ');
|
|
if (!empty($completed_lesson_data)) {
|
|
$completed_lessons = array_map('intval', explode(',', $completed_lesson_data));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Hapus duplikat dan sortir
|
|
$completed_lessons = array_unique($completed_lessons);
|
|
sort($completed_lessons);
|
|
|
|
// **HANYA tambahkan lesson jika project DITERIMA (status 1)**
|
|
if ($request->status == 1 && !in_array($lesson_id, $completed_lessons)) {
|
|
$completed_lessons[] = $lesson_id;
|
|
sort($completed_lessons);
|
|
}
|
|
|
|
// Hitung completion dengan benar (seperti referensi)
|
|
$completed_in_course = array_intersect($completed_lessons, $total_lesson);
|
|
$is_completed = count($total_lesson) > 0 &&
|
|
count($completed_in_course) >= count($total_lesson);
|
|
|
|
$data = [
|
|
'course_id' => $course_id,
|
|
'student_id' => $student_id,
|
|
'watching_lesson_id' => $lesson_id,
|
|
'completed_lesson' => json_encode($completed_lessons, JSON_NUMERIC_CHECK), // Gunakan JSON_NUMERIC_CHECK
|
|
'completed_date' => $is_completed ? date('Y-m-d H:i:s') : null, // Format MySQL datetime
|
|
];
|
|
|
|
// **FIX: Update dengan student_id yang benar (bukan auth user)**
|
|
Watch_history::where('course_id', $course_id)
|
|
->where('student_id', $student_id)
|
|
->update($data);
|
|
|
|
// Log untuk debugging
|
|
\Log::info('Project submission watch history updated:', [
|
|
'project_submission_id' => $id,
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'lesson_id' => $lesson_id,
|
|
'completed_lessons' => $completed_lessons,
|
|
'is_completed' => $is_completed,
|
|
'total_lessons_in_course' => count($total_lesson)
|
|
]);
|
|
|
|
} else {
|
|
// **Jika tidak ada watch_history, buat baru**
|
|
// Hanya buat jika project DITERIMA (status 1)
|
|
if ($request->status == 1) {
|
|
$completed_lessons = [$lesson_id];
|
|
|
|
// Hitung completion untuk data baru
|
|
$is_completed = count($total_lesson) == 1 && in_array($lesson_id, $total_lesson);
|
|
|
|
$data = [
|
|
'course_id' => $course_id,
|
|
'student_id' => $student_id,
|
|
'watching_lesson_id' => $lesson_id,
|
|
'completed_lesson' => json_encode($completed_lessons, JSON_NUMERIC_CHECK),
|
|
'completed_date' => $is_completed ? date('Y-m-d H:i:s') : null,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
];
|
|
|
|
Watch_history::create($data);
|
|
|
|
\Log::info('New watch history created from project submission:', [
|
|
'project_submission_id' => $id,
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'lesson_id' => $lesson_id
|
|
]);
|
|
}
|
|
}
|
|
|
|
// **LOGIKA SERTIFIKAT - dengan progress yang benar**
|
|
$completed_lessons = $completed_lessons ?? [];
|
|
$completed_in_course = array_intersect($completed_lessons, $total_lesson);
|
|
$completed_count = count($completed_in_course);
|
|
$total_count = count($total_lesson);
|
|
|
|
$progress_percentage = 0;
|
|
if ($total_count > 0) {
|
|
$progress_percentage = ($completed_count / $total_count) * 100;
|
|
}
|
|
|
|
// Jika progress 100% dan status project diterima, buat sertifikat
|
|
if ($progress_percentage >= 100 && $request->status == 1) {
|
|
$certificateExists = Certificate::where('user_id', $student_id)
|
|
->where('course_id', $course_id)
|
|
->exists();
|
|
|
|
if (!$certificateExists) {
|
|
// Cek jika method generateIdentifier ada
|
|
if (method_exists($this, 'generateIdentifier')) {
|
|
$identifier = $this->generateIdentifier(12);
|
|
} else {
|
|
// Fallback ke random string
|
|
$identifier = \Illuminate\Support\Str::random(12);
|
|
}
|
|
|
|
Certificate::create([
|
|
'user_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'identifier' => $identifier,
|
|
]);
|
|
|
|
\Log::info('Certificate created after project submission:', [
|
|
'student_id' => $student_id,
|
|
'course_id' => $course_id,
|
|
'project_submission_id' => $id
|
|
]);
|
|
}
|
|
}
|
|
|
|
return redirect()->back()->with('success', get_phrase('Project graded successfully'));
|
|
}
|
|
}
|