PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/api/ui/pull/sample_report.class.php

https://github.com/patrickdemond/beartooth
PHP | 206 lines | 163 code | 14 blank | 29 comment | 20 complexity | 04bfc46a05f1bb2b145ad6351a7a5421 MD5 | raw file
  1. <?php
  2. /**
  3. * sample.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\pull;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * pull sample report
  12. *
  13. * @abstract
  14. */
  15. class sample_report extends \cenozo\ui\pull\base_report
  16. {
  17. /**
  18. * Constructor
  19. *
  20. * @author Patrick Emond <emondpd@mcmaster.ca>
  21. * @param string $subject The subject to retrieve the primary information from.
  22. * @param array $args Pull arguments.
  23. * @access public
  24. */
  25. public function __construct( $args )
  26. {
  27. parent::__construct( 'sample', $args );
  28. }
  29. /**
  30. * Builds the report.
  31. * @author Patrick Emond <emondpd@mcmaster.ca>
  32. * @access protected
  33. */
  34. protected function build()
  35. {
  36. $participant_class_name = lib::create( 'database\participant' );
  37. $event_type_class_name = lib::create( 'database\event_type' );
  38. $site_id = $this->get_argument( 'restrict_site_id' );
  39. $db_site = $site_id ? lib::create( 'database\site', $site_id ) : NULL;
  40. $db_quota = lib::create( 'database\quota', $this->get_argument( 'quota_id' ) );
  41. $this->add_title(
  42. sprintf( 'For %s (%s, %s)',
  43. is_null( $db_site ) ? 'All sites' : $db_site->name,
  44. $db_quota->gender,
  45. $db_quota->get_age_group()->to_string() ) );
  46. $participant_mod = lib::create( 'database\modifier' );
  47. $participant_mod->where( 'gender', '=', $db_quota->gender );
  48. $participant_mod->where( 'age_group_id', '=', $db_quota->age_group_id );
  49. if( !is_null( $db_site ) )
  50. $participant_mod->where( 'participant_site.site_id', '=', $db_site->id );
  51. $participant_mod->where( 'IFNULL( participant_last_consent.accept, true )', '=', true );
  52. $participant_mod->where( 'interview.qnaire_id', '=', 'qnaire.id', false );
  53. $participant_mod->where_bracket( true );
  54. $participant_mod->where( 'qnaire.rank', '=', 1 );
  55. $participant_mod->where_bracket( true, true ); // or
  56. $participant_mod->where( 'qnaire.rank', '=', 2 );
  57. $participant_mod->where( 'interview.completed', '=', 0 );
  58. $participant_mod->where_bracket( false );
  59. $participant_mod->where_bracket( false );
  60. foreach( $participant_class_name::select( $participant_mod ) as $db_participant )
  61. {
  62. // determine the date the participant was imported
  63. $import_date = 'unknown';
  64. $event_mod = lib::create( 'database\modifier' );
  65. $event_mod->where(
  66. 'event_type.name', 'IN', array( 'consent to contact signed', 'imported by rdd' ) );
  67. $event_list = $db_participant->get_event_list( $event_mod );
  68. if( 0 < count( $event_list ) )
  69. {
  70. $db_event = current( $event_list );
  71. $import_date = util::get_datetime_object( $db_event->datetime )->format( 'Y-m-d' );
  72. }
  73. // get the home and site interview records (if they exist)
  74. $db_home_interview = NULL;
  75. $db_site_interview = NULL;
  76. $interview_list = $db_participant->get_interview_list();
  77. foreach( $interview_list as $db_interview )
  78. {
  79. if( 1 == $db_interview->get_qnaire()->rank ) $db_home_interview = $db_interview;
  80. else if( 2 == $db_interview->get_qnaire()->rank ) $db_site_interview = $db_interview;
  81. }
  82. // determine the number of home and site assignment counts and complete dates
  83. $home_interview_date = 'none';
  84. $home_interviewer = 'n/a';
  85. $site_interview_date = 'none';
  86. if( is_null( $db_home_interview ) || !$db_home_interview->completed )
  87. { // home interview isn't complete, the interview date will be the last appointment
  88. $appointment_mod = lib::create( 'database\modifier' );
  89. $appointment_mod->order_desc( 'datetime' );
  90. $appointment_mod->limit( 1 );
  91. $appointment_list = $db_participant->get_appointment_list();
  92. if( 0 < count( $appointment_list ) )
  93. {
  94. $db_appointment = current( $appointment_list );
  95. $db_user = $db_appointment->get_user();
  96. $home_interview_date =
  97. util::get_datetime_object( $db_appointment->datetime )->format( 'Y-m-d' );
  98. $home_interviewer = is_null( $db_user ) ? 'unset' : $db_user->name;
  99. }
  100. }
  101. else // the home interview is complete, get it's complete time from events
  102. {
  103. $event_mod = lib::create( 'database\modifier' );
  104. $event_mod->where( 'event_type.name', '=', 'completed (Baseline Home)' );
  105. $event_list = $db_participant->get_event_list( $event_mod );
  106. if( 0 < count( $event_list ) )
  107. {
  108. $db_event = current( $event_list );
  109. $home_interview_date =
  110. util::get_datetime_object( $db_event->datetime )->format( 'Y-m-d' );
  111. $home_interviewer = '(exported)';
  112. }
  113. else // the interview is complete yet there is no event
  114. {
  115. $home_interview_date = 'unknown';
  116. $home_interviewer = 'unknown';
  117. }
  118. // if the home interview is complete then the site interview may be as well
  119. if( is_null( $db_site_interview ) || !$db_site_interview->completed )
  120. { // site interview isn't complete, the interview date will be the last appointment
  121. $appointment_mod = lib::create( 'database\modifier' );
  122. $appointment_mod->order_desc( 'datetime' );
  123. $appointment_mod->limit( 1 );
  124. $appointment_list = $db_participant->get_appointment_list( $appointment_mod );
  125. if( 0 < count( $appointment_list ) )
  126. {
  127. $db_appointment = current( $appointment_list );
  128. if( is_null( $db_appointment->address_id ) )
  129. $site_interview_date =
  130. util::get_datetime_object( $db_appointment->datetime )->format( 'Y-m-d' );
  131. }
  132. }
  133. else // the site interview is complete, get it's complete time from events
  134. {
  135. $event_mod = lib::create( 'database\modifier' );
  136. $event_mod->where( 'event_type.name', '=', 'completed (Baseline Site)' );
  137. $event_list = $db_participant->get_event_list( $event_mod );
  138. if( 0 < count( $event_list ) )
  139. {
  140. $db_event = current( $event_list );
  141. $site_interview_date =
  142. util::get_datetime_object( $db_event->datetime )->format( 'Y-m-d' );
  143. }
  144. else // the interview is complete yet there is no event
  145. {
  146. $site_interview_date = 'unknown';
  147. }
  148. }
  149. }
  150. // get the last callback
  151. $callback_mod = lib::create( 'database\modifier' );
  152. $callback_mod->order_desc( 'datetime' );
  153. $callback_mod->limit( 1 );
  154. $callback_list = $db_participant->get_callback_list();
  155. $db_callback = 0 < count( $callback_list ) ? current( $callback_list ) : NULL;
  156. $db_effective_site = $db_participant->get_effective_site();
  157. $db_state = $db_participant->get_state();
  158. $import_datetime_object =
  159. $content[] = array(
  160. $db_participant->uid,
  161. is_null( $db_effective_site ) ? 'none' : $db_participant->get_effective_site()->name,
  162. $db_participant->active ? 'yes' : 'no',
  163. is_null( $db_state ) ? 'none' : $db_state->name,
  164. $import_date,
  165. $db_participant->get_release_date()->format( 'Y-m-d' ),
  166. is_null( $db_participant->email ) ? 'no' : 'yes',
  167. is_null( $db_home_interview ) ? 0 : $db_home_interview->get_assignment_count(),
  168. $home_interview_date,
  169. $home_interviewer,
  170. is_null( $db_home_interview ) ? 'no' : ( $db_home_interview->completed ? 'yes' : 'no' ),
  171. is_null( $db_site_interview ) ? 0 : $db_site_interview->get_assignment_count(),
  172. $site_interview_date,
  173. is_null( $db_callback ) ? 'none' : $db_callback->datetime );
  174. }
  175. $header = array(
  176. 'UID',
  177. 'Site',
  178. 'Active',
  179. 'Condition',
  180. 'Imported',
  181. 'Released',
  182. 'Has Email',
  183. '# Home Assignments',
  184. 'Home Interview Date',
  185. 'Home Interviewer',
  186. 'Home Completed',
  187. '# Site Assignments',
  188. 'Site Interview Date',
  189. 'Callback' );
  190. $this->add_table( NULL, $header, $content, NULL );
  191. }
  192. }