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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
4aef704e | 27 | |
28 | /** | |
29 | * | |
30 | * APIv3 functions for registering/processing mailing ab testing events. | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
4aef704e | 33 | */ |
34 | ||
4aef704e | 35 | /** |
1747ab99 | 36 | * Handle a create mailing ab testing. |
4aef704e | 37 | * |
38 | * @param array $params | |
4aef704e | 39 | * |
a6c01b45 | 40 | * @return array |
72b3a70c | 41 | * API Success Array |
4aef704e | 42 | */ |
fd843187 | 43 | function civicrm_api3_mailing_a_b_create($params) { |
4aef704e | 44 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
45 | } | |
46 | ||
47 | /** | |
48 | * Handle a delete event. | |
49 | * | |
50 | * @param array $params | |
4aef704e | 51 | * |
a6c01b45 | 52 | * @return array |
72b3a70c | 53 | * API Success Array |
4aef704e | 54 | */ |
fd843187 | 55 | function civicrm_api3_mailing_a_b_delete($params) { |
4aef704e | 56 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
57 | } | |
58 | ||
59 | /** | |
60 | * Handle a get event. | |
61 | * | |
62 | * @param array $params | |
dc64d047 | 63 | * |
4aef704e | 64 | * @return array |
65 | */ | |
fd843187 | 66 | function civicrm_api3_mailing_a_b_get($params) { |
4aef704e | 67 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
68 | } | |
69 | ||
7811a84b | 70 | /** |
1747ab99 | 71 | * Adjust Metadata for submit action. |
7811a84b | 72 | * |
0aa0303c EM |
73 | * The metadata is used for setting defaults, documentation & validation. |
74 | * | |
cf470720 | 75 | * @param array $params |
b081365f | 76 | * Array of parameters determined by getfields. |
7811a84b | 77 | */ |
768c558c TO |
78 | function _civicrm_api3_mailing_a_b_submit_spec(&$params) { |
79 | $mailingFields = CRM_Mailing_DAO_Mailing::fields(); | |
80 | $mailingAbFields = CRM_Mailing_DAO_MailingAB::fields(); | |
81 | $spec['id'] = $mailingAbFields['id']; | |
82 | $spec['status'] = $mailingAbFields['status']; | |
83 | $spec['scheduled_date'] = $mailingFields['scheduled_date']; | |
84 | $spec['approval_date'] = $mailingFields['approval_date']; | |
85 | $spec['approval_status_id'] = $mailingFields['approval_status_id']; | |
86 | $spec['approval_note'] = $mailingFields['approval_note']; | |
87 | // Note: we'll pass through approval_* fields to the underlying mailing, but they may be ignored | |
88 | // if the user doesn't have suitable permission. If separate approvals are required, they must be provided | |
89 | // outside the A/B Test UI. | |
7811a84b | 90 | } |
91 | ||
92 | /** | |
1747ab99 | 93 | * Send A/B mail to A/B recipients respectively. |
7811a84b | 94 | * |
95 | * @param array $params | |
1747ab99 | 96 | * |
7811a84b | 97 | * @return array |
768c558c | 98 | * @throws API_Exception |
7811a84b | 99 | */ |
768c558c TO |
100 | function civicrm_api3_mailing_a_b_submit($params) { |
101 | civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status')); | |
102 | ||
103 | if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) { | |
104 | throw new API_Exception("Missing parameter scheduled_date and/or approval_date"); | |
105 | } | |
7811a84b | 106 | |
768c558c TO |
107 | $dao = new CRM_Mailing_DAO_MailingAB(); |
108 | $dao->id = $params['id']; | |
109 | if (!$dao->find(TRUE)) { | |
110 | throw new API_Exception("Failed to locate A/B test by ID"); | |
7811a84b | 111 | } |
768c558c TO |
112 | if (empty($dao->mailing_id_a) || empty($dao->mailing_id_b) || empty($dao->mailing_id_c)) { |
113 | throw new API_Exception("Missing mailing IDs for A/B test"); | |
7811a84b | 114 | } |
115 | ||
768c558c TO |
116 | $submitParams = CRM_Utils_Array::subset($params, array( |
117 | 'scheduled_date', | |
118 | 'approval_date', | |
119 | 'approval_note', | |
120 | 'approval_status_id', | |
121 | )); | |
122 | ||
123 | switch ($params['status']) { | |
124 | case 'Testing': | |
125 | if (!empty($dao->status) && $dao->status != 'Draft') { | |
126 | throw new API_Exception("Cannot transition to state 'Testing'"); | |
127 | } | |
128 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
129 | 'id' => $dao->mailing_id_a, | |
130 | '_skip_evil_bao_auto_recipients_' => 0, | |
131 | )); | |
132 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
133 | 'id' => $dao->mailing_id_b, | |
134 | '_skip_evil_bao_auto_recipients_' => 1, | |
135 | )); | |
136 | CRM_Mailing_BAO_MailingAB::distributeRecipients($dao); | |
137 | break; | |
7811a84b | 138 | |
768c558c TO |
139 | case 'Final': |
140 | if ($dao->status != 'Testing') { | |
141 | throw new API_Exception("Cannot transition to state 'Final'"); | |
142 | } | |
143 | civicrm_api3('Mailing', 'submit', $submitParams + array( | |
144 | 'id' => $dao->mailing_id_c, | |
145 | '_skip_evil_bao_auto_recipients_' => 1, | |
146 | )); | |
147 | break; | |
148 | ||
149 | default: | |
150 | throw new API_Exception("Unrecognized submission status"); | |
7811a84b | 151 | } |
152 | ||
768c558c TO |
153 | return civicrm_api3('MailingAB', 'create', array( |
154 | 'id' => $dao->id, | |
155 | 'status' => $params['status'], | |
156 | 'options' => array( | |
157 | 'reload' => 1, | |
158 | ), | |
159 | )); | |
467cd00c | 160 | } |
161 | ||
162 | /** | |
1747ab99 | 163 | * Adjust Metadata for graph_stats action. |
467cd00c | 164 | * |
0aa0303c EM |
165 | * The metadata is used for setting defaults, documentation & validation. |
166 | * | |
cf470720 | 167 | * @param array $params |
b081365f | 168 | * Array of 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 | /** | |
1747ab99 | 184 | * Send graph detail for A/B tests mail. |
467cd00c | 185 | * |
186 | * @param array $params | |
1747ab99 | 187 | * |
467cd00c | 188 | * @return array |
360aaa75 | 189 | * @throws API_Exception |
467cd00c | 190 | */ |
191 | function civicrm_api3_mailing_a_b_graph_stats($params) { | |
202ebbab | 192 | civicrm_api3_verify_mandatory($params, |
193 | 'CRM_Mailing_DAO_MailingAB', | |
194 | array('id'), | |
195 | FALSE | |
196 | ); | |
467cd00c | 197 | |
360aaa75 TO |
198 | $defaults = array( |
199 | 'criteria' => 'Open', | |
200 | 'target_date' => CRM_Utils_Time::getTime('YmdHis'), | |
201 | 'split_count' => 6, | |
202 | 'split_count_select' => 1, | |
203 | ); | |
204 | $params = array_merge($defaults, $params); | |
202ebbab | 205 | |
360aaa75 | 206 | $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id'])); |
202ebbab | 207 | $graphStats = array(); |
467cd00c | 208 | $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b'); |
209 | ||
202ebbab | 210 | foreach ($ABFormat as $name => $column) { |
360aaa75 TO |
211 | switch (strtolower($params['criteria'])) { |
212 | case 'open': | |
768c558c TO |
213 | $result = CRM_Mailing_Event_BAO_Opened::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC"); |
214 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); | |
360aaa75 TO |
215 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
216 | $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); | |
202ebbab | 217 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
218 | $toDate = date('YmdHis', $toDate); | |
219 | $graphStats[$name] = array( | |
220 | $params['split_count_select'] => array( | |
221 | 'count' => CRM_Mailing_Event_BAO_Opened::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate), | |
21dfd5f5 TO |
222 | 'time' => CRM_Utils_Date::customFormat($toDate), |
223 | ), | |
202ebbab | 224 | ); |
225 | break; | |
ea100cb5 | 226 | |
360aaa75 | 227 | case 'total unique clicks': |
202ebbab | 228 | $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC"); |
229 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); | |
360aaa75 TO |
230 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
231 | $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); | |
202ebbab | 232 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
233 | $toDate = date('YmdHis', $toDate); | |
234 | $graphStats[$name] = array( | |
235 | $params['split_count_select'] => array( | |
236 | 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate), | |
21dfd5f5 TO |
237 | 'time' => CRM_Utils_Date::customFormat($toDate), |
238 | ), | |
202ebbab | 239 | ); |
240 | break; | |
ea100cb5 | 241 | |
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), | |
21dfd5f5 TO |
257 | 'time' => CRM_Utils_Date::customFormat($toDate), |
258 | ), | |
202ebbab | 259 | ); |
260 | break; | |
261 | } | |
262 | } | |
263 | ||
264 | return civicrm_api3_create_success($graphStats); | |
b0f9e1df | 265 | } |