PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ui/push/phone_call_begin.class.php

https://github.com/patrickdemond/beartooth
PHP | 114 lines | 70 code | 10 blank | 34 comment | 13 complexity | 766da08c692a48f5c7217829ff1e1032 MD5 | raw file
  1. <?php
  2. /**
  3. * phone_call_begin.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: phone_call begin
  12. *
  13. * Assigns a participant to a phone call.
  14. */
  15. class phone_call_begin extends \cenozo\ui\push
  16. {
  17. /**
  18. * Constructor.
  19. * @author Patrick Emond <emondpd@mcmaster.ca>
  20. * @param array $args Push arguments
  21. * @access public
  22. */
  23. public function __construct( $args )
  24. {
  25. parent::__construct( 'phone_call', 'begin', $args );
  26. }
  27. /**
  28. * Validate the operation.
  29. *
  30. * @author Patrick Emond <emondpd@mcmaster.ca>
  31. * @throws exception\notice
  32. * @access protected
  33. */
  34. protected function validate()
  35. {
  36. parent::validate();
  37. $session = lib::create( 'business\session' );
  38. // if the user is already in a phone call then don't start another one
  39. $db_phone_call = $session->get_current_phone_call();
  40. if( !is_null( $db_phone_call ) )
  41. throw lib::create( 'exception\notice',
  42. 'You are already registered in a call, please refresh the application. '.
  43. 'If this problem persists please report it to your coordinator.',
  44. __METHOD__ );
  45. $phone_id = $this->get_argument( 'phone_id', NULL );
  46. if( !is_null( $phone_id ) )
  47. {
  48. // make sure that interviewers are calling their current assignment only
  49. $db_assignment = $session->get_current_assignment();
  50. if( 'interviewer' == $session->get_role()->name )
  51. if( is_null( $db_assignment ) )
  52. throw lib::create( 'exception\runtime',
  53. 'Interviewer tried to make call without an assignment.', __METHOD__ );
  54. // make sure the person being called is the same one who is assigned
  55. if( !is_null( $db_assignment ) )
  56. {
  57. $db_phone = lib::create( 'database\phone', $this->get_argument( 'phone_id' ) );
  58. $db_participant = $db_phone->get_person()->get_participant();
  59. if( is_null( $db_participant ) ||
  60. $db_participant->id != $db_assignment->get_interview()->participant_id )
  61. throw lib::create( 'exception\runtime',
  62. 'User tried to make call to a different participant than who is currently assigned.',
  63. __METHOD__ );
  64. }
  65. }
  66. $phone_number = $this->get_argument( 'phone_number', NULL );
  67. if( !is_null( $phone_number ) )
  68. { // make sure the phone number is valid
  69. if( !util::validate_phone_number( $phone_number, true ) )
  70. throw lib::create( 'exception\notice',
  71. sprintf( 'Cannot dial phone number "%s" since it is not a valid.', $phone_number ),
  72. __METHOD__ );
  73. }
  74. }
  75. /**
  76. * This method executes the operation's purpose.
  77. *
  78. * @author Patrick Emond <emondpd@mcmaster.ca>
  79. * @access protected
  80. */
  81. protected function execute()
  82. {
  83. parent::execute();
  84. // connect voip to phone
  85. $phone_id = $this->get_argument( 'phone_id', NULL );
  86. if( !is_null( $phone_id ) )
  87. {
  88. $db_phone = lib::create( 'database\phone', $this->get_argument( 'phone_id' ) );
  89. lib::create( 'business\voip_manager' )->call( $db_phone );
  90. $db_assignment = lib::create( 'business\session' )->get_current_assignment();
  91. if( !is_null( $db_assignment ) )
  92. { // create a record of the phone call
  93. $db_phone_call = lib::create( 'database\phone_call' );
  94. $db_phone_call->assignment_id = $db_assignment->id;
  95. $db_phone_call->phone_id = $db_phone->id;
  96. $db_phone_call->save();
  97. }
  98. }
  99. else // must be a phone number
  100. {
  101. lib::create( 'business\voip_manager' )->call( $this->get_argument( 'phone_number' ) );
  102. }
  103. }
  104. }