Merge pull request #133 from pradpnayak/CRM-12061
[civicrm-core.git] / api / v3 / Participant.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30/**
31 * File for the CiviCRM APIv3 participant functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Participant
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Participant.php 30486 2010-11-02 16:12:09Z shot $
38 *
39 */
40
41/**
42 * Files required for this package
43 */
44
45/**
46 * Create an Event Participant
47 *
48 * This API is used for creating a participants in an event.
49 * Required parameters : event_id AND contact_id for new creation
50 * : participant as name/value with participantid for edit
51 *
52 * @param array $params an associative array of name/value property values of civicrm_participant
53 *
54 * @return array apiresult
55 * {@getfields participant_create}
56 * @access public
57 */
58function civicrm_api3_participant_create($params) {
59 //check that event id is not an template - should be done @ BAO layer
60 if (CRM_Utils_Array::value('event_id', $params)) {
61 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
62 if (!empty($isTemplate)) {
63 return civicrm_api3_create_error(ts('Event templates are not meant to be registered'));
64 }
65 }
66
67 $value = array();
68 _civicrm_api3_custom_format_params($params, $values, 'Participant');
69 $params = array_merge($values, $params);
70
71 $participantBAO = CRM_Event_BAO_Participant::create($params);
72
73 if(empty($params['price_set_id']) && empty($params['id']) && CRM_Utils_Array::value('fee_level', $params)){
74 _civicrm_api3_participant_createlineitem($params, $participantBAO);
75 }
76 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
77
78 return civicrm_api3_create_success($participant, $params, 'participant', 'create', $participantBAO);
79}
80
11e09c59 81/**
6a488035
TO
82 * Create a default participant line item
83 */
84function _civicrm_api3_participant_createlineitem(&$params, $participant){
85 $sql = "
86SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID
87FROM civicrm_price_set_entity cpse
88LEFT 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'
89LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
90LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id and pfv.label = '{$params['fee_level']}'
91where ps.id is not null
92";
93
94 $dao = CRM_Core_DAO::executeQuery($sql);
95 if ($dao->fetch()) {
96 $amount = CRM_Utils_Array::value('fee_amount', $params, 0);
97 $lineItemparams = array(
98 'price_field_id' => $dao->priceFieldID,
99 'price_field_value_id' => $dao->priceFieldValueID,
100 'entity_table' => 'civicrm_participant',
101 'entity_id' => $participant->id,
102 'label' => $params['fee_level'],
103 'qty' => 1,
104 'participant_count' => 0,
105 'unit_price' => $amount,
106 'line_total' => $amount,
107 'version' => 3,
108 );
109 civicrm_api('line_item', 'create', $lineItemparams);
110 }
111}
112
113
11e09c59 114/**
6a488035
TO
115 * Adjust Metadata for Create action
116 *
117 * The metadata is used for setting defaults, documentation & validation
118 * @param array $params array or parameters determined by getfields
119 */
120function _civicrm_api3_participant_create_spec(&$params) {
121 $params['status_id']['api.default'] = "1";
122 $params['register_date']['api.default'] = "now";
123 $params['event_id']['api.required'] = 1;
124 $params['contact_id']['api.required'] = 1;
125}
126
127/**
128 * Retrieve a specific participant, given a set of input params
129 * If more than one matching participant exists, return an error, unless
130 * the client has requested to return the first found contact
131 *
132 * @param array $params (reference ) input parameters
133 *
134 * @return array (reference ) array of properties, if error an array with an error id and error message
135 * {@getfields participant_get}
136 * @access public
137 */
138function civicrm_api3_participant_get($params) {
139
140 $options = _civicrm_api3_get_options_from_params($params, TRUE,'participant','get');
141 $sort = CRM_Utils_Array::value('sort', $options, NULL);
142 $offset = CRM_Utils_Array::value('offset', $options);
143 $rowCount = CRM_Utils_Array::value('limit', $options);
144 $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params);
145 $inputParams = CRM_Utils_Array::value('input_params', $options, array());
146 $returnProperties = CRM_Utils_Array::value('return', $options, NULL);
147
148 if (empty($returnProperties)) {
149 $returnProperties = CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT);
150 }
151 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
152 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
153 FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT
154 );
155 list($select, $from, $where, $having) = $query->query();
156
157 $sql = "$select $from $where $having";
158
159 if (!empty($sort)) {
160 $sql .= " ORDER BY $sort ";
161 }
162 $sql .= " LIMIT $offset, $rowCount ";
163 $dao = CRM_Core_DAO::executeQuery($sql);
164
165 $participant = array();
166 while ($dao->fetch()) {
167 $participant[$dao->participant_id] = $query->store($dao);
168 _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL);
169 }
170
171 return civicrm_api3_create_success($participant, $params, 'participant', 'get', $dao);
172}
173
11e09c59 174/**
6a488035
TO
175 * Adjust Metadata for Get action
176 *
177 * The metadata is used for setting defaults, documentation & validation
178 * @param array $params array or parameters determined by getfields
179 */
180function _civicrm_api3_participant_get_spec(&$params) {
181 $params['participant_test']['api.default'] = 0;
182}
183
184/**
185 * Deletes an existing contact participant
186 *
187 * This API is used for deleting a contact participant
188 *
189 * @param array $params Array containing Id of the contact participant to be deleted
190 *
191 * {@getfields participant_delete}
192 * @access public
193 */
194function civicrm_api3_participant_delete($params) {
195
196 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
197
198 if ($result) {
199 return civicrm_api3_create_success();
200 }
201 else {
202 throw new Exception('Error while deleting participant');
203 }
204}
205