| 1 |
<?php
|
| 2 |
|
| 3 |
//$Id$
|
| 4 |
//$Name$
|
| 5 |
// Copyright 2005 Khalid Baheyeldin http://2bits.com
|
| 6 |
|
| 7 |
define(CLICK_IGNORE_SITE, 'click_ignore_site');
|
| 8 |
define(CLICK_GROUP, 'click_group_');
|
| 9 |
define(CLICK_CRON_TIMESTAMP_DAY, 'click_cron_timestamp_day');
|
| 10 |
define(CLICK_CRON_TIMESTAMP_WEEK, 'click_cron_timestamp_week');
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_help().
|
| 14 |
*/
|
| 15 |
function click_help($path, $arg) {
|
| 16 |
switch ($path) {
|
| 17 |
case 'admin/help#click':
|
| 18 |
$output = t('Track click thrus from newletters and ads on other sites');
|
| 19 |
break;
|
| 20 |
}
|
| 21 |
return $output;
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_perm().
|
| 26 |
*/
|
| 27 |
function click_perm() {
|
| 28 |
return array ('view click links', 'view click stats');
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_menu().
|
| 33 |
*/
|
| 34 |
function click_menu() {
|
| 35 |
$items = array();
|
| 36 |
|
| 37 |
$items['node/%click_nid/click'] = array(
|
| 38 |
'title' => 'Clicks',
|
| 39 |
'page callback' => 'click_view',
|
| 40 |
'page arguments' => array(1),
|
| 41 |
'access arguments' => array('view click stats'),
|
| 42 |
'weight' => 10,
|
| 43 |
'type' => MENU_LOCAL_TASK,
|
| 44 |
);
|
| 45 |
$items['click/%click_nid/%click_gid'] = array(
|
| 46 |
'page callback' => 'click_do',
|
| 47 |
'page arguments' => array(1, 2),
|
| 48 |
'access arguments' => array('access content'),
|
| 49 |
'type' => MENU_CALLBACK,
|
| 50 |
);
|
| 51 |
$items['admin/settings/click'] = array(
|
| 52 |
'title' => 'Click Thru tracking',
|
| 53 |
'description' => 'Settings of the Click Thru tracking module',
|
| 54 |
'page callback' => 'drupal_get_form',
|
| 55 |
'page arguments' => array('click_admin_settings'),
|
| 56 |
'access arguments' => array('administer site configuration'),
|
| 57 |
'type' => MENU_NORMAL_ITEM,
|
| 58 |
);
|
| 59 |
$items['admin/reports/click'] = array(
|
| 60 |
'title' => 'Click report',
|
| 61 |
'page callback' => 'click_report',
|
| 62 |
'access arguments' => array('view click stats'),
|
| 63 |
'type' => MENU_NORMAL_ITEM,
|
| 64 |
);
|
| 65 |
|
| 66 |
return $items;
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Menu callback; presents the settings form for click
|
| 71 |
*/
|
| 72 |
function click_admin_settings() {
|
| 73 |
global $user;
|
| 74 |
$click_group = CLICK_GROUP . $user->uid;
|
| 75 |
|
| 76 |
$form[$click_group] = array(
|
| 77 |
'#type' => 'select',
|
| 78 |
'#title' => t('Active click group'),
|
| 79 |
'#default_value' => variable_get($click_group, 0),
|
| 80 |
'#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)),
|
| 81 |
'#description' => t('Current active click group for this user'),
|
| 82 |
'#extra' => 0,
|
| 83 |
'#multiple' => FALSE,
|
| 84 |
'#required' => TRUE,
|
| 85 |
);
|
| 86 |
|
| 87 |
$form[CLICK_IGNORE_SITE] = array(
|
| 88 |
'#type' => 'select',
|
| 89 |
'#title' => t('Ignore clicks from own site'),
|
| 90 |
'#default_value' => variable_get(CLICK_IGNORE_SITE, 0),
|
| 91 |
'#options' => array('No', 'Yes'),
|
| 92 |
'#description' => t('Whether to ignore clicks from your own site, or treat them as external ones'),
|
| 93 |
'#extra' => 0,
|
| 94 |
'#multiple' => FALSE,
|
| 95 |
'#required' => TRUE,
|
| 96 |
);
|
| 97 |
|
| 98 |
return system_settings_form($form);
|
| 99 |
}
|
| 100 |
|
| 101 |
function click_link($type, $node = null, $teaser = false) {
|
| 102 |
global $user;
|
| 103 |
$group = CLICK_GROUP . $user->uid;
|
| 104 |
|
| 105 |
$links = array();
|
| 106 |
|
| 107 |
$click_group = variable_get($group, 0);
|
| 108 |
|
| 109 |
if (user_access('view click links')) {
|
| 110 |
$links['click_link'] = array(
|
| 111 |
'title' => t('click link'),
|
| 112 |
'href' => "click/$node->nid/$click_group",
|
| 113 |
'attributes' => array('title' => t('Link to track click thrus.')),
|
| 114 |
);
|
| 115 |
}
|
| 116 |
|
| 117 |
if (user_access('view click stats')) {
|
| 118 |
$clicks = _click_count($node->nid);
|
| 119 |
if ($clicks > 0) {
|
| 120 |
$links['click_link_stats'] = array(
|
| 121 |
'title' => t('!num clicks', array('!num' => $clicks)),
|
| 122 |
);
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
return $links;
|
| 127 |
}
|
| 128 |
|
| 129 |
function click_view($nid) {
|
| 130 |
$fmt = 'Y-m-d H:i:s';
|
| 131 |
|
| 132 |
$result = db_query('SELECT * FROM {clicks} WHERE nid = %d ORDER BY gid ASC', $nid);
|
| 133 |
while ($click = db_fetch_object($result)) {
|
| 134 |
$group = "group: $click->gid";
|
| 135 |
$rows = array();
|
| 136 |
$rows[] = array(array('data' => t('clicks today')), array('data' => $click->day_clicks));
|
| 137 |
$rows[] = array(array('data' => t('clicks this week')), array('data' => $click->week_clicks));
|
| 138 |
$rows[] = array(array('data' => t('clicks total')), array('data' => $click->total_clicks));
|
| 139 |
$rows[] = array(array('data' => t('First click at: ')), array('data' => format_date($click->start, 'custom', $fmt)));
|
| 140 |
$rows[] = array(array('data' => ('Last click at: ')), array('data' => format_date($click->last, 'custom', $fmt)));
|
| 141 |
$output .= $group . theme('table', array(), $rows);
|
| 142 |
}
|
| 143 |
return theme('page', $output);
|
| 144 |
}
|
| 145 |
|
| 146 |
function click_report() {
|
| 147 |
$header = array(
|
| 148 |
array('data' => t('group'), 'field' => 'gid'),
|
| 149 |
array('data' => t('node title'), 'field' => 'title'),
|
| 150 |
array('data' => t('day clicks'), 'field' => 'day_clicks', 'sort' => 'desc'),
|
| 151 |
array('data' => t('week clicks'), 'field' => 'week_clicks'),
|
| 152 |
array('data' => t('total clicks'), 'field' => 'total_clicks')
|
| 153 |
);
|
| 154 |
|
| 155 |
$sql = 'SELECT n.title, c.* FROM {clicks} c, {node} n WHERE c.nid = n.nid' . tablesort_sql($header);
|
| 156 |
$result = pager_query($sql, 50);
|
| 157 |
while ($click = db_fetch_object($result)) {
|
| 158 |
$rows[] = array(
|
| 159 |
array('data' => $click->gid),
|
| 160 |
array('data' => l($click->title, "node/$click->nid/click")),
|
| 161 |
array('data' => $click->day_clicks),
|
| 162 |
array('data' => $click->week_clicks),
|
| 163 |
array('data' => $click->total_clicks)
|
| 164 |
);
|
| 165 |
}
|
| 166 |
if (!$rows) {
|
| 167 |
$rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
|
| 168 |
}
|
| 169 |
|
| 170 |
$pager = theme('pager', NULL, 50, 0);
|
| 171 |
|
| 172 |
if (!empty($pager)) {
|
| 173 |
$rows[] = array(array('data' => $pager, 'colspan' => '4'));
|
| 174 |
}
|
| 175 |
|
| 176 |
return theme('page', theme('table', $header, $rows), t('Click Thru Report'));
|
| 177 |
}
|
| 178 |
|
| 179 |
function click_do($nid, $group) {
|
| 180 |
global $base_url;
|
| 181 |
|
| 182 |
$path = "node/$nid";
|
| 183 |
$ignore = variable_get(CLICK_IGNORE_SITE, 0);
|
| 184 |
$referer = $_SERVER['HTTP_REFERER'];
|
| 185 |
if ($ignore && stristr($referer, $base_url)) {
|
| 186 |
// ignore clicks from the same domain
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
click_insert_update($nid, $group);
|
| 190 |
}
|
| 191 |
|
| 192 |
// Redirect to the real url
|
| 193 |
drupal_goto($path);
|
| 194 |
}
|
| 195 |
|
| 196 |
function click_insert_update($nid, $group = 0) {
|
| 197 |
$num = db_result(db_query('SELECT nid FROM {clicks} WHERE nid = %d AND gid = %d', $nid, $group));
|
| 198 |
if ($num > 0) {
|
| 199 |
db_query('UPDATE {clicks} SET day_clicks = day_clicks + 1,
|
| 200 |
week_clicks = week_clicks + 1, total_clicks = total_clicks + 1,
|
| 201 |
last = %d WHERE nid = %d AND gid = %d', time(), $nid, $group);
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
db_query('INSERT INTO {clicks} VALUES (%d, %d, %d, %d, %d, %d, %d)',
|
| 205 |
$group, $nid, 1, 1, 1, time(), time());
|
| 206 |
}
|
| 207 |
}
|
| 208 |
|
| 209 |
function _click_count($nid) {
|
| 210 |
// Total clicks
|
| 211 |
return db_result(db_query('SELECT SUM(total_clicks) FROM {clicks} WHERE nid = %d', $nid));
|
| 212 |
}
|
| 213 |
|
| 214 |
function click_cron() {
|
| 215 |
// 24 hours * 60 minutes * 60 seconds - 5 minutes
|
| 216 |
if ((time() - variable_get(CLICK_CRON_TIMESTAMP_DAY, 0)) >= 86100) {
|
| 217 |
variable_set(CLICK_CRON_TIMESTAMP_DAY, time());
|
| 218 |
db_query('UPDATE {clicks} SET day_clicks = 0');
|
| 219 |
}
|
| 220 |
|
| 221 |
// 7 days * 24 hours * 60 minutes * 60 seconds - 5 minutes
|
| 222 |
if ((time() - variable_get(CLICK_CRON_TIMESTAMP_WEEK, 0)) >= 604500) {
|
| 223 |
variable_set(CLICK_CRON_TIMESTAMP_WEEK, time());
|
| 224 |
db_query('UPDATE {clicks} SET week_clicks = 0');
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
function click_nid_load($arg) {
|
| 229 |
return (is_numeric($arg) ? $arg : FALSE);
|
| 230 |
}
|
| 231 |
|
| 232 |
function click_gid_load($arg) {
|
| 233 |
return (is_numeric($arg) ? $arg : FALSE);
|
| 234 |
}
|