PageRenderTime 69ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/api/ui/push/onyx_participants.class.php

https://github.com/patrickdemond/beartooth
PHP | 308 lines | 241 code | 32 blank | 35 comment | 68 complexity | f95c344298e7a81bfb997f7c3a7c913f MD5 | raw file
  1. <?php
  2. /**
  3. * onyx_participants.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\push;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * push: onyx participants
  12. *
  13. * Allows Onyx to update participant and interview details
  14. * NOTE: this class breaks the non-plural words naming convension in order to play
  15. * nicely with Onyx
  16. */
  17. class onyx_participants extends \cenozo\ui\push
  18. {
  19. /**
  20. * Constructor.
  21. * @author Patrick Emond <emondpd@mcmaster.ca>
  22. * @param array $args Push arguments
  23. * @access public
  24. */
  25. public function __construct( $args )
  26. {
  27. parent::__construct( 'onyx', 'participants', $args );
  28. }
  29. /**
  30. * This method executes the operation's purpose.
  31. *
  32. * @author Patrick Emond <emondpd@mcmaster.ca>
  33. * @access protected
  34. */
  35. protected function execute()
  36. {
  37. parent::execute();
  38. $participant_class_name = lib::create( 'database\participant' );
  39. $interview_class_name = lib::create( 'database\interview' );
  40. $qnaire_class_name = lib::create( 'database\qnaire' );
  41. $event_type_class_name = lib::create( 'database\event_type' );
  42. // get the body of the request
  43. $body = http_get_request_body();
  44. $data = util::json_decode( $body );
  45. if( !is_object( $data ) )
  46. throw lib::create( 'exception\runtime',
  47. 'Unable to decode request body, received: '.print_r( $body, true ), __METHOD__ );
  48. // loop through the participants array
  49. foreach( $data->Participants as $participant_list )
  50. {
  51. foreach( get_object_vars( $participant_list ) as $uid => $participant_data )
  52. {
  53. $object_vars = get_object_vars( $participant_data );
  54. $db_participant = $participant_class_name::get_unique_record( 'uid', $uid );
  55. if( is_null( $db_participant ) )
  56. throw lib::create( 'exception\runtime',
  57. sprintf( 'Participant UID "%s" does not exist.', $uid ), __METHOD__ );
  58. $db_next_of_kin = $db_participant->get_next_of_kin();
  59. if( is_null( $db_next_of_kin ) )
  60. {
  61. $db_next_of_kin = lib::create( 'database\next_of_kin' );
  62. $db_next_of_kin->participant_id = $db_participant->id;
  63. }
  64. $db_data_collection = $db_participant->get_data_collection();
  65. if( is_null( $db_data_collection ) )
  66. {
  67. $db_data_collection = lib::create( 'database\data_collection' );
  68. $db_data_collection->participant_id = $db_participant->id;
  69. }
  70. $participant_changed = false;
  71. $next_of_kin_changed = false;
  72. $data_collection_changed = false;
  73. // process fields which we want to update
  74. $method = 'Admin.Interview.status';
  75. if( !array_key_exists( $method, $object_vars ) )
  76. throw lib::create( 'exception\argument',
  77. $method, NULL, __METHOD__ );
  78. $interview_status = strtolower( $participant_data->$method );
  79. $method = 'Admin.ApplicationConfiguration.siteCode';
  80. if( !array_key_exists( $method, $object_vars ) )
  81. throw lib::create( 'exception\argument',
  82. $method, NULL, __METHOD__ );
  83. if( preg_match( '/dcs|site/i', $participant_data->$method ) ) $interview_type = 'site';
  84. else if( preg_match( '/home/i', $participant_data->$method ) ) $interview_type = 'home';
  85. else $interview_type = false;
  86. $method = 'Admin.Participant.firstName';
  87. if( array_key_exists( $method, $object_vars ) )
  88. {
  89. $value = $participant_data->$method;
  90. if( 0 != strcasecmp( $value, $db_participant->first_name ) )
  91. {
  92. $db_participant->first_name = $value;
  93. $participant_changed = true;
  94. }
  95. }
  96. $method = 'Admin.Participant.lastName';
  97. if( array_key_exists( $method, $object_vars ) )
  98. {
  99. $value = $participant_data->$method;
  100. if( 0 != strcasecmp( $value, $db_participant->last_name ) )
  101. {
  102. $db_participant->last_name = $value;
  103. $participant_changed = true;
  104. }
  105. }
  106. $method = 'Admin.Participant.consentToDrawBlood';
  107. if( array_key_exists( $method, $object_vars ) )
  108. {
  109. $value = $participant_data->$method;
  110. if( is_string( $value ) ) $value = preg_match( '/y|yes|true|1/i', $value ) ? 1 : 0;
  111. else $value = $value ? 1 : 0;
  112. if( is_null( $db_data_collection->draw_blood ) ||
  113. $value != $db_data_collection->draw_blood )
  114. {
  115. $db_data_collection->draw_blood = $value;
  116. $data_collection_changed = true;
  117. }
  118. }
  119. $method = 'Admin.Participant.gender';
  120. if( array_key_exists( $method, $object_vars ) )
  121. {
  122. $value =
  123. 0 == strcasecmp( 'f', substr( $participant_data->$method, 0, 1 ) )
  124. ? 'female' : 'male';
  125. if( $value != $db_participant->gender )
  126. {
  127. $db_participant->gender = $value;
  128. $participant_changed = true;
  129. }
  130. }
  131. $method = 'Admin.Participant.birthDate';
  132. if( array_key_exists( $method, $object_vars ) )
  133. {
  134. $value = util::get_datetime_object( $participant_data->$method )->format( 'Y-m-d' );
  135. if( $value != $db_participant->date_of_birth )
  136. {
  137. $db_participant->date_of_birth = $value;
  138. $participant_changed = true;
  139. }
  140. }
  141. $method = 'Admin.Participant.nextOfKin.firstName';
  142. if( array_key_exists( $method, $object_vars ) )
  143. {
  144. $value = $participant_data->$method;
  145. if( 0 != strcasecmp( $value, $db_next_of_kin->first_name ) )
  146. {
  147. $db_next_of_kin->first_name = $value;
  148. $next_of_kin_changed = true;
  149. }
  150. }
  151. $method = 'Admin.Participant.nextOfKin.lastName';
  152. if( array_key_exists( $method, $object_vars ) )
  153. {
  154. $value = $participant_data->$method;
  155. if( 0 != strcasecmp( $value, $db_next_of_kin->last_name ) )
  156. {
  157. $db_next_of_kin->last_name = $value;
  158. $next_of_kin_changed = true;
  159. }
  160. }
  161. $method = 'Admin.Participant.nextOfKin.gender';
  162. if( array_key_exists( $method, $object_vars ) )
  163. {
  164. $value = $participant_data->$method;
  165. if( 0 != strcasecmp( $value, $db_next_of_kin->gender ) )
  166. {
  167. $db_next_of_kin->gender = $value;
  168. $next_of_kin_changed = true;
  169. }
  170. }
  171. $method = 'Admin.Participant.nextOfKin.phone';
  172. if( array_key_exists( $method, $object_vars ) )
  173. {
  174. $value = $participant_data->$method;
  175. if( 0 != strcasecmp( $value, $db_next_of_kin->phone ) )
  176. {
  177. $db_next_of_kin->phone = $value;
  178. $next_of_kin_changed = true;
  179. }
  180. }
  181. $method = 'Admin.Participant.nextOfKin.street';
  182. if( array_key_exists( $method, $object_vars ) )
  183. {
  184. $value = $participant_data->$method;
  185. if( 0 != strcasecmp( $value, $db_next_of_kin->street ) )
  186. {
  187. $db_next_of_kin->street = $value;
  188. $next_of_kin_changed = true;
  189. }
  190. }
  191. $method = 'Admin.Participant.nextOfKin.city';
  192. if( array_key_exists( $method, $object_vars ) )
  193. {
  194. $value = $participant_data->$method;
  195. if( 0 != strcasecmp( $value, $db_next_of_kin->city ) )
  196. {
  197. $db_next_of_kin->city = $value;
  198. $next_of_kin_changed = true;
  199. }
  200. }
  201. $method = 'Admin.Participant.nextOfKin.province';
  202. if( array_key_exists( $method, $object_vars ) )
  203. {
  204. $value = $participant_data->$method;
  205. if( 0 != strcasecmp( $value, $db_next_of_kin->province ) )
  206. {
  207. $db_next_of_kin->province = $value;
  208. $next_of_kin_changed = true;
  209. }
  210. }
  211. $method = 'Admin.Participant.nextOfKin.postalCode';
  212. if( array_key_exists( $method, $object_vars ) )
  213. {
  214. $value = $participant_data->$method;
  215. if( 0 != strcasecmp( $value, $db_next_of_kin->postal_code ) )
  216. {
  217. $db_next_of_kin->postal_code = $value;
  218. $next_of_kin_changed = true;
  219. }
  220. }
  221. // now update the database
  222. if( $participant_changed ) $db_participant->save();
  223. if( $next_of_kin_changed ) $db_next_of_kin->save();
  224. if( $data_collection_changed ) $db_data_collection->save();
  225. // complete all appointments in the past
  226. $appointment_mod = lib::create( 'database\modifier' );
  227. $appointment_mod->where( 'completed', '=', false );
  228. if( 'home' == $interview_type ) $appointment_mod->where( 'address_id', '!=', NULL );
  229. else if( 'site' == $interview_type ) $appointment_mod->where( 'address_id', '=', NULL );
  230. foreach( $db_participant->get_appointment_list( $appointment_mod ) as $db_appointment )
  231. {
  232. $db_appointment->completed = true;
  233. $db_appointment->save();
  234. }
  235. if( 'completed' == $interview_status )
  236. {
  237. // get the most recent interview of the appropriate type
  238. $interview_mod = lib::create( 'database\modifier' );
  239. $interview_mod->where( 'participant_id', '=', $db_participant->id );
  240. if( $interview_type ) $interview_mod->where( 'qnaire.type', '=', $interview_type );
  241. $interview_mod->order_desc( 'qnaire.rank' );
  242. $interview_list = $interview_class_name::select( $interview_mod );
  243. // make sure the interview exists
  244. if( 0 == count( $interview_list ) )
  245. throw lib::create( 'exception\runtime',
  246. sprintf( 'Trying to export %s interview for participant %s but the '.
  247. 'interview doesn\'t exist.',
  248. $interview_type,
  249. $db_participant->uid ),
  250. __METHOD__ );
  251. // mark the interview as completed
  252. $db_interview = current( $interview_list );
  253. $db_interview->completed = true;
  254. $db_interview->save();
  255. // record the event (if one exists)
  256. $event_type_name = sprintf( 'completed (%s)', $db_interview->get_qnaire()->name );
  257. $db_event_type = $event_type_class_name::get_unique_record( 'name', $event_type_name );
  258. if( !is_null( $db_event_type ) )
  259. {
  260. // make sure the event doesn't already exist
  261. $event_mod = lib::create( 'database\modifier' );
  262. $event_mod->where( 'event_type_id', '=', $db_event_type->id );
  263. if( 0 == $db_participant->get_event_count( $event_mod ) )
  264. {
  265. $db_event = lib::create( 'database\event' );
  266. $db_event->participant_id = $db_participant->id;
  267. $db_event->event_type_id = $db_event_type->id;
  268. $db_event->datetime = util::get_datetime_object()->format( 'Y-m-d H:i:s' );
  269. $db_event->save();
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }