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 * Files required for this package
45 * Handle a create mailing ab testing
47 * @param array $params
53 function civicrm_api3_mailing_a_b_create($params) {
54 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
58 * Handle a delete event.
60 * @param array $params
66 function civicrm_api3_mailing_a_b_delete($params) {
67 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
73 * @param array $params
76 function civicrm_api3_mailing_a_b_get($params) {
77 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
81 * Adjust Metadata for submit action
83 * The metadata is used for setting defaults, documentation & validation
84 * @param array $params
85 * Array or parameters determined by getfields.
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.
102 * Send A/B mail to A/B recipients respectively
104 * @param array $params
106 * @throws API_Exception
108 function civicrm_api3_mailing_a_b_submit($params) {
109 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status'));
111 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
112 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
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");
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");
124 $submitParams = CRM_Utils_Array
::subset($params, array(
128 'approval_status_id',
131 switch ($params['status']) {
133 if (!empty($dao->status
) && $dao->status
!= 'Draft') {
134 throw new API_Exception("Cannot transition to state 'Testing'");
136 civicrm_api3('Mailing', 'submit', $submitParams +
array(
137 'id' => $dao->mailing_id_a
,
138 '_skip_evil_bao_auto_recipients_' => 0,
140 civicrm_api3('Mailing', 'submit', $submitParams +
array(
141 'id' => $dao->mailing_id_b
,
142 '_skip_evil_bao_auto_recipients_' => 1,
144 CRM_Mailing_BAO_MailingAB
::distributeRecipients($dao);
148 if ($dao->status
!= 'Testing') {
149 throw new API_Exception("Cannot transition to state 'Final'");
151 civicrm_api3('Mailing', 'submit', $submitParams +
array(
152 'id' => $dao->mailing_id_c
,
153 '_skip_evil_bao_auto_recipients_' => 1,
158 throw new API_Exception("Unrecognized submission status");
161 return civicrm_api3('MailingAB', 'create', array(
163 'status' => $params['status'],
171 * Adjust Metadata for graph_stats action
173 * The metadata is used for setting defaults, documentation & validation
174 * @param array $params
175 * Array or parameters determined by getfields.
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';
191 * Send graph detail for A/B tests mail
193 * @param array $params
195 * @throws API_Exception
197 function civicrm_api3_mailing_a_b_graph_stats($params) {
198 civicrm_api3_verify_mandatory($params,
199 'CRM_Mailing_DAO_MailingAB',
205 'criteria' => 'Open',
206 'target_date' => CRM_Utils_Time
::getTime('YmdHis'),
208 'split_count_select' => 1,
210 $params = array_merge($defaults, $params);
212 $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id']));
213 $graphStats = array();
214 $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b');
216 foreach ($ABFormat as $name => $column) {
217 switch (strtolower($params['criteria'])) {
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),
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),
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");
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),
270 return civicrm_api3_create_success($graphStats);