b6a1bfbfcdf0883c31ae0f9748eec3cda9a28040
[civicrm-core.git] / api / v3 / MailingAB.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 *
31 * APIv3 functions for registering/processing mailing ab testing events.
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_MailingAB
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * $Id$
37 *
38 */
39
40 /**
41 * Handle a create mailing ab testing
42 *
43 * @param array $params
44 * @param array $ids
45 *
46 * @return array
47 * API Success Array
48 */
49 function civicrm_api3_mailing_a_b_create($params) {
50 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
51 }
52
53 /**
54 * Handle a delete event.
55 *
56 * @param array $params
57 * @param array $ids
58 *
59 * @return array
60 * API Success Array
61 */
62 function civicrm_api3_mailing_a_b_delete($params) {
63 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
64 }
65
66 /**
67 * Handle a get event.
68 *
69 * @param array $params
70 * @return array
71 */
72 function civicrm_api3_mailing_a_b_get($params) {
73 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
74 }
75
76 /**
77 * Adjust Metadata for submit action
78 *
79 * The metadata is used for setting defaults, documentation & validation
80 * @param array $params
81 * Array or parameters determined by getfields.
82 */
83 function _civicrm_api3_mailing_a_b_submit_spec(&$params) {
84 $mailingFields = CRM_Mailing_DAO_Mailing::fields();
85 $mailingAbFields = CRM_Mailing_DAO_MailingAB::fields();
86 $spec['id'] = $mailingAbFields['id'];
87 $spec['status'] = $mailingAbFields['status'];
88 $spec['scheduled_date'] = $mailingFields['scheduled_date'];
89 $spec['approval_date'] = $mailingFields['approval_date'];
90 $spec['approval_status_id'] = $mailingFields['approval_status_id'];
91 $spec['approval_note'] = $mailingFields['approval_note'];
92 // Note: we'll pass through approval_* fields to the underlying mailing, but they may be ignored
93 // if the user doesn't have suitable permission. If separate approvals are required, they must be provided
94 // outside the A/B Test UI.
95 }
96
97 /**
98 * Send A/B mail to A/B recipients respectively
99 *
100 * @param array $params
101 * @return array
102 * @throws API_Exception
103 */
104 function civicrm_api3_mailing_a_b_submit($params) {
105 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status'));
106
107 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
108 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
109 }
110
111 $dao = new CRM_Mailing_DAO_MailingAB();
112 $dao->id = $params['id'];
113 if (!$dao->find(TRUE)) {
114 throw new API_Exception("Failed to locate A/B test by ID");
115 }
116 if (empty($dao->mailing_id_a) || empty($dao->mailing_id_b) || empty($dao->mailing_id_c)) {
117 throw new API_Exception("Missing mailing IDs for A/B test");
118 }
119
120 $submitParams = CRM_Utils_Array::subset($params, array(
121 'scheduled_date',
122 'approval_date',
123 'approval_note',
124 'approval_status_id',
125 ));
126
127 switch ($params['status']) {
128 case 'Testing':
129 if (!empty($dao->status) && $dao->status != 'Draft') {
130 throw new API_Exception("Cannot transition to state 'Testing'");
131 }
132 civicrm_api3('Mailing', 'submit', $submitParams + array(
133 'id' => $dao->mailing_id_a,
134 '_skip_evil_bao_auto_recipients_' => 0,
135 ));
136 civicrm_api3('Mailing', 'submit', $submitParams + array(
137 'id' => $dao->mailing_id_b,
138 '_skip_evil_bao_auto_recipients_' => 1,
139 ));
140 CRM_Mailing_BAO_MailingAB::distributeRecipients($dao);
141 break;
142
143 case 'Final':
144 if ($dao->status != 'Testing') {
145 throw new API_Exception("Cannot transition to state 'Final'");
146 }
147 civicrm_api3('Mailing', 'submit', $submitParams + array(
148 'id' => $dao->mailing_id_c,
149 '_skip_evil_bao_auto_recipients_' => 1,
150 ));
151 break;
152
153 default:
154 throw new API_Exception("Unrecognized submission status");
155 }
156
157 return civicrm_api3('MailingAB', 'create', array(
158 'id' => $dao->id,
159 'status' => $params['status'],
160 'options' => array(
161 'reload' => 1,
162 ),
163 ));
164 }
165
166 /**
167 * Adjust Metadata for graph_stats action
168 *
169 * The metadata is used for setting defaults, documentation & validation
170 * @param array $params
171 * Array or parameters determined by getfields.
172 */
173 function _civicrm_api3_mailing_a_b_graph_stats_spec(&$params) {
174 $params['criteria']['title'] = 'Criteria';
175 $params['criteria']['default'] = 'Open';
176 // mailing_ab_winner_criteria
177 $params['target_date']['title'] = 'Target Date';
178 $params['target_date']['type'] = CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME;
179 $params['split_count']['title'] = 'Split Count';
180 $params['split_count']['api.default'] = 6;
181 $params['split_count_select']['title'] = 'Split Count Select';
182 $params['split_count_select']['api.required'] = 1;
183 $params['target_url']['title'] = 'Target URL';
184 }
185
186 /**
187 * Send graph detail for A/B tests mail
188 *
189 * @param array $params
190 * @return array
191 * @throws API_Exception
192 */
193 function civicrm_api3_mailing_a_b_graph_stats($params) {
194 civicrm_api3_verify_mandatory($params,
195 'CRM_Mailing_DAO_MailingAB',
196 array('id'),
197 FALSE
198 );
199
200 $defaults = array(
201 'criteria' => 'Open',
202 'target_date' => CRM_Utils_Time::getTime('YmdHis'),
203 'split_count' => 6,
204 'split_count_select' => 1,
205 );
206 $params = array_merge($defaults, $params);
207
208 $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id']));
209 $graphStats = array();
210 $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b');
211
212 foreach ($ABFormat as $name => $column) {
213 switch (strtolower($params['criteria'])) {
214 case 'open':
215 $result = CRM_Mailing_Event_BAO_Opened::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC");
216 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
217 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
218 $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
219 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
220 $toDate = date('YmdHis', $toDate);
221 $graphStats[$name] = array(
222 $params['split_count_select'] => array(
223 'count' => CRM_Mailing_Event_BAO_Opened::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate),
224 'time' => CRM_Utils_Date::customFormat($toDate),
225 ),
226 );
227 break;
228
229 case 'total unique clicks':
230 $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC");
231 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
232 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
233 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
234 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
235 $toDate = date('YmdHis', $toDate);
236 $graphStats[$name] = array(
237 $params['split_count_select'] => array(
238 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate),
239 'time' => CRM_Utils_Date::customFormat($toDate),
240 ),
241 );
242 break;
243
244 case 'total clicks on a particular link':
245 if (empty($params['target_url'])) {
246 throw new API_Exception("Provide url to get stats result for total clicks on a particular link");
247 }
248 // FIXME: doesn't make sense to get url_id mailing_id_(a|b) while getting start date in mailing_id_a
249 $url_id = CRM_Mailing_BAO_TrackableURL::getTrackerURLId($mailingAB[$column], $params['target_url']);
250 $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, FALSE, $url_id, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC");
251 $startDate = CRM_Utils_Date::processDate($result[0]['date']);
252 $targetDate = CRM_Utils_Date::processDate($params['target_date']);
253 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
254 $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']);
255 $toDate = CRM_Utils_Date::processDate($toDate);
256 $graphStats[$name] = array(
257 $params['split_count_select'] => array(
258 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, $url_id, $toDate),
259 'time' => CRM_Utils_Date::customFormat($toDate),
260 ),
261 );
262 break;
263 }
264 }
265
266 return civicrm_api3_create_success($graphStats);
267 }