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