4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * APIv3 functions for registering/processing mailing ab testing events.
33 * @package CiviCRM_APIv3
34 * @subpackage API_MailingAB
35 * @copyright CiviCRM LLC (c) 2004-2014
41 * Handle a create mailing ab testing
43 * @param array $params
49 function civicrm_api3_mailing_a_b_create($params) {
50 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
54 * Handle a delete event.
56 * @param array $params
62 function civicrm_api3_mailing_a_b_delete($params) {
63 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
69 * @param array $params
72 function civicrm_api3_mailing_a_b_get($params) {
73 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
77 * Adjust Metadata for submit action
79 * The metadata is used for setting defaults, documentation & validation
80 * @param array $params
81 * Array or parameters determined by getfields.
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.
98 * Send A/B mail to A/B recipients respectively
100 * @param array $params
102 * @throws API_Exception
104 function civicrm_api3_mailing_a_b_submit($params) {
105 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status'));
107 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
108 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
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");
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");
120 $submitParams = CRM_Utils_Array
::subset($params, array(
124 'approval_status_id',
127 switch ($params['status']) {
129 if (!empty($dao->status
) && $dao->status
!= 'Draft') {
130 throw new API_Exception("Cannot transition to state 'Testing'");
132 civicrm_api3('Mailing', 'submit', $submitParams +
array(
133 'id' => $dao->mailing_id_a
,
134 '_skip_evil_bao_auto_recipients_' => 0,
136 civicrm_api3('Mailing', 'submit', $submitParams +
array(
137 'id' => $dao->mailing_id_b
,
138 '_skip_evil_bao_auto_recipients_' => 1,
140 CRM_Mailing_BAO_MailingAB
::distributeRecipients($dao);
144 if ($dao->status
!= 'Testing') {
145 throw new API_Exception("Cannot transition to state 'Final'");
147 civicrm_api3('Mailing', 'submit', $submitParams +
array(
148 'id' => $dao->mailing_id_c
,
149 '_skip_evil_bao_auto_recipients_' => 1,
154 throw new API_Exception("Unrecognized submission status");
157 return civicrm_api3('MailingAB', 'create', array(
159 'status' => $params['status'],
167 * Adjust Metadata for graph_stats action
169 * The metadata is used for setting defaults, documentation & validation
170 * @param array $params
171 * Array or parameters determined by getfields.
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';
187 * Send graph detail for A/B tests mail
189 * @param array $params
191 * @throws API_Exception
193 function civicrm_api3_mailing_a_b_graph_stats($params) {
194 civicrm_api3_verify_mandatory($params,
195 'CRM_Mailing_DAO_MailingAB',
201 'criteria' => 'Open',
202 'target_date' => CRM_Utils_Time
::getTime('YmdHis'),
204 'split_count_select' => 1,
206 $params = array_merge($defaults, $params);
208 $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id']));
209 $graphStats = array();
210 $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b');
212 foreach ($ABFormat as $name => $column) {
213 switch (strtolower($params['criteria'])) {
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),
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),
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");
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),
266 return civicrm_api3_create_success($graphStats);