public static function checkAnswer($quizItemID, $ans)
 {
     $correctAnswer = QuizItemAnswer::where('quizItemID', $quizItemID)->first();
     if (count($correctAnswer)) {
         return $correctAnswer->answer === $ans ? true : false;
     }
 }
 public function updateQuestion($id)
 {
     $quizItem = QuizItem::with('quizItemChoices', 'quizItemAnswer')->where('delFLag', 0)->whereId($id)->first();
     $in = Input::all();
     $rules = ['question' => 'required', 'choiceA' => 'required', 'choiceB' => 'required', 'choiceC' => 'required', 'choiceD' => 'required', 'answer' => 'required'];
     $validation = Validator::make($in, $rules);
     if ($validation->passes()) {
         QuizItem::find($id)->update(array('question' => $in['question']));
         QuizItemAnswer::where('quizItemID', $id)->update(array('answer' => $in['answer']));
         QuizItemChoice::where('quizItemID', $id)->update(array('choice1' => $in['choiceA'], 'choice2' => $in['choiceB'], 'choice3' => $in['choiceC'], 'choice4' => $in['choiceD']));
         return Redirect::to('/')->with('message', 'Question successfully updated')->with('url', '/quiz/view/' . $quizItem->quizID);
     } else {
         return Redirect::to('/')->with('message', 'Error in updating question');
     }
 }
Example #3
0
 public function updateQuiz($quiz_id)
 {
     $quiz = Quiz::find($quiz_id);
     $quiz->title = Input::get('title');
     $quiz->minutes = Input::get('minutes');
     $quiz->details = Input::get('details');
     $quiz->save();
     $questions = Input::get('question');
     $answers = Input::get('answer');
     $choices = Input::get('choice');
     if (!empty($questions['old'])) {
         $old_questions = $questions['old'];
         $old_answers = $answers['old'];
         $quiz_items = DB::table('quiz_items')->where('quiz_id', '=', $quiz_id)->get();
         foreach ($quiz_items as $question_index => $quiz_item) {
             $quiz_item_id = $quiz_item->id;
             $quiz_item = QuizItem::find($quiz_item_id);
             $quiz_item->quiz_id = $quiz_id;
             $quiz_item->question = $old_questions[$question_index];
             $quiz_item->save();
             $current_number_of_answers = 0;
             if (!empty($old_answers[$quiz_item_id])) {
                 $quiz_items_answers = DB::table('quiz_items_answers')->where('quiz_item_id', '=', $quiz_item_id)->get();
                 $item_answers = $old_answers[$quiz_item_id];
                 $total_answers = count($item_answers) - 1;
                 foreach ($quiz_items_answers as $qia_index => $qia) {
                     $answer = QuizItemAnswer::find($qia->id);
                     $answer->answer = $item_answers[$qia_index];
                     $answer->save();
                     $current_number_of_answers += 1;
                 }
                 $answer_index = $current_number_of_answers;
                 for ($x = $answer_index; $x <= $total_answers; $x++) {
                     $quiz_item_answer = new QuizItemAnswer();
                     $quiz_item_answer->quiz_item_id = $quiz_item_id;
                     $quiz_item_answer->answer = $item_answers[$x];
                     $quiz_item_answer->save();
                 }
             }
             if (!empty($choices['old'])) {
                 $item_choices = array();
                 $old_choices = $choices['old'];
                 $total_choices = count($old_choices);
                 $current_number_of_choices = 0;
                 $quiz_items_choices = DB::table('quiz_items_choices')->where('quiz_item_id', '=', $quiz_item_id)->get();
                 $current_number_of_choices = count($quiz_items_choices);
                 if (!empty($old_choices[$quiz_item_id])) {
                     $item_choices = $old_choices[$quiz_item_id];
                     foreach ($quiz_items_choices as $qic_index => $qic) {
                         $choice = QuizItemChoice::find($qic->id);
                         $choice->choice = $item_choices[$qic_index];
                         $choice->save();
                     }
                 }
                 if (!empty($item_choices)) {
                     while ($current_number_of_choices < $total_choices) {
                         if (!empty($item_choices[$current_number_of_choices])) {
                             $choice = $item_choices[$current_number_of_choices];
                             $quiz_item_choice = new QuizItemChoice();
                             $quiz_item_choice->quiz_item_id = $quiz_item_id;
                             $quiz_item_choice->choice = $choice;
                             $quiz_item_choice->save();
                         }
                         $current_number_of_choices += 1;
                     }
                 }
             }
         }
     }
     if (!empty($questions['new'])) {
         $new_questions = $questions['new'];
         $new_answers = $answers['new'];
         foreach ($new_questions as $index => $question) {
             $quiz_item = new QuizItem();
             $quiz_item->quiz_id = $quiz_id;
             $quiz_item->question = $question;
             $quiz_item->save();
             $quiz_item_id = $quiz_item->id;
             if (!empty($new_answers[$index])) {
                 $item_answers = $new_answers[$index];
                 foreach ($item_answers as $answer) {
                     $quiz_item_answer = new QuizItemAnswer();
                     $quiz_item_answer->quiz_item_id = $quiz_item_id;
                     $quiz_item_answer->answer = $answer;
                     $quiz_item_answer->save();
                 }
             }
             if (!empty($choices['new'][$index])) {
                 $new_choices = $choices['new'];
                 $item_choices = $new_choices[$index];
                 foreach ($item_choices as $choice) {
                     $quiz_item_choice = new QuizItemChoice();
                     $quiz_item_choice->quiz_item_id = $quiz_item_id;
                     $quiz_item_choice->choice = $choice;
                     $quiz_item_choice->save();
                 }
             }
         }
     }
     return Redirect::back()->with('message', array('type' => 'success', 'text' => 'Quiz Updated!'));
 }