Merge pull request #4901 from colemanw/INFRA-132
[civicrm-core.git] / api / v3 / Participant.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * File for the CiviCRM APIv3 participant functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Participant
34 *
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: Participant.php 30486 2010-11-02 16:12:09Z shot $
37 *
38 */
39
40 /**
41 * Create an Event Participant
42 *
43 * This API is used for creating a participants in an event.
44 * Required parameters : event_id AND contact_id for new creation
45 * : participant as name/value with participantid for edit
46 *
47 * @param array $params
48 * An associative array of name/value property values of civicrm_participant.
49 *
50 * @return array
51 * apiresult
52 * {@getfields participant_create}
53 * @access public
54 */
55 function civicrm_api3_participant_create($params) {
56 //check that event id is not an template - should be done @ BAO layer
57 if (!empty($params['event_id'])) {
58 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
59 if (!empty($isTemplate)) {
60 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
61 }
62 }
63
64 $values = $participant = array();
65 _civicrm_api3_custom_format_params($params, $values, 'Participant');
66 $params = array_merge($values, $params);
67
68 $participantBAO = CRM_Event_BAO_Participant::create($params);
69
70 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
71 _civicrm_api3_participant_createlineitem($params, $participantBAO);
72 }
73 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
74
75 return civicrm_api3_create_success($participant, $params, 'participant', 'create', $participantBAO);
76 }
77
78 /**
79 * @todo this should be done in the BAO not the api
80 * Create a default participant line item
81 */
82 function _civicrm_api3_participant_createlineitem(&$params, $participant) {
83 // it is possible that a fee level contains information about multiple
84 // price field values.
85
86 $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
87 $params["fee_level"]);
88
89 foreach ($priceFieldValueDetails as $detail) {
90 if (preg_match('/- ([0-9]+)$/', $detail, $matches)) {
91 // it is possible that a price field value is payd for multiple times.
92 // (FIXME: if the price field value ends in minus followed by whitespace
93 // and a number, things will go wrong.)
94
95 $qty = $matches[1];
96 preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
97 $label = $matches[1];
98 }
99 else {
100 $label = $detail;
101 $qty = 1;
102 }
103
104 $sql = "
105 SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.amount AS amount
106 FROM civicrm_price_set_entity cpse
107 LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = %1 AND cpse.entity_table = 'civicrm_event'
108 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
109 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
110 where ps.id is not null and pfv.label = %2
111 ";
112
113 $qParams = array(
114 1 => array($params['event_id'], 'Integer'),
115 2 => array($label, 'String'),
116 );
117
118 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
119 if ($dao->fetch()) {
120 $lineItemParams = array(
121 'price_field_id' => $dao->priceFieldID,
122 'price_field_value_id' => $dao->priceFieldValueID,
123 'entity_table' => 'civicrm_participant',
124 'entity_id' => $participant->id,
125 'label' => $label,
126 'qty' => $qty,
127 'participant_count' => 0,
128 'unit_price' => $dao->amount,
129 'line_total' => $qty * $dao->amount,
130 );
131 civicrm_api3('line_item', 'create', $lineItemParams);
132 }
133
134 }
135 }
136
137
138 /**
139 * Adjust Metadata for Create action
140 *
141 * The metadata is used for setting defaults, documentation & validation
142 * @param array $params
143 * Array or parameters determined by getfields.
144 */
145 function _civicrm_api3_participant_create_spec(&$params) {
146 $params['status_id']['api.default'] = "1";
147 $params['register_date']['api.default'] = "now";
148 $params['event_id']['api.required'] = 1;
149 $params['contact_id']['api.required'] = 1;
150 // These are for the sake of search builder options - can be removed if that is fixed
151 $params['role_id']['api.aliases'] = array('participant_role');
152 $params['status_id']['api.aliases'] = array('participant_status');
153 }
154
155 /**
156 * Retrieve a specific participant, given a set of input params
157 * If more than one matching participant exists, return an error, unless
158 * the client has requested to return the first found contact
159 *
160 * @param array $params
161 * (reference ) input parameters.
162 *
163 * @return array
164 * (reference ) array of properties, if error an array with an error id and error message
165 * {@getfields participant_get}
166 * @access public
167 */
168 function civicrm_api3_participant_get($params) {
169 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
170 $entity = 'participant';
171
172 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity);
173
174 $participant = array();
175 while ($dao->fetch()) {
176 $participant[$dao->participant_id] = $query->store($dao);
177 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
178 _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL);
179 }
180
181 return civicrm_api3_create_success($participant, $params, 'participant', 'get', $dao);
182 }
183
184 /**
185 * Adjust Metadata for Get action
186 *
187 * The metadata is used for setting defaults, documentation & validation
188 * @param array $params
189 * Array or parameters determined by getfields.
190 */
191 function _civicrm_api3_participant_get_spec(&$params) {
192 $params['participant_test']['api.default'] = 0;
193 $params['participant_test']['title'] = 'Get Test Participants';
194 }
195
196 /**
197 * Deletes an existing contact participant
198 *
199 * This API is used for deleting a contact participant
200 *
201 * @param array $params
202 * Array containing Id of the contact participant to be deleted.
203 *
204 * {@getfields participant_delete}
205 * @throws Exception
206 * @return array
207 * @access public
208 */
209 function civicrm_api3_participant_delete($params) {
210
211 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
212
213 if ($result) {
214 return civicrm_api3_create_success();
215 }
216 else {
217 throw new Exception('Error while deleting participant');
218 }
219 }