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