Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. public function random(Module $module, $name){
  2. // Get all questions for module.
  3.  
  4. $questions = $module->questions->shuffle()->pluck('id');
  5.  
  6. // Get unseenQuestions
  7.  
  8. $sessionName = 'unseenQuestions_' . $name . strval($module->id); //unseenQuestions_all_2
  9.  
  10. if (session()->has($sessionName)) {
  11. $unseenQuestions = session()->get($sessionName);
  12.  
  13. } else {
  14. $unseenQuestions = collect($questions);
  15.  
  16. }
  17.  
  18. // Pop new Question
  19.  
  20. $newQuestionID = $unseenQuestions->shift();;
  21.  
  22. // Store unseenQuestions OR remove unseenQuestions (if it has no items)
  23.  
  24. if ($unseenQuestions->count() == 0) {
  25. session()->forget($sessionName);
  26. } else {
  27. session()->put($sessionName, $unseenQuestions);
  28. }
  29.  
  30. return new QuestionResource(Question::find($newQuestionID));
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement