PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ui/pull/appointment_list.class.php

https://github.com/patrickdemond/beartooth
PHP | 222 lines | 152 code | 22 blank | 48 comment | 22 complexity | 80d6d77916d4628c3db485680e4a35cb MD5 | raw file
  1. <?php
  2. /**
  3. * appointment_list.class.php
  4. *
  5. * @author Dean Inglis <inglisd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\pull;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * Class for appointment list pull operations.
  12. *
  13. * @abstract
  14. */
  15. class appointment_list extends \cenozo\ui\pull\base_list
  16. {
  17. /**
  18. * Constructor
  19. *
  20. * @author Dean Inglis <inglisd@mcmaster.ca>
  21. * @param array $args Pull arguments.
  22. * @access public
  23. */
  24. public function __construct( $args )
  25. {
  26. parent::__construct( 'appointment', $args );
  27. }
  28. /**
  29. * Processes arguments, preparing them for the operation.
  30. *
  31. * @author Dean Inglis <inglisd@mcmaster.ca>
  32. * @access protected
  33. */
  34. protected function prepare()
  35. {
  36. parent::prepare();
  37. $span_in_days = lib::create( 'business\setting_manager' )->get_setting(
  38. 'appointment', 'update span' );
  39. $this->start_datetime = util::get_datetime_object();
  40. $this->start_datetime->setTime( 0, 0 );
  41. $this->end_datetime = clone( $this->start_datetime );
  42. $this->end_datetime->add( new \DateInterval( sprintf( 'P%dD', $span_in_days ) ) );
  43. }
  44. /**
  45. * This method executes the operation's purpose.
  46. *
  47. * @author Dean Inglis <inglisd@mcmaster.ca>
  48. * @access protected
  49. */
  50. protected function execute()
  51. {
  52. // replace the parent class to get a specific record list
  53. $this->data = array();
  54. $onyx_instance_class_name = lib::get_class_name( 'database\onyx_instance' );
  55. $appointment_class_name = lib::get_class_name( 'database\appointment' );
  56. $interview_class_name = lib::get_class_name( 'database\interview' );
  57. $qnaire_class_name = lib::get_class_name( 'database\qnaire' );
  58. // create a list of appointments between the start and end time
  59. $db_user = lib::create( 'business\session' )->get_user();
  60. $db_onyx = $onyx_instance_class_name::get_unique_record( 'user_id' , $db_user->id );
  61. $modifier = lib::create( 'database\modifier' );
  62. $modifier->where( 'datetime', '>=', $this->start_datetime->format( 'Y-m-d H:i:s' ) );
  63. $modifier->where( 'datetime', '<', $this->end_datetime->format( 'Y-m-d H:i:s' ) );
  64. // determine whether this is a site instance of onyx or an interviewer's laptop
  65. $interview_type = is_null( $db_onyx->interviewer_user_id ) ? 'site' : 'home';
  66. if( 'site' == $interview_type )
  67. { // restrict by site
  68. $modifier->where( 'participant_site.site_id', '=', $db_onyx->get_site()->id );
  69. $modifier->where( 'appointment.address_id', '=', NULL );
  70. }
  71. else
  72. { // restrict the the onyx instance's interviewer
  73. $modifier->where( 'appointment.user_id', '=', $db_onyx->interviewer_user_id );
  74. $modifier->where( 'appointment.address_id', '!=', NULL );
  75. }
  76. $appointment_list = $appointment_class_name::select( $modifier );
  77. if( is_null( $appointment_list ) )
  78. throw lib::create( 'exception\runtime',
  79. 'Cannot get an appointment list for onyx', __METHOD__ );
  80. foreach( $appointment_list as $db_appointment )
  81. {
  82. $start_datetime_obj = util::get_datetime_object( $db_appointment->datetime );
  83. $db_participant = $db_appointment->get_participant();
  84. $db_next_of_kin = $db_participant->get_next_of_kin();
  85. $db_address = $db_participant->get_primary_address();
  86. $dob = $db_participant->date_of_birth
  87. ? util::get_datetime_object( $db_participant->date_of_birth )->format( 'Y-m-d' )
  88. : '';
  89. $data = array(
  90. 'uid' => $db_participant->uid,
  91. 'first_name' => $db_participant->first_name,
  92. 'last_name' => $db_participant->last_name,
  93. 'dob' => $dob,
  94. 'gender' => $db_participant->gender,
  95. 'datetime' => $start_datetime_obj->format( \DateTime::ISO8601 ),
  96. 'street' => is_null( $db_address ) ? 'NA' : $db_address->address1,
  97. 'city' => is_null( $db_address ) ? 'NA' : $db_address->city,
  98. 'province' => is_null( $db_address ) ? 'NA' : $db_address->get_region()->name,
  99. 'postcode' => is_null( $db_address ) ? 'NA' : $db_address->postcode,
  100. 'email' => is_null( $db_participant->email ) ? 'NA' : $db_participant->email );
  101. if( !is_null( $db_next_of_kin ) )
  102. {
  103. if( !is_null( $db_next_of_kin->first_name ) )
  104. $data['nextOfKin.firstName'] = $db_next_of_kin->first_name;
  105. if( !is_null( $db_next_of_kin->last_name ) )
  106. $data['nextOfKin.lastName'] = $db_next_of_kin->last_name;
  107. if( !is_null( $db_next_of_kin->gender ) )
  108. $data['nextOfKin.gender'] = $db_next_of_kin->gender;
  109. if( !is_null( $db_next_of_kin->phone ) )
  110. $data['nextOfKin.phone'] = $db_next_of_kin->phone;
  111. if( !is_null( $db_next_of_kin->street ) )
  112. $data['nextOfKin.street'] = $db_next_of_kin->street;
  113. if( !is_null( $db_next_of_kin->city ) )
  114. $data['nextOfKin.city'] = $db_next_of_kin->city;
  115. if( !is_null( $db_next_of_kin->province ) )
  116. $data['nextOfKin.province'] = $db_next_of_kin->province;
  117. if( !is_null( $db_next_of_kin->postal_code ) )
  118. $data['nextOfKin.postalCode'] = $db_next_of_kin->postal_code;
  119. }
  120. // include consent to draw blood if this is a site appointment
  121. if( 'site' == $interview_type )
  122. {
  123. $db_data_collection = $db_participant->get_data_collection();
  124. if( !is_null( $db_data_collection ) )
  125. {
  126. $db_data_collection = $db_participant->get_data_collection();
  127. $data['consent_to_draw_blood'] = is_null( $db_data_collection )
  128. ? NULL
  129. : $db_data_collection->draw_blood;
  130. }
  131. }
  132. $this->data[] = $data;
  133. if( !$db_appointment->completed )
  134. {
  135. // now make sure that there is an incomplete interview of the same type as the appointment
  136. $interview_mod = lib::create( 'database\modifier' );
  137. $interview_mod->where( 'qnaire.type', '=', $interview_type );
  138. $interview_mod->where( 'completed', '=', false );
  139. $interview_list = $db_participant->get_interview_list( $interview_mod );
  140. if( 0 == count( $interview_list ) )
  141. { // there is no incomplete interview for this type, so create it now
  142. // get the next qnaire of the next interview by seeing which was last completed
  143. $last_interview_mod = lib::create( 'database\modifier' );
  144. $last_interview_mod->where( 'participant_id', '=', $db_participant->id );
  145. $last_interview_mod->where( 'completed', '=', true );
  146. $last_interview_mod->order_desc( 'qnaire.rank' );
  147. $last_interview_mod->limit( 1 );
  148. $last_interview_list = $interview_class_name::select( $last_interview_mod );
  149. $rank = 1;
  150. if( 0 < count( $last_interview_list ) )
  151. {
  152. $db_last_interview = current( $last_interview_list );
  153. $rank = $db_last_interview->get_qnaire()->rank + 1;
  154. }
  155. $db_qnaire = $qnaire_class_name::get_unique_record( 'rank', $rank );
  156. // check if the qnaire exists at all
  157. if( is_null( $db_qnaire ) )
  158. {
  159. throw lib::create( 'exception\runtime',
  160. sprintf( 'Tried to provide %s appointment for participant %s but participant has '.
  161. 'completed all interviews.',
  162. $interview_type,
  163. $db_participant->uid ),
  164. __METHOD__ );
  165. }
  166. else
  167. {
  168. // now make sure that the qnaire is of the same type as the appointment
  169. if( $db_qnaire->type != $interview_type )
  170. throw lib::create( 'exception\runtime',
  171. sprintf( 'Tried to provide %s appointment for participant %s but participant\'s '.
  172. 'next interview type is %s and rank %d.',
  173. $interview_type,
  174. $db_participant->uid,
  175. $db_qnaire->type,
  176. $db_qnaire->rank ),
  177. __METHOD__ );
  178. $db_interview = lib::create( 'database\interview' );
  179. $db_interview->qnaire_id = $db_qnaire->id;
  180. $db_interview->participant_id = $db_participant->id;
  181. $db_interview->completed = false;
  182. $db_interview->save();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * The start date/time of the appointment list
  190. * @var string
  191. * @access protected
  192. */
  193. protected $start_datetime = NULL;
  194. /**
  195. * The end date/time of the appointment list
  196. * @var string
  197. * @access protected
  198. */
  199. protected $end_datetime = NULL;
  200. }