Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-28-20-20-34
[civicrm-core.git] / api / v3 / Participant.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 an associative array of name/value property values of civicrm_participant
52 *
53 * @return array apiresult
54 * {@getfields participant_create}
55 * @access public
56 */
57 function civicrm_api3_participant_create($params) {
58 //check that event id is not an template - should be done @ BAO layer
59 if (CRM_Utils_Array::value('event_id', $params)) {
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'));
63 }
64 }
65
66 $value = array();
67 _civicrm_api3_custom_format_params($params, $values, 'Participant');
68 $params = array_merge($values, $params);
69
70 $participantBAO = CRM_Event_BAO_Participant::create($params);
71
72 if(empty($params['price_set_id']) && empty($params['id']) && CRM_Utils_Array::value('fee_level', $params)){
73 _civicrm_api3_participant_createlineitem($params, $participantBAO);
74 }
75 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
76
77 return civicrm_api3_create_success($participant, $params, 'participant', 'create', $participantBAO);
78 }
79
80 /**
81 * Create a default participant line item
82 */
83 function _civicrm_api3_participant_createlineitem(&$params, $participant){
84 $sql = "
85 SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID
86 FROM civicrm_price_set_entity cpse
87 LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = {$params['event_id']} AND cpse.entity_table = 'civicrm_event'
88 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
89 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id and pfv.label = '{$params['fee_level']}'
90 where ps.id is not null
91 ";
92
93 $dao = CRM_Core_DAO::executeQuery($sql);
94 if ($dao->fetch()) {
95 $amount = CRM_Utils_Array::value('fee_amount', $params, 0);
96 $lineItemparams = array(
97 'price_field_id' => $dao->priceFieldID,
98 'price_field_value_id' => $dao->priceFieldValueID,
99 'entity_table' => 'civicrm_participant',
100 'entity_id' => $participant->id,
101 'label' => $params['fee_level'],
102 'qty' => 1,
103 'participant_count' => 0,
104 'unit_price' => $amount,
105 'line_total' => $amount,
106 'version' => 3,
107 );
108 civicrm_api('line_item', 'create', $lineItemparams);
109 }
110 }
111
112
113 /**
114 * Adjust Metadata for Create action
115 *
116 * The metadata is used for setting defaults, documentation & validation
117 * @param array $params array or parameters determined by getfields
118 */
119 function _civicrm_api3_participant_create_spec(&$params) {
120 $params['status_id']['api.default'] = "1";
121 $params['register_date']['api.default'] = "now";
122 $params['event_id']['api.required'] = 1;
123 $params['contact_id']['api.required'] = 1;
124 }
125
126 /**
127 * Retrieve a specific participant, given a set of input params
128 * If more than one matching participant exists, return an error, unless
129 * the client has requested to return the first found contact
130 *
131 * @param array $params (reference ) input parameters
132 *
133 * @return array (reference ) array of properties, if error an array with an error id and error message
134 * {@getfields participant_get}
135 * @access public
136 */
137 function civicrm_api3_participant_get($params) {
138
139 $options = _civicrm_api3_get_options_from_params($params, TRUE,'participant','get');
140 $sort = CRM_Utils_Array::value('sort', $options, NULL);
141 $offset = CRM_Utils_Array::value('offset', $options);
142 $rowCount = CRM_Utils_Array::value('limit', $options);
143 $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params);
144 $inputParams = CRM_Utils_Array::value('input_params', $options, array());
145 $returnProperties = CRM_Utils_Array::value('return', $options, NULL);
146
147 if (empty($returnProperties)) {
148 $returnProperties = CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT);
149 }
150 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
151 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
152 FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT
153 );
154 list($select, $from, $where, $having) = $query->query();
155
156 $sql = "$select $from $where $having";
157
158 if (!empty($sort)) {
159 $sql .= " ORDER BY $sort ";
160 }
161 $sql .= " LIMIT $offset, $rowCount ";
162 $dao = CRM_Core_DAO::executeQuery($sql);
163
164 $participant = array();
165 while ($dao->fetch()) {
166 $participant[$dao->participant_id] = $query->store($dao);
167 _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL);
168 }
169
170 return civicrm_api3_create_success($participant, $params, 'participant', 'get', $dao);
171 }
172
173 /**
174 * Adjust Metadata for Get action
175 *
176 * The metadata is used for setting defaults, documentation & validation
177 * @param array $params array or parameters determined by getfields
178 */
179 function _civicrm_api3_participant_get_spec(&$params) {
180 $params['participant_test']['api.default'] = 0;
181 }
182
183 /**
184 * Deletes an existing contact participant
185 *
186 * This API is used for deleting a contact participant
187 *
188 * @param array $params Array containing Id of the contact participant to be deleted
189 *
190 * {@getfields participant_delete}
191 * @access public
192 */
193 function civicrm_api3_participant_delete($params) {
194
195 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
196
197 if ($result) {
198 return civicrm_api3_create_success();
199 }
200 else {
201 throw new Exception('Error while deleting participant');
202 }
203 }
204