public function questionsbyCourseName($coursename)
 {
     /*
     Currently get all question
     */
     if (count(Course::all()) == 1) {
         return Question::active();
     }
     //if more than one course
     $subjects = Course::where('name', $coursename)->first()->subjects;
     $this->subjects = $subjects->pluck('id');
     $questions = Question::where(function ($query) {
         foreach ($this->subjects as $key => $value) {
             //you can use orWhere the first time, dosn't need to be ->where
             $query->orWhere('subject_id', '=', $value);
         }
     });
     return $questions;
 }
Exemplo n.º 2
0
 public function generate(Request $req)
 {
     $date = $req->get("date");
     $options = $req->get('options');
     $totalnoq = 0;
     $questionResult = [];
     foreach ($options as $key => $option) {
         $allquestions = Question::active()->notsubquestion();
         $localQ = collect([]);
         $localNoq = $option["noq"];
         $totalnoq += $localNoq;
         $temp = null;
         if (isset($option["pureRandom"]) && $option["pureRandom"]) {
             $temp = $allquestions->get();
         } else {
             if (isset($option["topic"]) && $option["topic"]) {
                 $temp = $allquestions->where('topic_id', $option["topic"])->get();
             } else {
                 if (isset($option["chapter"]) && $option["chapter"]) {
                     $temp = $allquestions->where('chapter_id', $option["chapter"])->get();
                 } else {
                     if (isset($option["module"]) && $option["module"]) {
                         $temp = $allquestions->where('module_id', $option["module"])->get();
                     } else {
                         if (isset($option["subject"]) && $option["subject"]) {
                             $temp = $allquestions->where('subject_id', $option["subject"])->get();
                         } else {
                             $temp = $allquestions->get();
                         }
                     }
                 }
             }
         }
         // if($key+1==2){
         //  dd($option["subject"], $temp);
         // }
         //check for number of questions available
         if ($localNoq <= count($temp)) {
             //if has enough questions
             $localQ = $temp->random($localNoq)->toArray();
             if ($localNoq == 1) {
                 $localQ = [$localQ];
             }
             foreach ($localQ as $key => $question) {
                 if (in_array($question["id"], $questionResult)) {
                     //if question already there in result
                     //remove that question from data get
                     $temp = $temp->reject(function ($value, $key) {
                         return $value->id == $question->id;
                     });
                     //if more questions are there
                     //push one new random instead of current which is already there
                     if (count($temp) > 1) {
                         array_push($localQ, $temp->random(1)->toArray());
                     } else {
                         return \Response::json("Card no. " . ($key + 1) . " has not enough questions in database with the given options!", 500);
                     }
                 } else {
                     //if question no in result ; good to push
                     array_push($questionResult, $question["id"]);
                 }
             }
         } else {
             //not enough questions
             // dd($localNoq, $temp, $option);
             return \Response::json("Card no. " . ($key + 1) . " has not enough questions in database with the given options!", 500);
         }
     }
     $rq = new RandomQuestion();
     $rq->id = Uuid::generate();
     $rq->date = $date;
     $rq->noq = $totalnoq;
     $rq->created_by = \Auth::user()->id;
     $rq->save();
     foreach ($questionResult as $key => $value) {
         $lol = RandomQuestionList::create(["ref_id" => $rq->id, "question_id" => $value]);
     }
     return \Response::json(["id" => $rq->id . ""], 200);
 }