3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * APIv3 functions for registering/processing mailing ab testing events.
32 * @package CiviCRM_APIv3
38 function _civicrm_api3_mailing_a_b_create_spec(&$spec) {
39 $spec['created_date']['api.default'] = 'now';
40 $spec['created_id']['api.required'] = 1;
41 $spec['created_id']['api.default'] = 'user_contact_id';
45 * Handle a create mailing ab testing.
47 * @param array $params
52 function civicrm_api3_mailing_a_b_create($params) {
53 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
57 * Handle a delete event.
59 * @param array $params
64 function civicrm_api3_mailing_a_b_delete($params) {
65 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
71 * @param array $params
75 function civicrm_api3_mailing_a_b_get($params) {
76 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
80 * Adjust Metadata for submit action.
82 * The metadata is used for setting defaults, documentation & validation.
85 * Array of parameters determined by getfields.
87 function _civicrm_api3_mailing_a_b_submit_spec(&$spec) {
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
107 * @throws API_Exception
109 function civicrm_api3_mailing_a_b_submit($params) {
110 civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status'));
112 if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) {
113 throw new API_Exception("Missing parameter scheduled_date and/or approval_date");
116 $dao = new CRM_Mailing_DAO_MailingAB();
117 $dao->id
= $params['id'];
118 if (!$dao->find(TRUE)) {
119 throw new API_Exception("Failed to locate A/B test by ID");
121 if (empty($dao->mailing_id_a
) ||
empty($dao->mailing_id_b
) ||
empty($dao->mailing_id_c
)) {
122 throw new API_Exception("Missing mailing IDs for A/B test");
125 $submitParams = CRM_Utils_Array
::subset($params, array(
129 'approval_status_id',
132 switch ($params['status']) {
134 if (!empty($dao->status
) && $dao->status
!= 'Draft') {
135 throw new API_Exception("Cannot transition to state 'Testing'");
137 civicrm_api3('Mailing', 'submit', $submitParams +
array(
138 'id' => $dao->mailing_id_a
,
139 '_skip_evil_bao_auto_recipients_' => 0,
141 civicrm_api3('Mailing', 'submit', $submitParams +
array(
142 'id' => $dao->mailing_id_b
,
143 '_skip_evil_bao_auto_recipients_' => 1,
145 CRM_Mailing_BAO_MailingAB
::distributeRecipients($dao);
149 if ($dao->status
!= 'Testing') {
150 throw new API_Exception("Cannot transition to state 'Final'");
152 civicrm_api3('Mailing', 'submit', $submitParams +
array(
153 'id' => $dao->mailing_id_c
,
154 '_skip_evil_bao_auto_recipients_' => 1,
159 throw new API_Exception("Unrecognized submission status");
162 return civicrm_api3('MailingAB', 'create', array(
164 'status' => $params['status'],
172 * Adjust Metadata for graph_stats action.
174 * The metadata is used for setting defaults, documentation & validation.
176 * @param array $params
177 * Array of parameters determined by getfields.
179 function _civicrm_api3_mailing_a_b_graph_stats_spec(&$params) {
180 $params['criteria']['title'] = 'Criteria';
181 $params['criteria']['default'] = 'Open';
182 // mailing_ab_winner_criteria
183 $params['target_date']['title'] = 'Target Date';
184 $params['target_date']['type'] = CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
;
185 $params['split_count']['title'] = 'Split Count';
186 $params['split_count']['api.default'] = 6;
187 $params['split_count_select']['title'] = 'Split Count Select';
188 $params['split_count_select']['api.required'] = 1;
189 $params['target_url']['title'] = 'Target URL';
193 * Send graph detail for A/B tests mail.
195 * @param array $params
198 * @throws API_Exception
200 function civicrm_api3_mailing_a_b_graph_stats($params) {
201 civicrm_api3_verify_mandatory($params,
202 'CRM_Mailing_DAO_MailingAB',
208 'criteria' => 'Open',
209 'target_date' => CRM_Utils_Time
::getTime('YmdHis'),
211 'split_count_select' => 1,
213 $params = array_merge($defaults, $params);
215 $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id']));
216 $graphStats = array();
217 $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b');
219 foreach ($ABFormat as $name => $column) {
220 switch (strtolower($params['criteria'])) {
222 $result = CRM_Mailing_Event_BAO_Opened
::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC");
223 $startDate = CRM_Utils_Date
::processDate($result[0]['date']);
224 $targetDate = CRM_Utils_Date
::processDate($params['target_date']);
225 $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
226 $toDate = strtotime($startDate) +
($dateDuration * $params['split_count_select']);
227 $toDate = date('YmdHis', $toDate);
228 $graphStats[$name] = array(
229 $params['split_count_select'] => array(
230 'count' => CRM_Mailing_Event_BAO_Opened
::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate),
231 'time' => CRM_Utils_Date
::customFormat($toDate),
236 case 'total unique clicks':
237 $result = CRM_Mailing_Event_BAO_TrackableURLOpen
::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC");
238 $startDate = CRM_Utils_Date
::processDate($result[0]['date']);
239 $targetDate = CRM_Utils_Date
::processDate($params['target_date']);
240 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
241 $toDate = strtotime($startDate) +
($dateDuration * $params['split_count_select']);
242 $toDate = date('YmdHis', $toDate);
243 $graphStats[$name] = array(
244 $params['split_count_select'] => array(
245 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate),
246 'time' => CRM_Utils_Date
::customFormat($toDate),
251 case 'total clicks on a particular link':
252 if (empty($params['target_url'])) {
253 throw new API_Exception("Provide url to get stats result for total clicks on a particular link");
255 // FIXME: doesn't make sense to get url_id mailing_id_(a|b) while getting start date in mailing_id_a
256 $url_id = CRM_Mailing_BAO_TrackableURL
::getTrackerURLId($mailingAB[$column], $params['target_url']);
257 $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");
258 $startDate = CRM_Utils_Date
::processDate($result[0]['date']);
259 $targetDate = CRM_Utils_Date
::processDate($params['target_date']);
260 $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']);
261 $toDate = strtotime($startDate) +
($dateDuration * $params['split_count_select']);
262 $toDate = CRM_Utils_Date
::processDate($toDate);
263 $graphStats[$name] = array(
264 $params['split_count_select'] => array(
265 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen
::getTotalCount($params['mailing_id'], NULL, FALSE, $url_id, $toDate),
266 'time' => CRM_Utils_Date
::customFormat($toDate),
273 return civicrm_api3_create_success($graphStats);