vendredi 24 novembre 2017

Laravel get static array of random numbers and use it in pagination

I am developing an application in Laravel 5.5 ... it is kind of a testing system...

What I want to achieve is that when user clicks on RUN TEST, it will generate random set of questions (let's say 5) then I want them to be served to the user one by one ... I did a pagination in AJAX so the questions can be served by pages .. and after that user will click on SUBMIT which will submit the form ..

I am able to get the random list of questions

In the TestController within the method "exec_test" is

  // get random id
  $q_id = Question::select('id')
                  ->where('test_id','=',$test_id)
                  //->where('id','=',114)
                  ->orderByRaw("RAND()")
                  ->take(3)
                  ->get();

This will get 3 random ID's for that specific test ...

Following code in the same method is

  // get data 
  $data = array (
               'tests' => Test::select('id','code','name',DB::raw('duration_m * 60 as duration_m'),'questions','passing_score')
                              ->where('id','=',$test_id)  
                              ->orderBy('code', 'asc')   
                              ->paginate($this->page_limit),   
      'test_questions' => Question::select('questions.id','questions.question_header','questions.question_detail_local_url')
                              ->join('tests','tests.id','=','questions.test_id')
                              ->where('test_id','=',$test_id)
                              ->whereIn('questions.id',$q_id)
                              //->orderByRaw("RAND()")
                              //->take(4)
                              ->paginate($this->test_question_page_limit),

      'test_answers' => Answer::select('id','answer_text_cleaned')
                              ->whereIn('question_id',$q_id)
                              ->get()                      
  ); 


  // check for ajax requests
  if ($request->ajax()) {  
    return view('/custom/test/run_test_quests', $data)->render();  
  }

  return view('/custom/test/run_test_info',$data);

But I don't know how can I make that first query static and to make it served one by one (per page) to the user .. so he can go trough all questions

Do you have any suggestions please?




Aucun commentaire:

Enregistrer un commentaire