statistics.module 21.3 KB
Newer Older
1
2
3
<?php
// $Id$

Dries's avatar
   
Dries committed
4
5
6
7
8
/**
 * @file
 * Logs access statistics for your site.
 */

Dries's avatar
   
Dries committed
9
10
11
/**
 * Implementation of hook_help().
 */
Dries's avatar
   
Dries committed
12
function statistics_help($section) {
Dries's avatar
   
Dries committed
13
14
  switch ($section) {
    case 'admin/help#statistics':
Dries's avatar
   
Dries committed
15
      return t("
Dries's avatar
   
Dries committed
16
      <h3>Introduction</h3>
Dries's avatar
   
Dries committed
17
      <p>The statistics module keeps track of numerous statistics for your site but be warned, statistical collection does cause a little overhead, thus everything comes <strong>disabled</strong> by default.</p>
Dries's avatar
   
Dries committed
18
19
20
      <p>The module counts how many times, and from where -- using HTTP referrer -- each of your posts is viewed. Once we have that count the module can do the following with it:
      <ul>
      <li>The count can be displayed in the node's link section next to \"# comments\".</li>
21
      <li>A configurable block can be added which can display a configurable number of the day's top stories, the all time top stories, and the last stories read.</li>
Dries's avatar
   
Dries committed
22
23
24
25
26
27
28
29
30
      <li>A configurable user page can be added, which can display the day's top stories, the all time top stories, and the last stories read.  You can individually configure how many posts are displayed in each section.</li>
      </ul>
      <p>Notes on using the statistics:</p>
      <ul>
      <li>If you enable the view counters for content, this adds 1 database query for each node that is viewed (2 queries if it's the first time the node has ever been viewed).</li>
      <li>If you enable the access log, this adds 1 database query for each page that Drupal displays.  Logged information includes:  HTTP referrer (if any), node being accessed (if any), user ID (if any), the IP address of the user, and the time the page was viewed.</li>
      </ul>
      <p>As with any new module, the statistics module needs to be <a href=\"%modules\">enabled</a> before you can use it.  Also refer to the <a href=\"%permissions\">permissions section</a>, as this module supports four separate permissions.</p>
      <h3>Configuring the statistics module</h3>
31
      <p>There are some configuration options added to the main <a href=\"%configuration\">administer &raquo; settings &raquo; statistics</a> section:</p>
Dries's avatar
   
Dries committed
32
      <ul>
33
      <li><em>enable access log</em> -- allows you to turn the access log on and off.  This log is used to store data about every page accessed, such as the remote host's IP address, where they came from (referrer), what node they've viewed, and their user name.  Enabling the log adds one database call per page displayed by Drupal.</li>
Dries's avatar
   
Dries committed
34
35
      <li><em>discard access logs older than</em> -- allows you to configure how long an access log entry is saved, after which time it is deleted from the database table. To use this you need to run \"cron.php\"</li>
      <li><em>enable node view counter</em> -- allows you to turn on and off the node-counting functionality of this module.  If it is turned on, an extra database query is added for each node displayed, which increments a counter.</li>
36
      <li><em>display node view counters</em> -- allows you to globally disable the displaying of node view counters.</li>
Dries's avatar
   
Dries committed
37
38
39
      </ul>
      <h3>Popular content block</h3>
      <p>This module creates a block that can display the day's top viewed content, the all time top viewed content, and the last content viewed.  Each of these links can be enabled or disabled individually, and the number of posts displayed for each can be configured with a drop down menu.  If you disable all sections of this block, it will not appear.</p>
40
41
      <p>Don't forget to <a href=\"%block\">enable the block</a>.</p>",
      array('%modules' => url('admin/modules'), '%permissions' => url('admin/user/configure/permission'), '%referer' => url('admin/logs/referrers'), '%configuration' => url('admin/settings/statistics'), '%block' => url('admin/block')));
Dries's avatar
   
Dries committed
42
    case 'admin/modules#description':
Dries's avatar
   
Dries committed
43
      return t('Logs access statistics for your site.');
Dries's avatar
   
Dries committed
44
    case 'admin/settings/statistics':
45
      return t('Settings for the statistical information that Drupal will keep about the site. See <a href="%statistics">site statistics</a> for the actual information.', array('%statistics' => url('admin/logs/hits')));
46
47
    case 'admin/logs/hits':
      return t('This page shows you the most recent hits.');
Dries's avatar
   
Dries committed
48
    case 'admin/logs/referrers':
49
      return t('This page shows you all external referrers. These are links pointing to your web site from outside your web site.');
Dries's avatar
   
Dries committed
50
51
52
  }
}

Dries's avatar
   
Dries committed
53
54
55
56
57
/**
 * Implementation of hook_exit().
 *
 * This is where statistics are gathered on page accesses.
 */
58
function statistics_exit() {
Dries's avatar
   
Dries committed
59
  global $user, $recent_activity;
60

Dries's avatar
   
Dries committed
61
62
  if (variable_get('statistics_count_content_views', 0)) {
    // We are counting content views.
63
    if ((arg(0) == 'node') && arg(1)) {
Dries's avatar
   
Dries committed
64
      // A node has been viewed, so update the node's counters.
65
      db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1));
Dries's avatar
   
Dries committed
66
      // If we affected 0 rows, this is the first time viewing the node.
67
      if (!db_affected_rows()) {
Dries's avatar
   
Dries committed
68
        // We must create a new row to store counters for the new node.
69
        db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES(%d, 1, 1, %d)', arg(1), time());
70
71
72
      }
    }
  }
