PageRenderTime 77ms CodeModel.GetById 26ms RepoModel.GetById 9ms app.codeStats 0ms

/api/ui/widget/participant_list.class.php

https://github.com/patrickdemond/beartooth
PHP | 282 lines | 179 code | 30 blank | 73 comment | 39 complexity | b07bb5a05b9733013c5e2f82fad913f8 MD5 | raw file
  1. <?php
  2. /**
  3. * participant_list.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\widget;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * widget participant list
  12. */
  13. class participant_list extends \cenozo\ui\widget\site_restricted_list
  14. {
  15. /**
  16. * Constructor
  17. *
  18. * Defines all variables required by the participant list.
  19. * @author Patrick Emond <emondpd@mcmaster.ca>
  20. * @param array $args An associative array of arguments to be processed by the widget
  21. * @access public
  22. */
  23. public function __construct( $args )
  24. {
  25. parent::__construct( 'participant', $args );
  26. }
  27. /**
  28. * Processes arguments, preparing them for the operation.
  29. *
  30. * @author Patrick Emond <emondpd@mcmaster.ca>
  31. * @throws exception\notice
  32. * @access protected
  33. */
  34. protected function prepare()
  35. {
  36. parent::prepare();
  37. $session = lib::create( 'business\session' );
  38. // determine if the parent is an assignment select widget
  39. if( !is_null( $this->parent ) )
  40. {
  41. if( 'home_assignment_select' == $this->parent->get_class_name() )
  42. $this->assignment_type = 'home';
  43. else if( 'site_assignment_select' == $this->parent->get_class_name() )
  44. $this->assignment_type = 'site';
  45. }
  46. $this->set_variable( 'assignment_type', $this->assignment_type );
  47. $this->add_column( 'uid', 'string', 'UID', true );
  48. $this->add_column( 'first_name', 'string', 'First', true );
  49. $this->add_column( 'last_name', 'string', 'Last', true );
  50. if( is_null( $this->assignment_type ) )
  51. {
  52. $this->add_column( 'active', 'boolean', 'Active', true );
  53. $this->add_column( 'source.name', 'string', 'Source', true );
  54. if( 1 != $session->get_service()->get_cohort_count() )
  55. $this->add_column( 'cohort.name', 'string', 'Cohort', true );
  56. $this->add_column( 'site', 'string', 'Site', false );
  57. }
  58. else if( 'home' == $this->assignment_type )
  59. {
  60. $this->add_column( 'address.address1', 'string', 'Address', true );
  61. $this->add_column( 'address.city', 'string', 'City', true );
  62. $this->add_column( 'address.postcode', 'string', 'Postcode', true );
  63. }
  64. else // site assignment
  65. {
  66. // show the date of when the home interview was completed
  67. $this->add_column( 'home_interview', 'date', 'Home Interview Completed', false );
  68. }
  69. $this->extended_site_selection = true;
  70. if( $this->allow_restrict_state )
  71. {
  72. $restrict_state_id = $this->get_argument( 'restrict_state_id', '' );
  73. if( $restrict_state_id )
  74. $this->set_heading(
  75. sprintf( '%s, restricted to %s',
  76. $this->get_heading(),
  77. lib::create( 'database\state', $restrict_state_id )->name ) );
  78. }
  79. }
  80. /**
  81. * Sets up the operation with any pre-execution instructions that may be necessary.
  82. *
  83. * @author Patrick Emond <emondpd@mcmaster.ca>
  84. * @access protected
  85. */
  86. protected function setup()
  87. {
  88. parent::setup();
  89. $state_class_name = lib::get_class_name( 'database\state' );
  90. $operation_class_name = lib::get_class_name( 'database\operation' );
  91. $session = lib::create( 'business\session' );
  92. foreach( $this->get_record_list() as $record )
  93. {
  94. $db_source = $record->get_source();
  95. $db_address = $record->get_first_address();
  96. $db_site = $record->get_effective_site();
  97. $columns = array(
  98. 'uid' => $record->uid ? $record->uid : '(none)',
  99. 'first_name' => $record->first_name,
  100. 'last_name' => $record->last_name,
  101. // note count isn't a column, it's used for the note button
  102. 'note_count' => $record->get_note_count() );
  103. if( is_null( $this->assignment_type ) )
  104. {
  105. $columns['active'] = $record->active;
  106. $columns['source.name'] = is_null( $db_source ) ? '(none)' : $db_source->name;
  107. if( 1 != $session->get_service()->get_cohort_count() )
  108. $columns['cohort.name'] = $record->get_cohort()->name;
  109. $columns['site'] = $db_site ? $db_site->name : '(none)';
  110. }
  111. else if( 'home' == $this->assignment_type )
  112. {
  113. $columns['address.address1'] =
  114. is_null( $db_address ) ? '(none)' : $db_address->address1;
  115. $columns['address.city'] =
  116. is_null( $db_address ) ? '(none)' : $db_address->city;
  117. $columns['address.postcode'] =
  118. is_null( $db_address ) ? '(none)' : $db_address->postcode;
  119. }
  120. else // site assignment
  121. {
  122. $date = NULL;
  123. // get the last completed in-home appointment
  124. $appointment_mod = lib::create( 'database\modifier' );
  125. $appointment_mod->where( 'completed', '=', true );
  126. $appointment_mod->where( 'address_id', '!=', NULL );
  127. $appointment_mod->order_desc( 'datetime' );
  128. $appointment_mod->limit( 1 );
  129. $appointment_list = $record->get_appointment_list( $appointment_mod );
  130. if( 0 < count( $appointment_list ) )
  131. {
  132. $db_appointment = current( $appointment_list );
  133. $date = $db_appointment->datetime;
  134. }
  135. $columns['home_interview'] = $date;
  136. }
  137. $this->add_row( $record->id, $columns );
  138. }
  139. if( $this->allow_restrict_state )
  140. {
  141. $state_mod = lib::create( 'database\modifier' );
  142. $state_mod->order( 'rank' );
  143. $state_list = array();
  144. foreach( $state_class_name::select( $state_mod ) as $db_state )
  145. $state_list[$db_state->id] = $db_state->name;
  146. $this->set_variable( 'state_list', $state_list );
  147. $this->set_variable( 'restrict_state_id', $this->get_argument( 'restrict_state_id', '' ) );
  148. }
  149. // include the participant site reassign and search actions if the widget isn't parented
  150. if( is_null( $this->parent ) )
  151. {
  152. $db_operation =
  153. $operation_class_name::get_operation( 'widget', 'participant', 'site_reassign' );
  154. if( $session->is_allowed( $db_operation ) )
  155. $this->add_action( 'reassign', 'Site Reassign', $db_operation,
  156. 'Change the preferred site of multiple participants at once' );
  157. $db_operation =
  158. $operation_class_name::get_operation( 'widget', 'participant', 'search' );
  159. if( $session->is_allowed( $db_operation ) )
  160. $this->add_action( 'search', 'Search', $db_operation,
  161. 'Search for participants based on partial information' );
  162. }
  163. }
  164. /**
  165. * Overrides the parent class method to restrict participant list based on user's role
  166. *
  167. * @author Patrick Emond <emondpd@mcmaster.ca>
  168. * @param database\modifier $modifier Modifications to the list.
  169. * @return int
  170. * @access protected
  171. */
  172. public function determine_record_count( $modifier = NULL )
  173. {
  174. $session = lib::create( 'business\session' );
  175. if( $this->allow_restrict_state )
  176. {
  177. $restrict_state_id = $this->get_argument( 'restrict_state_id', '' );
  178. if( $restrict_state_id )
  179. {
  180. if( is_null( $modifier ) ) $modifier = lib::create( 'database\modifier' );
  181. $modifier->where( 'state_id', '=', $restrict_state_id );
  182. }
  183. }
  184. if( 'interviewer' == $session->get_role()->name )
  185. { // restrict interview lists to those they have unfinished appointments with
  186. if( is_null( $modifier ) ) $modifier = lib::create( 'database\modifier' );
  187. $modifier->where( 'appointment.completed', '=', false );
  188. $modifier->where( 'appointment.user_id', '=', $session->get_user()->id );
  189. }
  190. return parent::determine_record_count( $modifier );
  191. }
  192. /**
  193. * Overrides the parent class method to restrict participant list based on user's role
  194. *
  195. * @author Patrick Emond <emondpd@mcmaster.ca>
  196. * @param database\modifier $modifier Modifications to the list.
  197. * @return array( record )
  198. * @access protected
  199. */
  200. public function determine_record_list( $modifier = NULL )
  201. {
  202. $session = lib::create( 'business\session' );
  203. if( $this->allow_restrict_state )
  204. {
  205. $restrict_state_id = $this->get_argument( 'restrict_state_id', '' );
  206. if( $restrict_state_id )
  207. {
  208. if( is_null( $modifier ) ) $modifier = lib::create( 'database\modifier' );
  209. $modifier->where( 'state_id', '=', $restrict_state_id );
  210. }
  211. }
  212. if( 'interviewer' == $session->get_role()->name )
  213. { // restrict interview lists to those they have unfinished appointments with
  214. if( is_null( $modifier ) ) $modifier = lib::create( 'database\modifier' );
  215. $modifier->where( 'appointment.completed', '=', false );
  216. $modifier->where( 'appointment.user_id', '=', $session->get_user()->id );
  217. }
  218. return parent::determine_record_list( $modifier );
  219. }
  220. /**
  221. * Get whether to include a drop down to restrict the list by state
  222. * @author Patrick Emond <emondpd@mcmaster.ca>
  223. * @return boolean
  224. * @access public
  225. */
  226. public function get_allow_restrict_state()
  227. {
  228. return $this->allow_restrict_state;
  229. }
  230. /**
  231. * Set whether to include a drop down to restrict the list by state
  232. * @author Patrick Emond <emondpd@mcmaster.ca>
  233. * @param boolean $enable
  234. * @access public
  235. */
  236. public function set_allow_restrict_state( $enable )
  237. {
  238. $this->allow_restrict_state = $enable;
  239. }
  240. /**
  241. * Whether to include a drop down to restrict the list by state
  242. * @var boolean
  243. * @access protected
  244. */
  245. protected $allow_restrict_state = true;
  246. /**
  247. * The type of assignment select, or null if the list is not being used to select an assignment
  248. * @var string assignment_type
  249. * @access @protected
  250. */
  251. protected $assignment_type = NULL;
  252. }