PageRenderTime 120ms CodeModel.GetById 62ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/bin/ParticipantProcessor.php

https://github.com/michaelmcandrew/th
PHP | 242 lines | 157 code | 32 blank | 53 comment | 33 complexity | b5ea208e4ab317153b16f89f09f13e70 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /*
  28. * This file check and updates the status of all participant records.
  29. *
  30. * EventParticipantion.php prior to running this script.
  31. */
  32. require_once '../civicrm.config.php';
  33. require_once 'CRM/Core/Config.php';
  34. class CRM_ParticipantProcessor
  35. {
  36. function __construct( )
  37. {
  38. $config = CRM_Core_Config::singleton();
  39. //this does not return on failure
  40. require_once 'CRM/Utils/System.php';
  41. require_once 'CRM/Utils/Hook.php';
  42. CRM_Utils_System::authenticateScript( true );
  43. //log the execution time of script
  44. CRM_Core_Error::debug_log_message( 'ParticipantProcessor.php' );
  45. }
  46. public function updateParticipantStatus( )
  47. {
  48. require_once 'CRM/Event/PseudoConstant.php';
  49. $participantRole = CRM_Event_PseudoConstant::participantRole( );
  50. $pendingStatuses = CRM_Event_PseudoConstant::participantStatus( null, "class = 'Pending'" );
  51. $expiredStatuses = CRM_Event_PseudoConstant::participantStatus( null, "class = 'Negative'" );
  52. $waitingStatuses = CRM_Event_PseudoConstant::participantStatus( null, "class = 'Waiting'" );
  53. //build the required status ids.
  54. $statusIds = '(' . implode( ',', array_merge( array_keys( $pendingStatuses ), array_keys( $waitingStatuses ) ) ) . ')';
  55. $participantDetails = $fullEvents = array( );
  56. $expiredParticipantCount = $waitingConfirmCount = $waitingApprovalCount = 0;
  57. //get all participant who's status in class pending and waiting
  58. $query = "SELECT * FROM civicrm_participant WHERE status_id IN {$statusIds} ORDER BY register_date";
  59. $query = "
  60. SELECT participant.id,
  61. participant.contact_id,
  62. participant.status_id,
  63. participant.register_date,
  64. participant.registered_by_id,
  65. participant.event_id,
  66. event.title as eventTitle,
  67. event.expiration_time,
  68. event.requires_approval
  69. FROM civicrm_participant participant
  70. LEFT JOIN civicrm_event event ON ( event.id = participant.event_id )
  71. WHERE participant.status_id IN {$statusIds}
  72. AND (event.end_date > now() OR event.end_date IS NULL)
  73. AND event.is_active = 1
  74. ORDER BY participant.register_date, participant.id
  75. ";
  76. $dao =& CRM_Core_DAO::executeQuery( $query );
  77. while ( $dao->fetch( ) ) {
  78. $participantDetails[$dao->id] = array( 'id' => $dao->id,
  79. 'event_id' => $dao->event_id,
  80. 'status_id' => $dao->status_id,
  81. 'contact_id' => $dao->contact_id,
  82. 'register_date' => $dao->register_date,
  83. 'registered_by_id' => $dao->registered_by_id,
  84. 'eventTitle' => $dao->eventTitle,
  85. 'expiration_time' => $dao->expiration_time,
  86. 'requires_approval'=> $dao->requires_approval
  87. );
  88. }
  89. if ( !empty( $participantDetails ) ) {
  90. //cron 1. move participant from pending to expire if needed
  91. foreach ( $participantDetails as $participantId => $values ) {
  92. //process the additional participant at the time of
  93. //primary participant, don't process separately.
  94. if ( CRM_Utils_Array::value( 'registered_by_id', $values ) ) {
  95. continue;
  96. }
  97. $expirationTime = CRM_Utils_Array::value( 'expiration_time', $values );
  98. if ( $expirationTime && array_key_exists( $values['status_id'], $pendingStatuses ) ) {
  99. //get the expiration and registration pending time.
  100. $expirationSeconds = $expirationTime * 3600;
  101. $registrationPendingSeconds = CRM_Utils_Date::unixTime( $values['register_date'] );
  102. // expired registration since registration cross allow confirmation time.
  103. if ( ( $expirationSeconds + $registrationPendingSeconds ) < time( ) ) {
  104. //lets get the transaction mechanism.
  105. require_once 'CRM/Core/Transaction.php';
  106. $transaction = new CRM_Core_Transaction( );
  107. require_once 'CRM/Event/BAO/Participant.php';
  108. $ids = array( $participantId );
  109. $expiredId = array_search( 'Expired', $expiredStatuses );
  110. $results = CRM_Event_BAO_Participant::transitionParticipants( $ids, $expiredId, $values['status_id'], true, true );
  111. $transaction->commit( );
  112. if ( !empty( $results ) ) {
  113. //diaplay updated participants
  114. if ( is_array( $results['updatedParticipantIds'] ) && !empty( $results['updatedParticipantIds'] ) ) {
  115. foreach ( $results['updatedParticipantIds'] as $processedId ) {
  116. $expiredParticipantCount += 1;
  117. echo "<br /><br />- status updated to: Expired";
  118. //mailed participants.
  119. if ( is_array( $results['mailedParticipants'] ) &&
  120. array_key_exists( $processedId, $results['mailedParticipants']) ) {
  121. echo "<br />Expiration Mail sent to: {$results['mailedParticipants'][$processedId]}";
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }//cron 1 end.
  129. //cron 2. lets move participants from waiting list to pending status
  130. foreach ( $participantDetails as $participantId => $values ) {
  131. //process the additional participant at the time of
  132. //primary participant, don't process separately.
  133. if ( CRM_Utils_Array::value( 'registered_by_id', $values ) ) {
  134. continue;
  135. }
  136. if ( array_key_exists( $values['status_id'], $waitingStatuses ) &&
  137. !array_key_exists( $values['event_id'], $fullEvents ) ) {
  138. if ( $waitingStatuses[$values['status_id']] == 'On waitlist' ) {
  139. //check the target event having space.
  140. require_once 'CRM/Event/BAO/Participant.php';
  141. $eventOpenSpaces = CRM_Event_BAO_Participant::eventFull( $values['event_id'], true, false );
  142. if ( $eventOpenSpaces && is_numeric( $eventOpenSpaces ) || ( $eventOpenSpaces === null ) ) {
  143. //get the additional participant if any.
  144. $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds( $participantId );
  145. $allIds = array( $participantId );
  146. if ( !empty( $additionalIds ) ) $allIds = array_merge( $allIds, $additionalIds );
  147. $pClause = ' participant.id IN ( '. implode( ' , ', $allIds ).' )';
  148. $requiredSpaces = CRM_Event_BAO_Event::eventTotalSeats( $values['event_id'], $pClause );
  149. //need to check as to see if event has enough speces
  150. if ( ( $requiredSpaces <= $eventOpenSpaces ) || ( $eventOpenSpaces === null ) ) {
  151. require_once 'CRM/Core/Transaction.php';
  152. $transaction = new CRM_Core_Transaction( );
  153. require_once 'CRM/Event/BAO/Participant.php';
  154. $ids = array( $participantId );
  155. $updateStatusId = array_search( 'Pending from waitlist', $pendingStatuses );
  156. //lets take a call to make pending or need approval
  157. if ( $values['requires_approval'] ) {
  158. $updateStatusId = array_search( 'Awaiting approval', $waitingStatuses );
  159. }
  160. $results = CRM_Event_BAO_Participant::transitionParticipants( $ids, $updateStatusId,
  161. $values['status_id'], true, true );
  162. //commit the transaction.
  163. $transaction->commit( );
  164. if ( !empty( $results ) ) {
  165. //diaplay updated participants
  166. if ( is_array( $results['updatedParticipantIds'] ) &&
  167. !empty( $results['updatedParticipantIds'] ) ) {
  168. foreach ( $results['updatedParticipantIds'] as $processedId ) {
  169. if ( $values['requires_approval'] ) {
  170. $waitingApprovalCount += 1;
  171. echo "<br /><br />- status updated to: Awaiting approval";
  172. echo "<br />Will send you Confirmation Mail when registration get approved.";
  173. } else {
  174. $waitingConfirmCount += 1;
  175. echo "<br /><br />- status updated to: Pending from waitlist";
  176. if ( is_array( $results['mailedParticipants'] ) &&
  177. array_key_exists( $processedId, $results['mailedParticipants']) ) {
  178. echo "<br />Confirmation Mail sent to: {$results['mailedParticipants'][$processedId]}";
  179. }
  180. }
  181. }
  182. }
  183. }
  184. } else {
  185. //target event is full.
  186. $fullEvents[$values['event_id']] = $values['eventTitle'];
  187. }
  188. } else {
  189. //target event is full.
  190. $fullEvents[$values['event_id']] = $values['eventTitle'];
  191. }
  192. }
  193. }
  194. }//cron 2 ends.
  195. }
  196. echo "<br /><br />Number of Expired registration(s) = {$expiredParticipantCount}";
  197. echo "<br />Number of registration(s) require approval = {$waitingApprovalCount}";
  198. echo "<br />Number of registration changed to Pending from waitlist = {$waitingConfirmCount}<br /><br />";
  199. if ( !empty( $fullEvents ) ) {
  200. foreach ( $fullEvents as $eventId => $title ) {
  201. echo "Full Event : {$title}<br />";
  202. }
  203. }
  204. }
  205. }
  206. $obj = new CRM_ParticipantProcessor( );
  207. echo "Updating..";
  208. $obj->updateParticipantStatus( );
  209. echo "<br />Participant records updated. (Done)";
  210. ?>