4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * File for the CiviCRM APIv3 participant functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_Participant
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: Participant.php 30486 2010-11-02 16:12:09Z shot $
41 * Files required for this package
45 * Create an Event Participant
47 * This API is used for creating a participants in an event.
48 * Required parameters : event_id AND contact_id for new creation
49 * : participant as name/value with participantid for edit
51 * @param array $params an associative array of name/value property values of civicrm_participant
53 * @return array apiresult
54 * {@getfields participant_create}
57 function civicrm_api3_participant_create($params) {
58 //check that event id is not an template - should be done @ BAO layer
59 if (!empty($params['event_id'])) {
60 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
61 if (!empty($isTemplate)) {
62 return civicrm_api3_create_error(ts('Event templates are not meant to be registered'));
67 _civicrm_api3_custom_format_params($params, $values, 'Participant');
68 $params = array_merge($values, $params);
70 $participantBAO = CRM_Event_BAO_Participant
::create($params);
72 if(empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])){
73 _civicrm_api3_participant_createlineitem($params, $participantBAO);
75 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id
]);
77 return civicrm_api3_create_success($participant, $params, 'participant', 'create', $participantBAO);
81 * Create a default participant line item
83 function _civicrm_api3_participant_createlineitem(&$params, $participant){
84 // it is possible that a fee level contains information about multiple
85 // price field values.
87 $priceFieldValueDetails = CRM_Utils_Array
::explodePadded(
88 $params["fee_level"]);
90 foreach($priceFieldValueDetails as $detail) {
91 if (preg_match('/- ([0-9]+)$/', $detail, $matches)) {
92 // it is possible that a price field value is payd for multiple times.
93 // (FIXME: if the price field value ends in minus followed by whitespace
94 // and a number, things will go wrong.)
97 preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
106 SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.amount AS amount
107 FROM civicrm_price_set_entity cpse
108 LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = %1 AND cpse.entity_table = 'civicrm_event'
109 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
110 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
111 where ps.id is not null and pfv.label = %2
115 1 => array($params['event_id'], 'Integer'),
116 2 => array($label, 'String'),
119 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
121 $lineItemparams = array(
122 'price_field_id' => $dao->priceFieldID
,
123 'price_field_value_id' => $dao->priceFieldValueID
,
124 'entity_table' => 'civicrm_participant',
125 'entity_id' => $participant->id
,
128 'participant_count' => 0,
129 'unit_price' => $dao->amount
,
130 'line_total' => $qty*$dao->amount
,
133 civicrm_api('line_item', 'create', $lineItemparams);
141 * Adjust Metadata for Create action
143 * The metadata is used for setting defaults, documentation & validation
144 * @param array $params array or parameters determined by getfields
146 function _civicrm_api3_participant_create_spec(&$params) {
147 $params['status_id']['api.default'] = "1";
148 $params['register_date']['api.default'] = "now";
149 $params['event_id']['api.required'] = 1;
150 $params['contact_id']['api.required'] = 1;
151 // These are for the sake of search builder options - can be removed if that is fixed
152 $params['role_id']['api.aliases'] = array('participant_role');
153 $params['status_id']['api.aliases'] = array('participant_status');
157 * Retrieve a specific participant, given a set of input params
158 * If more than one matching participant exists, return an error, unless
159 * the client has requested to return the first found contact
161 * @param array $params (reference ) input parameters
163 * @return array (reference ) array of properties, if error an array with an error id and error message
164 * {@getfields participant_get}
167 function civicrm_api3_participant_get($params) {
168 $mode = CRM_Contact_BAO_Query
::MODE_EVENT
;
169 $entity = 'participant';
171 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity);
173 $participant = array();
174 while ($dao->fetch()) {
175 $participant[$dao->participant_id
] = $query->store($dao);
176 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
177 _civicrm_api3_custom_data_get($participant[$dao->participant_id
], 'Participant', $dao->participant_id
, NULL);
180 return civicrm_api3_create_success($participant, $params, 'participant', 'get', $dao);
184 * Adjust Metadata for Get action
186 * The metadata is used for setting defaults, documentation & validation
187 * @param array $params array or parameters determined by getfields
189 function _civicrm_api3_participant_get_spec(&$params) {
190 $params['participant_test']['api.default'] = 0;
194 * Deletes an existing contact participant
196 * This API is used for deleting a contact participant
198 * @param array $params Array containing Id of the contact participant to be deleted
200 * {@getfields participant_delete}
205 function civicrm_api3_participant_delete($params) {
207 $result = CRM_Event_BAO_Participant
::deleteParticipant($params['id']);
210 return civicrm_api3_create_success();
213 throw new Exception('Error while deleting participant');