73
  if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) {
Dries's avatar
   
Dries committed
74
    // Statistical logs are enabled.
75
    $referrer = referer_uri();
Dries's avatar
   
Dries committed
76
77
    $hostname = $_SERVER['REMOTE_ADDR'];

Dries's avatar
   
Dries committed
78
    // Log this page access.
79
    db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, timestamp) values('%s', '%s', '%s', '%s', %d, %d)", drupal_get_title(), $_GET['q'], $referrer, $hostname, $user->uid, time());
80
81
82
  }
}

Dries's avatar
   
Dries committed
83
84
85
/**
 * Implementation of hook_perm().
 */
86
function statistics_perm() {
87
  return array('access statistics');
88
89
}

Dries's avatar
   
Dries committed
90
91
92
/**
 * Implementation of hook_link().
 */
93
94
95
96
97
function statistics_link($type, $node = 0, $main = 0) {
  global $id;

  $links = array();

98
  if ($type != 'comment' && variable_get('statistics_display_counter', 0)) {
99
100
    $statistics = statistics_get($node->nid);
    if ($statistics) {
101
      $links[] = format_plural($statistics['totalcount'], '1 read', '%count reads');
102
103
104
105
106
    }
  }
  return $links;
}

Dries's avatar
   
Dries committed
107
108
109
/**
 * Implementation of hook_menu().
 */
Dries's avatar
   
Dries committed
110
function statistics_menu($may_cache) {
Dries's avatar
   
Dries committed
111
  $items = array();
Dries's avatar
   
Dries committed
112

113
  $access = user_access('access statistics');
Dries's avatar
   
Dries committed
114
  if ($may_cache) {
115
116
    $items[] = array('path' => 'admin/logs/hits', 'title' => t('recent hits'),
      'callback' => 'statistics_recent_hits', 'access' => $access,
Dries's avatar
   
Dries committed
117
      'weight' => 3);
118
119
    $items[] = array('path' => 'admin/logs/pages', 'title' => t('top pages'),
      'callback' => 'statistics_top_pages', 'access' => $access,
Dries's avatar
   
Dries committed
120
      'weight' => 1);
121
    $items[] = array('path' => 'admin/logs/users', 'title' => t('top users'),
Dries's avatar
   
Dries committed
122
123
      'callback' => 'statistics_top_users', 'access' => $access,
      'weight' => 2);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    $items[] = array('path' => 'admin/logs/referrers', 'title' => t('referrers'),
      'callback' => 'statistics_top_referrers', 'access' => $access);
    $items[] = array('path' => 'admin/logs/access', 'title' => t('details'),
      'callback' => 'statistics_access_log', 'access' => $access,
      'type' => MENU_CALLBACK);
  }
  else {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      $items[] = array('path' => 'user/'. arg(1) .'/track/navigation', 'title' => t('track page visits'),
        'callback' => 'statistics_user_tracker', 'access' => $access,
        'type' => MENU_LOCAL_TASK, 'weight' => 2);
    }
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $items[] = array('path' => 'node/'. arg(1) .'/track', 'title' => t('track'),
        'callback' => 'statistics_node_tracker', 'access' => $access,
        'type' => MENU_LOCAL_TASK, 'weight' => 2);
    }
