File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Http/Controllers/ResultsController.php
<?php
namespace App\Http\Controllers;
use DataTables;
use App\Form;
use App\HeaderValue;
use App\HeaderValueDetail;
use \Excel;
use App\Http\Controllers\Exports\ReportResults;
class ResultsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('results.list');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function list_answers($form_id)
{
$obj = Form::select('id', 'name', 'created_at')
->with('fields')
->where('id', $form_id)
->get();
$headers = [];
if (count($obj) > 0) {
foreach ($obj as $key => $form) {
if (count($form->fields) > 0) {
foreach ($form->fields as $key => $field) {
$headers[] = [
'data' => $field->name,
'name' => $field->name
];
}
}
}
}
return response()->view('results.detail_users.list', compact('form_id', 'headers'));
}
public function tableFilter()
{
$obj = Form::select('id', 'name', 'created_at');
return DataTables::of($obj)
->addColumn('actions', function ($obj) {
return '<i class="fa fa-users iconMini" onClick="clickDetailUsers(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" style="cursor:pointer;"></i>';
})
->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})
->rawColumns(['actions', 'created_at'])
->make(true);
}
public function tableFilterAnswers($form_id)
{
$obj = HeaderValue::select('id', 'form_id', 'user_id', 'created_at')
->where('form_id', $form_id)
->with('form', 'user', 'user.documentType', 'header_value_details')
->get();
$dt = DataTables::of($obj);
$dt->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})->rawColumns(['created_at']);
if (count($obj) > 0) {
foreach ($obj as $header) {
if (count($header->header_value_details) > 0) {
foreach ($header->header_value_details as $item) {
$header[$item->field->name] = $item->value;
}
}
}
}
return $dt->make(true);
}
public function answers_detail($header_value_id)
{
try {
$data = HeaderValueDetail::where("header_value_id", $header_value_id)->with('field')->get();
return response(array("r" => true, "type" => "error", "title" => "Oops...", "m" => "", "data" => $data));
} catch (\Throwable $th) {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_global'), "data" => $th->getMessage()));
}
}
public function validateExport($formId, $fromDate = null, $toDate = null)
{
$obj = HeaderValue::select('id', 'form_id', 'user_id', 'created_at')
->where('form_id', $formId)
->where(function ($query) use ($fromDate, $toDate) {
if ($fromDate && $toDate) {
$query->whereBetween('created_at', [$fromDate . " 00:00:00", $toDate . " 23:59:59"]);
}
})
->get();
if ($obj->count() == 0) {
return response()->json(['success' => false, 'message' => 'No existen datos para exportar en el rango de fechas']);
}
return response()->json(['success' => true, 'message' => 'Validación OK', 'data' => $obj->count()]);
}
public function export($formId, $fromDate = null, $toDate = null)
{
return Excel::download(new ReportResults($formId, $fromDate, $toDate), 'formresults' . $formId . '-' . time() . '.xlsx');
}
}