r75165 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75164‎ | r75165 | r75166 >
Date:18:52, 21 October 2010
Author:awjrichards
Status:deferred
Tags:
Comment:
Filled out module skeleton with code to configure the module, pull tracking data from contribution_tracking, listen for new/updates to contributions and to post to OWA
Modified paths:
  • /civicrm/trunk/sites/all/modules/wmf_owa/owa.module (modified) (history)

Diff [purge]

Index: civicrm/trunk/sites/all/modules/wmf_owa/owa.module
@@ -1,6 +1,33 @@
22 <?php
33
 4+/** The default OWA log script location **/
 5+define( 'OWA_LOG_DEFAULT', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php' );
 6+
47 /**
 8+ * Implementation of hook_perm().
 9+ */
 10+function wmf_owa_perm() {
 11+ return array('administer wmf_owa');
 12+}
 13+
 14+/**
 15+ * Implementation of hook_menu()
 16+ */
 17+function wmf_owa_menu() {
 18+ $items = array();
 19+
 20+ $items['admin/settings/wmf_owa'] = array(
 21+ 'title' => 'Wikimedia Foundation OWA Configuration',
 22+ 'description' => t('Configure OWA log script location.'),
 23+ 'access arguments' => array('administer wmf_owa'),
 24+ 'page callback' => 'drupal_get_form',
 25+ 'page arguments' => array( 'wmf_owa_settings' ),
 26+ );
 27+
 28+ return $items;
 29+}
 30+
 31+/**
532 * Callback for menu path "admin/settings/wmf_owa".
633 */
734 function wmf_owa_settings() {
@@ -8,40 +35,12 @@
936 '#type' => 'textfield',
1037 '#title' => t( 'OWA log path' ),
1138 '#required' => TRUE,
12 - '#default_value' => variable_get('wmf_owa_log', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php'),
 39+ '#default_value' => variable_get('wmf_owa_log', OWA_LOG_DEFAULT ),
1340 );
 41+ return system_settings_form( $form );
1442 }
1543
1644 /**
17 - * Format and send contribution tracking data to OWA
18 - * @param array $msg Array containing contribution information
19 - */
20 -function wmf_owa_record_contribution( $msg ){
21 -//NEED:
22 -//utm_campaign
23 -//referer URL
24 -//owa_session_id
25 -//OWA log URL
26 -
27 - // format the contribution tracking message
28 - $query_params = array(
29 - "owa_event_type" => "ecommerce.transaction",
30 - "owa_ct_order_id" => $msg[ 'contribution_tracking_id' ],
31 - "owa_ct_order_source" => $data['utm_campaign'],
32 - "owa_ct_total" => $msg['gross'],
33 - "owa_ct_tax" => 0,
34 - "owa_ct_shipping" => 0,
35 - "owa_ct_gateway" => $msg['gateway'],
36 - "owa_page_url" => $encodedReqURL,
37 - //ct_line_items[li_product_name]=foo&
38 - "owa_session_id"=> "some_id"
39 - );
40 -
41 - // send the tracking message to OWA
42 - wmf_owa_send_tracking( $query_params );
43 -}
44 -
45 -/**
4645 * Implementation of hook_civicrm_post
4746 *
4847 * @param unknown_type $op
@@ -54,22 +53,59 @@
5554 if ( $objectName != 'Contribution' && !in_array( $op, array( 'edit', 'create' )) ) {
5655 return;
5756 }
58 - var_dump( $objectRef ); exit;
59 - // load the OWA
60 - //$contribution_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId );
 57+
 58+ // load the OWA-specific tracking data
 59+ $contrib_tracking_data = wmf_owa_get_contribution_tracking_data( $objectId );
 60+ if ( !$contrib_tracking_data ) {
 61+ // log, don't post to OWA
 62+ watchdog( 'wmf_owa', 'Could not load tracking data for contribution_id ' . $objectId, WATCHDOG_WARNING );
 63+ return;
 64+ }
 65+
 66+ /**
 67+ * pull the gateway out fo the trxn_id string
 68+ * it is the element prior to the first space in the str
 69+ */
 70+ $gateway = substr( $objectRef->trxn_id, 0, strpos( $objectRef->trxn_id, "\s" ));
 71+
 72+ // format the contribution tracking message
 73+ $query_params = array(
 74+ "owa_event_type" => "ecommerce.transaction",
 75+ "owa_ct_order_id" => $contrib_tracking_data->contribution_tracking_id,
 76+ "owa_ct_order_source" => $contrib_tracking_data->utm_campaign,
 77+ "owa_ct_total" => $objectRef->total_amount,
 78+ "owa_ct_tax" => 0,
 79+ "owa_ct_shipping" => 0,
 80+ "owa_ct_gateway" => $gateway,
 81+ "owa_page_url" => $contrib_tracking_data->url,
 82+ "owa_session_id"=> $contrib_tracking_data->owa_session,
 83+ );
 84+
 85+ // log the query params
 86+ watchdog( 'wmf_owa', 'OWA tracking params for contribution_id ' . $objectId . ': ' . print_r( $query_params, true ), WATCHDOG_DEBUG );
 87+
 88+ // sent the query to OWA
 89+ wmf_owa_send_tracking( $query_params );
 90+ return;
6191
6292 }
6393
64 -
6594 /**
6695 * Fetch info needed for OWA from contribution_tracking table
6796 * @param int $contribution_tracking_id
 97+ * @return array
6898 */
6999 function wmf_owa_get_contribution_tracking_data( $contribution_id ) {
70100 $query = 'SELECT ct.id as "contribuion_tracking_id", ct.owa_session, ctor.url, ct.utm_campaign
71101 FROM contribution_tracking ct, contribution_tracking_owa_ref ctor
72102 WHERE ctor.id=ct.owa_ref && ct.contribution_id="%d"';
73 - return db_fetch_object( db_query( $query, $contribution_id ));
 103+ $result = db_fetch_object( db_query( $query, $contribution_id ));
 104+
 105+ // log $query and $result
 106+ watchdog( 'wmf_owa', 'OWA query to contribution_tracking for contribution_id ' . $contribution_id . ': ' . $query, WATCHDOG_DEBUG );
 107+ watchdog( 'wmf_owa', 'OWA query result from contribution_tracking for contribution_id ' . $contribution_id . ': ' . print_r( $result, true ), WATCHDOG_DEBUG );
 108+
 109+ return $result;
74110 }
75111
76112 /**
@@ -78,10 +114,16 @@
79115 * @param array $query_params The array of data for OWA to track
80116 */
81117 function wmf_owa_send_tracking( $query_params ) {
82 - $owa_log_url = variable_get( 'wmf_owa_log', 'http://analytics.tesla.usability.wikimedia.org/wiki/d/extensions/owa/log.php' );
 118+ $owa_log_url = variable_get( 'wmf_owa_log', OWA_LOG_DEFAULT );
83119 $ch = curl_init();
84120 curl_setopt( $ch, CURLOPT_URL, $owa_log_url . "?". http_build_query( $query_params ));
85121 curl_setopt( $ch, CURLOPT_USERAGENT, "WMF Ecommerce web service 1.0" );
86 - curl_exec( $ch );
 122+
 123+ if ( !curl_exec( $ch )) {
 124+ // log curl error curl_error( $ch );
 125+ watchdog( 'wmf_owa', 'Error posting to OWA with query params: ' . print_r( $query_params, true ), WATCHDOG_ERROR );
 126+ }
 127+
87128 curl_close( $ch );
 129+ return;
88130 }
\ No newline at end of file

Status & tagging log