PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ui/widget/self_assignment.class.php

https://github.com/patrickdemond/beartooth
PHP | 208 lines | 151 code | 24 blank | 33 comment | 23 complexity | abef9066892bba6ced321a96067d5a33 MD5 | raw file
  1. <?php
  2. /**
  3. * self_assignment.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 self assignment
  12. */
  13. class self_assignment extends \cenozo\ui\widget
  14. {
  15. /**
  16. * Constructor
  17. *
  18. * Defines all variables which need to be set for the associated template.
  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( 'self', 'assignment', $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. $this->set_heading( 'Current Assignment' );
  38. }
  39. /**
  40. * Sets up the operation with any pre-execution instructions that may be necessary.
  41. *
  42. * @author Patrick Emond <emondpd@mcmaster.ca>
  43. * @access protected
  44. */
  45. protected function setup()
  46. {
  47. parent::setup();
  48. $callback_class_name = lib::get_class_name( 'database\callback' );
  49. $phone_call_class_name = lib::get_class_name( 'database\phone_call' );
  50. $operation_class_name = lib::get_class_name( 'database\operation' );
  51. $session = lib::create( 'business\session' );
  52. $db_user = $session->get_user();
  53. $db_role = $session->get_role();
  54. $db_site = $session->get_site();
  55. // see if this user has an open assignment
  56. $db_current_assignment = $session->get_current_assignment();
  57. $db_current_phone_call = $session->get_current_phone_call();
  58. if( is_null( $db_current_assignment ) )
  59. throw lib::create( 'exception\notice', 'No active assignment.', __METHOD__ );
  60. // fill out the participant's details
  61. $db_interview = $db_current_assignment->get_interview();
  62. $db_participant = $db_interview->get_participant();
  63. $language = 'none';
  64. if( 'en' == $db_participant->language ) $language = 'english';
  65. else if( 'fr' == $db_participant->language ) $language = 'french';
  66. $db_last_consent = $db_participant->get_last_consent();
  67. $consent = is_null( $db_last_consent ) ? 'none' : $db_last_consent->to_string();
  68. $previous_call_list = array();
  69. $db_last_assignment = $db_participant->get_last_finished_assignment();
  70. if( !is_null( $db_last_assignment ) )
  71. {
  72. foreach( $db_last_assignment->get_phone_call_list() as $db_phone_call )
  73. {
  74. $db_phone = $db_phone_call->get_phone();
  75. $previous_call_list[] = sprintf( 'Called phone #%d (%s): %s',
  76. $db_phone->rank,
  77. $db_phone->type,
  78. $db_phone_call->status ? $db_phone_call->status : 'unknown' );
  79. }
  80. }
  81. $modifier = lib::create( 'database\modifier' );
  82. $modifier->where( 'active', '=', true );
  83. $modifier->order( 'rank' );
  84. $db_phone_list = $db_participant->get_phone_list( $modifier );
  85. $modifier = lib::create( 'database\modifier' );
  86. $modifier->where( 'end_datetime', '!=', NULL );
  87. $current_calls = $db_current_assignment->get_phone_call_count( $modifier );
  88. $on_call = !is_null( $db_current_phone_call );
  89. $phone_list = array();
  90. foreach( $db_phone_list as $db_phone )
  91. $phone_list[$db_phone->id] =
  92. sprintf( '%d. %s (%s)', $db_phone->rank, $db_phone->type, $db_phone->number );
  93. $this->set_variable( 'phone_list', $phone_list );
  94. $this->set_variable( 'status_list', $phone_call_class_name::get_enum_values( 'status' ) );
  95. if( 0 == $current_calls && !$on_call && $db_interview->completed )
  96. {
  97. log::crit(
  98. sprintf( 'User %s has been assigned participant %d who\'s interview is complete '.
  99. 'but the user has not made any calls.',
  100. $db_user->name,
  101. $db_participant->id ) );
  102. }
  103. $this->set_variable( 'assignment_id', $db_current_assignment->id );
  104. $this->set_variable( 'participant_id', $db_participant->id );
  105. $this->set_variable( 'interview_id', $db_interview->id );
  106. $this->set_variable( 'participant_note_count', $db_participant->get_note_count() );
  107. $this->set_variable( 'participant_name',
  108. sprintf( $db_participant->first_name.' '.$db_participant->last_name ) );
  109. $this->set_variable( 'participant_uid', $db_participant->uid );
  110. $this->set_variable( 'participant_language', $language );
  111. $this->set_variable(
  112. 'participant_consent', is_null( $db_last_consent ) ? 'none' : $db_last_consent->to_string() );
  113. $this->set_variable(
  114. 'withdrawing', !is_null( $db_last_consent ) && false == $db_last_consent->accept );
  115. $this->set_variable(
  116. 'allow_withdraw', !is_null( $db_interview->get_qnaire()->withdraw_sid ) );
  117. // get the callback associated with this assignment, if any
  118. $modifier = lib::create( 'database\modifier' );
  119. $modifier->where( 'assignment_id', '=', $db_current_assignment->id );
  120. $callback_list = $callback_class_name::select( $modifier );
  121. $db_callback = 0 == count( $callback_list ) ? NULL : $callback_list[0];
  122. if( !is_null( $db_callback ) )
  123. {
  124. $this->set_variable( 'callback',
  125. util::get_formatted_time( $db_callback->datetime, false ) );
  126. if( !is_null( $db_callback->phone_id ) )
  127. {
  128. $db_phone = lib::create( 'database\phone', $db_callback->phone_id );
  129. $this->set_variable( 'phone_id', $db_callback->phone_id );
  130. $this->set_variable( 'phone_at',
  131. sprintf( '%d. %s (%s)', $db_phone->rank, $db_phone->type, $db_phone->number ) );
  132. }
  133. else
  134. {
  135. $this->set_variable( 'phone_id', false );
  136. $this->set_variable( 'phone_at', false );
  137. }
  138. }
  139. else
  140. {
  141. $this->set_variable( 'callback', false );
  142. $this->set_variable( 'phone_id', false );
  143. $this->set_variable( 'phone_at', false );
  144. }
  145. if( !is_null( $db_last_assignment ) )
  146. {
  147. $this->set_variable( 'previous_assignment_id', $db_last_assignment->id );
  148. $this->set_variable( 'previous_assignment_date',
  149. util::get_formatted_date( $db_last_assignment->start_datetime ) );
  150. $this->set_variable( 'previous_assignment_time',
  151. util::get_formatted_time( $db_last_assignment->start_datetime ) );
  152. }
  153. $this->set_variable( 'previous_call_list', $previous_call_list );
  154. $this->set_variable( 'interview_completed', $db_interview->completed );
  155. $this->set_variable( 'allow_call', $session->get_allow_call() );
  156. $this->set_variable( 'on_call', $on_call );
  157. if( !is_null( $db_current_phone_call ) )
  158. {
  159. $note = $db_current_phone_call->get_phone()->note;
  160. $this->set_variable( 'phone_note', is_null( $note ) ? false : $note );
  161. }
  162. else $this->set_variable( 'phone_note', false );
  163. $allow_secondary = false;
  164. $phone_mod = lib::create( 'database\modifier' );
  165. $phone_mod->where( 'active', '=', true );
  166. if( 0 == $db_participant->get_phone_count( $phone_mod ) )
  167. {
  168. $allow_secondary = true;
  169. }
  170. else
  171. {
  172. $max_failed_calls =
  173. lib::create( 'business\setting_manager' )->get_setting( 'calling', 'max failed calls' );
  174. if( $max_failed_calls <= $db_interview->get_failed_call_count() )
  175. {
  176. $db_operation =
  177. $operation_class_name::get_operation( 'widget', 'participant', 'secondary' );
  178. if( lib::create( 'business\session' )->is_allowed( $db_operation ) )
  179. $allow_secondary = true;
  180. }
  181. }
  182. $this->set_variable( 'allow_secondary', $allow_secondary );
  183. }
  184. }