| pierre@0 | 1 <?php | 
| pierre@1 | 2 // $Id: ad.pages.inc,v 1.1.2.6.2.2 2009/02/28 23:39:47 jeremy Exp $ | 
| pierre@0 | 3 | 
| pierre@0 | 4 /** | 
| pierre@0 | 5  * @file | 
| pierre@0 | 6  * Advertisement nodes pages and forms. | 
| pierre@0 | 7  * | 
| pierre@0 | 8  * Copyright (c) 2005-2009. | 
| pierre@0 | 9  *   Jeremy Andrews <jeremy@tag1consulting.com> | 
| pierre@0 | 10  */ | 
| pierre@0 | 11 | 
| pierre@0 | 12 function theme_node_ad($node, $yield_form = TRUE) { | 
| pierre@0 | 13   $output = ''; | 
| pierre@1 | 14   if (ad_permission($node, 'access statistics')) { | 
| pierre@0 | 15     $output = theme('ad_status_display', $node); | 
| pierre@0 | 16     $output .= theme('ad_statistics_display', ad_statistics($node->nid)); | 
| pierre@0 | 17   } | 
| pierre@1 | 18   if (ad_permission($node, 'access click history')) { | 
| pierre@0 | 19     $header = array( | 
| pierre@0 | 20       array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'), | 
| pierre@0 | 21       array('data' => t('User'), 'field' => 'uid'), | 
| pierre@0 | 22       array('data' => t('URL where clicked'), 'field' => 'url'), | 
| pierre@0 | 23     ); | 
| pierre@0 | 24     if (function_exists('click_filter_status_text') && user_access('view filtered clicks')) { | 
| pierre@0 | 25       $header[] = array('data' => t('Status'), 'field' => 'status'); | 
| pierre@0 | 26     } | 
| pierre@0 | 27     $header[] = ''; | 
| pierre@0 | 28 | 
| pierre@1 | 29     if (isset($node->nid) && $node->nid > 0) { | 
| pierre@1 | 30       $sql = "SELECT cid, timestamp, uid, status, hostname, url FROM {ad_clicks} WHERE aid = %d"; | 
| pierre@0 | 31       $sql .= tablesort_sql($header); | 
| pierre@0 | 32       $result = pager_query($sql, 25, 0, NULL, $node->nid); | 
| pierre@0 | 33 | 
| pierre@0 | 34       while ($ad = db_fetch_object($result)) { | 
| pierre@0 | 35         if (module_exists('click_filter') && $ad->status != CLICK_VALID) { | 
| pierre@0 | 36           // Only show filtered clicks to users with permission to view them. | 
| pierre@0 | 37           if (!user_access('view filtered clicks')) { | 
| pierre@0 | 38             continue; | 
| pierre@0 | 39           } | 
| pierre@0 | 40         } | 
| pierre@0 | 41         if (strlen($ad->url) > 40) { | 
| pierre@0 | 42           $url = substr($ad->url, 0, 37) .'...'; | 
| pierre@0 | 43         } | 
| pierre@0 | 44         else { | 
| pierre@0 | 45           $url = $ad->url; | 
| pierre@0 | 46         } | 
| pierre@0 | 47         $row = array(); | 
| pierre@0 | 48         $click_user = user_load(array('uid' => $ad->uid)); | 
| pierre@0 | 49         $row[] = format_date($ad->timestamp, 'custom', 'M j H:i'); | 
| pierre@0 | 50         $row[] = theme('username', $click_user); | 
| pierre@0 | 51         $row[] = l($url, $ad->url); | 
| pierre@0 | 52         if (function_exists('click_filter_status_text') && user_access('view filtered clicks')) { | 
| pierre@0 | 53           $row[] = click_filter_status_text($ad->status); | 
| pierre@0 | 54         } | 
| pierre@0 | 55         $row[] = '['. l(t('details'), 'node/'. $node->nid .'/details/'. $ad->cid) .']'; | 
| pierre@0 | 56         $rows[] = $row; | 
| pierre@0 | 57       } | 
| pierre@0 | 58 | 
| pierre@0 | 59       if (empty($rows)) { | 
| pierre@0 | 60         $click_history = '<p>'. t('There are no clicks yet.') .'</p>'; | 
| pierre@0 | 61       } | 
| pierre@0 | 62       else { | 
| pierre@0 | 63         $click_history = theme('table', $header, $rows); | 
| pierre@0 | 64       } | 
| pierre@0 | 65       $click_history .= theme('pager', NULL, 25, 0); | 
| pierre@0 | 66       $output .= theme('box', t('Click history'), $click_history); | 
| pierre@0 | 67     } | 
| pierre@0 | 68   } | 
| pierre@0 | 69   return $output; | 
| pierre@0 | 70 } | 
| pierre@0 | 71 | 
| pierre@0 | 72 /** | 
| pierre@0 | 73  * Calculate statistics for the given advertisements. | 
| pierre@0 | 74  * TODO: Introduce caching to make this more efficient. | 
| pierre@0 | 75  */ | 
| pierre@0 | 76 function ad_statistics($aid) { | 
| pierre@0 | 77   // Get global statistics. | 
| pierre@0 | 78   $statistics['global']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view'", $aid)); | 
| pierre@0 | 79   $statistics['global']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click'", $aid)); | 
| pierre@0 | 80 | 
| pierre@0 | 81   // No sense in making further queries if the ad has no global statistics. | 
| pierre@0 | 82   if (!$statistics['global']['views'] && !$statistics['global']['clicks']) { | 
| pierre@0 | 83     return $statistics; | 
| pierre@0 | 84   } | 
| pierre@0 | 85 | 
| pierre@0 | 86   // Get statistics for this year and last year. | 
| pierre@0 | 87   $this_year = date('Y000000'); | 
| pierre@0 | 88   $last_year = date('Y') - 1 .'000000'; | 
| pierre@0 | 89   $statistics['last_year']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $last_year, $this_year)); | 
| pierre@0 | 90   $statistics['last_year']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $aid, $last_year, $this_year)); | 
| pierre@0 | 91   $statistics['this_year']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $this_year)); | 
| pierre@0 | 92   $statistics['this_year']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $this_year)); | 
| pierre@0 | 93 | 
| pierre@0 | 94   // No sense in making further queries if the ad has no statistics this year. | 
| pierre@0 | 95   if (!$statistics['this_year']['views'] && !$statistics['this_year']['clicks']) { | 
| pierre@0 | 96     return $statistics; | 
| pierre@0 | 97   } | 
| pierre@0 | 98 | 
| pierre@0 | 99   // Get statistics for this month and last month. | 
| pierre@0 | 100   $this_month = date('Ym0000'); | 
| pierre@0 | 101   $last_month = date('m') - 1; | 
| pierre@0 | 102   if ($last_month == 0) { | 
| pierre@0 | 103     $last_month = date('Y') - 1 .'120000'; | 
| pierre@0 | 104   } | 
| pierre@0 | 105   else { | 
| pierre@0 | 106     $last_month = date('Y') . ($last_month < 10 ? '0' : '') . $last_month .'0000'; | 
| pierre@0 | 107   } | 
| pierre@0 | 108   $statistics['last_month']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $last_month, $this_month)); | 
| pierre@0 | 109   $statistics['last_month']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $aid, $last_month, $this_month)); | 
| pierre@0 | 110   $statistics['this_month']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $this_month)); | 
| pierre@0 | 111   $statistics['this_month']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $this_month)); | 
| pierre@0 | 112 | 
| pierre@0 | 113   // No sense in making further queries if the ad has no statistics this month. | 
| pierre@0 | 114   if (!$statistics['this_month']['views'] && !$statistics['this_month']['clicks']) { | 
| pierre@0 | 115     return $statistics; | 
| pierre@0 | 116   } | 
| pierre@0 | 117 | 
| pierre@0 | 118   // Get statistics for this week. | 
| pierre@0 | 119   $this_week_start = date('Ymd00', time() - 60*60*24*6); | 
| pierre@0 | 120   $statistics['this_week']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date > %d", $aid, $this_week_start)); | 
| pierre@0 | 121   $statistics['this_week']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date > %d", $aid, $this_week_start)); | 
| pierre@0 | 122 | 
| pierre@0 | 123   // No sense in making further queries if the ad has no statistics this week. | 
| pierre@0 | 124   if (!$statistics['this_week']['views'] && !$statistics['this_week']['clicks']) { | 
| pierre@0 | 125     return $statistics; | 
| pierre@0 | 126   } | 
| pierre@0 | 127 | 
| pierre@0 | 128   // Get statistics for yesterday and today. | 
| pierre@0 | 129   $yesterday_start = date('Ymd00', time() - 60*60*24); | 
| pierre@0 | 130   $yesterday_end = date('Ymd24', time() - 60*60*24); | 
| pierre@0 | 131   $today_start = date('Ymd00', time()); | 
| pierre@0 | 132   $statistics['yesterday']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $yesterday_start, $yesterday_end)); | 
| pierre@0 | 133   $statistics['yesterday']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $aid, $yesterday_start, $yesterday_end)); | 
| pierre@0 | 134   $statistics['today']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $today_start)); | 
| pierre@0 | 135   $statistics['today']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $today_start)); | 
| pierre@0 | 136 | 
| pierre@0 | 137   // No sense in making further queries if the ad has no statistics today. | 
| pierre@0 | 138   if (!$statistics['today']['views'] && !$statistics['today']['clicks']) { | 
| pierre@0 | 139     return $statistics; | 
| pierre@0 | 140   } | 
| pierre@0 | 141 | 
| pierre@0 | 142   // Get statistics for this hour and the last hour. | 
| pierre@0 | 143   $last_hour = date('YmdH', time() - 60*60); | 
| pierre@0 | 144   $this_hour = date('YmdH', time()); | 
| pierre@0 | 145   $statistics['last_hour']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date = %d", $aid, $last_hour)); | 
| pierre@0 | 146   $statistics['last_hour']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date = %d", $aid, $last_hour)); | 
| pierre@0 | 147   $statistics['this_hour']['views'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date = %d", $aid, $this_hour)); | 
| pierre@0 | 148   $statistics['this_hour']['clicks'] = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date = %d", $aid, $this_hour)); | 
| pierre@0 | 149 | 
| pierre@0 | 150   return $statistics; | 
| pierre@0 | 151 } | 
| pierre@0 | 152 | 
| pierre@0 | 153 function theme_ad_statistics_display($statistics) { | 
| pierre@0 | 154   $header = array('', t('Impressions'), t('Clicks'), t('Click-thru')); | 
| pierre@0 | 155   $rows = array(); | 
| pierre@0 | 156 | 
| pierre@0 | 157   $data = array( | 
| pierre@0 | 158    'this_hour' => t('This hour'), | 
| pierre@0 | 159    'last_hour' => t('Last hour'), | 
| pierre@0 | 160    'today' => t('Today'), | 
| pierre@0 | 161    'yesterday' => t('Yesterday'), | 
| pierre@0 | 162    'this_week' => t('Last seven days'), | 
| pierre@0 | 163    'this_month' => t('This month'), | 
| pierre@0 | 164    'last_month' => t('Last month'), | 
| pierre@0 | 165    'this_year' => t('This year'), | 
| pierre@0 | 166    'last_year' => t('Last year'), | 
| pierre@0 | 167    'global' => t('All time') | 
| pierre@0 | 168   ); | 
| pierre@0 | 169 | 
| pierre@0 | 170   foreach ($data as $key => $value) { | 
| pierre@0 | 171     if (isset($statistics[$key]) && (isset($statistics[$key]['views']) || isset($statistics[$key]['clicks']) || $key == 'global')) { | 
| pierre@0 | 172       $rows[] = array( | 
| pierre@0 | 173         array('data' => $value), | 
| pierre@0 | 174         array('data' => (int)$statistics[$key]['views']), | 
| pierre@0 | 175         array('data' => (int)$statistics[$key]['clicks']), | 
| pierre@0 | 176         array('data' => $statistics[$key]['views'] ? sprintf('%1.2f', ((int)$statistics[$key]['clicks'] / (int)$statistics[$key]['views']) * 100) .'%' : '0.00%'), | 
| pierre@0 | 177       ); | 
| pierre@0 | 178     } | 
| pierre@0 | 179   } | 
| pierre@0 | 180   if (empty($rows) || (!$statistics['global']['views'] && !$statistics['global']['clicks'])) { | 
| pierre@1 | 181     $statistics = '<p>'. t('There are currently no statistics for this advertisement.') .'</p>'; | 
| pierre@0 | 182   } | 
| pierre@0 | 183   else { | 
| pierre@0 | 184     $statistics = theme('table', $header, $rows); | 
| pierre@0 | 185   } | 
| pierre@0 | 186 | 
| pierre@0 | 187   return theme('box', t('Statistics'), $statistics); | 
| pierre@0 | 188 } | 
| pierre@0 | 189 | 
| pierre@1 | 190 /** | 
| pierre@1 | 191  * Display details about a specific click. | 
| pierre@1 | 192  */ | 
| pierre@0 | 193 function ad_click_details($node, $cid) { | 
| pierre@0 | 194   drupal_set_breadcrumb(array(l(t('Home'), NULL), l(check_plain($node->title), 'node/'. $node->nid))); | 
| pierre@0 | 195   if ($click = db_fetch_object(db_query('SELECT * FROM {ad_clicks} WHERE cid = %d', $cid))) { | 
| pierre@0 | 196     $ad = node_load($click->aid); | 
| pierre@0 | 197     $account = user_load(array('uid' => $click->uid)); | 
| pierre@0 | 198     $rows = array( | 
| pierre@0 | 199       array( | 
| pierre@0 | 200         array('data' => t('Time'), 'header' => TRUE), | 
| pierre@0 | 201         format_date($click->timestamp, 'custom', 'D F j, Y h:i a'), | 
| pierre@0 | 202       ), | 
| pierre@0 | 203       array( | 
| pierre@0 | 204         array('data' => t('User'), 'header' => TRUE), | 
| pierre@0 | 205         theme('username', $account), | 
| pierre@0 | 206       ), | 
| pierre@0 | 207       array( | 
| pierre@0 | 208         array('data' => t('IP Address'), 'header' => TRUE), | 
| pierre@0 | 209         $click->hostname, | 
| pierre@0 | 210       ), | 
| pierre@0 | 211       array( | 
| pierre@0 | 212         array('data' => t('User Agent'), 'header' => TRUE), | 
| pierre@0 | 213         check_plain($click->user_agent), | 
| pierre@0 | 214       ), | 
| pierre@0 | 215       array( | 
| pierre@0 | 216         array('data' => t('URL'), 'header' => TRUE), | 
| pierre@0 | 217         l($click->url, $click->url), | 
| pierre@0 | 218       ), | 
| pierre@0 | 219       array( | 
| pierre@0 | 220         array('data' => t('Advertisement'), 'header' => TRUE), | 
| pierre@0 | 221         $ad->ad, | 
| pierre@0 | 222       ) | 
| pierre@0 | 223     ); | 
| pierre@0 | 224     if (function_exists('click_filter_status_text') && user_access('view filtered clicks')) { | 
| pierre@0 | 225       switch ($click->status) { | 
| pierre@0 | 226         case 0: | 
| pierre@0 | 227         default: | 
| pierre@0 | 228           $status = t('Not valid: this click has not been counted for unknown reasons.  This is an unexpected error.'); | 
| pierre@0 | 229           break; | 
| pierre@0 | 230         case 1: | 
| pierre@0 | 231           $status = t('Valid: this is a valid click.'); | 
| pierre@0 | 232           break; | 
| pierre@0 | 233         case 2: | 
| pierre@0 | 234           $status = t('Not valid: this click has not been counted because another click by the same IP address was already counted.'); | 
| pierre@0 | 235           break; | 
| pierre@0 | 236         case 3: | 
| pierre@0 | 237           $status = t('Not valid: this click has not been counted because it was generated by an owner of the advertisement.'); | 
| pierre@0 | 238           break; | 
| pierre@0 | 239         case 4: | 
| pierre@0 | 240           $status = t('Not valid: this click has not been counted because it was generated by a user in a filtered role.'); | 
| pierre@0 | 241           break; | 
| pierre@0 | 242         case 5: | 
| pierre@0 | 243           $status = t('Not valid: this click has not been counted because it was generated by an automated "bot".'); | 
| pierre@0 | 244           break; | 
| pierre@0 | 245       } | 
| pierre@0 | 246       $rows[] = array(array('data' => t('Status'), 'header' => TRUE), $status); | 
| pierre@0 | 247     } | 
| pierre@0 | 248     $output = theme('table', array(), $rows); | 
| pierre@0 | 249   } | 
| pierre@0 | 250   return $output; | 
| pierre@0 | 251 } | 
| pierre@0 | 252 | 
| pierre@0 | 253 |