/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $_key = $this->argument('key');
     $_blueprint = $this->argument('blueprint');
     $_file = $this->argument('file');
     $_creator = $this->argument('creator');
     $_header = escapeshellarg($this->argument('header'));
     $blueprint = Blueprint::find($_blueprint);
     if (!$blueprint) {
         Log::error("la encuesta con id: {$_blueprint} no se encontró");
         return;
     }
     $blueprint->sending_emails = 1;
     $blueprint->update();
     $counter = 0;
     Excel::load("storage/app/" . $_key . ".xlsx", function ($reader) use($blueprint, $counter, $_key, $_header) {
         $reader->each(function ($row) use($blueprint, $counter, $_key, $_header) {
             if (trim($row->correo) != "" && filter_var($row->correo, FILTER_VALIDATE_EMAIL)) {
                 $form_key = str_replace("/", "", Hash::make('blueprint' . $blueprint->id . $row->correo));
                 $applicant = Applicant::firstOrCreate(["blueprint_id" => $blueprint->id, "form_key" => $form_key, "user_email" => $row->correo, "temporal_key" => $_key]);
                 $path = base_path();
                 exec("php {$path}/artisan email:send {$applicant->id} {$_header} > /dev/null &");
                 $update = $blueprint->emails + 1;
                 $blueprint->emails = $update;
                 $blueprint->update();
             }
         });
     })->first();
     $total = Applicant::where("temporal_key", $_key)->count();
     $mailgun = new MailgunEmail(["blueprint" => $blueprint->id, "emails" => $total]);
     $mailgun->save();
     $blueprint->sending_emails = 0;
     $blueprint->update();
 }
 /**
  * Show the application dashboard.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $user = Auth::user();
     $data['title'] = 'Dashboard Tú Evalúas';
     $data['description'] = '';
     $data['body_class'] = 'dash';
     $data['user'] = $user;
     $data['admins'] = $user->level == 3 ? User::all() : [];
     $data['surveys'] = $user->level == 3 ? Blueprint::all() : $user->blueprints;
     return view('dashboard')->with($data);
 }
 public function saveAnswer(Request $request)
 {
     if ($request->input('is_test') != 1) {
         $applicant = Applicant::where('form_key', $request->input('form_key'))->first();
         $blueprint = Blueprint::find($applicant->blueprint_id);
         $question = Question::find($request->input('question_id'));
         $answer = Answer::firstOrCreate(["blueprint_id" => $blueprint->id, "question_id" => $question->id, "form_key" => $applicant->form_key]);
         if ($question->type == "integer" || $question->type == "number") {
             $answer->num_value = $request->input('question_value');
             $answer->text_value = null;
         } else {
             $answer->num_value = null;
             $answer->text_value = $request->input('question_value');
         }
         $answer->update();
         $answer->question_value = $request->input('question_value');
         $answer->new_token = csrf_token();
     } else {
         $question = Question::find($request->input('question_id'));
         $answer = new Answer(["blueprint_id" => $question->blueprint->id, "question_id" => $question->id, "form_key" => "xxx"]);
         $answer->num_value = $question->type == "number" ? $request->input('question_value') : null;
         $answer->text_value = $question->type != "number" ? $request->input('question_value') : null;
         $answer->question = $question;
         //$answer->update();
         $answer->question_value = $request->input('question_value');
         $answer->new_token = csrf_token();
     }
     return response()->json($answer)->header('Access-Control-Allow-Origin', '*');
 }
Beispiel #4
0
 function result($id)
 {
     $blueprint = Blueprint::with(["questions.options"])->find($id);
     $test = function ($question_type, $inegi_key) {
         if (!$inegi_key) {
             return "-";
         }
         switch ($question_type) {
             case "location-a":
                 $key = substr($inegi_key->text_value, 0, 2);
                 $name = !empty($key) ? $this->states[$key] : "-";
                 break;
             case "location-b":
                 $state_key = substr($inegi_key->text_value, 0, 2);
                 $state_name = !empty($state_key) ? $this->states[$state_key] : "-";
                 $city_key = substr($inegi_key->text_value, 2, 3);
                 $city = !empty($city_key) ? City::where("clave", $city_key)->where("estado_id", (int) $state_key)->first() : null;
                 $city_name = $city ? $city->nombre : "-";
                 $name = $city_name . ", " . $state_name;
                 break;
             case "location-c":
                 $state_key = substr($inegi_key->text_value, 0, 2);
                 $state_name = !empty((int) $state_key) ? $this->states[$state_key] : "-";
                 $city_key = substr($inegi_key->text_value, 2, 3);
                 $city = !empty($city_key) ? City::where("clave", $city_key)->where("estado_id", (int) $state_key)->first() : null;
                 $city_name = $city ? $city->nombre : "-";
                 $location_key = substr($inegi_key->text_value, 5, 4);
                 $location = !empty((int) $location_key) ? Location::where("clave", $location_key)->where("municipio_id", $city->id)->first() : null;
                 $location_name = $location ? $location->nombre : "-";
                 $name = $location_name . ", " . $city_name . ", " . $state_name;
                 break;
             default:
                 $name = "-";
                 break;
         }
         return $name;
     };
     if (!$blueprint) {
         die("Este formulario no existe!");
     }
     $data = [];
     $data['blueprint'] = $blueprint;
     $data['title'] = 'Resultados | Tú Evalúas';
     $data['description'] = 'Resultados de cuestionarios en Tú Evalúas';
     $data['body_class'] = 'results';
     $data['test'] = $test;
     return view("frontend.result_survey")->with($data);
 }
 public function questions(Request $request)
 {
     // [1] validate the title and the CSV
     $messages = ['title.required' => 'El título es requerido', 'the-csv.required' => 'El archivo de excel es requerido', 'the-csv.mimetypes' => 'El archivo debe ser excel'];
     $this->validate($request, ['title' => 'bail|required|max:255', 'the-csv' => 'required|mimetypes:application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'], $messages);
     // [2] save the quiz blueprint
     $user = Auth::user();
     $blueprint = new Blueprint();
     $blueprint->title = $request->input("title");
     $blueprint->user_id = $user->id;
     $blueprint->is_closed = 0;
     $blueprint->is_public = 0;
     $blueprint->is_visible = 0;
     $blueprint->type = "generated";
     $blueprint->save();
     // [3] add the questions
     $file = $request->file("the-csv");
     $temp = $file->getPathName();
     Excel::load($temp, function ($reader) use($blueprint) {
         $reader->each(function ($row) use($blueprint) {
             if (trim($row->pregunta) != "") {
                 $question = new Question();
                 //$options  = !empty($row->seccion) ? (int)$row->seccion : 1;
                 $question->blueprint_id = $blueprint->id;
                 $question->question = $row->pregunta;
                 $question->section_id = !empty($row->seccion) ? (int) $row->seccion : 1;
                 $type = strtr(strtolower($row->tipo), "áéíóú", "aeiou");
                 if (in_array($type, ['numero', 'numerico', 'numerica', 'cantidad', 'cifra'])) {
                     $question->type = "number";
                 } elseif ($type == "estado") {
                     $question->type = "location-a";
                 } elseif ($type == "municipio") {
                     $question->type = "location-b";
                 } elseif ($type == "localidad") {
                     $question->type = "location-c";
                 } elseif ($type == "multiple" || $type == "opcion multiple") {
                     $question->type = "multiple";
                 } elseif ($type == "multiple multiple" || $type == "multiple-multiple") {
                     $question->type = "multiple-multiple";
                 } else {
                     $question->type = "text";
                 }
                 $question->save();
                 if (!empty($row->opciones) && ($question->type == "multiple" || $question->type == "multiple-multiple")) {
                     $options = explode("|", $row->opciones);
                     for ($i = 0; $i < count($options); $i++) {
                         $option = new Option();
                         $option->question_id = $question->id;
                         $option->blueprint_id = $blueprint->id;
                         $option->description = $options[$i];
                         $option->value = $i + 1;
                         //$options[$i];//$i+1;
                         $option->name = uniqid();
                         $option->order_num = $i;
                         $option->save();
                     }
                     // for
                 }
                 // if
             }
         });
     })->first();
     $request->session()->flash('status', ['type' => 'create', 'name' => $blueprint->title]);
     return redirect("dashboard/encuestas");
 }
 public function saveRule(Request $request)
 {
     // [1] CHECK IF THE BLUEPRINT EXIST AND THE USER CAN CHANGE IT
     $user = Auth::user();
     $blueprint = Blueprint::find($request->input('blueprint_id'));
     if (!$blueprint) {
         if ($user->level == 3) {
             abort(404, 'El formulario no existe!');
         } else {
             abort(403, 'El formulario no pertenece al usuario');
         }
     }
     // [2] CREATE THE RULE OBJECT
     $rule = new Rule();
     $rule->blueprint_id = $blueprint->id;
     $rule->section_id = (int) $request->input("section_id");
     $rule->question_id = (int) $request->input("question_id");
     $rule->value = $request->input("value");
     $rule->save();
     // [4] GENERATE A NEW TOKEN TO PREVENT TOKEN MISSMATCH
     $rule->new_token = csrf_token();
     // [5] RETURN THE JSON
     return response()->json($rule)->header('Access-Control-Allow-Origin', '*');
 }
 public function search(Request $request)
 {
     $user = Auth::user();
     $query = $request->input("query");
     if ($user->level == 3) {
         $response = Blueprint::where("title", "like", "%{$query}%")->get();
     } else {
         $response = $user->blueprints()->where("title", "like", "%{$query}%")->get();
     }
     return response()->json($response)->header('Access-Control-Allow-Origin', '*');
 }
Beispiel #8
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $id = $this->argument('blueprint');
     $type = $this->argument('type');
     $full = (int) $this->argument('full');
     /// $type, public_path('csv')
     $path = $full ? storage_path('app') : public_path('csv');
     $user = Auth::user();
     $blueprint = Blueprint::with("questions.options")->find($id);
     $title = $this->sluggable($blueprint->title);
     Excel::create($title, function ($excel) use($blueprint, $full) {
         // Set the title
         $excel->setTitle($blueprint->title);
         // Chain the setters
         $excel->setCreator('Tú Evalúas');
         //->setCompany('Transpar');
         // Call them separately
         $excel->setDescription("Resultado desagregados");
         // add a sheet for each day, and set the date as the name of the sheet
         $excel->sheet("encuestas", function ($sheet) use($blueprint, $full) {
             //var_dump($titles->toArray());
             $questions = $blueprint->questions;
             $titles = $questions->pluck("question");
             $titles = $titles->toArray();
             array_unshift($titles, "id");
             $sheet->appendRow($titles);
             $applicants = $blueprint->applicants()->has("answers")->with("answers")->get();
             foreach ($applicants as $applicant) {
                 $row = [];
                 $row[] = $applicant->id;
                 foreach ($questions as $question) {
                     if ($question->is_description) {
                         $row[] = "es descripción";
                     } elseif (in_array($question->type, ['location-a', 'location-b', 'location-c'])) {
                         $inegi_key = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         $row[] = $this->find_location($question->type, $inegi_key);
                     } elseif ($question->type == "multiple-multiple") {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         if ($open_answer && !empty($open_answer->text_value)) {
                             $r = Option::whereIn("value", explode(",", $open_answer->text_value))->where("blueprint_id", $blueprint->id)->where("question_id", $question->id)->lists('description')->toArray();
                             $row[] = implode(",", $r);
                         } else {
                             $row[] = "no contestó";
                         }
                     } elseif (in_array($question->type, ['text', 'multiple'])) {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $open_answer ? $open_answer->text_value : "no contestó";
                     } elseif ($question->type == "personal") {
                         $open_answer = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $open_answer && $full ? $open_answer->text_value : "";
                     } else {
                         $num_value = $applicant->answers()->where("question_id", $question->id)->get()->first();
                         //->text_value;
                         $row[] = $num_value ? $num_value->num_value : "-";
                     }
                 }
                 $sheet->appendRow($row);
             }
         });
     })->store($type, $path);
     $blueprint->csv_file = $title;
     $blueprint->update();
     $this->info('El archivo se guardó!');
 }