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; }
public function exportScores($id) { //check if current user has created the quiz $user_id = Auth::user()->id; $quiz_schedule = QuizSchedule::find($id); $quiz = Quiz::find($quiz_schedule->quiz_id); $class = DB::table('classes')->where('id', '=', $quiz_schedule->class_id)->first(); if ($quiz_schedule->user_id != $user_id) { return Redirect::to('/scores/' . $id)->with('message', array('type' => 'danger', 'text' => 'You do not have the permission to export scores for this quiz.')); } $filename = "{$class->name}-{$quiz->title}-{$quiz_schedule->datetime_from}"; Excel::create($filename, function ($excel) use($id, $quiz_schedule) { $excel->sheet('score-sheet', function ($sheet) use($id, $quiz_schedule) { $scores = DB::table('student_quizzes')->where('quiz_schedule_id', '=', $id)->lists('score', 'student_id'); $students = DB::table('students')->join('student_classes', 'students.id', '=', 'student_classes.student_id')->select('students.id', 'last_name', 'first_name', 'middle_initial')->where('student_classes.class_id', '=', $quiz_schedule->class_id)->orderBy('gender', 'DESC')->orderBy('last_name', 'ASC')->get(); $student_scores = array(array('ID Number', 'Student Name', 'Score')); foreach ($students as $student) { $score = ''; if (isset($scores[$student->id])) { $score = $scores[$student->id]; } $student_scores[] = array('id' => $student->id, 'name' => "{$student->last_name}, {$student->first_name} {$student->middle_initial}", 'score' => $score); } $sheet->fromArray($student_scores, null, 'A1', false, false); }); })->export('xls'); }