Mercurial > defr > drupal > ad
comparison report/ad_report.module @ 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_report.module,v 1.1.2.3.2.7.2.6 2009/02/16 23:12:29 jeremy Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Provides comprehensive charts and reports about advertising statistics. | |
7 * | |
8 * Copyright (c) 2005-2009. | |
9 * Jeremy Andrews <jeremy@tag1consulting.com>. | |
10 */ | |
11 | |
12 /** | |
13 * Implementation of hook_menu(). | |
14 */ | |
15 function ad_report_menu() { | |
16 $items = array(); | |
17 | |
18 $items['node/%node/report'] = array( | |
19 'title' => 'Reports', | |
20 'page callback' => 'ad_report_bargraph', | |
21 'page arguments' => array(1), | |
22 'type' => MENU_LOCAL_TASK, | |
23 'access callback' => 'ad_report_access', | |
24 'access arguments' => array(1), | |
25 ); | |
26 $items['node/%node/report/monthly'] = array( | |
27 'title' => 'Monthly Reports', | |
28 'page callback' => 'ad_report_bargraph', | |
29 'page arguments' => array(1,3), | |
30 'type' => MENU_LOCAL_TASK, | |
31 'access callback' => 'ad_report_access', | |
32 'access arguments' => array(1), | |
33 ); | |
34 $items['node/%node/report/weekly'] = array( | |
35 'title' => 'Weekly Reports', | |
36 'page callback' => 'ad_report_bargraph', | |
37 'page arguments' => array(1,3), | |
38 'type' => MENU_LOCAL_TASK, | |
39 'access callback' => 'ad_report_access', | |
40 'access arguments' => array(1), | |
41 ); | |
42 $items['node/%node/report/daily'] = array( | |
43 'title' => 'Daily Reports', | |
44 'page callback' => 'ad_report_bargraph', | |
45 'page arguments' => array(1,3), | |
46 'type' => MENU_DEFAULT_LOCAL_TASK, | |
47 'access callback' => 'ad_report_access', | |
48 'access arguments' => array(1), | |
49 ); | |
50 $items['node/%node/report/hourly'] = array( | |
51 'title' => 'Hourly Reports', | |
52 'page callback' => 'ad_report_bargraph', | |
53 'page arguments' => array(1,3), | |
54 'type' => MENU_LOCAL_TASK, | |
55 'access callback' => 'ad_report_access', | |
56 'access arguments' => array(1), | |
57 ); | |
58 $items['ad_report/%node/bargraph'] = array( | |
59 'title' => 'Bar graph', | |
60 'page callback' => 'ad_report_generate_bargraph', | |
61 'page arguments' => array(1), | |
62 'type' => MENU_CALLBACK, | |
63 'access callback' => 'ad_report_access', | |
64 'access arguments' => array(1), | |
65 ); | |
66 | |
67 return $items; | |
68 } | |
69 /** | |
70 * Implementation of access callback. | |
71 * | |
72 * @param mixed $node | |
73 * Ad object. | |
74 */ | |
75 function ad_report_access($node){ | |
76 return ($node->type == 'ad') && ad_adaccess($node, 'access statistics'); | |
77 } | |
78 /** | |
79 * Page to display ad with bargraph. | |
80 */ | |
81 function ad_report_bargraph($node, $granularity = 'daily', $type = 'node') { | |
82 switch ($granularity) { | |
83 case 'hourly': | |
84 drupal_set_title(t('Past twelve hours')); | |
85 break; | |
86 case 'daily': | |
87 drupal_set_title(t('Past twelve days')); | |
88 break; | |
89 case 'weekly': | |
90 drupal_set_title(t('Past twelve weeks')); | |
91 break; | |
92 case 'monthly': | |
93 drupal_set_title(t('Past twelve months')); | |
94 break; | |
95 } | |
96 | |
97 switch ($type) { | |
98 case 'node': | |
99 if ($node->aid) { | |
100 $output = '<img src="'. url("ad_report/$node->nid/bargraph/$granularity/node") .'" />'; | |
101 $ad_link = module_invoke('ad_' . $node->adtype, 'display_ad', $node); | |
102 $output .= theme('box', $node->title, $ad_link); | |
103 } | |
104 break; | |
105 default: | |
106 $output = '<img src="'. url("ad_report/$node->uid/bargraph/$granularity/$type") .'" />'; | |
107 break; | |
108 } | |
109 return $output; | |
110 } | |
111 | |
112 /** | |
113 * Page that utilizes gd to generate a bargraph. | |
114 * | |
115 * TODO: Make this more dynamic, allowing to move through time, etc. | |
116 */ | |
117 function ad_report_generate_bargraph($node, $granularity = 'daily', $type = 'node') { | |
118 $id = $node->nid; | |
119 header("Content-type: image/png"); | |
120 | |
121 // Preperation. | |
122 $views = array(); | |
123 $max_views = 0; | |
124 $statistics = array(); | |
125 $clicks = array(); | |
126 $max_clicks = 0; | |
127 $time = time(); | |
128 | |
129 $increments = 12; | |
130 $end_add = 0; | |
131 switch ($granularity) { | |
132 case 'hourly': | |
133 $start_time = (60 * 60 * 11); | |
134 // Increment hourly. | |
135 $increment_time = (60 * 60); | |
136 | |
137 $format_start = 'YmdH'; | |
138 $format_end = 'YmdH'; | |
139 $format_end_append = ''; | |
140 $format_upper = 'M d'; | |
141 $format_lower = 'ga'; | |
142 $graph_height = 250; | |
143 break; | |
144 case 'daily': | |
145 default: | |
146 $start_time = (60 * 60 * 24 * 11); | |
147 // Increment daily. | |
148 $increment_time = (60 * 60 * 24); | |
149 | |
150 $format_start = 'Ymd00'; | |
151 $format_end = 'Ymd'; | |
152 $format_end_append = '24'; | |
153 $format_upper = 'D'; | |
154 $format_lower = 'M d'; | |
155 break; | |
156 case 'weekly': | |
157 $start_time = (60 * 60 * 24 * 7 * 11); | |
158 // Increment weekly. | |
159 $increment_time = (60 * 60 * 24 * 7); | |
160 | |
161 $format_start = 'Ymd00'; | |
162 $format_end = 'Ymd'; | |
163 $format_end_append = '24'; | |
164 $end_add = (60 * 60 * 24 * 6); | |
165 //$end_add = 600; | |
166 $format_upper = 'M d -'; | |
167 $format_lower = ''; | |
168 break; | |
169 case 'monthly': | |
170 $start_time = ((60 * 60 * 24 * 2) + (60 * 60 * 24 * 7 * 4)) * 11; | |
171 // Increment monthly (every 30 days). | |
172 $increment_time = (60 * 60 * 24 * 2) + (60 * 60 * 24 * 7 * 4); | |
173 | |
174 $format_start = 'Ymd00'; | |
175 $format_end = 'Ymd'; | |
176 $format_end_append = '24'; | |
177 $end_add = (60 * 60 * 24 * 29); | |
178 $format_upper = 'M d -'; | |
179 $format_lower = ''; | |
180 break; | |
181 } | |
182 | |
183 // Retrive data from database. | |
184 for ($i = $time - $start_time; $i <= $time; $i = $i + $increment_time) { | |
185 $day_start = date($format_start, $i); | |
186 $day_end = date($format_end, $i + $end_add). $format_end_append; | |
187 if ($type == 'node') { | |
188 $view = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $id, $day_start, $day_end)); | |
189 $click = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $id, $day_start, $day_end)); | |
190 } | |
191 else if ($type == 'user') { | |
192 $view = (int)db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE uid = %d AND type = 'ad' AND (action = 'view' OR action = 'count') AND date >= %d AND date <= %d", $id, $day_start, $day_end)); | |
193 $click = (int)db_result(db_query("SELECT SUM(a.count) FROM {ad_statistics} a LEFT JOIN {node} n ON a.aid = n.nid WHERE uid = %d AND type = 'ad' AND action = 'click' AND date >= %d AND date <= %d", $id, $day_start, $day_end)); | |
194 } | |
195 else { | |
196 $function = "ad_report_views_$type"; | |
197 if (function_exists("$function")) { | |
198 $view = $function($id, $day_start, $day_end); | |
199 } | |
200 $function = "ad_report_clicks_$type"; | |
201 if (function_exists("$function")) { | |
202 $click = $function($id, $day_start, $day_end); | |
203 } | |
204 } | |
205 if ($view > $max_views) { | |
206 $max_views = $view; | |
207 } | |
208 $statistics[] = array( | |
209 'upper' => date($format_upper, $i), | |
210 'lower' => date($format_lower, $i), | |
211 'views' => $view, | |
212 'clicks' => $click | |
213 ); | |
214 } | |
215 | |
216 // Build graph image. | |
217 $image_width = 50 * $increments + 1; | |
218 $image_height = 300; | |
219 $graph_width = 50 * $increments; | |
220 $graph_height = 250; | |
221 | |
222 $graph = imagecreate($image_width, $image_height); | |
223 | |
224 // Configure colors to use in chart. | |
225 $color = array( | |
226 'white' => imagecolorallocate($graph, 255, 255, 255), | |
227 'black' => imagecolorallocate($graph, 0, 0, 0), | |
228 'grey' => imagecolorallocate($graph, 192, 192, 192), | |
229 'blue' => imagecolorallocate($graph, 0, 0, 255), | |
230 'orange' => imagecolorallocate($graph, 220, 210, 60), | |
231 ); | |
232 | |
233 // Draw the outside edges of the graph. | |
234 imageline($graph, 0, 0, 0, $graph_height, $color['grey']); | |
235 imageline($graph, 0, 0, $graph_width, 0, $color['grey']); | |
236 imageline($graph, $graph_width - 1, 0, $graph_width - 1, $graph_height, $color['grey']); | |
237 imageline($graph, 0, $graph_height - 1, $graph_width - 1, $graph_height - 1, $color['grey']); | |
238 | |
239 // Draw a grid. | |
240 for ($i = 0; $i < ($increments + 1); $i++) { | |
241 imageline($graph, $i*50, 0, $i*50, $graph_height, $color['grey']); | |
242 } | |
243 for ($i = 0; $i < 11; $i++) { | |
244 imageline($graph, 0, $i*25, $graph_width, $i*25, $color['grey']); | |
245 } | |
246 | |
247 $multiply = 0; | |
248 if ($max_views > $graph_height) { | |
249 if (!$multiply) { | |
250 $multiply = .9; | |
251 } | |
252 while (($max_views * $multiply) >= $graph_height) { | |
253 $multiply *= .9; | |
254 } | |
255 } | |
256 else if ($max_views) { | |
257 while (($max_views * ($multiply + 1)) <= $graph_height) { | |
258 $multiply++; | |
259 } | |
260 } | |
261 | |
262 // Display impressions. | |
263 for ($i = 0; $i < $increments ; $i++) { | |
264 $view = $multiply ? $statistics[$i]['views'] * $multiply : $statistics[$i]['views']; | |
265 if ($view) { | |
266 imagefilledrectangle($graph, $i*50 + 4, $graph_height-$view, ($i+1)*50, $graph_height, $color['grey']); | |
267 $string_height = $view < 10 ? $graph_height - 10 : $graph_height - $view; | |
268 imagestring($graph, 2, $i*50 + 15, $string_height, $statistics[$i]['views'], $color['black']); | |
269 } | |
270 // Display timestamp | |
271 imagestring($graph, 2, $i*50 + 2, 255, $statistics[$i]['upper'], $color['black']); | |
272 imagestring($graph, 2, $i*50 + 3, 265, $statistics[$i]['lower'], $color['black']); | |
273 } | |
274 | |
275 // Display clicks. | |
276 for ($i = 0; $i < $increments; $i++) { | |
277 $click = $multiply ? $statistics[$i]['clicks'] * $multiply : $statistics[$i]['clicks']; | |
278 if ($click) { | |
279 imagefilledrectangle($graph, $i*50 + 10, $graph_height-$click, ($i+1)*50, $graph_height, $color['blue']); | |
280 $string_height = $click < 10 ? $graph_height - 10 : $graph_height - $click; | |
281 imagestring($graph, 2, $i*50 + 20, $string_height, $statistics[$i]['clicks'], $color['white']); | |
282 } | |
283 } | |
284 | |
285 imagepng($graph); | |
286 imagedestroy($graph); | |
287 | |
288 } | |
289 |