Dries's avatar
   
Dries committed
141
142
  }

Dries's avatar
   
Dries committed
143
144
145
  return $items;
}

146
147
148
149
150
151
152
153
154
function statistics_access_log($aid) {
  $result = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = %d', $aid);
  if ($access = db_fetch_object($result)) {
    $output  = '<table border="1" cellpadding="2" cellspacing="2">';
    $output .= ' <tr><th>'. t('Page URL') ."</th><td>". l(url($access->path, NULL, NULL, TRUE), $access->url) ."</td></tr>";
    $output .= ' <tr><th>'. t('Page title') ."</th><td>$access->title</td></tr>";
    $output .= ' <tr><th>'. t('Referrer') ."</th><td>". ($access->url ? l($access->url, $access->url) : '') ."</td></tr>";
    $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($access->timestamp, 'large') .'</td></tr>';
    $output .= ' <tr><th>'. t('User') .'</th><td>'. format_name($access) .'</td></tr>';
Dries's avatar
Dries committed
155
    $output .= ' <tr><th>'. t('Hostname') ."</th><td>$access->hostname</td></tr>";
156
157
158
159
160
    $output .= '</table>';
    print theme('page', $output);
  }
  else {
    drupal_not_found();
161
  }
162
}
163

164
165
166
167
168
169
170
171
172
function statistics_node_tracker() {
  if ($node = node_load(array('nid' => arg(1)))) {

    $header = array(
        array('data' => t('Time'), 'field' => 'a.timestamp', 'sort' => 'desc'),
        array('data' => t('Referrer'), 'field' => 'a.url'),
        array('data' => t('User'), 'field' => 'u.name'),
        array('data' => t('Operations')));

Steven Wittens's avatar
Steven Wittens committed
173
    $result = pager_query('SELECT a.aid, a.timestamp, a.url, a.uid, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE a.path LIKE \'node/%d%%\'' . tablesort_sql($header), 30, 0, NULL, $node->nid);
174
175
176
    while ($log = db_fetch_object($result)) {
      $rows[] = array(
        array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'),
Dries's avatar
Dries committed
177
        l(_statistics_column_width($log->url), $log->url),
178
179
180
        format_name($log),
        l(t('details'), "admin/logs/access/$log->aid"));
    }
Dries's avatar
   
Dries committed
181

182
183
184
    if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
      $rows[] = array(array('data' => $pager, 'colspan' => '4'));
    }
Dries's avatar
   
Dries committed
185

186
187
    drupal_set_title($node->title);
    print theme('page', theme('table', $header, $rows));
188
189
190
191
192
  }
  else {
    drupal_not_found();
  }
}
193

194
195
function statistics_user_tracker() {
  if ($account = user_load(array('uid' => arg(1)))) {
Dries's avatar
   
Dries committed
196

197
198
199
200
    $header = array(
        array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
        array('data' => t('Page'), 'field' => 'path'),
        array('data' => t('Operations')));
201

202
    $result = pager_query('SELECT aid, timestamp, path, title FROM {accesslog} WHERE uid = %d' . tablesort_sql($header), 30, 0, NULL, $account->uid);
203
204
205
    while ($log = db_fetch_object($result)) {
      $rows[] = array(
        array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'),
Dries's avatar
   
Dries committed
206
        _statistics_format_item($log->title, $log->path),
207
208
        l(t('details'), "admin/logs/access/$log->aid"));
    }
209

210
211
212
    if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
      $rows[] = array(array('data' => $pager, 'colspan' => '3'));
    }
213

214
215
    drupal_set_title($account->name);
    print theme('page', theme('table', $header, $rows));
216
217
218
219
  }
  else {
    drupal_not_found();
  }
Dries's avatar
   
Dries committed
220
221
222
}

