����JFIF��x�x����'
Server IP : 66.29.137.217 / Your IP : 18.191.31.198 Web Server : LiteSpeed System : Linux premium294.web-hosting.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : gltevjme ( 1095) PHP Version : 7.0.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/gltevjme/./cbt.bofirm.com/app/Http/Controllers/Admin/ |
Upload File : |
<?php namespace App\Http\Controllers\Admin; use App\Exports\QuizSessionsExport; use App\Http\Controllers\Controller; use App\Models\Quiz; use App\Models\QuizSchedule; use App\Models\QuizSession; use App\Repositories\QuizRepository; use App\Settings\LocalizationSettings; use App\Settings\SiteSettings; use App\Transformers\Admin\QuizScoreReportTransformer; use App\Transformers\Admin\QuizSessionExportTransformer; use App\Transformers\Admin\UserQuizSessionTransformer; use App\Transformers\Platform\QuizSolutionTransformer; use Barryvdh\DomPDF\Facade as PDF; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Inertia\Inertia; use Maatwebsite\Excel\Facades\Excel; class QuizAnalyticsController extends Controller { /** * @var QuizRepository */ private QuizRepository $repository; public function __construct(QuizRepository $repository) { $this->middleware(['role:admin|instructor']); $this->repository = $repository; } /** * Get Quiz Overall Report with statistics from sessions * * @param Quiz $quiz * @return \Inertia\Response */ public function overallReport(Quiz $quiz) { $schedule = null; if(request()->has('schedule')) { $schedule = QuizSchedule::where('id', '=', request()->get('schedule'))->first(); $stats = $this->repository->getQuizSessionStats($quiz->id, request()->get('schedule')); } else { $stats = $this->repository->getQuizSessionStats($quiz->id, null); } return Inertia::render('Admin/Quiz/OverallReport', [ 'quiz' => $quiz->only('id', 'code', 'title', 'slug'), 'schedule' => $schedule != null ? $schedule->only('id', 'code') : null, 'stats' => [ 'total_attempts' => $stats[0]->total_attempts, 'pass_count' => $stats[0]->pass_count, 'failed_count' => $stats[0]->failed_count, 'unique_test_takers' => $stats[0]->unique_test_takers, 'avg_time' => formattedSeconds(round($stats[0]->avg_time, 0)), 'avg_score' => round($stats[0]->avg_score, 1).'/'.$quiz->total_marks, 'high_score' => round($stats[0]->high_score, 1), 'low_score' => round($stats[0]->low_score, 1), 'avg_percentage' => round($stats[0]->avg_percentage, 0)."%", 'avg_accuracy' => round($stats[0]->avg_accuracy, 0)."%", 'avg_speed' => round($stats[0]->avg_speed, 0)." que/hr", 'avg_questions_answered' => round(calculatePercentage($stats[0]->sum_answered, $stats[0]->sum_questions), 0)."%", ], ]); } /** * Get Quiz Detailed Report with user sessions * * @param Quiz $quiz * @return \Inertia\Response */ public function detailedReport(Quiz $quiz) { $schedule = null; if(request()->has('schedule')) { $schedule = QuizSchedule::where('id', '=', request()->get('schedule'))->first(); } $key = request()->has('schedule') ? 'quiz_schedule_id' : 'quiz_id'; $value = request()->has('schedule') ? request()->get('schedule') : $quiz->id; $sessions = QuizSession::with('user:id,first_name,last_name,email') ->where($key, '=', $value) ->where('status', '=', 'completed') ->orderBy('completed_at', 'desc') ->paginate(request()->perPage != null ? request()->perPage : 20); return Inertia::render('Admin/Quiz/DetailedReport', [ 'schedule' => $schedule != null ? $schedule->only('id', 'code') : null, 'quiz' => $quiz->only('id', 'code', 'title', 'slug'), 'quizSessions' => fractal($sessions, new UserQuizSessionTransformer($quiz))->toArray(), ]); } /** * Export Quiz Report in Excel * * @param Quiz $quiz * @param LocalizationSettings $localization * @param SiteSettings $settings * @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse * @throws \PhpOffice\PhpSpreadsheet\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception */ public function exportReport(Quiz $quiz, LocalizationSettings $localization, SiteSettings $settings) { $now = Carbon::now()->timezone($localization->default_timezone); $user = auth()->user(); $footer = "Generated by {$user->first_name} {$user->last_name} on {$now->toDayDateTimeString()}"; if(request()->has('schedule')) { $schedule = QuizSchedule::where('id', '=', request()->get('schedule'))->first(); $stats = $this->repository->getQuizSessionStats($quiz->id, request()->get('schedule')); $title = "{$quiz->title} - Schedule ({$schedule->code}) Detailed Report, {$settings->app_name}"; } else { $stats = $this->repository->getQuizSessionStats($quiz->id, null); $title = "{$quiz->title} - Detailed Report, {$settings->app_name}"; } $key = request()->has('schedule') ? 'quiz_schedule_id' : 'quiz_id'; $value = request()->has('schedule') ? request()->get('schedule') : $quiz->id; $sessions = fractal(QuizSession::with('user:id,first_name,last_name,email') ->where($key, '=', $value) ->where('status', '=', 'completed') ->orderBy('completed_at', 'desc') ->get(), new QuizSessionExportTransformer())->toArray()['data']; if(count($sessions) == 0) { return redirect()->back()->with('errorMessage', __('Nothing to export.')); } return Excel::download(new QuizSessionsExport($stats[0], $sessions, $title, $footer), "quiz-detailed-report-{$now->timestamp}.xlsx"); } /** * User Quiz Session Export PDF * * @param Quiz $quiz * @param $session * @param LocalizationSettings $localization * @param SiteSettings $settings * @return \Illuminate\Http\Response */ public function exportPDF(Quiz $quiz, $session, LocalizationSettings $localization, SiteSettings $settings) { $session = QuizSession::with('user')->where('code', $session)->firstOrFail(); $now = Carbon::now()->timezone($localization->default_timezone); $user = auth()->user()->first_name.' '.auth()->user()->last_name; $pdf = PDF::loadView('pdf.quiz-session-report', [ 'quiz' => $quiz->only('code', 'title'), 'session' => fractal($session, new QuizScoreReportTransformer())->toArray()['data'], 'footer' => "* Report Generated from {$settings->app_name} by {$user} on {$now->toDayDateTimeString()}", 'logo' => url('storage/'.$settings->logo_path), 'rtl' => $localization->default_direction == 'rtl' ]); return $pdf->download("report-{$session->code}.pdf"); } /** * Quiz Session Analysis and Progress Status * * @param Quiz $quiz * @param $session * @return \Inertia\Response */ public function results(Quiz $quiz, $session) { $session = QuizSession::with('user')->where('code', $session)->firstOrFail(); return Inertia::render('Admin/Quiz/SessionResults', [ 'quiz' => $quiz->only('code', 'title', 'slug', 'total_questions', 'total_marks', 'settings'), 'session' => fractal($session, new UserQuizSessionTransformer($quiz))->toArray()['data'], ]); } /** * Get quiz solutions api endpoint * * @param Quiz $quiz * @param $session * @return \Illuminate\Http\JsonResponse */ public function solutions(Quiz $quiz, $session) { $session = QuizSession::where('code', $session)->firstOrFail(); $questions = fractal($session->questions()->with(['questionType:id,name,code', 'skill:id,name']) ->get(['id','code', 'question', 'question_type_id', 'skill_id', 'solution', 'solution_video']), new QuizSolutionTransformer())->toArray(); return response()->json([ 'questions' => $questions['data'], ], 200); } /** * Delete User Quiz Session * * @param Quiz $quiz * @param $session * @return \Illuminate\Http\RedirectResponse */ public function deleteSession(Quiz $quiz, $session) { $session = QuizSession::where('code', $session)->firstOrFail(); try { DB::transaction(function () use ($session) { $session->questions()->detach(); $session->forceDelete(); }); } catch (\Illuminate\Database\QueryException $e){ return redirect()->back()->with('errorMessage', 'Something went wrong! Unable to delete quiz session. Please try again later.'); } return redirect()->back()->with('successMessage', 'Quiz session was successfully deleted!'); } }