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