pierre@0: . pierre@0: */ pierre@0: pierre@0: /** pierre@0: * Implementation of hook_menu(). pierre@0: */ pierre@0: function ad_report_menu() { pierre@0: $items = array(); pierre@0: pierre@0: $items['node/%node/report'] = array( pierre@0: 'title' => 'Reports', pierre@0: 'page callback' => 'ad_report_bargraph', pierre@0: 'page arguments' => array(1), pierre@0: 'type' => MENU_LOCAL_TASK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: $items['node/%node/report/monthly'] = array( pierre@0: 'title' => 'Monthly Reports', pierre@0: 'page callback' => 'ad_report_bargraph', pierre@0: 'page arguments' => array(1,3), pierre@0: 'type' => MENU_LOCAL_TASK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: $items['node/%node/report/weekly'] = array( pierre@0: 'title' => 'Weekly Reports', pierre@0: 'page callback' => 'ad_report_bargraph', pierre@0: 'page arguments' => array(1,3), pierre@0: 'type' => MENU_LOCAL_TASK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: $items['node/%node/report/daily'] = array( pierre@0: 'title' => 'Daily Reports', pierre@0: 'page callback' => 'ad_report_bargraph', pierre@0: 'page arguments' => array(1,3), pierre@0: 'type' => MENU_DEFAULT_LOCAL_TASK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: $items['node/%node/report/hourly'] = array( pierre@0: 'title' => 'Hourly Reports', pierre@0: 'page callback' => 'ad_report_bargraph', pierre@0: 'page arguments' => array(1,3), pierre@0: 'type' => MENU_LOCAL_TASK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: $items['ad_report/%node/bargraph'] = array( pierre@0: 'title' => 'Bar graph', pierre@0: 'page callback' => 'ad_report_generate_bargraph', pierre@0: 'page arguments' => array(1), pierre@0: 'type' => MENU_CALLBACK, pierre@0: 'access callback' => 'ad_report_access', pierre@0: 'access arguments' => array(1), pierre@0: ); pierre@0: pierre@0: return $items; pierre@0: } pierre@0: /** pierre@0: * Implementation of access callback. pierre@0: * pierre@0: * @param mixed $node pierre@0: * Ad object. pierre@0: */ pierre@0: function ad_report_access($node){ pierre@0: return ($node->type == 'ad') && ad_adaccess($node, 'access statistics'); pierre@0: } pierre@0: /** pierre@0: * Page to display ad with bargraph. pierre@0: */ pierre@0: function ad_report_bargraph($node, $granularity = 'daily', $type = 'node') { pierre@0: switch ($granularity) { pierre@0: case 'hourly': pierre@0: drupal_set_title(t('Past twelve hours')); pierre@0: break; pierre@0: case 'daily': pierre@0: drupal_set_title(t('Past twelve days')); pierre@0: break; pierre@0: case 'weekly': pierre@0: drupal_set_title(t('Past twelve weeks')); pierre@0: break; pierre@0: case 'monthly': pierre@0: drupal_set_title(t('Past twelve months')); pierre@0: break; pierre@0: } pierre@0: pierre@0: switch ($type) { pierre@0: case 'node': pierre@0: if ($node->aid) { pierre@0: $output = 'nid/bargraph/$granularity/node") .'" />'; pierre@0: $ad_link = module_invoke('ad_' . $node->adtype, 'display_ad', $node); pierre@0: $output .= theme('box', $node->title, $ad_link); pierre@0: } pierre@0: break; pierre@0: default: pierre@0: $output = 'uid/bargraph/$granularity/$type") .'" />'; pierre@0: break; pierre@0: } pierre@0: return $output; pierre@0: } pierre@0: pierre@0: /** pierre@0: * Page that utilizes gd to generate a bargraph. pierre@0: * pierre@0: * TODO: Make this more dynamic, allowing to move through time, etc. pierre@0: */ pierre@0: function ad_report_generate_bargraph($node, $granularity = 'daily', $type = 'node') { pierre@0: $id = $node->nid; pierre@0: header("Content-type: image/png"); pierre@0: pierre@0: // Preperation. pierre@0: $views = array(); pierre@0: $max_views = 0; pierre@0: $statistics = array(); pierre@0: $clicks = array(); pierre@0: $max_clicks = 0; pierre@0: $time = time(); pierre@0: pierre@0: $increments = 12; pierre@0: $end_add = 0; pierre@0: switch ($granularity) { pierre@0: case 'hourly': pierre@0: $start_time = (60 * 60 * 11); pierre@0: // Increment hourly. pierre@0: $increment_time = (60 * 60); pierre@0: pierre@0: $format_start = 'YmdH'; pierre@0: $format_end = 'YmdH'; pierre@0: $format_end_append = ''; pierre@0: $format_upper = 'M d'; pierre@0: $format_lower = 'ga'; pierre@0: $graph_height = 250; pierre@0: break; pierre@0: case 'daily': pierre@0: default: pierre@0: $start_time = (60 * 60 * 24 * 11); pierre@0: // Increment daily. pierre@0: $increment_time = (60 * 60 * 24); pierre@0: pierre@0: $format_start = 'Ymd00'; pierre@0: $format_end = 'Ymd'; pierre@0: $format_end_append = '24'; pierre@0: $format_upper = 'D'; pierre@0: $format_lower = 'M d'; pierre@0: break; pierre@0: case 'weekly': pierre@0: $start_time = (60 * 60 * 24 * 7 * 11); pierre@0: // Increment weekly. pierre@0: $increment_time = (60 * 60 * 24 * 7); pierre@0: pierre@0: $format_start = 'Ymd00'; pierre@0: $format_end = 'Ymd'; pierre@0: $format_end_append = '24'; pierre@0: $end_add = (60 * 60 * 24 * 6); pierre@0: //$end_add = 600; pierre@0: $format_upper = 'M d -'; pierre@0: $format_lower = ''; pierre@0: break; pierre@0: case 'monthly': pierre@0: $start_time = ((60 * 60 * 24 * 2) + (60 * 60 * 24 * 7 * 4)) * 11; pierre@0: // Increment monthly (every 30 days). pierre@0: $increment_time = (60 * 60 * 24 * 2) + (60 * 60 * 24 * 7 * 4); pierre@0: pierre@0: $format_start = 'Ymd00'; pierre@0: $format_end = 'Ymd'; pierre@0: $format_end_append = '24'; pierre@0: $end_add = (60 * 60 * 24 * 29); pierre@0: $format_upper = 'M d -'; pierre@0: $format_lower = ''; pierre@0: break; pierre@0: } pierre@0: pierre@0: // Retrive data from database. pierre@0: for ($i = $time - $start_time; $i <= $time; $i = $i + $increment_time) { pierre@0: $day_start = date($format_start, $i); pierre@0: $day_end = date($format_end, $i + $end_add). $format_end_append; pierre@0: if ($type == 'node') { pierre@0: $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)); pierre@0: $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)); pierre@0: } pierre@0: else if ($type == 'user') { pierre@0: $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)); pierre@0: $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)); pierre@0: } pierre@0: else { pierre@0: $function = "ad_report_views_$type"; pierre@0: if (function_exists("$function")) { pierre@0: $view = $function($id, $day_start, $day_end); pierre@0: } pierre@0: $function = "ad_report_clicks_$type"; pierre@0: if (function_exists("$function")) { pierre@0: $click = $function($id, $day_start, $day_end); pierre@0: } pierre@0: } pierre@0: if ($view > $max_views) { pierre@0: $max_views = $view; pierre@0: } pierre@0: $statistics[] = array( pierre@0: 'upper' => date($format_upper, $i), pierre@0: 'lower' => date($format_lower, $i), pierre@0: 'views' => $view, pierre@0: 'clicks' => $click pierre@0: ); pierre@0: } pierre@0: pierre@0: // Build graph image. pierre@0: $image_width = 50 * $increments + 1; pierre@0: $image_height = 300; pierre@0: $graph_width = 50 * $increments; pierre@0: $graph_height = 250; pierre@0: pierre@0: $graph = imagecreate($image_width, $image_height); pierre@0: pierre@0: // Configure colors to use in chart. pierre@0: $color = array( pierre@0: 'white' => imagecolorallocate($graph, 255, 255, 255), pierre@0: 'black' => imagecolorallocate($graph, 0, 0, 0), pierre@0: 'grey' => imagecolorallocate($graph, 192, 192, 192), pierre@0: 'blue' => imagecolorallocate($graph, 0, 0, 255), pierre@0: 'orange' => imagecolorallocate($graph, 220, 210, 60), pierre@0: ); pierre@0: pierre@0: // Draw the outside edges of the graph. pierre@0: imageline($graph, 0, 0, 0, $graph_height, $color['grey']); pierre@0: imageline($graph, 0, 0, $graph_width, 0, $color['grey']); pierre@0: imageline($graph, $graph_width - 1, 0, $graph_width - 1, $graph_height, $color['grey']); pierre@0: imageline($graph, 0, $graph_height - 1, $graph_width - 1, $graph_height - 1, $color['grey']); pierre@0: pierre@0: // Draw a grid. pierre@0: for ($i = 0; $i < ($increments + 1); $i++) { pierre@0: imageline($graph, $i*50, 0, $i*50, $graph_height, $color['grey']); pierre@0: } pierre@0: for ($i = 0; $i < 11; $i++) { pierre@0: imageline($graph, 0, $i*25, $graph_width, $i*25, $color['grey']); pierre@0: } pierre@0: pierre@0: $multiply = 0; pierre@0: if ($max_views > $graph_height) { pierre@0: if (!$multiply) { pierre@0: $multiply = .9; pierre@0: } pierre@0: while (($max_views * $multiply) >= $graph_height) { pierre@0: $multiply *= .9; pierre@0: } pierre@0: } pierre@0: else if ($max_views) { pierre@0: while (($max_views * ($multiply + 1)) <= $graph_height) { pierre@0: $multiply++; pierre@0: } pierre@0: } pierre@0: pierre@0: // Display impressions. pierre@0: for ($i = 0; $i < $increments ; $i++) { pierre@0: $view = $multiply ? $statistics[$i]['views'] * $multiply : $statistics[$i]['views']; pierre@0: if ($view) { pierre@0: imagefilledrectangle($graph, $i*50 + 4, $graph_height-$view, ($i+1)*50, $graph_height, $color['grey']); pierre@0: $string_height = $view < 10 ? $graph_height - 10 : $graph_height - $view; pierre@0: imagestring($graph, 2, $i*50 + 15, $string_height, $statistics[$i]['views'], $color['black']); pierre@0: } pierre@0: // Display timestamp pierre@0: imagestring($graph, 2, $i*50 + 2, 255, $statistics[$i]['upper'], $color['black']); pierre@0: imagestring($graph, 2, $i*50 + 3, 265, $statistics[$i]['lower'], $color['black']); pierre@0: } pierre@0: pierre@0: // Display clicks. pierre@0: for ($i = 0; $i < $increments; $i++) { pierre@0: $click = $multiply ? $statistics[$i]['clicks'] * $multiply : $statistics[$i]['clicks']; pierre@0: if ($click) { pierre@0: imagefilledrectangle($graph, $i*50 + 10, $graph_height-$click, ($i+1)*50, $graph_height, $color['blue']); pierre@0: $string_height = $click < 10 ? $graph_height - 10 : $graph_height - $click; pierre@0: imagestring($graph, 2, $i*50 + 20, $string_height, $statistics[$i]['clicks'], $color['white']); pierre@0: } pierre@0: } pierre@0: pierre@0: imagepng($graph); pierre@0: imagedestroy($graph); pierre@0: pierre@0: } pierre@0: