-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d7bf68
commit 89f7613
Showing
11 changed files
with
374 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,138 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Questions; | ||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Answer; | ||
use App\Models\Question; | ||
use App\Models\Quiz; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class QuestionController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
if ($request->user()) { | ||
$categories = ['History', 'Art', 'Geography', 'Science', 'Sports']; | ||
$questions = []; | ||
|
||
//getting 4 random questions from each category | ||
{ | ||
if ($request->user()) { | ||
$categories = ['Art', 'History', 'Geography', 'Science', 'Sports']; | ||
$questions = []; | ||
|
||
// Check if questions are already in session | ||
if ($request->session()->has('quiz_questions')) { | ||
$questions = $request->session()->get('quiz_questions'); | ||
Log::info('Questions retrieved from session:', $questions); | ||
} else { | ||
// Fetching random questions from each category | ||
foreach ($categories as $cat) { | ||
$query_questions = Question::inRandomOrder()->where('category', $cat)->limit(4)->get(); | ||
// Fetch 4 random questions from the category | ||
$query_questions = Question::with(['answers', 'category']) | ||
->whereHas('category', function($query) use ($cat) { | ||
$query->where('name', $cat); | ||
}) | ||
->inRandomOrder() | ||
->limit(4) | ||
->get(); | ||
|
||
// Add fetched questions to the main questions array | ||
foreach ($query_questions as $qq) { | ||
array_push($questions, $qq); | ||
$questions[] = $qq; | ||
} | ||
|
||
// Log the fetched questions for each category | ||
Log::info('Questions fetched from category ' . $cat . ':', $query_questions->toArray()); | ||
} | ||
|
||
shuffle($questions); | ||
} else { | ||
// Store questions in the session if found | ||
if (!empty($questions)) { | ||
$request->session()->put('quiz_questions', $questions); | ||
Log::info('Questions stored in session:', $questions); | ||
} else { | ||
Log::error('No questions found in the database.'); | ||
} | ||
} | ||
|
||
return view('questions.list'); | ||
} | ||
// Check if questions were fetched | ||
if (empty($questions)) { | ||
return view('quiz')->with('questions', []); // Return empty questions if none found | ||
} | ||
|
||
// Shuffle questions to randomize their order | ||
shuffle($questions); | ||
|
||
// Optionally take only the first 10 questions if needed | ||
$questions = array_slice($questions, 0, 10); | ||
|
||
return view('quiz', ['questions' => $questions]); | ||
} else { | ||
return redirect()->route('login'); // Redirect to login if not authenticated | ||
} | ||
} | ||
|
||
|
||
public function results(Request $request) | ||
{ | ||
//get quiz from DB | ||
$quiz = Quiz::where('id', $request->quiz)->get()->first(); | ||
$request = $request->all(); | ||
//makes quiz completed | ||
$quiz['completed'] = 1; | ||
// Fetch the quiz from the database | ||
$quiz = Quiz::where('id', $request->quiz)->first(); | ||
|
||
$results = array('overall' => 0, 'art' => 0, 'geography' => 0, 'history' => 0, 'science' => 0, 'sports' => 0); | ||
$xp = 0; | ||
// Ensure all questions are answered | ||
$questions = $request->session()->get('quiz_questions'); // Retrieve the questions from session | ||
$answers = $request->all(); | ||
|
||
// Validate that all questions have been answered | ||
foreach ($questions as $question) { | ||
if (!isset($answers[$question->id])) { | ||
return back()->with('error', 'You must answer all questions before submitting.'); | ||
} | ||
} | ||
|
||
//figuring out which answers are correct | ||
foreach ($request as $key => $value) { | ||
// Mark quiz as completed | ||
$quiz->completed = 1; | ||
|
||
// Initialize results and XP tracking | ||
$results = [ | ||
'overall' => 0, | ||
'art' => 0, | ||
'geography' => 0, | ||
'history' => 0, | ||
'science' => 0, | ||
'sports' => 0 | ||
]; | ||
$xp = 0; // Initialize XP variable (if applicable) | ||
|
||
// Determine which answers are correct | ||
foreach ($answers as $key => $value) { | ||
if (is_numeric($key)) { | ||
$correct_answer = Answer::where('question_id', $key)->where('correct', 1)->get()->first()['answer']; | ||
if ($correct_answer == $value) { | ||
$question = Question::where('id', $key)->get()->first(); | ||
$correct_answer = Answer::where('question_id', $key)->where('correct', 1)->first(); | ||
if ($correct_answer && $correct_answer->answer == $value) { // Check if correct answer exists | ||
$question = Question::find($key); | ||
$results['overall']++; | ||
|
||
// Increment the category score | ||
$results[strtolower($question->category)]++; | ||
} | ||
} | ||
} | ||
|
||
//adding xp to the | ||
Auth::user()['xp'] += $xp; | ||
// Add XP to user (assuming XP needs to be updated) | ||
Auth::user()->xp += $xp; | ||
|
||
//adding categories score to the user | ||
foreach ($results as $key => $value) { | ||
if ($key != 'overall') { | ||
[$correct, $total] = [explode("/", Auth::user()[$key])[0], explode("/", Auth::user()[$key])[1]]; | ||
Auth::user()[$key] = ($correct + $value) . "/" . ($total + 4); | ||
// Update user's category-specific score | ||
foreach ($results as $category => $value) { | ||
if ($category != 'overall') { | ||
[$correct, $total] = explode("/", Auth::user()->$category); // Use property access instead of array | ||
Auth::user()->$category = ($correct + $value) . "/" . ($total + 2); // 2 questions per category | ||
} | ||
} | ||
|
||
//adding xp to the user | ||
Auth::user()['xp'] += $xp; | ||
|
||
//save changes in DB | ||
// Save the updated data | ||
Auth::user()->save(); | ||
$quiz->save(); | ||
|
||
// Clear the session questions after quiz completion | ||
$request->session()->forget('quiz_questions'); | ||
|
||
// Redirect to results page | ||
return view('questions.results', ['results' => $results]); | ||
} | ||
} | ||
} |
Oops, something went wrong.