| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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 | */ |
| 34 | |
| 35 | /** |
| 36 | * @param array $spec |
| 37 | */ |
| 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'; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Handle a create mailing ab testing. |
| 46 | * |
| 47 | * @param array $params |
| 48 | * |
| 49 | * @return array |
| 50 | * API Success Array |
| 51 | */ |
| 52 | function civicrm_api3_mailing_a_b_create($params) { |
| 53 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handle a delete event. |
| 58 | * |
| 59 | * @param array $params |
| 60 | * |
| 61 | * @return array |
| 62 | * API Success Array |
| 63 | */ |
| 64 | function civicrm_api3_mailing_a_b_delete($params) { |
| 65 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Handle a get event. |
| 70 | * |
| 71 | * @param array $params |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | function civicrm_api3_mailing_a_b_get($params) { |
| 76 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adjust Metadata for submit action. |
| 81 | * |
| 82 | * The metadata is used for setting defaults, documentation & validation. |
| 83 | * |
| 84 | * @param array $spec |
| 85 | * Array of parameters determined by getfields. |
| 86 | */ |
| 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. |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Send A/B mail to A/B recipients respectively. |
| 103 | * |
| 104 | * @param array $params |
| 105 | * |
| 106 | * @return array |
| 107 | * @throws API_Exception |
| 108 | */ |
| 109 | function civicrm_api3_mailing_a_b_submit($params) { |
| 110 | civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_MailingAB', array('id', 'status')); |
| 111 | |
| 112 | if (!isset($params['scheduled_date']) && !isset($updateParams['approval_date'])) { |
| 113 | throw new API_Exception("Missing parameter scheduled_date and/or approval_date"); |
| 114 | } |
| 115 | |
| 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"); |
| 120 | } |
| 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"); |
| 123 | } |
| 124 | |
| 125 | $submitParams = CRM_Utils_Array::subset($params, array( |
| 126 | 'scheduled_date', |
| 127 | 'approval_date', |
| 128 | 'approval_note', |
| 129 | 'approval_status_id', |
| 130 | )); |
| 131 | |
| 132 | switch ($params['status']) { |
| 133 | case 'Testing': |
| 134 | if (!empty($dao->status) && $dao->status != 'Draft') { |
| 135 | throw new API_Exception("Cannot transition to state 'Testing'"); |
| 136 | } |
| 137 | civicrm_api3('Mailing', 'submit', $submitParams + array( |
| 138 | 'id' => $dao->mailing_id_a, |
| 139 | '_skip_evil_bao_auto_recipients_' => 0, |
| 140 | )); |
| 141 | civicrm_api3('Mailing', 'submit', $submitParams + array( |
| 142 | 'id' => $dao->mailing_id_b, |
| 143 | '_skip_evil_bao_auto_recipients_' => 1, |
| 144 | )); |
| 145 | CRM_Mailing_BAO_MailingAB::distributeRecipients($dao); |
| 146 | break; |
| 147 | |
| 148 | case 'Final': |
| 149 | if ($dao->status != 'Testing') { |
| 150 | throw new API_Exception("Cannot transition to state 'Final'"); |
| 151 | } |
| 152 | civicrm_api3('Mailing', 'submit', $submitParams + array( |
| 153 | 'id' => $dao->mailing_id_c, |
| 154 | '_skip_evil_bao_auto_recipients_' => 1, |
| 155 | )); |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | throw new API_Exception("Unrecognized submission status"); |
| 160 | } |
| 161 | |
| 162 | return civicrm_api3('MailingAB', 'create', array( |
| 163 | 'id' => $dao->id, |
| 164 | 'status' => $params['status'], |
| 165 | 'options' => array( |
| 166 | 'reload' => 1, |
| 167 | ), |
| 168 | )); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Adjust Metadata for graph_stats action. |
| 173 | * |
| 174 | * The metadata is used for setting defaults, documentation & validation. |
| 175 | * |
| 176 | * @param array $params |
| 177 | * Array of parameters determined by getfields. |
| 178 | */ |
| 179 | function _civicrm_api3_mailing_a_b_graph_stats_spec(&$params) { |
| 180 | $params['criteria'] = array( |
| 181 | 'title' => 'Criteria', |
| 182 | 'default' => 'Open', |
| 183 | 'type' => CRM_Utils_Type::T_STRING, |
| 184 | ); |
| 185 | |
| 186 | // mailing_ab_winner_criteria |
| 187 | $params['target_date']['title'] = 'Target Date'; |
| 188 | $params['target_date']['type'] = CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME; |
| 189 | $params['split_count'] = array( |
| 190 | 'title' => 'Split Count', |
| 191 | 'api.default' => 6, |
| 192 | 'type' => CRM_Utils_Type::T_INT, |
| 193 | ); |
| 194 | $params['split_count_select']['title'] = 'Split Count Select'; |
| 195 | $params['split_count_select']['api.required'] = 1; |
| 196 | $params['target_url']['title'] = 'Target URL'; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Send graph detail for A/B tests mail. |
| 201 | * |
| 202 | * @param array $params |
| 203 | * |
| 204 | * @return array |
| 205 | * @throws API_Exception |
| 206 | */ |
| 207 | function civicrm_api3_mailing_a_b_graph_stats($params) { |
| 208 | civicrm_api3_verify_mandatory($params, |
| 209 | 'CRM_Mailing_DAO_MailingAB', |
| 210 | array('id'), |
| 211 | FALSE |
| 212 | ); |
| 213 | |
| 214 | $defaults = array( |
| 215 | 'criteria' => 'Open', |
| 216 | 'target_date' => CRM_Utils_Time::getTime('YmdHis'), |
| 217 | 'split_count' => 6, |
| 218 | 'split_count_select' => 1, |
| 219 | ); |
| 220 | $params = array_merge($defaults, $params); |
| 221 | |
| 222 | $mailingAB = civicrm_api3('MailingAB', 'getsingle', array('id' => $params['id'])); |
| 223 | $graphStats = array(); |
| 224 | $ABFormat = array('A' => 'mailing_id_a', 'B' => 'mailing_id_b'); |
| 225 | |
| 226 | foreach ($ABFormat as $name => $column) { |
| 227 | switch (strtolower($params['criteria'])) { |
| 228 | case 'open': |
| 229 | $result = CRM_Mailing_Event_BAO_Opened::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_opened.time_stamp ASC"); |
| 230 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); |
| 231 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
| 232 | $dateDuration = round(round(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); |
| 233 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
| 234 | $toDate = date('YmdHis', $toDate); |
| 235 | $graphStats[$name] = array( |
| 236 | $params['split_count_select'] => array( |
| 237 | 'count' => CRM_Mailing_Event_BAO_Opened::getTotalCount($mailingAB[$column], NULL, TRUE, $toDate), |
| 238 | 'time' => CRM_Utils_Date::customFormat($toDate), |
| 239 | ), |
| 240 | ); |
| 241 | break; |
| 242 | |
| 243 | case 'total unique clicks': |
| 244 | $result = CRM_Mailing_Event_BAO_TrackableURLOpen::getRows($mailingAB['mailing_id_a'], NULL, TRUE, 0, 1, "civicrm_mailing_event_trackable_url_open.time_stamp ASC"); |
| 245 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); |
| 246 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
| 247 | $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); |
| 248 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
| 249 | $toDate = date('YmdHis', $toDate); |
| 250 | $graphStats[$name] = array( |
| 251 | $params['split_count_select'] => array( |
| 252 | 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, NULL, $toDate), |
| 253 | 'time' => CRM_Utils_Date::customFormat($toDate), |
| 254 | ), |
| 255 | ); |
| 256 | break; |
| 257 | |
| 258 | case 'total clicks on a particular link': |
| 259 | if (empty($params['target_url'])) { |
| 260 | throw new API_Exception("Provide url to get stats result for total clicks on a particular link"); |
| 261 | } |
| 262 | // FIXME: doesn't make sense to get url_id mailing_id_(a|b) while getting start date in mailing_id_a |
| 263 | $url_id = CRM_Mailing_BAO_TrackableURL::getTrackerURLId($mailingAB[$column], $params['target_url']); |
| 264 | $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"); |
| 265 | $startDate = CRM_Utils_Date::processDate($result[0]['date']); |
| 266 | $targetDate = CRM_Utils_Date::processDate($params['target_date']); |
| 267 | $dateDuration = round(abs(strtotime($targetDate) - strtotime($startDate)) / $params['split_count']); |
| 268 | $toDate = strtotime($startDate) + ($dateDuration * $params['split_count_select']); |
| 269 | $toDate = CRM_Utils_Date::processDate($toDate); |
| 270 | $graphStats[$name] = array( |
| 271 | $params['split_count_select'] => array( |
| 272 | 'count' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTotalCount($params['mailing_id'], NULL, FALSE, $url_id, $toDate), |
| 273 | 'time' => CRM_Utils_Date::customFormat($toDate), |
| 274 | ), |
| 275 | ); |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return civicrm_api3_create_success($graphStats); |
| 281 | } |