132 lines
4.7 KiB
PHP
132 lines
4.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\API;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Models\ManageQuizQuestion;
|
||
|
|
use App\Models\QuizQuestionsAnswer;
|
||
|
|
use App\Models\UserQuizPoints;
|
||
|
|
use App\Models\LeaderboardMaster;
|
||
|
|
|
||
|
|
class QuizController extends Controller
|
||
|
|
{
|
||
|
|
// public function getQuiz(){
|
||
|
|
// $currentDateTime = now();
|
||
|
|
// $data = $data = ManageQuizQuestion::with('answer')->where('is_active', '1')->where('schedule_timing', '=', $currentDateTime)->inRandomOrder()->limit(3)->get()->toArray();
|
||
|
|
// // $data = $data = ManageQuizQuestion::with('answer')->where('is_active', '1')->inRandomOrder()->limit(3)->get()->toArray();
|
||
|
|
|
||
|
|
|
||
|
|
// if (empty($data)) {
|
||
|
|
// return response()->json([
|
||
|
|
// 'success' => false,
|
||
|
|
// 'message' => 'No quiz found.',
|
||
|
|
// 'result' => []
|
||
|
|
// ]);
|
||
|
|
// }
|
||
|
|
|
||
|
|
// return response()->json([
|
||
|
|
// 'success' => true,
|
||
|
|
// 'message' => 'Data fetched successfully.',
|
||
|
|
// 'result' => $data
|
||
|
|
// ]);
|
||
|
|
|
||
|
|
// }
|
||
|
|
|
||
|
|
public function getQuiz() {
|
||
|
|
$currentDateTime = now();
|
||
|
|
|
||
|
|
$todayQuizzes = ManageQuizQuestion::with('answer')->where('is_active', '1')->whereDate('schedule_timing', '=', $currentDateTime)->latest()->limit(3)->get()->toArray();
|
||
|
|
|
||
|
|
// If there are no quizzes scheduled for today or if any one quiz is inactive, fetch past quizzes
|
||
|
|
if (count($todayQuizzes) === 0 || count($todayQuizzes) < 3) {
|
||
|
|
$remainingSlots = 3 - count($todayQuizzes);
|
||
|
|
|
||
|
|
$pastQuizzes = ManageQuizQuestion::with('answer')->where('is_active', '1')->whereDate('schedule_timing', '<', $currentDateTime)->inRandom()->limit($remainingSlots)->get()->toArray();
|
||
|
|
|
||
|
|
// Merge today's and past quizzes
|
||
|
|
$data = array_merge($todayQuizzes, $pastQuizzes);
|
||
|
|
} else {
|
||
|
|
// If there are quizzes scheduled for today, use those
|
||
|
|
$data = $todayQuizzes;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (empty($data)) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'No quiz found.',
|
||
|
|
'result' => []
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Data fetched successfully.',
|
||
|
|
'result' => $data
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function storeQuizPoints(Request $request){
|
||
|
|
|
||
|
|
$token = readHeaderToken();
|
||
|
|
if ($token) {
|
||
|
|
$user_id = $token['sub'];
|
||
|
|
$currentDate = now()->toDateString(); // Extracting only the date part
|
||
|
|
// dd($currentDate);
|
||
|
|
|
||
|
|
$existingRecord = UserQuizPoints::whereDate('created_at', $currentDate)
|
||
|
|
->where('user_id', $user_id)
|
||
|
|
->first();
|
||
|
|
// dd($existingRecord);
|
||
|
|
$points = $request->points*10;
|
||
|
|
if ($existingRecord == null) {
|
||
|
|
UserQuizPoints::create(['user_id'=>$user_id,'quize_points'=>$points,'is_active'=>'1']);
|
||
|
|
$leaderboard = LeaderboardMaster::where('user_id', $user_id)->first();
|
||
|
|
$leaderboard->total_score += $points;
|
||
|
|
$leaderboard->save();
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Points Adeed Succefully'
|
||
|
|
]);
|
||
|
|
}else{
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Points Already Added'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}else {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'Authentication failed.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getUerQuizpoints(){
|
||
|
|
|
||
|
|
$token = readHeaderToken();
|
||
|
|
if ($token) {
|
||
|
|
$user_id = $token['sub'];
|
||
|
|
$currentDate = now()->toDateString(); // Extracting only the date part
|
||
|
|
// dd($currentDate);
|
||
|
|
|
||
|
|
$userQuizPoints = UserQuizPoints::whereDate('created_at', $currentDate)
|
||
|
|
->where('user_id', $user_id)->get();
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Data fetched successfully.',
|
||
|
|
'result' => $userQuizPoints
|
||
|
|
]);
|
||
|
|
// dd($existingRecord);
|
||
|
|
}else {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'Authentication failed.',
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|