@@ -108,13 +108,19 @@ function finalize($quiz, $points) {
108108
109109 // get final screen and replace vars
110110 $ snapshot = '' ; // The SOAP note data to be saved in the submission record snapshot.
111- $ output = stripslashes ($ quiz ->output );
112- $ email_output = $ quiz ->set_email_output ? stripslashes ($ quiz ->email_output ) : $ output ;
113-
111+ $ patient_output = stripslashes ($ quiz ->output );
112+ $ provider_output = $ quiz ->set_email_output ? stripslashes ($ quiz ->email_output ) : $ patient_output ;
113+
114+ /** PATIENT OUTPUT **/
115+ // Find the first occurrence of the shortcode and replace with the patient notes.
116+ if (strstr ($ patient_output , '{{patient-note}} ' )) {
117+ $ patient_output = str_replace ('{{patient-note}} ' , $ this ->patient_note ($ completion_id ), $ patient_output );
118+ }
119+
114120 // Find the first occurrence of the shortcode and replace with the HTML answers table.
115- if (strstr ($ output , '{{answers-table}} ' )) {
121+ if (strstr ($ patient_output , '{{answers-table}} ' )) {
116122 $ snapshot = $ this ->answers_table ($ completion_id );
117- $ output = str_replace ('{{answers-table}} ' , $ snapshot , $ output );
123+ $ patient_output = str_replace ('{{answers-table}} ' , $ snapshot , $ patient_output );
118124 }
119125
120126 // If there is no HTML answers table, then substitute it for the soap note instead.
@@ -123,19 +129,24 @@ function finalize($quiz, $points) {
123129 }
124130
125131 // Find the first occurrence of the shortcode and replace with the SOAP note.
126- if (strstr ($ output , '{{soap-note}} ' )) {
127- $ output = str_replace ('{{soap-note}} ' , $ snapshot , $ output );
132+ if (strstr ($ patient_output , '{{soap-note}} ' )) {
133+ $ patient_output = str_replace ('{{soap-note}} ' , $ snapshot , $ patient_output );
128134 }
129135
130- $ output = str_replace ('{{result-title}} ' , @$ result ->title , $ output );
131- $ output = str_replace ('{{result-text}} ' , stripslashes (@$ result ->description ), $ output );
132- $ output = str_replace ('{{points}} ' , $ points , $ output );
133- $ output = str_replace ('{{questions}} ' , $ _POST ['total_questions ' ], $ output );
136+ $ patient_output = str_replace ('{{result-title}} ' , @$ result ->title , $ patient_output );
137+ $ patient_output = str_replace ('{{result-text}} ' , stripslashes (@$ result ->description ), $ patient_output );
138+ $ patient_output = str_replace ('{{points}} ' , $ points , $ patient_output );
139+ $ patient_output = str_replace ('{{questions}} ' , $ _POST ['total_questions ' ], $ patient_output );
140+
141+ /** PROVIDER OUTPUT **/
142+ // Find the first occurrence of the shortcode and replace with the patient notes.
143+ if (strstr ($ provider_output , '{{patient-note}} ' )) {
144+ $ provider_output = str_replace ('{{patient-note}} ' , $ this ->patient_note ($ completion_id ), $ provider_output );
145+ }
134146
135-
136147 // Find the first occurrence of the shortcode and replace with the HTML answers table.
137- if (strstr ($ email_output , '{{answers-table}} ' )) {
138- $ email_output = str_replace ('{{answers-table}} ' , $ this ->answers_table ($ completion_id ), $ email_output );
148+ if (strstr ($ provider_output , '{{answers-table}} ' )) {
149+ $ provider_output = str_replace ('{{answers-table}} ' , $ this ->answers_table ($ completion_id ), $ provider_output );
139150 }
140151
141152 // If there is no HTML answers table, then substitute it for the SOAP note instead.
@@ -144,14 +155,14 @@ function finalize($quiz, $points) {
144155 }
145156
146157 // Find the first occurrence of the shortcode and replace with the SOAP note.
147- if (strstr ($ email_output , '{{soap-note}} ' )) {
148- $ email_output = str_replace ('{{soap-note}} ' , $ this ->soap_note ($ completion_id , $ result ), $ email_output );
158+ if (strstr ($ provider_output , '{{soap-note}} ' )) {
159+ $ provider_output = str_replace ('{{soap-note}} ' , $ this ->soap_note ($ completion_id , $ result ), $ provider_output );
149160 }
150161
151- $ email_output = str_replace ('{{result-title}} ' , @$ result ->title , $ email_output );
152- $ email_output = str_replace ('{{result-text}} ' , stripslashes (@$ result ->description ), $ email_output );
153- $ email_output = str_replace ('{{points}} ' , $ points , $ email_output );
154- $ email_output = str_replace ('{{questions}} ' , $ _POST ['total_questions ' ], $ email_output );
162+ $ provider_output = str_replace ('{{result-title}} ' , @$ result ->title , $ provider_output );
163+ $ provider_output = str_replace ('{{result-text}} ' , stripslashes (@$ result ->description ), $ provider_output );
164+ $ provider_output = str_replace ('{{points}} ' , $ points , $ provider_output );
165+ $ provider_output = str_replace ('{{questions}} ' , $ _POST ['total_questions ' ], $ provider_output );
155166
156167 // Email attachment
157168 /** TODO: Fetch the email method from config. */
@@ -162,19 +173,19 @@ function finalize($quiz, $points) {
162173 // Write to file.
163174 $ file = plugin_dir_path ( __DIR__ ) . 'output_files/ ' .$ completion_id .'.html ' ;
164175 $ open = fopen ( $ file , "a " ); // Open the file for writing (a) only.
165- $ write = fputs ( $ open , $ email_output );
176+ $ write = fputs ( $ open , $ provider_output );
166177 fclose ( $ open );
167178
168179 // Send email with result in attachment.
169180 $ this ->send_emails ($ quiz , "SOAP note for diabetes triage. " , $ file ); // Sends email to either the performing USER or ADMIN.
170181 } else {
171- $ this ->send_emails ($ quiz , $ email_output , null ); // Sends email to either the performing USER or ADMIN.
182+ $ this ->send_emails ($ quiz , $ provider_output , null ); // Sends email to either the performing USER or ADMIN.
172183 }
173184
174185 $ GLOBALS ['chained_completion_id ' ] = $ completion_id ;
175186 $ GLOBALS ['chained_result_id ' ] = @$ result ->id ;
176- $ output = do_shortcode ($ output );
177- $ output = wpautop ($ output );
187+ $ patient_output = do_shortcode ($ patient_output );
188+ $ patient_output = wpautop ($ patient_output );
178189
179190 // only if the quiz is published on more than one page, store info about source url
180191 $ source_url = '' ;
@@ -195,7 +206,7 @@ function finalize($quiz, $points) {
195206 $ wpdb ->query ( $ wpdb ->prepare ("UPDATE " .CHAINED_COMPLETED ." SET
196207 quiz_id = %d, points = %f, result_id = %d, datetime = NOW(), ip = %s, user_id = %d,
197208 snapshot = %s, source_url=%s, email=%s WHERE id=%d " ,
198- $ quiz ->id , $ points , @$ result ->id , $ _SERVER ['REMOTE_ADDR ' ], $ user_id , $ email_output ,
209+ $ quiz ->id , $ points , @$ result ->id , $ _SERVER ['REMOTE_ADDR ' ], $ user_id , $ provider_output ,
199210 $ source_url , $ user_email , intval ($ _SESSION ['chained_completion_id ' ])));
200211 $ taking_id = $ _SESSION ['chained_completion_id ' ];
201212 unset($ _SESSION ['chained_completion_id ' ]);
@@ -205,17 +216,17 @@ function finalize($quiz, $points) {
205216 $ wpdb ->query ( $ wpdb ->prepare ("INSERT INTO " .CHAINED_COMPLETED ." SET
206217 quiz_id = %d, points = %f, result_id = %d, datetime = NOW(), ip = %s, user_id = %d, snapshot = %s,
207218 source_url=%s, email=%s " ,
208- $ quiz ->id , $ points , @$ result ->id , $ _SERVER ['REMOTE_ADDR ' ], $ user_id , $ email_output , $ source_url , $ user_email ));
219+ $ quiz ->id , $ points , @$ result ->id , $ _SERVER ['REMOTE_ADDR ' ], $ user_id , $ provider_output , $ source_url , $ user_email ));
209220 $ taking_id = $ wpdb ->insert_id ;
210221 }
211222
212223 // send API call for other plugins
213224 do_action ('chained_quiz_completed ' , $ taking_id );
214225
215226 // if the result needs to redirect, replace the output with the redirect URL
216- if (!empty ($ result ->redirect_url )) $ output = "[CHAINED_REDIRECT] " .$ result ->redirect_url ;
227+ if (!empty ($ result ->redirect_url )) $ patient_output = "[CHAINED_REDIRECT] " .$ result ->redirect_url ;
217228
218- return $ output ;
229+ return $ patient_output ;
219230 }
220231
221232
@@ -584,6 +595,32 @@ function soap_note($completion_id, $result) {
584595 return $ output ;
585596 }
586597
598+
599+ /**************************************************************************
600+ * FUNCTION: Builds a patient note using the user's answers and question config.
601+ **************************************************************************/
602+ function patient_note ($ completion_id ) {
603+ global $ wpdb ;
604+ $ _question = new ChainedQuizQuestion ();
605+ $ output = '' ;
606+
607+ $ answers = $ wpdb ->get_results ($ wpdb ->prepare ("SELECT tC.patient_note as patient_note
608+ FROM " .CHAINED_USER_ANSWERS ." tUA
609+ JOIN " .CHAINED_QUESTIONS ." tQ ON tQ.id = tUA.question_id
610+ JOIN " .CHAINED_CHOICES ." tC ON tC.id = tUA.answer
611+ WHERE tUA.completion_id=%d
612+ AND tC.patient_note IS NOT NULL
613+ ORDER BY tUA.ID " , $ completion_id ));
614+
615+ if (isset ($ answers )) {
616+ $ output .= '<ul> ' ;
617+ foreach ($ answers as $ answer ) {
618+ $ output .= '<li> ' . $ answer ->patient_note . '</li> ' ;
619+ }
620+ $ output .= '</ul> ' ;
621+ }
622+ return $ output ;
623+ }
587624
588625 /**************************************************************************
589626 * FUNCTION: Copy/duplicate an entire quis.
0 commit comments