mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 20:51:07 +03:30
232 lines
9.7 KiB
PHP
232 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Requests\StoreTestResultRequest;
|
|
use App\TestResult;
|
|
use App\InputValue;
|
|
use App\Input;
|
|
|
|
class TestResultController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$TestResult = TestResult::with('test_type', 'user', 'user.medical_center')->paginate(10);
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
// public function CreateChildFormValue2($Input,$TestResultObj,$TestResultInfo)
|
|
// {
|
|
// if ($Input['type'] != 'Form') {
|
|
// $Value = $TestResultInfo[$Input->name];
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value]);
|
|
// }
|
|
// else {
|
|
// foreach ($Input->children as $InputChild) {
|
|
// $this->CreateChildFormValue2($InputChild,$TestResultObj,$TestResultInfo[$Input->name]);
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
|
|
public function store(StoreTestResultRequest $request)
|
|
{
|
|
try {
|
|
$return_value = \DB::transaction(function () use ($request) {
|
|
|
|
$TestResult = [
|
|
'user_id' => auth('api')->user()->id,
|
|
'autoclave_id' => $request->autoclave_id,
|
|
'test_type_id' => $request->test_type_id,
|
|
'result' => $request->result,
|
|
'verified' => 'NotVerified',
|
|
];
|
|
$TestResultObj = TestResult::create($TestResult);
|
|
$TestResultInfo = $request->all();
|
|
|
|
$Inputs = Input::with('options','children','children.children')->whereNull('parent_id')->where('test_type_id',$request->test_type_id)->get();
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////--2
|
|
foreach ($Inputs as $Input) {
|
|
if ($Input['type'] != 'Form') {
|
|
$Value = $TestResultInfo[$Input->name];
|
|
|
|
if ($Input['type'] == 'Checkbox' || $Input['type'] == 'Select' ) {
|
|
|
|
foreach ($Value as $Option) {
|
|
// dd($Option);
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $Input->id, 'option_id' => $Option]);
|
|
}
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $Input->id, 'value' => $Value]);
|
|
}
|
|
|
|
}
|
|
else {
|
|
foreach ($Input->children as $InputChild) {
|
|
$Value = $TestResultInfo[$InputChild->name];
|
|
|
|
if ($InputChild['type'] == 'Checkbox' || $InputChild['type'] == 'Select') {
|
|
// dd($InputChild['type']);
|
|
foreach ($Value as $Option) {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $InputChild->id, 'option_id' => $Option]);
|
|
}
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $InputChild->id, 'value' => $Value]);
|
|
}
|
|
|
|
}
|
|
//-------------------------------------------------------
|
|
// $this->CreateChildFormValue2($Input,$TestResultObj,$TestResultInfo);
|
|
// foreach ($Input->children as $InputChild) {
|
|
//
|
|
// if ($InputChild['type'] != 'Form') {
|
|
// $Value = $TestResultInfo[$Input->name][$InputChild->name];
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild->id , 'value' => $Value]);
|
|
// }
|
|
// else {
|
|
//
|
|
// foreach ($InputChild->children as $InputChild2) {
|
|
//
|
|
// if ($InputChild2['type'] != 'Form') {
|
|
// $Value = $TestResultInfo[$Input->name][$InputChild->name][$InputChild2->name];
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild2->id , 'value' => $Value]);
|
|
// }
|
|
// else {
|
|
//
|
|
// }
|
|
//
|
|
// }
|
|
// }
|
|
//
|
|
// }
|
|
//---------------------------------------------------------
|
|
|
|
}
|
|
}
|
|
// if ($request->hasFile('image')) {
|
|
// $image = $request->file('image');
|
|
// $input['imagename'] = time().$image->getClientOriginalExtension();
|
|
// $path = '/Uploads'.'/'.$TestResultObj->id;
|
|
// Storage::disk('public')->makeDirectory($TestResultObj->id);
|
|
// $image->move($path, $input['imagename']);
|
|
// }
|
|
|
|
|
|
});
|
|
} catch (\Exception $e) {
|
|
return response()->json($e->getMessage(), 441);
|
|
}
|
|
return \MessageResponse::success(null,trans('messages.create'));
|
|
|
|
}
|
|
|
|
public function show(TestResult $TestResult)
|
|
{
|
|
foreach ($TestResult->input_values as $key => $value) {
|
|
$input = $value->input;
|
|
if ($input->type == 'Checkbox' || $input->type == 'Select') {
|
|
$input_options = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $input->id)->get()->pluck('option_id')->toArray();
|
|
$TestResult[$input->name] = $input_options;
|
|
}
|
|
else {
|
|
$TestResult[$input->name] = $value->value;
|
|
}
|
|
}
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
//Todo:: pass Inputs
|
|
public function edit(TestResult $TestResult)
|
|
{
|
|
foreach ($TestResult->input_values as $key => $value) {
|
|
$input = $value->input;
|
|
if ($input->type == 'Checkbox' || $input->type == 'Select') {
|
|
$input_options = InputValue::where('test_result_id', $TestResult->id)->where('input_id', $input->id)->get()->pluck('option_id')->toArray();
|
|
$TestResult[$input->name] = $input_options;
|
|
}
|
|
else {
|
|
$TestResult[$input->name] = $value->value;
|
|
}
|
|
}
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
public function update(StoreTestResultRequest $request, TestResult $TestResult)
|
|
{
|
|
$TestResultData = [
|
|
'user_id' => auth('api')->user()->id,
|
|
'autoclave_id' => $request->autoclave_id,
|
|
'test_type_id' => $request->test_type_id,
|
|
'result' => $request->result,
|
|
'verified' => 'NotVerified',
|
|
];
|
|
$TestResult->update($TestResultData);
|
|
$TestResultInfo = $request->all();
|
|
|
|
$Inputs = Input::with('options','children','children.children')->whereNull('parent_id')->where('test_type_id',$request->test_type_id)->get();
|
|
|
|
foreach ($Inputs as $Input) {
|
|
if ($Input['type'] != 'Form') {
|
|
$Value = $TestResultInfo[$Input->name];
|
|
|
|
if ($Input['type'] == 'Checkbox' || $Input['type'] == 'Select' ) {
|
|
foreach ($Value as $Option) {
|
|
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id, 'input_id' => $Input->id, 'option_id' => $Option]);
|
|
}
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::where('test_result_id',$TestResult->id)->where('input_id', $Input['id'])->update(['value' => $Value]);
|
|
}
|
|
|
|
}
|
|
else {
|
|
foreach ($Input->children as $InputChild) {
|
|
$Value = $TestResultInfo[$InputChild->name];
|
|
if ($InputChild['type'] == 'Checkbox' || $InputChild['type'] == 'Select') {
|
|
$Value = json_encode($Value);
|
|
}
|
|
else {
|
|
$InputValueObj = InputValue::where('test_result_id',$TestResult->id)->where('input_id', $InputChild['id'])->update(['value' => $Value]);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
return \MessageResponse::success(null,trans('messages.update'));
|
|
}
|
|
|
|
public function destroy(TestResult $TestResult)
|
|
{
|
|
if (auth('api')->user()->can('delete', $TestResult)) {
|
|
return \MessageResponse::error(trans('messages.failed'));
|
|
}
|
|
$TestResult->delete();
|
|
return \MessageResponse::success(null, trans('messages.failed'));
|
|
}
|
|
|
|
public function verifyTestResult(TestResult $TestResult)
|
|
{
|
|
if (auth('api')->user()->can('verifyTestResult', $TestResult)) {
|
|
return \MessageResponse::error(trans('messages.failed'));
|
|
}
|
|
if ($TestResult->verified == 'Verified') {
|
|
$TestResult->verified = 'NotVerified';
|
|
} else {
|
|
$TestResult->verified = 'Verified';
|
|
}
|
|
$TestResult->save();
|
|
return \MessageResponse::data($TestResult);
|
|
}
|
|
|
|
}
|