/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     DB::statement('SET FOREIGN_KEY_CHECKS=0;');
     DB::table('answers')->truncate();
     $quiz = new Answer();
     $quiz->question_id = 1;
     $quiz->user_id = 1;
     $quiz->title = 'Seeded answer 1';
     $quiz->is_right = 1;
     $quiz->save();
     $quiz = new Answer();
     $quiz->question_id = 1;
     $quiz->user_id = 1;
     $quiz->title = 'Seeded answer 1';
     $quiz->is_right = 0;
     $quiz->save();
     $quiz = new Answer();
     $quiz->question_id = 2;
     $quiz->user_id = 1;
     $quiz->title = 'Seeded answer 1';
     $quiz->is_right = 0;
     $quiz->save();
     $quiz = new Answer();
     $quiz->question_id = 2;
     $quiz->user_id = 1;
     $quiz->title = 'Seeded answer 1';
     $quiz->is_right = 1;
     $quiz->save();
     DB::statement('SET FOREIGN_KEY_CHECKS=1;');
 }
Beispiel #2
0
 /**
  * Responds to request to GET /answer/add/{question_id}
  */
 public function getAnswerAdd($question_id)
 {
     $question = Quiz::find($question_id);
     $answer = new Answer();
     $answer->question_id = $question_id;
     $answer->save();
     return redirect('/answer/edit/' . $answer->id);
 }
