1111use Illuminate \Http \Request ;
1212use Illuminate \Support \Facades \DB ;
1313use Illuminate \Support \Facades \Gate ;
14+ use Illuminate \Support \Facades \Log ;
1415
1516class 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