320 lines
13 KiB
PHP
320 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\frontend;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Blog;
|
|
use App\Models\Builder_page;
|
|
use App\Models\Category;
|
|
use App\Models\Certificate;
|
|
use App\Models\Course;
|
|
use App\Models\Lesson;
|
|
use App\Models\Message;
|
|
use App\Models\Message_code;
|
|
use App\Models\Review;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
|
use Illuminate\Support\Facades\Log;
|
|
use DB;
|
|
|
|
use App\Http\Controllers\NewsletterController;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
function homepage_switcher($id){
|
|
session(['home' => $id]);
|
|
return redirect(route('home'));
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if(session('home')){
|
|
$page_builder = Builder_page::where('id', session('home'))->first();
|
|
}else{
|
|
$page_builder = Builder_page::where('status', 1)->first();
|
|
}
|
|
|
|
if ($page_builder && $page_builder->is_permanent == 1) {
|
|
$page_data['blogs'] = Blog::where('status', 1)->orderBy('is_popular', 'desc')->orderBy('id', 'desc')->take(3)->get();
|
|
$page_data['reviews'] = Review::all();
|
|
return view('components.home_permanent_templates.' . $page_builder->identifier, $page_data);
|
|
} else {
|
|
$page_data['instructor'] = User::join('courses', 'users.id', 'courses.user_id')
|
|
->select('users.*', 'courses.title as course_title')
|
|
->get()->unique()->take(4);
|
|
|
|
$page_data['blogs'] = Blog::where('status', 1)->orderBy('is_popular', 'desc')->orderBy('id', 'desc')->take(3)->get();
|
|
$page_data['category'] = Category::take(8)->get();
|
|
|
|
$view_path = 'frontend' . '.' . get_frontend_settings('theme') . '.home.index';
|
|
return view($view_path, $page_data);
|
|
}
|
|
}
|
|
|
|
public function download_certificate($identifier)
|
|
{
|
|
$certificate = Certificate::where('identifier', $identifier);
|
|
if ($certificate->count() > 0) {
|
|
$qr_code_content_value = route('certificate', ['identifier' => $identifier]);
|
|
$qrcode = QrCode::size(300)->generate($qr_code_content_value);
|
|
|
|
$page_data['certificate'] = $certificate->first();
|
|
$page_data['qrcode'] = $qrcode;
|
|
|
|
// dd($qr_code_content_value);
|
|
return view('curriculum.certificate.download', $page_data);
|
|
} else {
|
|
return redirect(route('home'))->with('error', get_phrase('Certificate not found at this url'));
|
|
}
|
|
}
|
|
|
|
public function update_watch_history_with_duration(Request $request)
|
|
{
|
|
$userId = auth()->user()->id;
|
|
$courseProgress = 0;
|
|
$isCompleted = 0;
|
|
|
|
// Retrieve and sanitize input data
|
|
$courseId = (int) htmlspecialchars($request->input('course_id'));
|
|
$lessonId = (int) htmlspecialchars($request->input('lesson_id'));
|
|
$currentDuration = htmlspecialchars($request->input('current_duration'));
|
|
|
|
// Fetch current watch history record
|
|
$currentHistory = DB::table('watch_durations')
|
|
->where([
|
|
'watched_course_id' => $courseId,
|
|
'watched_lesson_id' => $lessonId,
|
|
'watched_student_id' => $userId,
|
|
])
|
|
->first();
|
|
|
|
// Fetch course details
|
|
$courseDetails = DB::table('courses')->where('id', $courseId)->first();
|
|
|
|
if (!$courseDetails) {
|
|
return response()->json([
|
|
'lesson_id' => $lessonId,
|
|
'course_progress' => 0,
|
|
'is_completed' => 0,
|
|
'error' => 'Course not found'
|
|
]);
|
|
}
|
|
|
|
$dripContentSettings = json_decode($courseDetails->drip_content_settings, true) ?? [];
|
|
|
|
if ($currentHistory) {
|
|
$watchedDurationArr = json_decode($currentHistory->watched_counter, true);
|
|
if (!is_array($watchedDurationArr)) {
|
|
$watchedDurationArr = [];
|
|
}
|
|
|
|
// Pastikan tidak ada duplikat
|
|
if (!in_array($currentDuration, $watchedDurationArr)) {
|
|
$watchedDurationArr[] = $currentDuration;
|
|
sort($watchedDurationArr);
|
|
}
|
|
|
|
DB::table('watch_durations')
|
|
->where([
|
|
'watched_course_id' => $courseId,
|
|
'watched_lesson_id' => $lessonId,
|
|
'watched_student_id' => $userId,
|
|
])
|
|
->update([
|
|
'watched_counter' => json_encode($watchedDurationArr),
|
|
'current_duration' => $currentDuration,
|
|
'updated_at' => now(),
|
|
]);
|
|
} else {
|
|
$watchedDurationArr = [$currentDuration];
|
|
DB::table('watch_durations')->insert([
|
|
'watched_course_id' => $courseId,
|
|
'watched_lesson_id' => $lessonId,
|
|
'watched_student_id' => $userId,
|
|
'current_duration' => $currentDuration,
|
|
'watched_counter' => json_encode($watchedDurationArr),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
|
|
// Jika drip content tidak aktif, return early
|
|
if ($courseDetails->enable_drip_content != 1) {
|
|
return response()->json([
|
|
'lesson_id' => $lessonId,
|
|
'course_progress' => 0,
|
|
'is_completed' => 0
|
|
]);
|
|
}
|
|
|
|
// Fetch lesson details for duration calculations
|
|
$lessonTotalDuration = DB::table('lessons')->where('id', $lessonId)->value('duration');
|
|
if (!$lessonTotalDuration) {
|
|
return response()->json([
|
|
'lesson_id' => $lessonId,
|
|
'course_progress' => 0,
|
|
'is_completed' => 0,
|
|
'error' => 'Lesson duration not found'
|
|
]);
|
|
}
|
|
|
|
$lessonTotalDurationArr = explode(':', $lessonTotalDuration);
|
|
if (count($lessonTotalDurationArr) < 3) {
|
|
return response()->json([
|
|
'lesson_id' => $lessonId,
|
|
'course_progress' => 0,
|
|
'is_completed' => 0,
|
|
'error' => 'Invalid duration format'
|
|
]);
|
|
}
|
|
|
|
$lessonTotalSeconds = ($lessonTotalDurationArr[0] * 3600) +
|
|
($lessonTotalDurationArr[1] * 60) +
|
|
$lessonTotalDurationArr[2];
|
|
$currentTotalSeconds = count($watchedDurationArr) * 5;
|
|
|
|
// Drip content completion logic
|
|
if (!empty($dripContentSettings) && isset($dripContentSettings['lesson_completion_role'])) {
|
|
if ($dripContentSettings['lesson_completion_role'] == 'duration') {
|
|
$minimumDuration = $dripContentSettings['minimum_duration'] ?? 0;
|
|
if ($currentTotalSeconds >= $minimumDuration) {
|
|
$isCompleted = 1;
|
|
} elseif (($currentTotalSeconds + 4) >= $lessonTotalSeconds) {
|
|
$isCompleted = 1;
|
|
}
|
|
} else {
|
|
$minimumPercentage = $dripContentSettings['minimum_percentage'] ?? 0;
|
|
$requiredDuration = ($lessonTotalSeconds / 100) * $minimumPercentage;
|
|
if ($currentTotalSeconds >= $requiredDuration) {
|
|
$isCompleted = 1;
|
|
} elseif (($currentTotalSeconds + 4) >= $lessonTotalSeconds) {
|
|
$isCompleted = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update course progress if the lesson is completed
|
|
if ($isCompleted == 1) {
|
|
$watchHistory = DB::table('watch_histories')
|
|
->where([
|
|
'course_id' => $courseId,
|
|
'student_id' => $userId,
|
|
])
|
|
->first();
|
|
|
|
if ($watchHistory) {
|
|
$lessonIds = json_decode($watchHistory->completed_lesson, true);
|
|
|
|
if (!is_array($lessonIds)) {
|
|
$lessonIds = [];
|
|
} else {
|
|
// Konversi semua nilai ke integer
|
|
$lessonIds = array_map('intval', $lessonIds);
|
|
$lessonIds = array_values(array_unique($lessonIds));
|
|
sort($lessonIds);
|
|
}
|
|
|
|
if (!in_array($lessonId, $lessonIds)) {
|
|
// Tambahkan lesson baru
|
|
$lessonIds[] = $lessonId;
|
|
$lessonIds = array_unique($lessonIds);
|
|
sort($lessonIds);
|
|
|
|
// Hitung total lesson yang valid di course
|
|
$totalLesson = DB::table('lessons')->where('course_id', $courseId)->count();
|
|
|
|
// Hitung progress yang akurat
|
|
if ($totalLesson > 0) {
|
|
// Ambil semua lesson ID yang valid di course ini
|
|
$validLessonIds = DB::table('lessons')
|
|
->where('course_id', $courseId)
|
|
->pluck('id')
|
|
->map(function($id) {
|
|
return (int)$id;
|
|
})
|
|
->toArray();
|
|
|
|
// Hitung hanya lesson yang valid (ada di course ini)
|
|
$completedValidLessons = array_intersect($lessonIds, $validLessonIds);
|
|
$completedCount = count($completedValidLessons);
|
|
|
|
$courseProgress = ($completedCount / $totalLesson) * 100;
|
|
$courseProgress = round($courseProgress, 2);
|
|
$courseProgress = min(100, $courseProgress); // Pastikan tidak lebih dari 100%
|
|
}
|
|
|
|
// Tentukan completed_date
|
|
$updateData = [
|
|
'course_progress' => $courseProgress,
|
|
'completed_lesson' => json_encode($lessonIds, JSON_NUMERIC_CHECK), // Flag penting!
|
|
'watching_lesson_id' => $lessonId,
|
|
'updated_at' => now(),
|
|
];
|
|
|
|
// Set completed_date hanya jika progress 100% dan belum ada
|
|
if ($courseProgress >= 100 && !$watchHistory->completed_date) {
|
|
$updateData['completed_date'] = now();
|
|
} elseif ($watchHistory->completed_date) {
|
|
$updateData['completed_date'] = $watchHistory->completed_date;
|
|
}
|
|
|
|
DB::table('watch_histories')
|
|
->where('id', $watchHistory->id)
|
|
->update($updateData);
|
|
|
|
} else {
|
|
// Jika lesson sudah ada, ambil progress yang ada
|
|
$courseProgress = $watchHistory->course_progress ?? 0;
|
|
}
|
|
} else {
|
|
// Buat record baru di watch_histories
|
|
$lessonIds = [$lessonId];
|
|
$totalLesson = DB::table('lessons')->where('course_id', $courseId)->count();
|
|
|
|
if ($totalLesson > 0) {
|
|
$courseProgress = (1 / $totalLesson) * 100;
|
|
$courseProgress = round($courseProgress, 2);
|
|
}
|
|
|
|
$isCourseCompleted = $courseProgress >= 100;
|
|
|
|
dd(json_encode($lessonIds, JSON_NUMERIC_CHECK));
|
|
|
|
DB::table('watch_histories')->insert([
|
|
'course_id' => $courseId,
|
|
'student_id' => $userId,
|
|
'watching_lesson_id' => $lessonId,
|
|
'completed_lesson' => json_encode($lessonIds, JSON_NUMERIC_CHECK),
|
|
'course_progress' => $courseProgress,
|
|
'completed_date' => $isCourseCompleted ? now() : null,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Return the response
|
|
return response()->json([
|
|
'lesson_id' => $lessonId,
|
|
'course_progress' => round($courseProgress),
|
|
'is_completed' => $isCompleted,
|
|
'current_total_seconds' => $currentTotalSeconds,
|
|
'lesson_total_seconds' => $lessonTotalSeconds,
|
|
]);
|
|
}
|
|
|
|
public function sendEmailToAssignedAddresses()
|
|
{
|
|
// Accessing the method from NewsletterController
|
|
$newsletterController = new NewsletterController();
|
|
$response = $newsletterController->sendEmailToAssignedAddresses();
|
|
|
|
if ($response) {
|
|
return response($response);
|
|
}
|
|
return response('No response', 404); // Default response if no data
|
|
}
|
|
}
|