215 lines
8.5 KiB
PHP
215 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Certificate;
|
|
use App\Models\Course;
|
|
use App\Models\Enrollment;
|
|
use App\Models\Forum;
|
|
use App\Models\Lesson;
|
|
use App\Models\Watch_history;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class PlayerController extends Controller
|
|
{
|
|
public function reload_sidebar($course_id, $lesson_id)
|
|
{
|
|
$course_details = Course::findOrFail($course_id);
|
|
$lesson_details = Lesson::findOrFail($lesson_id);
|
|
|
|
return view('course_player.side_bar', compact(
|
|
'course_details',
|
|
'lesson_details'
|
|
));
|
|
}
|
|
|
|
public function course_player(Request $request, $slug, $id = '')
|
|
{
|
|
$course = Course::where('slug', $slug)->firstOrNew();
|
|
|
|
// check if course is paid
|
|
if ($course->is_paid && auth()->user()->role != 'admin') {
|
|
if (auth()->user()->role == 'student') { // for student check enrollment status
|
|
$enroll_status = enroll_status($course->id, auth()->user()->id);
|
|
if ($enroll_status == 'expired') {
|
|
Session::flash('error', get_phrase('Your course accessibility has expired. You need to buy it again'));
|
|
return redirect()->route('course.details', ['slug' => $slug]);
|
|
} elseif(! $enroll_status) {
|
|
Session::flash('error', get_phrase('Not registered for this course.'));
|
|
return redirect()->route('course.details', ['slug' => $slug]);
|
|
}
|
|
} elseif (auth()->user()->role == 'instructor') { // for instructor check who is course instructor
|
|
if ($course->user_id != auth()->user()->id) {
|
|
Session::flash('error', get_phrase('Not valid instructor.'));
|
|
return redirect()->route('my.courses');
|
|
}
|
|
}
|
|
}
|
|
//cek histori tontonan
|
|
$check_lesson_history = Watch_history::where('course_id', $course->id)
|
|
->where('student_id', auth()->user()->id)->first();
|
|
$first_lesson_of_course = Lesson::where('course_id', $course->id)->orderBy('section_id', 'asc')->value('id');
|
|
if ($id == '') {
|
|
$id = $check_lesson_history->watching_lesson_id ?? $first_lesson_of_course;
|
|
}
|
|
|
|
|
|
// if user has any watched history or not
|
|
if (! $check_lesson_history && $id > 0) {
|
|
$data = [
|
|
'course_id' => $course->id,
|
|
'student_id' => auth()->user()->id,
|
|
'watching_lesson_id' => $id,
|
|
'completed_lesson' => json_encode([])
|
|
];
|
|
Watch_history::insert($data);
|
|
}
|
|
|
|
// when user plays a lesson, update that lesson id as watch history
|
|
if ($id > 0) {
|
|
Watch_history::where('course_id', $course->id)
|
|
->where('student_id', auth()->user()->id)
|
|
->update(['watching_lesson_id' => $id]);
|
|
}
|
|
|
|
$page_data['course_details'] = $course;
|
|
$page_data['lesson_details'] = Lesson::where('id', $id)->firstOrNew();
|
|
$page_data['history'] = Watch_history::where('course_id', $course->id)->where('student_id', auth()->user()->id)->first();
|
|
|
|
$forum_query = Forum::join('users', 'forums.user_id', 'users.id')
|
|
->select('forums.*', 'users.name as user_name', 'users.photo as user_photo')
|
|
->latest('forums.id')
|
|
->where('forums.parent_id', 0)
|
|
->where('forums.course_id', $course->id);
|
|
|
|
if (isset($_GET['search'])) {
|
|
$forum_query->where(function ($query) use ($request) {
|
|
$query->where('forums.title', 'like', '%' . $request->search . '%')->where('forums.description', 'like', '%' . $request->search . '%');
|
|
});
|
|
}
|
|
|
|
$page_data['questions'] = $forum_query->get();
|
|
|
|
return view('course_player.index', $page_data);
|
|
}
|
|
|
|
public function set_watch_history(Request $request)
|
|
{
|
|
$course = Course::where('id', $request->course_id)->first();
|
|
$enrollment = Enrollment::where('course_id', $course->id)->where('user_id', auth()->user()->id)->first();
|
|
if (! $enrollment && (auth()->user()->role != 'admin' || ! is_course_instructor($course->id))) {
|
|
Session::flash('error', get_phrase('Not registered for this course.'));
|
|
return redirect()->back();
|
|
}
|
|
|
|
$data['course_id'] = $request->course_id;
|
|
$data['student_id'] = auth()->user()->id;
|
|
|
|
$total_lesson = Lesson::where('course_id', $request->course_id)->pluck('id')->toArray();
|
|
$watch_history = Watch_history::where('course_id', $request->course_id)->where('student_id', auth()->user()->id)->first();
|
|
$student_id = auth()->user()->id;
|
|
|
|
if (isset($watch_history) && $watch_history->id) {
|
|
$completed_lessons = [];
|
|
if (!empty($watch_history->completed_lesson)) {
|
|
$decoded = json_decode($watch_history->completed_lesson, true);
|
|
if (is_array($decoded)) {
|
|
$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));
|
|
}
|
|
}
|
|
}
|
|
|
|
$completed_lessons = array_unique($completed_lessons);
|
|
sort($completed_lessons);
|
|
|
|
$lesson_id_int = (int)$request->lesson_id;
|
|
if (!in_array($lesson_id_int, $completed_lessons)) {
|
|
$completed_lessons[] = $lesson_id_int;
|
|
sort($completed_lessons);
|
|
}
|
|
|
|
$data['completed_lesson'] = json_encode($completed_lessons);
|
|
$data['watching_lesson_id'] = $lesson_id_int;
|
|
|
|
Log::info('Updated lessons (integer array):', $completed_lessons);
|
|
|
|
Watch_history::where('course_id', $request->course_id)
|
|
->where('student_id', $student_id)
|
|
->update($data);
|
|
} else {
|
|
$lesson_id_int = (int)$request->lesson_id;
|
|
$completed_lessons = [$lesson_id_int];
|
|
|
|
$data['completed_lesson'] = json_encode($completed_lessons);
|
|
$data['watching_lesson_id'] = $lesson_id_int;
|
|
|
|
Log::info('New lessons (integer array):', $completed_lessons);
|
|
|
|
Watch_history::create($data);
|
|
}
|
|
|
|
// **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;
|
|
}
|
|
Log::info('Progress percentage: ' . $progress_percentage);
|
|
// Jika progress 100% dan status project diterima, buat sertifikat
|
|
if ($progress_percentage >= 100) {
|
|
$certificateExists = Certificate::where('user_id', $student_id)
|
|
->where('course_id', $request->course_id)
|
|
->exists();
|
|
|
|
if (!$certificateExists) {
|
|
if (method_exists($this, 'generateIdentifier')) {
|
|
$identifier = $this->generateIdentifier(12);
|
|
} else {
|
|
$identifier = \Illuminate\Support\Str::random(12);
|
|
}
|
|
|
|
Certificate::create([
|
|
'user_id' => $student_id,
|
|
'course_id' => $request->course_id,
|
|
'identifier' => $identifier,
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
private function generateIdentifier($length = 12)
|
|
{
|
|
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
|
|
return $randomString;
|
|
}
|
|
|
|
|
|
|
|
|
|
public function prepend_watermark()
|
|
{
|
|
return view('course_player.watermark');
|
|
}
|
|
}
|