Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function random(Module $module, $name){
- // Get all questions for module.
- $questions = $module->questions->shuffle()->pluck('id');
- // Get unseenQuestions
- $sessionName = 'unseenQuestions_' . $name . strval($module->id); //unseenQuestions_all_2
- if (session()->has($sessionName)) {
- $unseenQuestions = session()->get($sessionName);
- } else {
- $unseenQuestions = collect($questions);
- }
- // Pop new Question
- $newQuestionID = $unseenQuestions->shift();;
- // Store unseenQuestions OR remove unseenQuestions (if it has no items)
- if ($unseenQuestions->count() == 0) {
- session()->forget($sessionName);
- } else {
- session()->put($sessionName, $unseenQuestions);
- }
- return new QuestionResource(Question::find($newQuestionID));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement