Merge pull request #4185 from samuelsov/CRM-15330
[civicrm-core.git] / CRM / Event / BAO / Participant.php
index cc905d78e4ff83b852b7efc4497642f1e926847a..d324b1e9e998267969619f63e52ae6f011e44f14 100644 (file)
@@ -24,7 +24,7 @@
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
 
-*/
+ */
 
 /**
  *
@@ -40,7 +40,6 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * Static field for all the participant information that we can potentially import
    *
    * @var array
-   * @static
    */
   static $_importableFields = NULL;
 
@@ -48,7 +47,6 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * Static field for all the participant information that we can potentially export
    *
    * @var array
-   * @static
    */
   static $_exportableFields = NULL;
 
@@ -56,7 +54,6 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * Static array for valid status transitions rules
    *
    * @var array
-   * @static
    */
   static $_statusTransitionsRules = array(
     'Pending from pay later' => array('Registered', 'Cancelled'),
@@ -68,7 +65,6 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
   );
 
   /**
-   *
    */
   public function __construct() {
     parent::__construct();
@@ -84,8 +80,7 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Event_BAO_Participant object
-   * @static
+   * @return CRM_Event_BAO_Participant
    */
   public static function &add(&$params) {
 
@@ -111,10 +106,14 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
 
     // ensure that role ids are encoded as a string
     if (isset($params['role_id']) && is_array($params['role_id'])) {
+      if (in_array(key($params['role_id']), CRM_Core_DAO::acceptedSQLOperators())) {
+        $op = key($params['role_id']);
+        $params['role_id'] = $params['role_id'][$op];
+      }
       $params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']);
     }
 
-    $participantBAO = new CRM_Event_BAO_Participant;
+    $participantBAO = new CRM_Event_BAO_Participant();
     if (!empty($params['id'])) {
       $participantBAO->id = CRM_Utils_Array::value('id', $params);
       $participantBAO->find(TRUE);
@@ -166,7 +165,6 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * @param $ids
    *
    * @return CRM_Event_BAO_Participant|null the found object or null
-   * @static
    */
   public static function getValues(&$params, &$values, &$ids) {
     if (empty($params)) {
@@ -190,8 +188,7 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Event_BAO_Participant object
-   * @static
+   * @return CRM_Event_BAO_Participant
    */
   public static function create(&$params) {
 
@@ -352,10 +349,10 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
    * @param bool $returnWaitingCount
    * @param bool $considerTestParticipant
    *
-   * @return bool|int|null|string 1. false                 => If event having some empty spaces.
-   * @static
+   * @return bool|int|null|string
+   *   1. false                 => If event having some empty spaces.
    */
-  static function eventFull(
+  public static function eventFull(
     $eventId,
     $returnEmptySeats = FALSE,
     $includeWaitingList = TRUE,
@@ -496,10 +493,10 @@ SELECT  event.event_full_text,
    * @param bool $considerWaiting
    * @param bool $considerTestParticipants
    *
-   * @return array $optionsCount an array of each option id and total count
-   * @static
+   * @return array
+   *   an array of each option id and total count
    */
-  static function priceSetOptionsCount(
+  public static function priceSetOptionsCount(
     $eventId,
     $skipParticipantIds = array(),
     $considerCounted = TRUE,
@@ -574,8 +571,8 @@ INNER JOIN  civicrm_price_field field       ON ( value.price_field_id = field.id
    * @param int $eventId
    *   Event id.
    *
-   * @return int $spaces  Number of Empty Seats/null.
-   * @static
+   * @return int
+   *   $spaces  Number of Empty Seats/null.
    */
   public static function pendingToConfirmSpaces($eventId) {
     $emptySeats = 0;
@@ -623,8 +620,8 @@ GROUP BY  participant.event_id
    * @param bool $status
    * @param bool $onlyParticipant
    *
-   * @return array array of importable Fields
-   * @static
+   * @return array
+   *   array of importable Fields
    */
   public static function &importableFields($contactType = 'Individual', $status = TRUE, $onlyParticipant = FALSE) {
     if (!self::$_importableFields) {
@@ -715,11 +712,9 @@ GROUP BY  participant.event_id
       $extIdentifier = CRM_Utils_Array::value('external_identifier', $contactFields);
       if ($extIdentifier) {
         $tmpContactField['external_identifier'] = $extIdentifier;
-        $tmpContactField['external_identifier']['title'] =
-          CRM_Utils_Array::value('title', $extIdentifier) . ' (match to contact)';
+        $tmpContactField['external_identifier']['title'] = CRM_Utils_Array::value('title', $extIdentifier) . ' (match to contact)';
       }
-      $tmpFields['participant_contact_id']['title'] =
-        $tmpFields['participant_contact_id']['title'] . ' (match to contact)';
+      $tmpFields['participant_contact_id']['title'] = $tmpFields['participant_contact_id']['title'] . ' (match to contact)';
 
       $fields = array_merge($fields, $tmpContactField);
       $fields = array_merge($fields, $tmpFields);
@@ -735,8 +730,8 @@ GROUP BY  participant.event_id
   /**
    * Combine all the exportable fields from the lower levels object
    *
-   * @return array array of exportable Fields
-   * @static
+   * @return array
+   *   array of exportable Fields
    */
   public static function &exportableFields() {
     if (!self::$_exportableFields) {
@@ -747,10 +742,12 @@ GROUP BY  participant.event_id
       $fields = array();
 
       $participantFields = CRM_Event_DAO_Participant::export();
+      $eventFields = CRM_Event_DAO_Event::export();
       $noteField = array(
         'participant_note' => array(
           'title' => 'Participant Note',
           'name' => 'participant_note',
+          'type' => CRM_Utils_Type::T_STRING,
         ),
       );
 
@@ -758,6 +755,7 @@ GROUP BY  participant.event_id
         'participant_status' => array(
           'title' => 'Participant Status',
           'name' => 'participant_status',
+          'type' => CRM_Utils_Type::T_STRING,
         ),
       );
 
@@ -765,26 +763,13 @@ GROUP BY  participant.event_id
         'participant_role' => array(
           'title' => 'Participant Role',
           'name' => 'participant_role',
-        ),
-      );
-
-      //CRM-13595 add event id to participant export
-      $eventid = array(
-        'event_id' => array(
-          'title' => 'Event ID',
-          'name' => 'event_id',
-        ),
-      );
-      $eventtitle = array(
-        'event_title' => array(
-          'title' => 'Event Title',
-          'name' => 'event_title',
+          'type' => CRM_Utils_Type::T_STRING,
         ),
       );
 
       $discountFields = CRM_Core_DAO_Discount::export();
 
-      $fields = array_merge($participantFields, $participantStatus, $participantRole, $eventid, $eventtitle, $noteField, $discountFields);
+      $fields = array_merge($participantFields, $participantStatus, $participantRole, $eventFields, $noteField, $discountFields);
 
       // add custom data
       $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Participant'));
@@ -800,8 +785,8 @@ GROUP BY  participant.event_id
    * @param int $participantId
    *   Id of the participant.
    *
-   * @return array $name associated array with sort_name and event title
-   * @static
+   * @return array
+   *   associated array with sort_name and event title
    */
   public static function participantDetails($participantId) {
     $query = "
@@ -832,7 +817,6 @@ WHERE  civicrm_participant.id = {$participantId}
    *   True if we want to resolve the values in the reverse direction (value -> name).
    *
    * @return void
-   * @static
    */
   public static function resolveDefaults(&$defaults, $reverse = FALSE) {
     self::lookupValue($defaults, 'event', CRM_Event_PseudoConstant::event(), $reverse);
@@ -841,7 +825,7 @@ WHERE  civicrm_participant.id = {$participantId}
   }
 
   /**
-   * This function is used to convert associative array names to values
+   * convert associative array names to values
    * and vice-versa.
    *
    * This function is used by both the web form layer and the api. Note that
@@ -876,7 +860,6 @@ WHERE  civicrm_participant.id = {$participantId}
    *   Id of the participation to delete.
    *
    * @return void
-   * @static
    */
   public static function deleteParticipant($id) {
     CRM_Utils_Hook::pre('delete', 'Participant', $id, CRM_Core_DAO::$_nullArray);
@@ -933,14 +916,13 @@ WHERE  civicrm_participant.id = {$participantId}
   /**
    * Checks duplicate participants
    *
-   * @param array $duplicates
-   *   (reference ) an assoc array of name/value pairs.
    * @param array $input
    *   An assosiative array of name /value pairs.
-   * from other function
+   *   from other function
+   * @param array $duplicates
+   *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Contribute_BAO_Contribution object
-   * @static
+   * @return CRM_Contribute_BAO_Contribution
    */
   public static function checkDuplicate($input, &$duplicates) {
     $eventId = CRM_Utils_Array::value('event_id', $input);
@@ -985,7 +967,6 @@ WHERE  civicrm_participant.id = {$participantId}
    * @param string $eventLevel
    *   Event_leval string from db.
    *
-   * @static
    *
    * @return void
    */
@@ -1021,8 +1002,7 @@ WHERE  civicrm_participant.id = {$participantId}
    *
    * @param int $oldStatusId
    *
-   * @return array $additionalParticipantIds
-   * @static
+   * @return array
    */
   public static function getAdditionalParticipantIds($primaryParticipantId, $excludeCancel = TRUE, $oldStatusId = NULL) {
     $additionalParticipantIds = array();
@@ -1063,8 +1043,7 @@ WHERE  civicrm_participant.id = {$participantId}
    * @param bool $hasLineItems
    *   Do fetch from line items.
    *
-   * @return array $feeDetails
-   * @static
+   * @return array
    */
   public function getFeeDetails($participantIds, $hasLineItems = FALSE) {
     $feeDetails = array();
@@ -1138,8 +1117,8 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_
    * @param int $primaryParticipantID
    *   Id of primary participant record.
    *
-   * @return array $additionalParticipants $displayName => $viewUrl
-   * @static
+   * @return array
+   *   $displayName => $viewUrl
    */
   public static function getAdditionalParticipants($primaryParticipantID) {
     $additionalParticipantIDs = array();
@@ -1170,12 +1149,11 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_
    * @param int $newStatusID
    * @param bool $updatePrimaryStatus
    *
-   * @return bool|void
-   * @static
+   * @return bool|NULL
    */
   public static function updateParticipantStatus($participantID, $oldStatusID, $newStatusID = NULL, $updatePrimaryStatus = FALSE) {
     if (!$participantID || !$oldStatusID) {
-      return;
+      return NULL;
     }
 
     if (!$newStatusID) {
@@ -1208,7 +1186,6 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_
    *
    * @return void
    *
-   * @static
    */
   public static function updateStatus($participantIds, $statusId, $updateRegisterDate = FALSE) {
     if (!is_array($participantIds) || empty($participantIds) || !$statusId) {
@@ -1244,20 +1221,17 @@ UPDATE  civicrm_participant
    *   Update status id.
    * @param int $fromStatusId
    *   From status id.
-   *
-   * return  void
    * @param bool $returnResult
    * @param bool $skipCascadeRule
    *
-   * @return array
-   * @static
+   * @return array|NULL
    */
-  static function transitionParticipants(
+  public static function transitionParticipants(
     $participantIds, $toStatusId,
     $fromStatusId = NULL, $returnResult = FALSE, $skipCascadeRule = FALSE
   ) {
     if (!is_array($participantIds) || empty($participantIds) || !$toStatusId) {
-      return;
+      return NULL;
     }
 
     //thumb rule is if we triggering  primary participant need to triggered additional
@@ -1486,12 +1460,9 @@ UPDATE  civicrm_participant
    * @param string $mailType
    *   (eg 'approval', 'confirm', 'expired' ).
    *
-   * return  void
-   *
    * @return bool
-   * @static
    */
-  static function sendTransitionParticipantMail(
+  public static function sendTransitionParticipantMail(
     $participantId,
     $participantValues,
     $eventDetails,
@@ -1688,7 +1659,8 @@ UPDATE  civicrm_participant
    *
    * @param int $participantId
    *
-   * @return true if participant is primary
+   * @return bool
+   *   true if participant is primary
    */
   public static function isPrimaryParticipant($participantId) {
 
@@ -1711,7 +1683,8 @@ UPDATE  civicrm_participant
    * @param int $newStatusId
    *   New status.
    *
-   * @return true if allowed
+   * @return bool
+   *   true if allowed
    */
   public static function getValidAdditionalIds($participantId, $oldStatusId, $newStatusId) {
 
@@ -1737,8 +1710,8 @@ UPDATE  civicrm_participant
    *
    * @param int $contactID
    *
-   * @return int count of participant records
-   * @static
+   * @return int
+   *   count of participant records
    */
   public static function getContactParticipantCount($contactID) {
     $query = "SELECT count(*)
@@ -1756,8 +1729,7 @@ WHERE    civicrm_participant.contact_id = {$contactID} AND
    * @param bool $excludeCancelled
    *   Exclude cancelled additional participant.
    *
-   * @return array $participantsId
-   * @static
+   * @return array
    */
   public static function getParticipantIds($contributionId, $excludeCancelled = FALSE) {
 
@@ -1788,8 +1760,8 @@ WHERE    civicrm_participant.contact_id = {$contactID} AND
    * @param array $participantIds
    *   An array of additional participant ids.
    *
-   * @return array of Urls.
-   * @static
+   * @return array
+   *   Array of Urls.
    */
   public static function getAdditionalParticipantUrl($participantIds) {
     foreach ($participantIds as $value) {
@@ -1808,7 +1780,7 @@ WHERE    civicrm_participant.contact_id = {$contactID} AND
   }
 
   /**
-   * To create trxn entry if an event has discount.
+   * create trxn entry if an event has discount.
    *
    * @param int $eventID
    *   Event id.
@@ -1817,7 +1789,6 @@ WHERE    civicrm_participant.contact_id = {$contactID} AND
    *
    * @param $feeLevel
    *
-   * @static
    */
   public static function createDiscountTrxn($eventID, $contributionParams, $feeLevel) {
     // CRM-11124
@@ -1838,14 +1809,16 @@ WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2";
         $contributionParams['contribution']->financial_type_id, $relationTypeId);
       if (!empty($contributionParams['trxnParams']['from_financial_account_id'])) {
         $contributionParams['trxnParams']['total_amount'] = $mainAmount - $contributionParams['total_amount'];
-        $contributionParams['trxnParams']['payment_processor_id'] = $contributionParams['trxnParams']['payment_instrument_id'] =
-        $contributionParams['trxnParams']['check_number'] = $contributionParams['trxnParams']['trxn_id'] =
-        $contributionParams['trxnParams']['net_amount'] = $contributionParams['trxnParams']['fee_amount'] = NULL;
+        $contributionParams['trxnParams']['payment_processor_id'] = NULL;
+        $contributionParams['trxnParams']['payment_instrument_id'] = NULL;
+        $contributionParams['trxnParams']['check_number'] = NULL;
+        $contributionParams['trxnParams']['trxn_id'] = NULL;
+        $contributionParams['trxnParams']['net_amount'] = NULL;
+        $contributionParams['trxnParams']['fee_amount'] = NULL;
 
         CRM_Core_BAO_FinancialTrxn::create($contributionParams['trxnParams']);
       }
     }
-    return;
   }
 
   /**
@@ -1856,7 +1829,6 @@ WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2";
    * @param int $contactId
    *   Contact id.
    *
-   * @static
    */
   public static function deleteContactParticipant($contactId) {
     $participant = new CRM_Event_DAO_Participant();
@@ -2115,8 +2087,11 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
       $ftDetail = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
       // adjusted amount financial_trxn creation
       if (empty($ftDetail['trxn_id'])) {
-        $updatedContribution =
-          CRM_Contribute_BAO_Contribution::getValues(array('id' => $contributionId), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray);
+        $updatedContribution = CRM_Contribute_BAO_Contribution::getValues(
+          array('id' => $contributionId),
+          CRM_Core_DAO::$_nullArray,
+          CRM_Core_DAO::$_nullArray
+        );
         $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
         $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId);
 
@@ -2191,11 +2166,11 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
    *
    * @param string $fieldName
    * @param string $context
-   *   : @see CRM_Core_DAO::buildOptionsContext.
+   * @see CRM_Core_DAO::buildOptionsContext
    * @param array $props
-   *   whatever is known about this dao object.
+   *   whatever is known about this dao object.
    *
-   * @return Array|bool
+   * @return array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $params = array('condition' => array());
@@ -2210,4 +2185,5 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI
 
     return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
   }
+
 }