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