-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmechelenblogt.module
149 lines (124 loc) · 4.45 KB
/
mechelenblogt.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// $Id$
/**
* Implements hook_help.
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function mechelenblogt_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#mechelenblogt":
$output = '<p>'. t("Levert een aantal views en maakt ook een 'statistiekjes' blok beschikbaar, waarvan de gegevens gecached worden. Cache wordt leeg gemaakt wanneer er een nieuwe reactie geplaatst wordt.</p><p>Wist de standaard naam 'Anoniem' op het reactieformulier zodat de gebruiker iets anders moet invullen.") .'</p>';
break;
}
return $output;
}
/**
* Implements hook_views_api.
* Tell Views where to look for default views.
*/
function mechelenblogt_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'mechelenblogt') . '/includes/views',
);
}
/**
* Implements hook_block_info.
*/
function mechelenblogt_block_info() {
$blocks['statistiekjes'] = array(
'info' => 'Mechelen Blogt statistiekjes',
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_view.
*/
function mechelenblogt_block_view($delta = '') {
if ($delta != 'statistiekjes') {
return $stats_block;
}
// set up the block
$stats_block = array(
'subject' => 'Statistiekjes',
'content' => mechelenblogt_get_statistiekjes($delta),
);
return $stats_block;
}
/**
* Implements hook_comment_insert.
* Clear the statistics cache when a comment is added.
*/
function mechelenblogt_comment_insert($comment) {
echo("Comment insert");
mechelenblogt_clear_statistiekjes();
}
/**
* Implements hook_comment_delete.
* Clear the statistics cache when a comment is removed.
*/
function mechelenblogt_comment_delete($comment) {
mechelenblogt_clear_statistiekjes();
}
/**
* Implements hook_comment_publish.
* Clear the statistics cache when a comment is published.
*/
function mechelenblogt_comment_publish($comment) {
mechelenblogt_clear_statistiekjes();
}
/**
* Implements hook_comment_unpublish.
* Clear the statistics cache when a comment is unpublished.
*/
function mechelenblogt_comment_unpublish($comment) {
mechelenblogt_clear_statistiekjes();
}
/**
* Implements hook_node_insert().
* Clear the statistics cache when a node is inserted.
*/
function mechelenblogt_node_insert($node) {
mechelenblogt_clear_statistiekjes();
}
/**
* Implements hook_node_delete().
* Clear the statistics cache when a node is deleted.
*/
function mechelenblogt_node_delete($node) {
mechelenblogt_clear_statistiekjes();
}
/**
* Clear the statistics cache.
*/
function mechelenblogt_clear_statistiekjes() {
static $mechelenblogt_statistiekjes_message;
cache_clear_all('mechelenblogt_statistiekjes_message', 'cache');
unset($mechelenblogt_statistiekjes_message);
}
/**
* Calculate the statistics and save it into a cached message.
*/
function mechelenblogt_get_statistiekjes($delta) {
$REFRESH_INTERVAL = 60*60; // hard refresh every hour
static $mechelenblogt_statistiekjes_message;
if (!isset($mechelenblogt_statistiekjes_message)) {
// try to load from cache
$cache = cache_get('mechelenblogt_statistiekjes_message');
if ($cache && !empty($cache->data)) {
$mechelenblogt_statistiekjes_message = $cache->data;
}
else {
$posts = db_query("SELECT COUNT(*) AS number FROM {node} WHERE created <= UNIX_TIMESTAMP() AND status = 1 AND type in ('blog_artikel', 'gastblog', 'enquete');")->fetchObject();
$comments = db_query("SELECT SUM(comment_count) AS number FROM {node_comment_statistics} INNER JOIN {node} ON {node_comment_statistics}.nid = {node}.nid WHERE {node}.created <= UNIX_TIMESTAMP() AND {node}.type IN ('blog_artikel', 'gastblog', 'enquete') AND {node}.status = 1 AND {node.comment} > 0;")->fetchObject();
$mechelenblogt_statistiekjes_message = '<p class="mechelenblogt-statistiekjes">Mechelen Blogt telt ' . number_format($posts->number, 0, ',', '.') . ' artikels en ' . number_format($comments->number, 0, ',', '.') . ' reacties.</p>';
cache_set('mechelenblogt_statistiekjes_message', $mechelenblogt_statistiekjes_message, 'cache', time() + $REFRESH_INTERVAL);
}
}
return $mechelenblogt_statistiekjes_message;
}