����JFIF��x�x����'
Server IP : 66.29.137.217 / Your IP : 3.141.40.242 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\ExamSessionsExport; use App\Http\Controllers\Controller; use App\Models\Exam; use App\Models\ExamSchedule; use App\Models\ExamSession; use App\Repositories\ExamRepository; use App\Settings\LocalizationSettings; use App\Settings\SiteSettings; use App\Transformers\Admin\ExamScoreReportTransformer; use App\Transformers\Admin\ExamSessionExportTransformer; use App\Transformers\Admin\UserExamSessionTransformer; 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 ExamAnalyticsController extends Controller { /** * @var ExamRepository */ private ExamRepository $repository; public function __construct(ExamRepository $repository) { $this->middleware(['role:admin|instructor']); $this->repository = $repository; } /** * Get Exam Overall Report with statistics from sessions * * @param Exam $exam * @return \Inertia\Response */ public function overallReport(Exam $exam) { $schedule = null; if(request()->has('schedule')) { $schedule = ExamSchedule::where('id', '=', request()->get('schedule'))->first(); $stats = $this->repository->getExamSessionStats($exam->id, request()->get('schedule')); } else { $stats = $this->repository->getExamSessionStats($exam->id, null); } return Inertia::render('Admin/Exam/OverallReport', [ 'exam' => $exam->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).'/'.$exam->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 Exam Detailed Report with user sessions * * @param Exam $exam * @return \Inertia\Response */ public function detailedReport(Exam $exam) { $schedule = null; if(request()->has('schedule')) { $schedule = ExamSchedule::where('id', '=', request()->get('schedule'))->first(); } $key = request()->has('schedule') ? 'exam_schedule_id' : 'exam_id'; $value = request()->has('schedule') ? request()->get('schedule') : $exam->id; $sessions = ExamSession::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/Exam/DetailedReport', [ 'schedule' => $schedule != null ? $schedule->only('id', 'code') : null, 'exam' => $exam->only('id', 'code', 'title', 'slug'), 'examSessions' => fractal($sessions, new UserExamSessionTransformer($exam))->toArray(), ]); } /** * Export Exam Report in Excel * * @param Exam $exam * @param LocalizationSettings $localization * @param SiteSettings $settings * @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse */ public function exportReport(Exam $exam, 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 = ExamSchedule::where('id', '=', request()->get('schedule'))->first(); $stats = $this->repository->getExamSessionStats($exam->id, request()->get('schedule')); $title = "{$exam->title} - Schedule ({$schedule->code}) Detailed Report, {$settings->app_name}"; } else { $stats = $this->repository->getExamSessionStats($exam->id, null); $title = "{$exam->title} - Detailed Report, {$settings->app_name}"; } $key = request()->has('schedule') ? 'exam_schedule_id' : 'exam_id'; $value = request()->has('schedule') ? request()->get('schedule') : $exam->id; $sessions = fractal(ExamSession::with('user:id,first_name,last_name,email') ->where($key, '=', $value) ->where('status', '=', 'completed') ->orderBy('completed_at', 'desc') ->get(), new ExamSessionExportTransformer())->toArray()['data']; if(count($sessions) == 0) { return redirect()->back()->with('errorMessage', __('Nothing to export.')); } return Excel::download(new ExamSessionsExport($stats[0], $sessions, $title, $footer), "exam-detailed-report-{$now->timestamp}.xlsx"); } /** * User Exam Session Export PDF * * @param Exam $exam * @param $session * @param LocalizationSettings $localization * @param SiteSettings $settings * @return \Illuminate\Http\Response */ public function exportPDF(Exam $exam, $session, LocalizationSettings $localization, SiteSettings $settings) { $session = ExamSession::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.exam-session-report', [ 'exam' => $exam->only('code', 'title'), 'session' => fractal($session, new ExamScoreReportTransformer())->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"); } /** * Exam Session Analysis and Progress Status * * @param Exam $exam * @param $session * @return \Inertia\Response */ public function results(Exam $exam, $session) { $session = ExamSession::with('user')->where('code', $session)->firstOrFail(); return Inertia::render('Admin/Exam/SessionResults', [ 'exam' => $exam->only('code', 'title', 'slug', 'total_questions', 'total_marks', 'settings'), 'session' => fractal($session, new UserExamSessionTransformer($exam))->toArray()['data'], ]); } /** * Get exam solutions api endpoint * * @param Exam $exam * @param $session * @return \Illuminate\Http\JsonResponse */ public function solutions(Exam $exam, $session) { $session = ExamSession::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 Exam Session * * @param Exam $exam * @param $session * @return \Illuminate\Http\RedirectResponse */ public function deleteSession(Exam $exam, $session) { $session = ExamSession::where('code', $session)->firstOrFail(); try { DB::transaction(function () use ($session) { $session->questions()->detach(); $session->sections()->detach(); $session->forceDelete(); }); } catch (\Illuminate\Database\QueryException $e){ return redirect()->back()->with('errorMessage', 'Something went wrong! Unable to delete exam session. Please try again later.'); } return redirect()->back()->with('successMessage', 'Exam session was successfully deleted!'); } }