Exemplo n.º 1
0
 public function delete($chid, Choice $choices)
 {
     $message = 'تم حذف العنصر بنجاح';
     $choice = $choices->findOrFail($chid);
     $choice->delete();
     return redirect()->route('choice.index', $choice->question_id)->with('success', $message);
 }
 public function updatequestion(Question $question, QuestionRequest $request, $id, $activity_id)
 {
     $question = $question->findOrFail($id);
     $choices = $request->input('choices', 0);
     $choices_ids = (array) $request->input('choices_ids', 0);
     if (!$question->fill($request->all())->save()) {
         return redirect()->back()->with('error', 'وقع مشكل اثناء تحديث السؤال');
     }
     if ($question->type == 'essay') {
         Choice::where('question_id', $question->id)->delete();
     } else {
         Choice::where('question_id', $question->id)->whereNotIn('id', $choices_ids)->delete();
     }
     if (is_array($choices)) {
         foreach ($choices as $key => $choice) {
             if (array_key_exists($key, $choices_ids)) {
                 $id = $choices_ids[$key];
             } else {
                 $id = 0;
             }
             //dd($request);
             if ($question->type == 'multiple_choice') {
                 $istrue = (int) $request->input('choices_correct_' . ($id > 0 ? $id : "new_" . $key));
             }
             if ($question->type != 'multiple_choice') {
                 $istrue = $request->input('choices_correct_new_00') == $id ? 1 : 0;
             }
             if ($id > 0) {
                 Choice::where('id', $id)->update(['choice' => $choice, 'istrue' => $istrue]);
             } else {
                 Choice::create(['choice' => $choice, 'question_id' => $question->id, 'istrue' => $istrue]);
             }
         }
     }
     $lesson = Lesson::findOrFail($question->lesson_id);
     $message = 'تم تعديل السؤال بنجاح';
     if (request('submit') == 'save') {
         //return redirect()->route('choice.create',array('id'=>$id))->with('success' ,$message);
         return redirect()->back()->with('success', $message);
     } else {
         return redirect()->route('teachers.profile.questions', array('id' => $activity_id))->with('success', $message);
     }
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Model::unguard();
     if (Question::count()) {
         return;
     }
     DB::statement('SET FOREIGN_KEY_CHECKS = 0');
     Question::truncate();
     Choice::truncate();
     // $this->call("OthersTableSeeder");
     $q_type = [1 => 'true_false', 2 => 'true_false', 7 => 'single_choice', 6 => 'multiple_choice', 5 => 'essay', 4 => 'complete'];
     ////////////////////// TRUE FALSE
     $this->command->info('connect to old database');
     $questions = DB::connection('old')->table('questions')->select('l2.les_id as l2_les_id', 'questions.*', 'questionanswers.*')->whereIn('q_typeid', [1, 2])->join('lessons', 'lessons.les_id', '=', 'questions.q_lessonid')->leftJoin('lessons as l2', function ($j) {
         $j->on('lessons.les_subjectid', '=', 'l2.les_subjectid')->where('l2.les_active', '=', 'active');
     })->join('questionanswers', 'questions.q_id', '=', 'questionanswers.qans_questionid')->groupBy('questions.q_id')->get();
     $all_q = [];
     $all_answer = [];
     $this->command->info('start first foreach');
     foreach ($questions as $question) {
         $new_question = [];
         $new_question['id'] = $question->q_id;
         $new_question['question'] = $question->q_name;
         $new_question['type'] = $q_type[$question->q_typeid];
         $new_question['lesson_id'] = $question->l2_les_id ? $question->l2_les_id : $question->q_lessonid;
         $new_question['difficulty'] = $question->q_hardlevelid;
         $new_question['created_at'] = $question->q_created;
         $new_question['updated_at'] = $question->q_modified;
         $new_question['isactive'] = $question->q_active;
         $new_answer = [];
         $new_answer['choice'] = $question->q_answer1;
         $new_answer['question_id'] = $question->q_id;
         $new_answer['istrue'] = $question->qans_correct == 0 ? 1 : 0;
         $new_answer['created_at'] = $question->qans_created;
         $new_answer['updated_at'] = $question->qans_modified;
         $all_answer[] = $new_answer;
         $new_answer['choice'] = $question->q_answer2;
         $new_answer['question_id'] = $question->q_id;
         $new_answer['istrue'] = $question->qans_correct == 1 ? 1 : 0;
         $new_answer['created_at'] = $question->qans_created;
         $new_answer['updated_at'] = $question->qans_modified;
         $all_answer[] = $new_answer;
         $all_q[] = $new_question;
     }
     foreach (array_chunk($all_q, 500) as $chunk) {
         DB::table('questionbank_questions')->insert($chunk);
     }
     foreach (array_chunk($all_answer, 500) as $chunk) {
         DB::table('questionbank_choices')->insert($chunk);
     }
     $this->command->info('Questionbank TF entered!');
     ///////////////////////other Q (MCQ + subject)
     $questions = DB::connection('old')->table('questions')->select('l2.les_id as l2_les_id', 'questions.*')->whereIn('q_typeid', [5, 6, 7, 4])->join('lessons', 'lessons.les_id', '=', 'questions.q_lessonid')->leftJoin('lessons as l2', function ($j) {
         $j->on('lessons.les_subjectid', '=', 'l2.les_subjectid')->where('l2.les_active', '=', 'active');
     })->groupBy('questions.q_id')->get();
     $all_q = [];
     foreach ($questions as $question) {
         $new_question = [];
         $new_question['id'] = $question->q_id;
         $new_question['question'] = $question->q_name;
         $new_question['type'] = $q_type[$question->q_typeid];
         $new_question['lesson_id'] = $question->l2_les_id ? $question->l2_les_id : $question->q_lessonid;
         $new_question['difficulty'] = $question->q_hardlevelid;
         $new_question['created_at'] = $question->q_created;
         $new_question['updated_at'] = $question->q_modified;
         $new_question['isactive'] = $question->q_active;
         $all_q[] = $new_question;
     }
     foreach (array_chunk($all_q, 500) as $chunk) {
         DB::table('questionbank_questions')->insert($chunk);
     }
     $this->command->info('Questionbank q 567 entered!');
     $question_ans = DB::connection('old')->table('questionanswers')->join('questions', 'questions.q_id', '=', 'questionanswers.qans_questionid')->whereIn('q_typeid', ['4', '5', '6', '7'])->get();
     $all_answer = [];
     foreach ($question_ans as $ans) {
         $new_answer = [];
         $new_answer['choice'] = $ans->qans_value;
         $new_answer['question_id'] = $ans->q_id;
         $new_answer['istrue'] = $ans->qans_correct == 1 ? 1 : 0;
         $new_answer['created_at'] = $ans->qans_created;
         $new_answer['updated_at'] = $ans->qans_modified;
         $all_answer[] = $new_answer;
     }
     foreach (array_chunk($all_answer, 500) as $chunk) {
         DB::table('questionbank_choices')->insert($chunk);
     }
     $this->command->info('Questionbank ans 567 entered!');
 }