Beispiel #3
0
 public function store_answer(Request $request)
 {
     $answer = new Answer();
     $answer->content = $request->answer_content;
     $answer->question_id = $request->question_id;
     $answer->user_id = $this->user->id;
     $answer->save();
     return $answer->id;
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param CreateAnswerRequest $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function store(CreateAnswerRequest $request)
 {
     $answer = new Answer();
     $answer->fill($request->all());
     $answer->user_id = Auth::user()->id;
     $answer->save();
     $message = trans('messages.answer_created_successfully');
     Flash::success($message);
     return redirect()->route('teacher.units.show', $answer->question->unit);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function postReply(AnswerRequest $request, $id)
 {
     $answer = Question::find($id)->answer;
     if (!$answer) {
         $answer = new Answer();
     }
     $answer->user_id_answered = Auth::id();
     $answer->content = $request->content;
     $answer->question_id = $id;
     $answer->save();
 }
Beispiel #6
0
 public function store(AnswerRequest $request)
 {
     $question = Question::findOrFail($request->input('question_id'));
     $this->authorize('view-course', $question->course);
     $answer = new Answer($request->only(['body']));
     $answer->question()->associate($question);
     $answer->user()->associate(auth()->user());
     $answer->save();
     session()->flash('success', 'La respuesta ha sido enviada.');
     return redirect()->back();
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store($id, Request $request)
 {
     //save to answers table
     $answer = new Answer();
     $answer->answer = $request['answer'];
     $answer->user_id = $request->user()->id;
     $answer->homework_id = $id;
     $answer->save();
     //redirect to /homework
     return redirect('/homework');
 }
 public function store(Request $request, $lecture_id, $subject_id, $topic_id, $knowledgeunit_id, $question_id)
 {
     $this->validate($request, ['description' => 'required']);
     $question = Question::find($question_id);
     Log::info('$knowledgeUnit: ' . $question->title);
     $answer = new Answer();
     $answer->correct = $request->correct ? $request->correct : 0;
     $answer->description = $request->description;
     $answer->question()->associate($question);
     $answer->save();
     return redirect('/lectures/' . $lecture_id . '/subjects/' . $subject_id . '/topics/' . $topic_id . '/knowledgeunits/' . $knowledgeunit_id . '/questions/' . $question_id . '/answers');
 }
Beispiel #9
0
 public static function addUserFeedback($data)
 {
     $userID = \Auth::user()->id;
     foreach ($data['data'] as $d) {
         $answer = new Answer();
         $answer->uid = $userID;
         $answer->qid = $d['id'];
         $answer->answer = $d['value'];
         $answer->save();
         //echo $d['id'] ." ". $d['value']."\n";
     }
     return true;
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $json = Storage::get('questions.json');
     $this->command->info($json);
     $questions = json_decode($json, true);
     foreach ($questions['questions'] as $question_keys => $question) {
         $this->command->info("Adding Question: " . $question_keys . "...");
         if ($question["type"] != "true-false") {
             $answers = $question['answers'];
             unset($question['answers']);
         }
         $q = new Question($question);
         $q->save();
         foreach ($question as $question_attribute_name => $question_attribute_value) {
             $this->command->info($question_attribute_name);
         }
         if ($q->type == "true-false") {
             $this->command->info("Adding True/False Question...");
             $answer = $question['answer'];
             $aFalse;
             $aTrue;
             if (Answer::where('text', 'true')->count() >= 1) {
                 $aTrue = Answer::where('text', 'true')->first();
             } else {
                 $aTrue = new Answer(['text' => 'true']);
                 $aTrue->save();
             }
             if (Answer::where('text', 'false')->count() >= 1) {
                 $aFalse = Answer::where('text', 'false')->first();
             } else {
                 $aFalse = new Answer(['text' => 'false']);
                 $aFalse->save();
             }
             $q->answers()->save($aFalse, ['is_correct' => $answer ? 0 : 1]);
             $q->answers()->save($aTrue, ['is_correct' => $answer ? 1 : 0]);
         } else {
             $this->command->info("Adding answers...");
             $answer_ids = array();
             foreach ($answers as $answer_index => $answer) {
                 $this->command->info("Adding " . ($answer_index + 1) . "...");
                 $a = new Answer($answer);
                 $a->save();
                 $q->answers()->save($a, ['is_correct' => $answer['is_correct'] === "false" ? 0 : 1]);
             }
         }
         $this->command->info("");
     }
 }
 public function store(AnswerRequest $request)
 {
     $client = Client::create();
     //Was macht hier das ->all();
     $questionData = $request->all();
     foreach ($questionData["radio_question"] as $question => $value) {
         $answer = new Answer();
         $answer->question_id = $question;
         $answer->answer = $value;
         $answer->client_id = $client->id;
         //Unterschied zwischen ->save und ::create
         $answer->save();
     }
     //Kann man das irgendwie sauberer lösen als mit dieser foreach Schleife, damit es DRY-Kompatibel ist?
     foreach ($questionData["text_question"] as $question => $value) {
         $answer = new Answer();
         $answer->question_id = $question;
         $answer->comment = $value;
         $answer->client_id = $client->id;
         //Unterschied zwischen ->save und ::create
         $answer->save();
     }
 }
Beispiel #12
0
 /**
  * Update the specified resource in storage.
  *
  * @param  Request $request
  * @param  int     $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $question = Question::findOrFail($id);
     $question->question = $request->get('question');
     $question->save();
     $question->answers()->delete();
     foreach ($request->get('choice') as $key => $choice) {
         $answer = new Answer();
         $answer->type = "string";
         $answer->textual = $choice;
         $answer->correct = in_array($key + 1, $request->get('answer')) <=> 0;
         $answer->question()->associate($question);
         $answer->save();
     }
     return redirect()->to(route("paper.edit", $question->paper->id));
 }
 public function update(Request $request, $id)
 {
     // the form sends the request
     // the id indicates which question to update
     // store
     //echo $request;
     if ($request->type == 'single' || $request->type == 'multi-value') {
         $question = Question::find($id);
         $question->prompt = $request->input('prompt');
         // works!
         // what happens if we leave it unselected?
         // nice, doesn't change it if nothing selected
         // I think it has the previous selected
         $question->difficulty = $request->input('difficulty');
         // works!
         $question->total_score = $request->input('total_score');
         // works!
         $question->save();
         // detach all answers from the question
         // attach the newly created ones from the form; overwrites.
         $question->answers()->detach();
         // detach all answers from question
         $a1 = new Answer();
         $a2 = new Answer();
         $a3 = new Answer();
         $a4 = new Answer();
         $a5 = new Answer();
         $a1->text = $request->choice1;
         $a2->text = $request->choice2;
         $a3->text = $request->choice3;
         $a4->text = $request->choice4;
         $a5->text = $request->choice5;
         $a1->save();
         $a2->save();
         $a3->save();
         $a4->save();
         $a5->save();
         $question->answers()->attach($a1->id, array('is_correct' => $request->isCorrect1 != 1 ? 0 : 1));
         $question->answers()->attach($a2->id, array('is_correct' => $request->isCorrect2 != 1 ? 0 : 1));
         $question->answers()->attach($a3->id, array('is_correct' => $request->isCorrect3 != 1 ? 0 : 1));
         $question->answers()->attach($a4->id, array('is_correct' => $request->isCorrect4 != 1 ? 0 : 1));
         $question->answers()->attach($a5->id, array('is_correct' => $request->isCorrect5 != 1 ? 0 : 1));
         // question now has the new answers attached to it.
         // need this line or else it redirects to the non-GET URL
         // it has a PUT request instead, so nothing displays
         // because there is no view associated with a PUT
         // YES IT WORKS
         // THAT IS WHAt I AM TALKING ABOUT
     } elseif ($request->type == 'true-false') {
         $question = Question::find($id);
         $question->prompt = $request->input('prompt');
         // works!
         $question->difficulty = $request->input('difficulty');
         // works!
         $question->total_score = $request->input('total_score');
         // works!
         $question->type = 'true-false';
         $question->save();
         // detach all answers from the question
         // attach the newly created ones from the form; overwrites.
         $question->answers()->detach();
         // detach all answers from question
         $a1 = new Answer();
         $a2 = new Answer();
         $a1->text = $request->choice1;
         $a2->text = $request->choice2;
         $a1->save();
         $a2->save();
         $question->answers()->attach($a1->id, array('is_correct' => $request->isCorrect1 != 1 ? 0 : 1));
         $question->answers()->attach($a2->id, array('is_correct' => $request->isCorrect2 != 1 ? 0 : 1));
     } elseif ($request->type == 'free-response') {
         $question = Question::find($id);
         $question->prompt = $request->prompt;
         $question->difficulty = $request->difficulty;
         $question->subject_id = 1;
         $question->type = 'free-response';
         $question->total_score = $request->total_score;
         $question->save();
         $question->answers()->detach();
         $a1 = new Answer();
         $a1->text = $request->choice1;
         $a1->save();
         $question->answers()->attach($a1->id, array('is_correct' => 0));
     } else {
         echo "Nothing worked!";
     }
     return view('question.show', ['question' => $question]);
 }
Beispiel #14
0
 protected function _saveTest($data)
 {
     $isNewEntry = !array_key_exists('id', $data);
     if ($isNewEntry) {
         $test = new Test();
         $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first();
         $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1;
         $test->order = $next_course_order;
     } else {
         $test = Test::find($data['id']);
         $submitted_questions = [];
         foreach ($data['questions'] as $qkey => $qdata) {
             if (array_key_exists('id', $qdata)) {
                 $submitted_questions[] = $qdata['id'];
             }
         }
         foreach ($test->questions as $question) {
             if (!in_array($question->id, $submitted_questions)) {
                 $question->delete();
             }
         }
         if ($test->course->id != $data['course']['id']) {
             $latest_test_on_course = Test::where(['course_id' => $data['course']['id']])->orderBy('order', 'DESC')->first();
             $next_course_order = ($latest_test_on_course ? $latest_test_on_course->order : 0) + 1;
             $test->order = $next_course_order;
         }
     }
     $test->course_id = $data['course']['id'];
     $test->title = $data['title'];
     $test->description = $data['description'];
     $test->autodiscard = $data['autodiscard'];
     $test->save();
     ///////////////////////
     $isNewPage = !array_key_exists('id', $data['page']);
     $pageBody = trim($data['page']['body']);
     $page = false;
     if ($isNewPage) {
         $page = new Page();
     } else {
         $page = Page::find($data['page']['id']);
     }
     if ($page) {
         $page->test_id = $test->id;
         $page->body = $pageBody;
         $page->hidden = false;
         if (strlen(trim(strip_tags($pageBody, '<img>'))) == 0) {
             $page->hidden = true;
         }
         $page->save();
     }
     /////////////////////////
     foreach ($data['questions'] as $qkey => $question_data) {
         if (!array_key_exists('id', $question_data)) {
             $question = new Question();
         } else {
             $question = Question::find($question_data['id']);
             $submitted_answers = [];
             foreach ($question_data['answers'] as $adata) {
                 if (array_key_exists('id', $adata)) {
                     $submitted_answers[] = $adata['id'];
                 }
             }
             foreach ($question->answers as $akey => &$answer) {
                 if (!in_array($answer->id, $submitted_answers) || $question_data['type'] == "TEXT" && $akey > 0 || $question_data['type'] == "TEXTAREA") {
                     $answer->delete();
                 }
             }
         }
         $question->test_id = $test->id;
         $question->type = $question_data['type'];
         $question->title = $question_data['title'];
         $question->subtitle = $question_data['subtitle'];
         $question->order = $qkey + 1;
         $question->data = array_key_exists('data', $question_data) ? $question_data['data'] : '{}';
         switch ($question->type) {
             case 'MULTITEXT':
                 if (!property_exists($question->data, 'multitext_required') || is_null($question->data->multitext_required) || $question->data->multitext_required > count($question_data['answers'])) {
                     $question->data->multitext_required = count($question_data['answers']);
                 }
                 break;
         }
         $question->save();
         ///////////////////////////
         foreach ($question_data['answers'] as $akey => $answer_data) {
             if (!array_key_exists('id', $answer_data)) {
                 $answer = new Answer();
             } else {
                 $answer = Answer::find($answer_data['id']);
             }
             if (!@$answer) {
                 $answer = new Answer();
             }
             $answer->question_id = $question->id;
             $answer->text = $answer_data['text'];
             if ($question->type == "CHOICE") {
                 $answer->is_correct = intval($question_data['correct_answer']) == $akey;
             } else {
                 $answer->is_correct = @$answer_data['is_correct'] ? true : false;
             }
             switch ($question->type) {
                 case 'TEXT':
                 case 'MULTITEXT':
                     $answer->is_correct = true;
                     break;
             }
             if (array_key_exists('error_margin', $answer_data)) {
                 $answer->error_margin = $answer_data['error_margin'];
             } else {
                 $answer->error_margin = 10;
             }
             $answer->save();
         }
     }
     return $test->id;
 }
 /**
  * Update answer
  *
  * @param Request  $request
  * @param Question $question
  * @param Answer   $answer
  *
  * @return Redirect
  */
 public function answerUpdate(Request $request, Question $question, Answer $answer)
 {
     $rules = ["answer" => "required"];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return Redirect::route("ask.answer.edit", [$question->id, $answer->id])->withErrors($validator)->withInput();
     }
     $answer->answer = $request->input("answer");
     $answer->save();
     return Redirect::route("ask.show", [$question->id, $question->slug])->with("success", "Cevabınız başarıyla güncellendi!");
 }
 public function edit_question()
 {
     $data = Input::all();
     $event = Event::where('id', Session::get('event_id'))->first();
     $question = Question::where('id', '=', Session::get('qid'))->first();
     $question->event_id = Session::get('event_id');
     $question->question = $data['question'];
     $image = array();
     if (isset($data['file'])) {
         if (Input::file('file')->isValid()) {
             $destinationPathvfile = 'uploads';
             $extensionvfile = Input::file('file')->getClientOriginalExtension();
             $fileNamevfile = $event->id . '.' . $extensionvfile;
             // renaming image
             Input::file('file')->move($destinationPathvfile, $fileNamevfile);
             $question->image = $fileNamevfile;
         }
     }
     if (isset($data['html'])) {
         $question->html = $data['html'];
     }
     if (intval($event->type) > 2) {
         $question->options = serialize($data['options']);
         $answers = $data['answers'];
         $question->save();
         Session::put('qid', Question::all()->last()->id);
         $answer = Answer::where('ques_id', '=', Session::get('qid'))->delete();
         foreach ($answers as $ans) {
             $answer = new Answer();
             $answer->ques_id = Session::get('qid');
             $answer->answer = $ans;
             $answer->score = 1;
             $answer->incorrect = 0;
             $answer->save();
         }
     } else {
         $question->level = $data['level'];
         $question->save();
         Session::put('qid', Question::all()->last()->id);
         $answer = Answer::where('ques_id', '=', Session::get('qid'))->first();
         $answer->ques_id = Session::get('qid');
         $answer->answer = $data['answer'];
         $answer->score = 1;
         $answer->incorrect = 0;
         $answer->save();
     }
     Session::put('event_id', $event->id);
     return Redirect::route('viewquestions', ['event_id' => $event->id])->with('message', 'Question Successfully Edited');
 }
 public function saveDraw($e_id, $f_id)
 {
     $experiment = Experiment::where("key", $e_id)->first();
     $answer = new Answer();
     $answer->student = session('user');
     $answer->element = $f_id;
     $answer->experiment = $experiment->id;
     $answer->value = $_POST['draw'];
     //$array = json_decode($answer->value, true);
     $answer->save();
     //return ;
     return "ok";
 }
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // DB::table('classes')->insert([
        //     'name' => str_random(10),
        //     'term' => str_random(10).'@gmail.com',
        //     'lecturer_id' => 1,
        // ]);
        //$this->command->info('test');
        // string is a JSON that contains ALL of the questions we want to add to the database
        /** Question object has the following attributes: 
                    subject_id
                    prompt
                    difficulty
                    type
                    image
                    total_score
                
                We also want to associate the Question object with 5 Answer objects
                Answer objects contain: 
                    text
                    image
            
                Each Question will have 5 answer options total
            
                
            
            */
        // for consistency, everything is listed in the JSON in the order from diagram
        $string = '{
            "questions": [{
                "prompt" : "Which of the following operations are NOT part of the Abstract Data Type Stack? ",
                "difficulty": "easy", 
                "type": "multiple", 
                "total_score" : 1,
                "subject_id": 1,
                "image": null, 
                
                "answers":[
                {
                    "text": "push()", 
                    "image": null, 
                    "is_correct" : "false"
                },
                {
                    "text": "reverse()", 
                    "image": null, 
                    "is_correct" : "true"
                },
                {
                    "text": "getFirstElement()", 
                    "image": null, 
                    "is_correct" : "true"
                },
                {
                    "text": "pop()", 
                    "image": null, 
                    "is_correct" : "false"
                },
                {
                    "text": "rotate", 
                    "image": null, 
                    "is_correct" : "true"
                }

                ]
            },
            
            {
                "prompt" : "What is the storage policy for a Stack?",
                "difficulty": "easy", 
                "type": "single", 
                "total_score" : 1,
                "subject_id": 1,
                "image": null, 
                
                "answers":[
                {
                    "text": "LIFO", 
                    "image": null, 
                    "is_correct" : "true"
                },
                {
                    "text": "FIFO", 
                    "image": null, 
                    "is_correct" : "false"
                },
                {
                    "text": "NEMO", 
                    "image": null, 
                    "is_correct" : "false"
                },
                {
                    "text": "NOMO", 
                    "image": null, 
                    "is_correct" : "false"
                },
                {
                    "text": "LIMO", 
                    "image": null, 
                    "is_correct" : "false"
                }

                ]
            }
            
            
            
            ]

        }';
        // we can name an object, can't name array
        // so when we used square brackets above, couldn't name it
        $questions = json_decode($string, true);
        // $questions is a array of questions
        // iterate through array
        // print out all information to test
        // $this->command->info('Course Created!');
        // we didn't have any subjects in there at the time so created one
        // $s = new Subject(array('name' => 'Data Structures'));
        // $s->save();
        foreach ($questions['questions'] as $i => $v) {
            $this->command->info("");
            $this->command->info("Making question: " . ($i + 1));
            $this->command->info("    Prompt: " . $v['prompt']);
            $this->command->info("    Difficulty: " . $v['difficulty']);
            $this->command->info("    Type: " . $v['type']);
            $this->command->info("    Total Score: " . $v['total_score']);
            $this->command->info("    Subject ID: " . $v['subject_id']);
            $this->command->info("    Image: " . $v['image']);
            // create the question
            $q = Question::create(['prompt' => $v['prompt'], 'difficulty' => $v['difficulty'], 'type' => $v['type'], 'total_score' => $v['total_score'], 'subject_id' => $v['subject_id'], 'image' => $v['image']]);
            $q->save();
            $this->command->info("    Answer Choices:");
            // print out all the answers for this question
            foreach ($v['answers'] as $answerIndex => $answer) {
                $a = new Answer($answer);
                $a->save();
                $q->answers()->sync([$a->id]);
                $this->command->info("        Answer ID = " . $a->id);
                $this->command->info("        Option: " . $answer['text']);
                $this->command->info("        Image: " . $answer['image']);
                $this->command->info("        Is Correct: " . $answer['is_correct']);
                $this->command->info("");
            }
        }
    }