110 lines
4.7 KiB
PHP
110 lines
4.7 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 FormsubmissionController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'form';
|
|
$this->_module = 'formentity';
|
|
$this->_viewname = 'formsubmission';
|
|
$this->_title = ' : Submitted Data';
|
|
$this->_base_url = '/formsubmission';
|
|
}
|
|
|
|
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("-1 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 ];
|
|
if(isset($post['startdate']) && isset($post['enddate'])) {
|
|
$reqdata['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);
|
|
if($response->statusCode == 401) {
|
|
return redirect('/unauthorised');
|
|
}
|
|
//echo '<pre>'; print_r($response->data); exit;
|
|
$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){
|
|
$fields['field_'.$qid] = ['title' => $question->label, 'type' => 'text'];
|
|
}
|
|
$items = [];
|
|
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){
|
|
$item['field_'.$qid] = count((array)$question->options) > 0 && isset($question->options->{ $submission->formdata->{$qid} }) ? $question->options->{ $submission->formdata->{$qid} } : $submission->formdata->{$qid};
|
|
}
|
|
$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);
|
|
$fields = ['Submitted By', 'User identification', 'Record Date', 'Submitted On'];
|
|
foreach($response->data->questions as $qid => $question){
|
|
$fields[] = $question->label;
|
|
}
|
|
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;
|
|
|
|
foreach($response->data->submissions as $submission) {
|
|
$item = [$submission->user_info->name, $submission->user_info->id, 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};
|
|
}
|
|
$exceldata[] = $item;
|
|
}
|
|
return Excel::download(new InvoicesExport($exceldata), 'report.xlsx');
|
|
}
|
|
}
|