comparison ad.pages.inc @ 0:d8a3998dac8e ad

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