forked from mantisbt-plugins/IssueViewers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIssueViewers.php
144 lines (127 loc) · 4.18 KB
/
IssueViewers.php
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
<?php
/**
* Issue Viewers - This plugin stores and shows issue viewers and their view count
* @author Dentxinho
*
*/
class IssueViewersPlugin extends MantisPlugin {
function register() {
$this->name = 'Issue Viewers';
$this->description = 'This plugin stores and shows issue viewers and their view count';
$this->page = '';
$this->version = '0.1';
$this->requires = array(
'MantisCore' => '1.2.0',
);
$this->author = 'Dentxinho';
$this->contact = 'https://github.com/Dentxinho';
$this->url = 'https://github.com/Dentxinho';
}
function hooks() {
return array(
'EVENT_VIEW_BUG_DETAILS' => 'store_viewer',
'EVENT_VIEW_BUG_EXTRA' => 'display_viewers',
'EVENT_MENU_MAIN' => 'print_menu_issueviews'
);
}
function schema() {
$schema[] = array( "CreateTableSQL", array( plugin_table( "bug_viewer_data" ), "
id I NOTNULL UNSIGNED AUTOINCREMENT PRIMARY,
bug_id I NOTNULL,
user_id I DEFAULT NULL,
view_date I NOTNULL
", array( "mysql" => "DEFAULT CHARSET=utf8" ) ) );
$schema[] = array( 'CreateIndexSQL', array( 'idx_bug_id', plugin_table( 'bug_viewer_data' ), 'bug_id' ) );
$schema[] = array( 'CreateIndexSQL', array( 'idx_user_id', plugin_table( 'bug_viewer_data' ), 'user_id' ) );
return $schema;
}
function store_viewer($p_event, $bug_id) {
if (current_user_is_anonymous() || !auth_is_user_authenticated())
$user_id = NULL;
else
$user_id = current_user_get_field( "id" );
$dbtable = plugin_table( "bug_viewer_data" );
$dbquery = "INSERT INTO {$dbtable} (bug_id, user_id, view_date) VALUES (" . db_param() . "," . db_param() . "," . db_now() . ")";
$dboutput = db_query_bound( $dbquery, array( $bug_id, $user_id ) );
}
function display_viewers( $p_event, $bug_id ) {
$dbtable = plugin_table( "bug_viewer_data" );
$dbquery = "SELECT IFNULL(user_id, 'anon') AS user_id, COUNT(IFNULL(user_id,'anon')) AS count, MAX(view_date) AS last_view
FROM {$dbtable}
WHERE bug_id = " . db_param() . "
GROUP BY user_id
ORDER BY MAX(view_date) DESC";
$dboutput = db_query_bound( $dbquery, array( $bug_id ) );
$viewers = array();
$view_count = 0;
$last_view = FALSE;
if ( $dboutput->RecordCount() > 0 ) {
$data = $dboutput->GetArray();
$last_view = $data[0]['last_view'];
foreach ($data as $row) {
$viewers[$row['user_id']] = $row['count'];
$view_count += $row['count'];
}
}
$this->_print_viewers_layout( $viewers, $view_count, $last_view );
}
private function _print_viewers_layout( $viewers, $view_count, $last_view ) {
echo '<a name="viewers" id="viewers" /><br />';
collapse_open( 'viewers' );
echo '<table class="width100" cellspacing="1">
<tr>
<td class="form-title" colspan="2">';
collapse_icon( 'viewers' );
echo plugin_lang_get( 'viewed_by' ),
'</td>
</tr>
<tr class="row-1">
<td class="category" width="15%">',
plugin_lang_get( 'user_list' ),
'</td>
<td>';
if ( 0 == count($viewers) ) {
echo plugin_lang_get( 'no_views' );
} else {
foreach ( $viewers as $user_id => &$count ) {
$count = ( ( $user_id != 'anon' ) ? prepare_user_name( $user_id ) : lang_get( 'anonymous' ) ) . " (" . $count . "x)";
}
echo implode( ', ',$viewers );
}
echo '</td>
</tr>
<tr class="row-1">
<td class="category" width="15%">',
plugin_lang_get( 'total_views' ),
'</td>
<td>' . $view_count . '</td>
</tr>
<tr class="row-1">
<td class="category" width="15%">',
plugin_lang_get( 'last_view' ),
'</td>
<td>' . ($last_view ? date( config_get( 'normal_date_format' ), $last_view ) : ' - ') . '</td>
</tr>
</table>';
collapse_closed( 'viewers' );
echo '<table class="width100" cellspacing="1">
<tr>
<td class="form-title" colspan="2">';
collapse_icon( 'viewers' );
echo plugin_lang_get( 'viewed_by' ),
'</td>
</tr>
</table>';
collapse_end( 'viewers' );
}
/*
* Default plugin configuration.
*/
function print_menu_issueviews( ) {
$t_links = array();
$t_page = plugin_page( 'index' );
$t_lang = plugin_lang_get( 'menu_issueviews' );
$t_links[] = "<a href=\"$t_page\">$t_lang</a>";
return $t_links;
}
}