/**
223
 * Menu callback; presents the "Recent hits" page.
Dries's avatar
   
Dries committed
224
 */
225
function statistics_recent_hits($type = 'all', $id = 0) {
Dries's avatar
   
Dries committed
226
  $header = array(
227
228
229
    array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
    array('data' => t('Page'), 'field' => 'a.path'),
    array('data' => t('User'), 'field' => 'u.name'),
Dries's avatar
   
Dries committed
230
    array('data' => t('Operations'))
Dries's avatar
   
Dries committed
231
232
  );

233
234
235
236
237
238
  $sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . tablesort_sql($header);

  $result = pager_query($sql, 30);
  while ($log = db_fetch_object($result)) {
    $rows[] = array(
      array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'),
Dries's avatar
   
Dries committed
239
      _statistics_format_item($log->title, $log->path),
240
241
      format_name($log),
      l(t('details'), "admin/logs/access/$log->aid"));
Dries's avatar
   
Dries committed
242
  }
243
244
245

  if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
    $rows[] = array(array('data' => $pager, 'colspan' => '4'));
Dries's avatar
   
Dries committed
246
247
  }

248
  print theme('page', theme('table', $header, $rows));
Dries's avatar
   
Dries committed
249
250
251
}

/**
252
 * Menu callback; presents the "Top pages" page.
Dries's avatar
   
Dries committed
253
 */
254
255
256
function statistics_top_pages() {
  $sql = "SELECT COUNT(path) AS hits, path, title FROM {accesslog} GROUP BY path";
  $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}";
Dries's avatar
   
Dries committed
257
258

  $header = array(
Dries's avatar
   
Dries committed
259
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
260
    array('data' => t('Page'), 'field' => 'path')
Dries's avatar
   
Dries committed
261
262
  );
  $sql .= tablesort_sql($header);
263
  $result = pager_query($sql, 30, 0, $sql_cnt);
Dries's avatar
   
Dries committed
264

265
  while ($page = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
266
    $rows[] = array($page->hits, _statistics_format_item($page->title, $page->path));
Dries's avatar
   
Dries committed
267
  }
268
269
  if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
    $rows[] = array(array('data' => $pager, 'colspan' => '2'));
Dries's avatar
   
Dries committed
270
271
  }

272
273
  drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
  print theme('page', theme('table', $header, $rows));
Dries's avatar
   
Dries committed
274
275
276
}

/**
277
 * Menu callback; presents the "Top users" page.
Dries's avatar
   
Dries committed
278
 */
279
function statistics_top_users() {
Dries's avatar
   
Dries committed
280
281

  $header = array(
Dries's avatar
   
Dries committed
282
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
283
    array('data' => t('User'), 'field' => 'u.name'),
Dries's avatar
   
Dries committed
284
285
  );

286
287
288
289
290
291
  $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.uid". tablesort_sql($header);
  $sql_cnt = "SELECT COUNT(DISTINCT(uid)) FROM {accesslog}";
  $result = pager_query($sql, 30, 0, $sql_cnt);

  while ($account = db_fetch_object($result)) {
    $rows[] = array($account->hits, format_name($account));
Dries's avatar
   
Dries committed
292
  }
293
294
295

  if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
    $rows[] = array(array('data' => $pager, 'colspan' => '2'));
Dries's avatar
   
Dries committed
296
297
  }

298
299
  drupal_set_title(t('Top users in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
  print theme('page', theme('table', $header, $rows));
300
301
}

Dries's avatar
   
Dries committed
302
/**
Dries's avatar
Dries committed
303
 * Menu callback; presents the "Top referrers" page.
Dries's avatar
   
Dries committed
304
 */
305
function statistics_top_referrers() {
306
307
  $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog} WHERE url NOT LIKE '%%%s%%' AND url <> '' GROUP BY url";
  $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%%%s%%'";
308
  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))));
309

Dries's avatar
   
Dries committed
310
  $header = array(
Dries's avatar
   
Dries committed
311
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
Dries's avatar
   
Dries committed
312
313
    array('data' => t('Url'), 'field' => 'url'),
    array('data' => t('Last visit'), 'field' => 'last'),
Dries's avatar
   
Dries committed
314
  );
Dries's avatar
   
Dries committed
315

