/**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['option' => 'required|unique:single_selects|max:100']);
     $selection = new SingleSelect();
     $selection->option = $request->option;
     $selection->save();
     Session::flash('flash_message', 'Form successfully added!');
     return redirect()->route("singleselect.index");
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function create()
 {
     $formgroups = FormGroup::lists('group_desc', 'id')->all();
     $activitytypes = FormType::lists('form_type', 'id')->all();
     $multiselects = MultiSelect::lists('option', 'id')->all();
     $singleselects = SingleSelect::lists('option', 'id')->all();
     return view('form.create', compact('formgroups', 'activitytypes', 'multiselects', 'singleselects'));
 }
 public function addform($id)
 {
     $audittemplate = AuditTemplate::findOrFail($id);
     $formcategories = FormCategory::getLists();
     $formgroups = FormGroup::getLists();
     $activitytypes = FormType::getLists();
     $multiselects = MultiSelect::getLists();
     $singleselects = SingleSelect::getLists();
     return view('audittemplate.addform', compact('audittemplate', 'formcategories', 'formgroups', 'activitytypes', 'multiselects', 'singleselects'));
 }
 public static function insertForm($template, $code, $type, $required, $prompt, $choices, $expected_answer, $image, $choices2 = null, $con_datas = null, $default_answer = null)
 {
     if (strtoupper($type) == 'DOUBLE') {
         $form_type = FormType::where('form_type', "NUMERIC")->first();
     } else {
         $form_type = FormType::where('form_type', strtoupper($type))->first();
     }
     switch ($required) {
         case 't':
             $required = 1;
             break;
         case 'yes':
             $required = 1;
             break;
         case 'f':
             $required = 0;
             break;
         case 'no':
             $required = 0;
             break;
         default:
             # code...
             break;
     }
     $form = Form::create(array('audit_template_id' => $template->id, 'form_type_id' => $form_type->id, 'prompt' => strtoupper($prompt), 'required' => $required, 'exempt' => 0, 'default_answer' => $default_answer, 'image' => $image, 'code' => $code));
     if ($form_type->id == 9) {
         $choices = explode("~", $choices);
         foreach ($choices as $choice) {
             $sel = MultiSelect::where('option', $choice)->first();
             if (empty($sel)) {
                 $sel = MultiSelect::create(array('option' => strtoupper($choice)));
             }
             FormMultiSelect::create(array('form_id' => $form->id, 'multi_select_id' => $sel->id));
         }
         if (!empty($default_answer)) {
             $_form = Form::find($form->id);
             $ans = MultiSelect::where('option', strtoupper($default_answer))->first();
             $_form->default_answer = $ans->id;
             $_form->update();
         }
     }
     if ($form_type->id == 10) {
         $choices = explode("~", $choices);
         foreach ($choices as $choice) {
             if ($choice == "1") {
                 $opt = "YES";
             } elseif ($choice == "0") {
                 $opt = "NO";
             } else {
                 $opt = $choice;
             }
             $sel = SingleSelect::firstOrCreate(array('option' => strtoupper($opt)));
             FormSingleSelect::create(array('form_id' => $form->id, 'single_select_id' => $sel->id));
         }
         if (!empty($expected_answer)) {
             $_form = Form::find($form->id);
             // $ans = SingleSelect::where('option',strtoupper($expected_answer))->first();
             // $_form->expected_answer = $ans->id;
             // $_form->update();
             $ans = explode("^", $expected_answer);
             $pos_ans = [];
             foreach ($ans as $value) {
                 $_ans = SingleSelect::where('option', strtoupper($value))->first();
                 $pos_ans[] = $_ans->id;
             }
             if (!empty($pos_ans)) {
                 $_form->expected_answer = implode("^", $pos_ans);
                 $_form->update();
             }
         }
         if (!empty($default_answer)) {
             $_form = Form::find($form->id);
             // $ans = SingleSelect::where('option',strtoupper($default_answer))->first();
             // $_form->default_answer = $ans->id;
             // $_form->update();
             $ans = explode("^", $default_answer);
             $pos_ans = [];
             foreach ($ans as $value) {
                 $_ans = SingleSelect::where('option', strtoupper($value))->first();
                 $pos_ans[] = $_ans->id;
             }
             if (!empty($pos_ans)) {
                 $_form->default_answer = implode("^", $pos_ans);
                 $_form->update();
             }
         }
     }
     if ($form_type->id == 11) {
         // dd($choices2);
         FormFormula::create(['form_id' => $form->id, 'formula' => $choices, 'formula_desc' => $choices2]);
     }
     if ($form_type->id == 12) {
         foreach ($con_datas as $con_data) {
             $con = FormCondition::create(['form_id' => $form->id, 'option' => $con_data['option'], 'condition' => $con_data['condition'], 'condition_desc' => $con_data['condition_desc']]);
         }
         if (!empty($expected_answer)) {
             $_form = Form::find($form->id);
             $ans = explode("^", $expected_answer);
             $pos_ans = [];
             foreach ($ans as $value) {
                 $_ans = FormCondition::where('option', strtoupper($value))->where('form_id', $form->id)->first();
                 $pos_ans[] = $_ans->id;
             }
             if (!empty($pos_ans)) {
                 $_form->expected_answer = implode("^", $pos_ans);
                 $_form->update();
             }
         }
         if (!empty($default_answer)) {
             if (!empty($default_answer)) {
                 $_form = Form::find($form->id);
                 $ans = FormCondition::where('option', strtoupper($default_answer))->where('form_id', $form->id)->first();
                 if (!empty($ans)) {
                     $_form->default_answer = $ans->id;
                     $_form->update();
                 }
             }
         }
     }
     return $form;
 }