create Test Creator

This commit is contained in:
saeid
2019-04-23 20:40:43 +04:30
parent e8196ceb39
commit d23c5f134b
11 changed files with 3783 additions and 2820 deletions
+79 -69
View File
@@ -4,111 +4,121 @@ 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
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$TestResult = TestResult::with('testtype')->paginate(10);
return response()->json(['TestResult' => $TestResult, 'LastPage' => $TestResult->lastPage()]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function CreateChildFormValue($InputChild,$TestResultObj,$TestResultInfo)
{
$TestResult = [
'user_id' => Auth::user()->id,
'autoclave_id' => $request->autoclave_id,
'test_type_id' => $request->test_type_id,
'result' => $request->result,
'verified' => 0,
];
$TestResultObj = TestResult::create($TestResult);
$TestResultInfo = $request->info;
// dd($TestResultInfo[$InputChild->name]);
// foreach ($Input->children as $InputChild) {
// $TestType = TestType::with('inputs')->where('id',$request->test_type_id)->first();
$Inputs = Input::where('test_type_id',$request->test_type_id)->get();
foreach ($Inputs as $Input) {
if ($Input->type == 'Select' || $Input->type == 'CheckBox') {
$OptionIDs = $Input->options->pluck('id')->toArray();
$Value = $OptionIDs;
$TestResultInfo = $TestResultInfo[$InputChild->name];
if ($InputChild->type == 'Form') {
foreach ($InputChild->children as $InputChild2) {
// dd($TestResultInfo[$InputChild2->name]);
$this->CreateChildFormValue($InputChild2,$TestResultObj,$TestResultInfo[$InputChild2->name]);
}
}
else {
$Value = $TestResultInfo[$Input->name];
}
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value]);
$Value = $TestResultInfo;
// dd($Value);
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild['id'] , 'value' => $Value]);
// dd(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild['id'] , 'value' => $Value]);
// }
}
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' => 0,
];
$TestResultObj = TestResult::create($TestResult);
// $TestResultInfo = $request['info'];
$TestResultInfo = [];
$Inputs = Input::with('options','children')->where('test_type_id',$request->test_type_id)->get();
foreach ($Inputs as $Input) {
if ($Input['type'] == 'Form') {
$TestResultInfo = $request[$Input->name];
// $this->CreateChildFormValue($Input,$TestResultObj,$TestResultInfo);
///--------------------------------------------------
foreach ($Input->children as $InputChild) {
$this->CreateChildFormValue($InputChild,$TestResultObj,$TestResultInfo);
///--------------------------------------------------
// $Value = $TestResultInfo[$InputChild->name];
//
// if ($InputChild->type == 'Form') {
// foreach ($InputChild->children as $InputChild2) {
// $Value = $TestResultInfo[$InputChild->name][$InputChild2->name];
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild2['id'] , 'value' => $Value]);
// return 'success';
// }
//
// }
// $InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $InputChild['id'] , 'value' => $Value]);
}
///--------------------------------------------------
}
else {
$TestResultInfo = $request->all();
}
$Value = $TestResultInfo[$Input->name];
$InputValueObj = InputValue::create(['test_result_id' => $TestResultObj->id , 'input_id' => $Input->id , 'value' => $Value]);
}
});
} catch (\Exception $e) {
return response()->json($e->getMessage(), 441);
}
return \MessageResponse::success();
// }
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, TestResult $TestResult)
public function update(StoreTestResultRequest $request, TestResult $TestResult)
{
$this->validate($request, [
'user_id' => 'required',
'autoclave_id' => 'required',
'test_type_id' => 'required',
'result' => 'required',
'verified' => 'required',
]);
$TestResult->update($request->all());
return response()->json(trans('messages.success'));
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
+107 -44
View File
@@ -21,9 +21,14 @@ class TestTypeController extends Controller
//
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////// Add TestType /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
public function CreateInput($Input,$TestType,$ParentID = null)
{
// dd($Input['name'],$TestType->id,$ParentID);
//---------------------------------------------Input
$InputData = [
'name' => $Input['name'],
@@ -57,24 +62,31 @@ class TestTypeController extends Controller
}
public function store(StoreTestTypeRequest $request)
{
$TestType = [
'name' => $request['name'],
'unique_code' => $request['unique_code'],
'user_id' => auth('api')->user()->id
];
try {
$return_value = \DB::transaction(function () use ($request) {
$TestType = TestType::create($TestType);
$TestTypeInfo = $request['info'];
$TestType = [
'name' => $request['name'],
'unique_code' => $request['unique_code'],
'user_id' => auth('api')->user()->id
];
foreach ($TestTypeInfo as $Input) {
$TestType = TestType::create($TestType);
$TestTypeInfo = $request['info'];
$InputObj = $this->CreateInput($Input,$TestType);
foreach ($TestTypeInfo as $Input) {
if ($Input['type'] == 'Form') {
$this->CreateChildForm($Input,$TestType,$InputObj);
}
$InputObj = $this->CreateInput($Input,$TestType);
if ($Input['type'] == 'Form') {
$this->CreateChildForm($Input,$TestType,$InputObj);
}
}
});
} catch (\Exception $e) {
return response()->json($e->getMessage(), 441);
}
return response()->json(trans('messages.success'));
return \MessageResponse::success();
}
public function show($id)
@@ -84,39 +96,90 @@ class TestTypeController extends Controller
public function edit($id)
{
//
$TestType = TestType::with('inputs','inputs.children','inputs.children.children','inputs.options')->findOrFail($id);//?????
// $TestType = TestType::where('id',$id)->with('inputs','inputs.children','inputs.children.children','inputs.options')->whereHas('inputs' , function($query){
// $query->where('parent_id',null);
// })->get();
return response()->json(['TestType' => $TestType]);
}
public function update(Request $request, TestType $TestType)
{
$this->validate($request, [
'name' => 'required|max:100|unique:test_types,'.$TestType->id,
'unique_code' => 'required|max:100|unique:test_types,'.$TestType->id,
'info.*.name'=>'required',
'info.*.type'=>'required',
'info.*.size'=>'required',
'info.*.parent_id'=>'nullable|numeric|exists:inputs',
]);
$TestType = [
'name' => $request->name,
'unique_code' => $request->unique_code
];
$TestType->update($TestType);
$TestTypeInfo = $request->info;
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////// Update TestType /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// foreach ($TestTypeInfo as $Input) {
// if ($Input['id'] == 0) {
// $InputObj = $this->CreateInput($Input,$TestType);
// }
// else {
// // code...
// }
// $InputObj = $this->CreateInput($Input,$TestType);
//
// if ($Input->Type == 'Form') {
// $this->CreateChildForm($Input);
// }
// }
public function UpdateInput($Input,$TestType,$ParentID = null)
{
//---------------------------------------------Input
$InputData = [
'name' => $Input['name'],
'type' => $Input['type'],
'size' => $Input['size'],
'test_type_id' => $TestType->id,
'parent_id' => $ParentID
];
$InputObj = Input::where('id',$Input['id'])->update($InputData);
//---------------------------------------------Input
//---------------------------------------------Option
if ($Input['type'] == 'CheckBox' || $Input['type'] == 'Select') {
foreach ($Input['options'] as $Option) {
if ($Option['id'] == 0) {//--new
$Option = Option::create(['title' => $Option['title'] , 'input_id' => $InputObj->id]);
}
else {
$Option = Option::where('id', $Option['id'])->update(['title' => $Option['title'] ]);
}
}
}
//---------------------------------------------Option
return $InputObj;
}
public function UpdateChildForm($Input,$TestType,$InputObj)
{
if (isset($Input['children'])) {
foreach ($Input['children'] as $InputChild) {
if ($InputChild['id'] == 0) {//--newChild
$InputChildObj = $this->CreateInput($InputChild,$TestType,$InputObj->id);
if (isset($InputChild['children'])) {
$this->CreateChildForm($InputChild,$TestType,$InputChildObj);
}
}
else {
$InputChildObj = $this->UpdateInput($InputChild,$TestType,$InputObj->id);
if (isset($InputChild['children'])) {
$this->UpdateChildForm($InputChild,$TestType,$InputChildObj);
}
}
}
}
}
public function update(StoreTestTypeRequest $request, TestType $TestType)
{
$TestTypeData = [
'name' => $request['name'],
'unique_code' => $request['unique_code'],
];
$TestType->update($TestTypeData);
$TestTypeInfo = $request['info'];
foreach ($TestTypeInfo as $Input) {
if ($Input['id'] == 0) {//--new
$InputObj = $this->CreateInput($Input,$TestType);
}
else {
$InputObj = $this->UpdateInput($Input,$TestType);
}
if ($Input->Type == 'Form') {
$this->UpdateChildForm($Input,$TestType,$InputObj);
}
}
}
public function destroy($id)