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