public function takeQuiz() { //check if quiz can be taken by student, //based on the time and the ID number $page_title = 'Cannot Take Quiz'; $page_content = View::make('no_quiz'); $id_number = Session::get('id_number'); $quiz_code = Session::get('quiz_code'); $current_datetime = Carbon::now()->toDateTimeString(); $quiz_schedule = QuizSchedule::where('quiz_code', '=', $quiz_code)->whereRaw(DB::raw("'{$current_datetime}' BETWEEN datetime_from AND datetime_to"))->first(); if ($quiz_schedule) { $quiz_schedule_id = $quiz_schedule->id; $class_id = $quiz_schedule->class_id; $quiz_id = $quiz_schedule->quiz_id; $student = StudentClass::where('student_id', '=', $id_number)->where('class_id', '=', $class_id)->first(); if ($student) { //when a student takes a quiz, a row is created on the //student_quizzes table, add the started_at, quiz_id, and student_id //when another student comes along and inputs the same id number //and quiz code, they won't be allowed since the quiz is already taken $student_quiz_count = StudentQuiz::where('student_id', '=', $id_number)->where('quiz_id', '=', $quiz_id)->count(); if ($student_quiz_count === 0) { //can take //get quiz details and show it to the student $quiz = Quiz::where('id', '=', $quiz_id)->first(); Session::put('quiz_id', $quiz_id); Session::put('quiz_title', $quiz->title); $items = QuizItem::where('quiz_id', '=', $quiz_id)->orderByRaw("RAND()")->get(); $item_ids = DB::table('quiz_items')->where('quiz_id', '=', $quiz_id)->lists('id'); $quiz_items_answers = DB::table('quiz_items_answers')->whereIn('quiz_item_id', $item_ids)->get(); $quiz_items_choices = DB::table('quiz_items_choices')->whereIn('quiz_item_id', $item_ids)->get(); $quiz_items = array(); foreach ($items as $item) { $quiz_items[$item->id] = array('question' => $item->question); } foreach ($quiz_items_choices as $qic) { $quiz_items[$qic->quiz_item_id]['choices'][] = $qic->choice; } $seconds = $quiz->minutes * 60 + 1; $page_data = array('quiz' => $quiz, 'quiz_items' => $quiz_items, 'seconds' => $seconds); //create student quiz $student_quiz = new StudentQuiz(); $student_quiz->student_id = $id_number; $student_quiz->quiz_id = $quiz_id; $student_quiz->quiz_schedule_id = $quiz_schedule_id; $student_quiz->started_at = Carbon::now()->toDateTimeString(); $student_quiz->save(); $page_title = 'Take Quiz'; $page_content = View::make('quiz', $page_data); } } } $this->layout->title = $page_title; $this->layout->quiz = true; $this->layout->content = $page_content; }