316
  $query .= tablesort_sql($header);
317
  $result = pager_query($query, 30, 0, $query_cnt, $_SERVER['HTTP_HOST']);
318

Dries's avatar
   
Dries committed
319
  while ($referrer = db_fetch_object($result)) {
Dries's avatar
Dries committed
320
    $rows[] = array($referrer->hits, '<a href="'. $referrer->url .'">'. _statistics_column_width($referrer->url) .'</a>', t('%time ago', array('%time' => format_interval(time() - $referrer->last))));
Dries's avatar
   
Dries committed
321
  }
322
  if ($pager = theme('pager', NULL, 30, 0, tablesort_pager())) {
Dries's avatar
   
Dries committed
323
    $rows[] = array(array('data' => $pager, 'colspan' => '3'));
324
325
  }

326
  print theme('page', theme('table', $header, $rows));
327
328
}

Dries's avatar
   
Dries committed
329
330
331
/**
 * Implementation of hook_settings().
 */
332
function statistics_settings() {
Dries's avatar
   
Dries committed
333
  // access log settings:
Dries's avatar
   
Dries committed
334
  $group = form_radios(t('Enable access log'), 'statistics_enable_access_log', variable_get('statistics_enable_access_log', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Log each page access.  Required for referrer statistics.'));
Dries's avatar
   
Dries committed
335

Dries's avatar
   
Dries committed
336
337
338
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  $group .= form_select(t('Discard access logs older than'), 'statistics_flush_accesslog_timer', variable_get('statistics_flush_accesslog_timer', 259200), $period, t('Older access log entries (including referrer statistics) will be automatically discarded.  Requires crontab.'));
  $output = form_group(t('Access log settings'), $group);
339

Dries's avatar
   
Dries committed
340
  // count content views settings
Dries's avatar
   
Dries committed
341
  $group = form_radios(t('Count content views'), 'statistics_count_content_views', variable_get('statistics_count_content_views', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Increment a counter each time content is viewed.'));
342
  $group .= form_radios(t('Display counter values'), 'statistics_display_counter', variable_get('statistics_display_counter', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Display how many times given content has been viewed.'));
Dries's avatar
   
Dries committed
343
  $output .= form_group(t('Content viewing counter settings'), $group);
344

Dries's avatar
   
Dries committed
345
  return $output;
346
347
}

Dries's avatar
   
Dries committed
348
349
350
/**
 * Saves the values entered in the "config statistics" administration form.
 */
351
function statistics_save_statistics($edit) {
Dries's avatar
   
Dries committed
352
  variable_set('statistics_display_counter', $edit['statistics_display_counter']);
353
354
}

Dries's avatar
   
Dries committed
355
356
357
/**
 * Implementation of hook_cron().
 */
358
function statistics_cron() {
Dries's avatar
   
Dries committed
359
  $statistics_timestamp = variable_get('statistics_day_timestamp', '');
360
361
362

  if ((time() - $statistics_timestamp) >= 86400) {
    /* reset day counts */
Dries's avatar
   
Dries committed
363
364
    db_query('UPDATE {node_counter} SET daycount = 0');
    variable_set('statistics_day_timestamp', time());
365
366
367
  }

  /* clean expired access logs */
Dries's avatar
   
Dries committed
368
  db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200));
369
370
}

Dries's avatar
   
Dries committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * Returns all time or today top or last viewed node(s).
 *
 * @param $dbfield
 *   one of
 *   - 'totalcount': top viewed content of all time.
 *   - 'daycount': top viewed content for today.
 *   - 'timestamp': last viewed node.
 *
 * @param $dbrows
 *   number of rows to be returned.
 *
 * @return
 *   A query result containing n.nid, n.title, u.uid, u.name of the selected node(s)
 *   or FALSE if the query could not be executed correctly.
 */
387
function statistics_title_list($dbfield, $dbrows) {
Dries's avatar
   
Dries committed
388
  return db_query_range("SELECT s.nid, n.title, u.uid, u.name FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE %s <> '0' AND n.status = 1 ORDER BY %s DESC", 's.'. $dbfield, 's.'. $dbfield, 0, $dbrows);
389
390
}

Dries's avatar
   
Dries committed
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
 * Retrieves a node's "view statistics".
 *
 * @param $nid
 *   node ID
 *
 * @return
 *   An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp
 *   - totalcount: count of the total number of times that node has been viewed.
 *   - daycount: count of the total number of times that node has been viewed "today".
 *     For the daycount to be reset, cron must be enabled.
 *   - timestamp: timestamp of when that node was last viewed.
 */
404
405
406
407
function statistics_get($nid) {

  if ($nid > 0) {
    /* retrieves an array with both totalcount and daycount */
Dries's avatar
   
Dries committed
408
    $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid));
409
410
411
412
413
  }

  return $statistics;
}

Dries's avatar
   
Dries committed
414
415
416
/**
 * Implementation of hook_block().
 */
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
function statistics_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      if (variable_get('statistics_count_content_views', 0)) {
        $blocks[0]['info'] = t('Popular content');
      }
      return $blocks;

    case 'configure':
      // Popular content block settings
      $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
      $output = form_select(t("Number of day's top views to display"), 'statistics_block_top_day_num', variable_get('statistics_block_top_day_num', 0), $numbers, t('How many content items to display in "day" list.'));
      $output .= form_select(t('Number of all time views to display'), 'statistics_block_top_all_num', variable_get('statistics_block_top_all_num', 0), $numbers, t('How many content items to display in "all time" list.'));
      $output .= form_select(t('Number of most recent views to display'), 'statistics_block_top_last_num', variable_get('statistics_block_top_last_num', 0), $numbers, t('How many content items to display in "recently viewed" list.'));
      return $output;

    case 'save':
      variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
      variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
      variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
      break;

    case 'view':
      if (user_access('access content')) {
Dries's avatar
   
Dries committed
441
442
        $content = array();

Dries's avatar
   
Dries committed
443
        $daytop = variable_get('statistics_block_top_day_num', 0);
Dries's avatar
   
Dries committed
444
        if ($daytop) {
Dries's avatar
   
Dries committed
445
          $content[] = node_title_list(statistics_title_list('daycount', $daytop), t("Today's:"));
Dries's avatar
   
Dries committed
446
447
        }

Dries's avatar
   
Dries committed
448
        $alltimetop = variable_get('statistics_block_top_all_num', 0);
Dries's avatar
   
Dries committed
449
        if ($alltimetop) {
Dries's avatar
   
Dries committed
450
          $content[] = node_title_list(statistics_title_list('totalcount', $alltimetop), t('All time:'));
Dries's avatar
   
Dries committed
451
452
        }

Dries's avatar
   
Dries committed
453
        $lasttop = variable_get('statistics_block_top_last_num', 0);
Dries's avatar
   
Dries committed
454
        if ($lasttop) {
Dries's avatar
   
Dries committed
455
          $content[] = node_title_list(statistics_title_list('timestamp', $lasttop), t('Last viewed:'));
Dries's avatar
   
Dries committed
456
        }
Dries's avatar
   
Dries committed
457
        $output = implode($content, '<br />');
Dries's avatar
   
Dries committed
458

459
        $block['subject'] = t('Popular content');
Dries's avatar
   
Dries committed
460
        $block['content'] = $output;
461

462
463
        return $block;
      }
464
465
466
  }
}

Dries's avatar
   
Dries committed
467
468
/**
 * It is possible to adjust the width of columns generated by the
Dries's avatar
Dries committed
469
 * statistics module.
Dries's avatar
   
Dries committed
470
 */
Dries's avatar
Dries committed
471
472
473
474
475
476
477
478
479
function _statistics_column_width($column, $width = 35) {
  return (strlen($column) > $width ? substr($column, 0, $width) . '...' : $column);
}

function _statistics_format_item($title, $link) {
  $link = ($link ? $link : '/');
  $output  = ($title ? "$title<br />" : '');
  $output .= l($link, $link);
  return $output;
Dries's avatar
   
Dries committed
480
481
}

Dries's avatar
   
Dries committed
482
483
484
/**
 * Implementation of hook_nodeapi().
 */
485
486
function statistics_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
Dries's avatar
   
Dries committed
487
    case 'delete':
488
489
      // clean up statistics table when node is deleted
      db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
490
491
492
493
  }
}

?>