Commit | Line | Data |
---|---|---|
4aef704e | 1 | <?php |
4aef704e | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
4aef704e | 5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2014 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * APIv3 functions for registering/processing mailing ab testing events. | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_MailingAB | |
34 | * @copyright CiviCRM LLC (c) 2004-2014 | |
35 | * $Id$ | |
36 | * | |
37 | */ | |
38 | ||
4aef704e | 39 | /** |
40 | * Handle a create mailing ab testing | |
41 | * | |
42 | * @param array $params | |
4aef704e | 43 | * |
a6c01b45 | 44 | * @return array |
72b3a70c | 45 | * API Success Array |
4aef704e | 46 | */ |
fd843187 | 47 | function civicrm_api3_mailing_a_b_create($params) { |
4aef704e | 48 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
49 | } | |
50 | ||
51 | /** | |
52 | * Handle a delete event. | |
53 | * | |
54 | * @param array $params | |
4aef704e | 55 | * |
a6c01b45 | 56 | * @return array |
72b3a70c | 57 | * API Success Array |
4aef704e | 58 | */ |
fd843187 | 59 | function civicrm_api3_mailing_a_b_delete($params) { |
4aef704e | 60 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
61 | } | |
62 | ||
63 | /** | |
64 | * Handle a get event. | |
65 | * | |
66 | * @param array $params | |
67 | * @return array | |
68 | */ | |
fd843187 | 69 | function civicrm_api3_mailing_a_b_get($params) { |
4aef704e | 70 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
71 | } | |
72 | ||
7811a84b | 73 | /** |
768c558c | 74 | * Adjust Metadata for submit action |
7811a84b | 75 | * |
76 | * The metadata is used for setting defaults, documentation & validation | |
cf470720 TO |
77 | * @param array $params |
78 | * Array or parameters determined by getfields. | |
7811a84b | 79 | */ |
768c558c TO |
80 | function _civicrm_api3_mailing_a_b_submit_spec(&$params) { |
81 | $mailingFields = CRM_Mailing_DAO_Mailing::fields(); | |
82 | $mailingAbFields = CRM_Mailing_DAO_MailingAB::fields(); | |
83 | $spec['id'] = $mailingAbFields['id']; | |
84 | $spec['status'] = $mailingAbFields['status']; | |
85 | $spec['scheduled_date'] = $mailingFields['scheduled_date']; | |
86 | $spec['approval_date'] = $mailingFields['approval_date']; | |
87 | $spec['approval_status_id'] = $mailingFields['approval_status_id']; | |
88 | $spec['approval_note'] = $mailingFields['approval_note']; | |
89 | // Note: we'll pass through approval_* fields to the underlying mailing, but they may be ignored | |
90 | // if the user doesn't have suitable permission. If separate approvals are required, they must be provided | |
91 | // outside the A/B Test UI. | |
7811a84b | 92 | } |
93 | ||
94 | /** | |
95 | * Send A/B mail to A/B recipients respectively | |
96 | * | |
97 | * @param array $params | |
98 | * @return array | |
768c558c | 99 | * @throws API_Exception |
7811a84b | 100 | */ |
768c558c TO |
101 | function civicrm_api3_mailing_a_b_submit($params) { |
102 | civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status')); | |
103 | ||
104 | if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) { | |
105 | throw new API_Exception("Missing parameter scheduled_date and/or approval_date"); | |
106 | } | |
7811a84b | 107 | |
768c558c TO |
108 | $dao = new CRM_Mailing_DAO_MailingAB(); |
109 | $dao->id = $params['id']; | |
110 | if (!$dao->find(TRUE)) { | |
111 | throw new API_Exception("Failed to locate A/B test by ID"); | |
7811a84b | 112 | } |
768c558c TO |
113 | if (empty($dao->mailing_id_a) || empty($dao->mailing_id_b) || empty($dao->mailing_id_c)) { |
114 | throw new API_Exception("Missing mailing IDs for A/B test"); | |
7811a84b | 115 | } |
116 | ||
768c558c TO |
117 | $submitParams = CRM_Utils_Array::subset($params, array( |
118 | 'scheduled_date', | |
119 | 'approval_date', | |
120 | 'approval_note', | |
121 | 'approval_status_id', | |
122 | )); | |
123 | ||
124 | switch ($params['status']) { | |
125 | case 'Testing': | |
126 | if (!empty($dao->status) && $dao->status != 'Draft') { | |
127 | throw new API_Exception("Cannot transition to state 'Testing'"); | |
128 | } | |
129 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
130 | 'id' => $dao->mailing_id_a, | |
131 | '_skip_evil_bao_auto_recipients_' => 0, | |
132 | )); | |
133 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
134 | 'id' => $dao->mailing_id_b, | |
135 | '_skip_evil_bao_auto_recipients_' => 1, | |
136 | )); | |
137 | CRM_Mailing_BAO_MailingAB::distributeRecipients($dao); | |
138 | break; | |
7811a84b | 139 | |
768c558c TO |
140 | case 'Final': |
141 | if ($dao->status != 'Testing') { | |
142 | throw new API_Exception("Cannot transition to state 'Final'"); | |
143 | } | |
144 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
145 | 'id' => $dao->mailing_id_c, | |
146 | '_skip_evil_bao_auto_recipients_' => 1, | |
147 | )); | |
148 | break; | |
149 | ||
150 | default: | |
151 | throw new API_Exception("Unrecognized submission status"); | |
7811a84b | 152 | } |
153 | ||
768c558c TO |
154 | return civicrm_api3('MailingAB', 'create', array( |
155 | 'id' => $dao->id, | |
156 | 'status' => $params['status'], | |
157 | 'options' => array( | |
158 | 'reload' => 1, | |
159 | ), | |
160 | )); | |
467cd00c | 161 | } |
162 | ||
163 | /** | |
164 | * Adjust Metadata for graph_stats action | |
165 | * | |
166 | * The metadata is used for setting defaults, documentation & validation | |
cf470720 TO |
167 | * @param array $params |
168 | * Array or parameters determined by getfields. | |
467cd00c | 169 | */ |
170 | function _civicrm_api3_mailing_a_b_graph_stats_spec(&$params) { | |
360aaa75 TO |
171 | $params['criteria']['title'] = 'Criteria'; |
172 | $params['criteria']['default'] = 'Open'; | |
173 | // mailing_ab_winner_criteria | |
174 | $params['target_date']['title'] = 'Target Date'; | |
175 | $params['target_date']['type'] = CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME; | |
bd6658bd | 176 | $params['split_count']['title'] = 'Split Count'; |
202ebbab | 177 | $params['split_count']['api.default'] = 6; |
bd6658bd | 178 | $params['split_count_select']['title'] = 'Split Count Select'; |
202ebbab | 179 | $params['split_count_select']['api.required'] = 1; |
360aaa75 | 180 | $params['target_url']['title'] = 'Target URL'; |
202ebbab | 181 | } |
467cd00c | 182 | |
183 | /** | |
184 | * Send graph detail for A/B tests mail | |
185 | * | |
186 | * @param array $params | |
187 | * @return array | |
360aaa75 | 188 | * @throws API_Exception |
467cd00c | 189 | */ |
190 | function civicrm_api3_mailing_a_b_graph_stats($params) { | |
202ebbab | 191 | civicrm_api3_verify_mandatory($params, |
192 | 'CRM_Mailing_DAO_MailingAB', | |
193 | array('id'), | |
194 | FALSE | |
195 | ); | |
467cd00c | 196 | |
360aaa75 TO |
197 | $defaults = array( |
198 | 'criteria' => 'Open', | |
199 | 'target_date' => CRM_Utils_Time::getTime('YmdHis'), | |
200 | 'split_count' => 6, | |
201 | 'split_count_select' => 1, | |
202 | ); | |
203 | $params = array_merge($defaults, $params); | |
202ebbab | 204 | |
360aaa75 | 205 | $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id'])); |
202ebbab | 206 | $graphStats = array(); |
467cd00c | 207 | $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b'); |
208 | ||
202ebbab | 209 | foreach ($ABFormat as $name => $column) { |
360aaa75 TO |
210 | switch (strtolower($params['criteria'])) { |
211 | case 'open': | |
768c558c TO |
212 | $result = CRM_Mailing_Event_BAO_Opened::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC"); |
213 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); | |
360aaa75 TO |
214 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
215 | $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); | |
202ebbab | 216 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
217 | $toDate = date('YmdHis', $toDate); | |
218 | $graphStats[$name] = array( | |
219 | $params['split_count_select'] => array( | |
220 | 'count' => CRM_Mailing_Event_BAO_Opened::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate), | |
21dfd5f5 TO |
221 | 'time' => CRM_Utils_Date::customFormat($toDate), |
222 | ), | |
202ebbab | 223 | ); |
224 | break; | |
ea100cb5 | 225 | |
360aaa75 | 226 | case 'total unique clicks': |
202ebbab | 227 | $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC"); |
228 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); | |
360aaa75 TO |
229 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
230 | $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); | |
202ebbab | 231 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
232 | $toDate = date('YmdHis', $toDate); | |
233 | $graphStats[$name] = array( | |
234 | $params['split_count_select'] => array( | |
235 | 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate), | |
21dfd5f5 TO |
236 | 'time' => CRM_Utils_Date::customFormat($toDate), |
237 | ), | |
202ebbab | 238 | ); |
239 | break; | |
ea100cb5 | 240 | |
360aaa75 TO |
241 | case 'total clicks on a particular link': |
242 | if (empty($params['target_url'])) { | |
243 | throw new API_Exception("Provide url to get stats result for total clicks on a particular link"); | |
202ebbab | 244 | } |
360aaa75 TO |
245 | // FIXME: doesn't make sense to get url_id mailing_id_(a|b) while getting start date in mailing_id_a |
246 | $url_id = CRM_Mailing_BAO_TrackableURL::getTrackerURLId($mailingAB[$column], $params['target_url']); | |
202ebbab | 247 | $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"); |
248 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); | |
360aaa75 TO |
249 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
250 | $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); | |
202ebbab | 251 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
252 | $toDate = CRM_Utils_Date::processDate($toDate); | |
253 | $graphStats[$name] = array( | |
254 | $params['split_count_select'] => array( | |
255 | 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, $url_id, $toDate), | |
21dfd5f5 TO |
256 | 'time' => CRM_Utils_Date::customFormat($toDate), |
257 | ), | |
202ebbab | 258 | ); |
259 | break; | |
260 | } | |
261 | } | |
262 | ||
263 | return civicrm_api3_create_success($graphStats); | |
b0f9e1df | 264 | } |