137 lines
5.7 KiB
PHP
137 lines
5.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 QuizsubmissionController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'form';
|
|
$this->_module = 'quiz';
|
|
$this->_viewname = 'quizsubmission';
|
|
$this->_title = ' : Submitted Data';
|
|
$this->_base_url = '/quizsubmission';
|
|
}
|
|
|
|
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);
|
|
if ($response->statusCode == 401) {
|
|
return redirect('/unauthorised');
|
|
}
|
|
//echo '<pre>'; print_r($response); exit;
|
|
$fields = [
|
|
'user' => ['title' => 'Submitted By', 'type' => 'text'],
|
|
'date' => ['title' => 'Record Date', 'type' => 'text'],
|
|
'sdate' => ['title' => 'Submitted On', 'type' => 'text'],
|
|
'total_questions' => ['title' => 'Total Questions', 'type' => 'text'],
|
|
'correct_answers' => ['title' => 'Correct Answers', 'type' => 'text'],
|
|
'time_taken' => ['title' => 'Time Taken (in Sec)', 'type' => 'text'],
|
|
];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
$fields['field_' . $qid] = ['title' => $question->label, 'type' => 'text'];
|
|
}
|
|
//echo '<pre>';
|
|
// print_r($fields);
|
|
// exit;
|
|
$items = [];
|
|
// print_r($response->data->questions);
|
|
|
|
//echo '<pre>'; 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)),
|
|
'total_questions' => $submission->total_questions,
|
|
'correct_answers' => $submission->correct_answers,
|
|
'time_taken' => $submission->time_taken,
|
|
];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
$item['field_' . $qid] = $submission->formdata->{$qid} ? 'Correct' : 'Incorrect';
|
|
}
|
|
$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($reqdata); print_r($response); exit;
|
|
$fields = ['Submitted By', 'User identification', 'Email', 'Employee Code', 'Record Date', 'Submitted On', 'Total Questions', 'Correct Answers', 'Time Taken (in Sec)'];
|
|
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,
|
|
$submission->email,
|
|
$submission->employee_code,
|
|
date("d M Y", strtotime($submission->record_date)),
|
|
date("d M Y", strtotime($submission->createdAt)),
|
|
$submission->total_questions,
|
|
$submission->correct_answers,
|
|
$submission->time_taken,
|
|
];
|
|
foreach ($response->data->questions as $qid => $question) {
|
|
$item[] = $submission->formdata->{$qid} ? 'Correct' : 'Incorrect';
|
|
}
|
|
$exceldata[] = $item;
|
|
}
|
|
return Excel::download(new InvoicesExport($exceldata), 'report.xlsx');
|
|
}
|
|
}
|