313 lines
16 KiB
PHP
313 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use DB;
|
|
|
|
use League\Csv\Reader;
|
|
use Auth;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\InvoicesExport;
|
|
|
|
class SurveysubmissionController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'form';
|
|
$this->_module = 'survey';
|
|
$this->_viewname = 'surveysubmission';
|
|
$this->_title = ' : Submitted Data';
|
|
$this->_base_url = '/surveysubmission';
|
|
}
|
|
|
|
protected function get_list_buttons()
|
|
{
|
|
return [['title' => 'Date Filter', 'link' => '#.'], ['title' => 'Export Excel', 'link' => $this->_base_url . '/export']];
|
|
}
|
|
|
|
public function list(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
$form_id = $request->session()->get('entity_id');
|
|
$post = $request->input();
|
|
if (isset($post['startdate']))
|
|
$startdate = $post['startdate'];
|
|
else
|
|
$startdate = date("Y/m/d", strtotime("-365 day"));
|
|
|
|
if (isset($post['enddate']))
|
|
$enddate = $post['enddate'];
|
|
else
|
|
$enddate = date("Y/m/d", strtotime("now") + 86400);
|
|
|
|
$reqdata = ["partition" => 'rapport-' . $user->domain, "form_id" => $form_id, "alldata" => true, "date_range" => $startdate . '_' . $enddate];
|
|
$request->session()->put('formdata_request', $reqdata);
|
|
//echo '<pre>'; print_r($reqdata); exit;
|
|
$response = LambdaRequest::signedrequest('post', $this->_service, $this->_module . '/getformhistory', $reqdata);
|
|
//echo '<pre>'; print_r($response->data);
|
|
if ($response->statusCode == 401) {
|
|
return redirect('/unauthorised');
|
|
}
|
|
|
|
$fields = [
|
|
'user' => ['title' => 'Submitted By', 'type' => 'text'],
|
|
'date' => ['title' => 'Record Date', 'type' => 'text'],
|
|
'sdate' => ['title' => 'Submitted On', 'type' => 'text'],
|
|
];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
if($question->type == 'multitextbox') {
|
|
foreach ($question->orig_options as $opt) {
|
|
$fields['field_' . $qid . '_' . $opt->value] = ['title' => $question->label . ' : ' . $opt->display, 'type' => 'text'];
|
|
//isset($submission->formdata->{$opt->value}) ? $opt->value : '-';
|
|
}
|
|
} else if($question->type == 'radiometrix') {
|
|
foreach ($question->orig_options as $opt) {
|
|
$fields['field_' . $qid . '_' . str_replace(' ', '_', $opt->display)] = ['title' => $question->label . ' : ' . $opt->display, 'type' => 'text'];
|
|
//isset($submission->formdata->{$opt->value}) ? $opt->value : '-';
|
|
}
|
|
} else {
|
|
$fields['field_' . $qid] = ['title' => $question->label, 'type' => 'text'];
|
|
}
|
|
|
|
}
|
|
//echo '<pre>'; print_r($fields); exit;
|
|
$items = [];
|
|
// print_r($response->data->questions);
|
|
// print_r($response->data->submissions);
|
|
// exit;
|
|
foreach ($response->data->submissions as $submission) {
|
|
$item = ['user' => $submission->user_info->name, 'date' => date("d M Y", strtotime($submission->record_date)), 'sdate' => date("d M Y", strtotime($submission->createdAt))];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
//print_r($submission->formdata->{$qid});
|
|
|
|
switch($question->type) {
|
|
case 'text':
|
|
case 'date':
|
|
case 'number':
|
|
$item['field_' . $qid] = isset($submission->formdata->{$qid}) ? $submission->formdata->{$qid} : '';
|
|
break;
|
|
case 'dropdown':
|
|
case 'radio':
|
|
$item['field_' . $qid] = isset($submission->formdata->{$qid}) ? (isset($question->options->{$submission->formdata->{$qid}}) ? $question->options->{$submission->formdata->{$qid}} : '') : '';
|
|
break;
|
|
case 'checkbox':
|
|
$item['field_' . $qid] = isset($submission->formdata->{$qid}) ? ( is_array($submission->formdata->{$qid}) ? implode(',', $submission->formdata->{$qid}) : $submission->formdata->{$qid} ) : '';
|
|
$item['field_' . $qid] = explode(',', $item['field_' . $qid]);
|
|
foreach($item['field_' . $qid] as &$v) {
|
|
$v = (isset($question->options->{$v}) ? $question->options->{$v} : '');
|
|
}
|
|
$item['field_' . $qid] = implode(', ', $item['field_' . $qid]);
|
|
|
|
//$item['field_' . $qid] = array_map(function($v) { return (isset($question->options->{$v}) ? $question->options->{$v} : ''); } , $item['field_' . $qid] );
|
|
|
|
break;
|
|
case 'radiometrix':
|
|
//$fields['field_' . $qid . '_' . str_replace(' ', '_', $opt->display)] = ['title' => $question->label . ' : ' . $opt->display, 'type' => 'text'];
|
|
if(isset($submission->formdata->{$qid}) && is_object($submission->formdata->{$qid})) {
|
|
$rm = [];
|
|
foreach ($submission->formdata->$qid as $key => $val) {
|
|
$rm[] = $key . ':' . $val;
|
|
}
|
|
$item['field_' . $qid] = implode(' , ', $rm);
|
|
} else {
|
|
foreach ($question->orig_options as $opt) {
|
|
$item['field_' . $qid . '_' . str_replace(' ', '_', $opt->display)] = isset($submission->formdata->{$qid . '_' . str_replace(' ', '_', $opt->display)}) ? $submission->formdata->{$qid . '_' . str_replace(' ', '_', $opt->display)} : '';
|
|
//isset($submission->formdata->{$opt->value}) ? $opt->value : '-';
|
|
}
|
|
$item['field_' . $qid] = '';
|
|
}
|
|
|
|
|
|
//echo '<pre>'; print_r($submission->formdata->{$qid}); echo '</pre>';
|
|
break;
|
|
case 'multitextbox':
|
|
foreach ($question->orig_options as $opt) {
|
|
$item['field_' . $qid . '_' . $opt->value] = isset($submission->formdata->{str_replace(' ', '_', $opt->value)}) ? $submission->formdata->{str_replace(' ', '_', $opt->value)} : '-';
|
|
}
|
|
|
|
// if(isset($submission->formdata->{$qid}) && is_object($submission->formdata->{$qid})) {
|
|
// $rm = [];
|
|
// foreach ($submission->formdata->$qid as $key => $val) {
|
|
// $rm[] = $key . ':' . $val;
|
|
// }
|
|
// $item['field_' . $qid] = implode(' , ', $rm);
|
|
// } else {
|
|
// $item['field_' . $qid] = '';
|
|
// }
|
|
|
|
//echo '<pre>'; print_r($question); print_r($submission->formdata); echo '</pre>'; exit;
|
|
//echo '<pre>'; print_r($submission->formdata->{$qid}); echo '</pre>';
|
|
break;
|
|
default:
|
|
if ($question->type == 'radiometrix') {
|
|
$rm = [];
|
|
// foreach ($submission->formdata->$qid as $key => $val) {
|
|
// $rm[] = $key . ':' . $val;
|
|
// }
|
|
// $item['field_' . $qid] = implode(', ', $rm);
|
|
$item['field_' . $qid] = $question->type;
|
|
} else {
|
|
//$item['field_' . $qid] = count((array)$question->options) > 0 && isset($question->options->{$submission->formdata->{$qid}}) ? $question->options->{$submission->formdata->{$qid}} : $submission->formdata->{$qid};
|
|
$item['field_' . $qid] = $question->type;
|
|
}
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
$items[] = (object)$item;
|
|
}
|
|
// echo '<pre>';
|
|
// print_r($items);
|
|
// exit;
|
|
|
|
return $this->view('list')->with([
|
|
'title' => $response->data->formname . $this->_title,
|
|
'fields' => $fields,
|
|
'base_url' => $this->_base_url,
|
|
'items' => $items,
|
|
'buttons' => $this->get_list_buttons(),
|
|
'startdate' => $startdate,
|
|
'enddate' => $enddate
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$reqdata = $request->session()->get('formdata_request');
|
|
//$reqdata = ["partition" => 'rapport-vitalis1', "form_id" => '1650365457-c86bd7f2-a33c-49a5-9671-3c2511b57f3b', "alldata" => true, "date_range" => '2022/_'.$enddate ];
|
|
$response = LambdaRequest::signedrequest('post', $this->_service, $this->_module . '/getformhistory', $reqdata);
|
|
//echo '<pre>'; print_r($response); exit;
|
|
|
|
$fields = ['Submitted By', 'User identification', 'Email', 'Employee Code', 'Record Date', 'Submitted On'];
|
|
$fieldoptions = ['', '', '', '', '', ''];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
$fields[] = $question->label;
|
|
if($question->type == 'multitextbox') {
|
|
$count = 0;
|
|
foreach($question->orig_options as $opv) {
|
|
$fieldoptions[] = $opv->display;
|
|
if($count > 0) $fields[] = '';
|
|
$count++;
|
|
}
|
|
} else if($question->type == 'radiometrix') {
|
|
$count = 0;
|
|
// foreach ($question->orig_options as $opt) {
|
|
// $fieldoptions[] = $opt->display;
|
|
// $count++;
|
|
// }
|
|
|
|
foreach($question->orig_options as $opv) {
|
|
$fieldoptions[] = $opv->display;
|
|
if($count > 0) $fields[] = '';
|
|
$count++;
|
|
}
|
|
} else if($question->type == 'checkbox') {
|
|
$count = 0;
|
|
foreach($question->options as $opk => $opv) {
|
|
$fieldoptions[] = $opv;
|
|
if($count > 0) $fields[] = '';
|
|
$count++;
|
|
}
|
|
} else {
|
|
$fieldoptions[] = '';
|
|
}
|
|
}
|
|
//echo '<pre>'; print_r($fields); print_r($fieldoptions); exit;
|
|
list($startdate, $enddate) = explode('_', $reqdata['date_range']);
|
|
$exceldata = [];
|
|
$exceldata[] = [ucwords($response->data->formname . $this->_title) . ' Report'];
|
|
$exceldata[] = ['Start Date: ' . date("d M Y", strtotime($startdate))];
|
|
$exceldata[] = ['End Date: ' . date("d M Y", strtotime($enddate))];
|
|
$exceldata[] = [''];
|
|
$exceldata[] = $fields;
|
|
$exceldata[] = $fieldoptions;
|
|
|
|
foreach ($response->data->submissions as $submission) {
|
|
$item = [$submission->user_info->name,
|
|
$submission->user_info->id,
|
|
$submission->email,
|
|
$submission->employee_code,
|
|
date("d M Y", strtotime($submission->record_date)),
|
|
date("d M Y", strtotime($submission->createdAt))];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
//$item[] = $submission->formdata->{$qid};
|
|
switch($question->type) {
|
|
case 'text':
|
|
case 'date':
|
|
case 'number':
|
|
case 'textarea':
|
|
$item[] = isset($submission->formdata->{$qid}) ? $submission->formdata->{$qid} : '';
|
|
break;
|
|
case 'dropdown':
|
|
case 'radio':
|
|
$opts = array_values((array)$question->options);
|
|
$item[] = isset($submission->formdata->{$qid}) ? (isset($question->options->{$submission->formdata->{$qid}}) ? $question->options->{$submission->formdata->{$qid}} : ( $submission->formdata->{$qid} == 'on' ? $opts[0] : '') ) : '';
|
|
break;
|
|
case 'checkbox':
|
|
foreach($question->options as $opk => $opv) {
|
|
$item[] = isset($submission->formdata->{$qid}) ? ( is_array($submission->formdata->{$qid}) ? (in_array($opk,$submission->formdata->{$qid}) ? 'Yes' : '' ) : $submission->formdata->{$qid} ) : '';
|
|
}
|
|
|
|
// $it = isset($submission->formdata->{$qid}) ? ( is_array($submission->formdata->{$qid}) ? implode(',', $submission->formdata->{$qid}) : $submission->formdata->{$qid} ) : '';
|
|
// $it = explode(',', $it);
|
|
// foreach($it as &$v) {
|
|
// $v = (isset($question->options->{$v}) ? $question->options->{$v} : '');
|
|
// }
|
|
// $item[] = implode(', ', $it);
|
|
|
|
//$item['field_' . $qid] = array_map(function($v) { return (isset($question->options->{$v}) ? $question->options->{$v} : ''); } , $item['field_' . $qid] );
|
|
|
|
break;
|
|
case 'radiometrix':
|
|
foreach($question->orig_options as $opv) {
|
|
if(isset($submission->formdata->{$qid}) && is_object($submission->formdata->{$qid})) {
|
|
// $rm = [];
|
|
// foreach ($submission->formdata->$qid as $key => $val) {
|
|
// $rm[] = $key . ':' . $val;
|
|
// }
|
|
// $item[] = implode(' , ', $rm);
|
|
$item[] = isset($submission->formdata->{$qid}->{$opv->display}) ? $submission->formdata->{$qid}->{$opv->display} : '';
|
|
} else {
|
|
//foreach ($question->orig_options as $opt) {
|
|
$item[] = isset($submission->formdata->{$qid . '_' . str_replace(' ', '_', $opv->display)}) ? $submission->formdata->{$qid . '_' . str_replace(' ', '_', $opv->display)} : '';
|
|
//isset($submission->formdata->{$opt->value}) ? $opt->value : '-';
|
|
//}
|
|
//$item[] = '';
|
|
}
|
|
}
|
|
|
|
//echo '<pre>'; print_r($submission->formdata->{$qid}); echo '</pre>';
|
|
break;
|
|
case 'multitextbox':
|
|
foreach ($question->orig_options as $opt) {
|
|
$item[] = isset($submission->formdata->{str_replace(' ', '_', $opt->value)}) ? $submission->formdata->{str_replace(' ', '_', $opt->value)} : '-';
|
|
}
|
|
break;
|
|
default:
|
|
if ($question->type == 'radiometrix') {
|
|
$rm = [];
|
|
// foreach ($submission->formdata->$qid as $key => $val) {
|
|
// $rm[] = $key . ':' . $val;
|
|
// }
|
|
// $item['field_' . $qid] = implode(', ', $rm);
|
|
$item['field_' . $qid] = $question->type;
|
|
} else {
|
|
//$item['field_' . $qid] = count((array)$question->options) > 0 && isset($question->options->{$submission->formdata->{$qid}}) ? $question->options->{$submission->formdata->{$qid}} : $submission->formdata->{$qid};
|
|
$item['field_' . $qid] = $question->type;
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
$exceldata[] = $item;
|
|
}
|
|
return Excel::download(new InvoicesExport($exceldata), 'report.xlsx');
|
|
}
|
|
}
|