Skip to content

Commit a12e79a

Browse files
committed
Multiple information updates for nursing questions
1 parent b3222da commit a12e79a

12 files changed

Lines changed: 864 additions & 690 deletions

File tree

app/CaseStudyNote.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,36 @@ class CaseStudyNote extends Model
88
{
99
protected $guarded = [];
1010

11-
public function updateBasedOnVersion($request, $version){
12-
13-
foreach ($request->case_study_notes as $case_study_note) {
14-
if ($case_study_note['version'] === $version) {
15-
CaseStudyNote::updateOrCreate(
16-
['assignment_id' => $request->assignment_id,
17-
'type' => $case_study_note['type'],
18-
'version' => $version],
19-
['text' => $case_study_note['text']]
20-
);
11+
/**
12+
* @param $assignment
13+
* @return array
14+
*/
15+
public function getByType($assignment): array
16+
{
17+
$case_study_notes = $this->where('assignment_id', $assignment->id)
18+
->orderBy('first_application','DESC')
19+
->get();
20+
if ($case_study_notes->isNotEmpty()) {
21+
foreach ($case_study_notes as $key => $value) {
22+
$case_study_notes[$key]['expanded'] = false;
23+
}
24+
}
25+
$case_study_notes_by_type = [];
26+
$types = [];
27+
foreach ($case_study_notes as $item) {
28+
if (!in_array($item->type, $types)) {
29+
$case_study_notes_by_type[] = ['type' => $item->type, 'notes' => []];
30+
}
31+
$types[] = $item->type;
32+
}
33+
foreach ($case_study_notes as $item) {
34+
foreach ($case_study_notes_by_type as $key => $value) {
35+
if ($item->type === $value['type']) {
36+
$case_study_notes_by_type[$key]['notes'][] = $item;
37+
}
2138
}
2239
}
40+
return $case_study_notes_by_type;
2341
}
2442
/**
2543
* @param string $type

app/Http/Controllers/AssignmentQuestionSyncCaseStudyNotesController.php

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Http\Request;
1212
use Illuminate\Support\Facades\DB;
1313
use Illuminate\Support\Facades\Gate;
14+
use Illuminate\Support\Facades\Log;
1415

1516
class AssignmentQuestionSyncCaseStudyNotesController extends Controller
1617
{
@@ -21,9 +22,9 @@ class AssignmentQuestionSyncCaseStudyNotesController extends Controller
2122
* @return array
2223
* @throws Exception
2324
*/
24-
public function index(Assignment $assignment,
25-
int $order,
26-
CaseStudyNote $caseStudyNote): array
25+
public function index(Assignment $assignment,
26+
int $order,
27+
CaseStudyNote $caseStudyNote): array
2728
{
2829
$response['type'] = 'error';
2930
$authorized = Gate::inspect('view', $assignment);
@@ -32,53 +33,48 @@ public function index(Assignment $assignment,
3233
$response['message'] = $authorized->message();
3334
return $response;
3435
}
36+
$question = DB::table('assignment_question')
37+
->where('assignment_id', $assignment->id)
38+
->where('question_id', $order)
39+
->first();
40+
if ($question){
41+
$order = $question->order;
42+
} else {
43+
$response['type'] = 'success';
44+
$response['case_study_notes'] = [];
45+
return $response;
46+
}
3547

3648
try {
49+
3750
$patient_information = DB::table('patient_informations')
3851
->where('assignment_id', $assignment->id)
3952
->first();
40-
$assignment_case_study_notes = DB::table('case_study_notes')
41-
->where('assignment_id', $assignment->id)
42-
->where('version', 0)
43-
->get();
44-
$assignment_case_study_notes_by_type = [];
45-
foreach ($assignment_case_study_notes as $value) {
46-
$assignment_case_study_notes_by_type[$value->type] = $value;
47-
}
48-
49-
$updated_informations = DB::table('case_study_notes')
50-
->where('assignment_id', $assignment->id)
51-
->where('version', 1)
52-
->get();
53-
$updated_informations_by_type = [];
54-
foreach ($updated_informations as $value) {
55-
$updated_informations_by_type[$value->type] = $value;
56-
}
57-
foreach ($assignment_case_study_notes_by_type as $key => $value) {
58-
if (isset($updated_informations_by_type[$value->type])
59-
&& $order >= $updated_informations_by_type[$value->type]->first_application) {
60-
$assignment_case_study_notes_by_type[$key] = $updated_informations_by_type[$value->type];
61-
}
62-
}
53+
$case_study_notes_by_type = $caseStudyNote->getByType($assignment);
6354
$case_study_notes = [];
55+
6456
if ($patient_information) {
65-
if ($order >= $patient_information->first_application_of_updated_information) {
57+
if ($patient_information->first_application_of_updated_information && $order >= $patient_information->first_application_of_updated_information) {
6658
$patient_information->bmi = $patient_information->updated_bmi;
6759
$patient_information->weight = $patient_information->updated_weight;
6860
}
61+
6962
$case_study_notes[] = ['title' => 'Patient Information',
7063
'text' => $patient_information,
71-
'updated_information' =>$order === $patient_information->first_application_of_updated_information ];
64+
'updated_information' => $order === $patient_information->first_application_of_updated_information];
7265
}
66+
foreach ($case_study_notes_by_type as $value) {
7367

74-
foreach ($assignment_case_study_notes_by_type as $case_study_note) {
75-
$case_study_notes[] = [
76-
'title' => $caseStudyNote->formatType($case_study_note->type),
77-
'text' => $case_study_note->text,
78-
'updated_information' =>$order === $case_study_note->first_application];
79-
68+
$type = $value['type'];
69+
$case_study_notes[$type] = ['text' => null, 'title' => $caseStudyNote->formatType($type)];
70+
foreach ($value['notes'] as $notes) {
71+
if ($order >= $notes['first_application'] && !$case_study_notes[$type]['text']) {
72+
$case_study_notes[$type]['text'] = $notes->text;
73+
$case_study_notes[$type]['updated_information'] = $order > 1 && $order === $notes['first_application'];
74+
}
75+
}
8076
}
81-
$response['case_study_notes'] = $case_study_notes;
77+
$response['case_study_notes'] = array_values($case_study_notes);
8278
$response['type'] = 'success';
8379
return $response;
8480
} catch (Exception $e) {

0 commit comments

Comments
 (0)