-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2apro-tagexperts.php
263 lines (233 loc) · 6.17 KB
/
q2apro-tagexperts.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
Question2Answer Plugin: q2apro Tag Experts
Plugin Author URI: http://www.q2apro.com/
License: http://www.gnu.org/licenses/gpl.html
*/
class q2apro_tagexperts
{
var $directory;
var $urltoroot;
function load_module($directory, $urltoroot)
{
$this->directory=$directory;
$this->urltoroot=$urltoroot;
}
// for display in admin interface under admin/pages
function suggest_requests()
{
return array(
array(
'title' => 'Tag Experts', // title of page
'request' => 'tagexperts', // request name
'nav' => 'M', // 'M'=main, 'F'=footer, 'B'=before main, 'O'=opposite main, null=none
),
);
}
// for url query
function match_request($request)
{
if ($request=='tagexperts')
{
return true;
}
return false;
}
function process_request($request)
{
// get param from URL
$tag = qa_get('tag');
// start
$qa_content = qa_content_prepare();
$qa_content['custom'] = '';
$qa_content['title'] = 'Tag Experts';
// return if not admin
$level=qa_get_logged_in_level();
if ($level<QA_USER_LEVEL_ADMIN) // or QA_USER_LEVEL_EDITOR, QA_USER_LEVEL_EXPERT
{
$qa_content['error'] = 'You are not allowed to access this page.';
return $qa_content;
}
if(empty($tag))
{
$qa_content['error'] = 'No tag specified.';
$qa_content['custom'] .= '
<form method="GET" class="newtagform">
<p>
Enter a tag:
</p>
<input type="text" name="tag" placeholder="Enter tag here" autofocus>
<button type="button">Send</button>
</form>
';
return $qa_content;
}
$qa_content['title'] = 'Experts for <a target="_blank" href="'.qa_path('tag').'/'.$tag.'">"'.$tag.'"</a>';
// read all questions with specified tag
$tagsQuestions = qa_db_read_all_values(
qa_db_query_sub('SELECT `postid` FROM `^posts`
WHERE `type` = "Q"
AND `tags` LIKE "'.$tag.'%"
AND acount > 0
'));
if(empty($tagsQuestions))
{
$qa_content['error'] = 'Sorry, no tag found.';
$qa_content['custom'] .= '
<form method="GET" class="newtagform">
<p>
Enter another tag:
</p>
<input type="text" name="tag" placeholder="Enter tag here" autofocus>
<button type="button">Send</button>
</form>
';
return $qa_content;
}
$quids = join(',',$tagsQuestions);
// memo selchildid is best-answer
// get all answers to those tag-questions with data
$answers_qu = qa_db_read_all_assoc(
qa_db_query_sub('SELECT `userid`, `postid`, `upvotes` FROM `^posts`
WHERE `type` = "A"
AND `userid` IS NOT NULL
AND `parentid` IN('.$quids.')
'));
$upvotecount = [];
$answercount = [];
$answerids = [];
foreach($answers_qu as $dat)
{
if(isset($upvotecount[$dat['userid']]))
{
$upvotecount[$dat['userid']] += $dat['upvotes'];
$answercount[$dat['userid']]++;
$answerids[$dat['userid']] .= ','.$dat['postid'];
}
else {
$upvotecount[$dat['userid']] = $dat['upvotes'];
$answercount[$dat['userid']] = 1;
$answerids[$dat['userid']] = $dat['postid'];
}
}
// do statistics
arsort($upvotecount);
$qa_content['custom'] .= '
<p>
Click on the table headers to sort.
</p>
<table class="tagexperts-table">
<thead>
<tr>
<th>Pos</th>
<th>User</th>
<th>Upvotes</th>
<th>Answers</th>
</tr>
</thead>
<tbody>
';
$pos = 1;
foreach($upvotecount as $userid=>$vcount)
{
$userhandle = qa_userid_to_handle($userid);
$qa_content['custom'] .= '
<tr>
<td>
'.$pos++.'
</td>
<td>
<a target="_blank" href="'.qa_path('user').'/'.$userhandle.'">'.$userhandle.'</a>
</td>
<td>
<span class="thmbup">👍</span> '.$vcount.'
</td>
<td title="Answer IDs: '.$answerids[$userid].'">
'.$answercount[$userid].'
</td>
</tr>
';
}
$qa_content['custom'] .= '
</tbody>
</table>
';
$qa_content['custom'] .= '
<form method="GET" class="newtagform">
<p>
Choose another tag:
</p>
<input type="text" name="tag" placeholder="Enter tag here">
<button type="button">Send</button>
</form>
';
// simple jquery table sort, credits to https://stackoverflow.com/a/19947532/1066234
$qa_content['custom'] .= "
<script type=\"text/javascript\">
$(document).ready(function()
{
$('.tagexperts-table th').click(function(){
var table = $(this).parents('table').eq(0)
var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
this.asc = !this.asc
if (!this.asc){rows = rows.reverse()}
for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})
function comparer(index) {
return function(a, b) {
var valA = getCellValue(a, index), valB = getCellValue(b, index)
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
}
}
function getCellValue(row, index){ return $(row).children('td').eq(index).text() }
});
</script>
";
// CSS
$qa_content['custom'] .= '
<style type="text/css">
.tagexperts-table {
border-collapse: collapse;
width: 100%;
background: #fff;
}
.tagexperts-table th {
background-color: #326295;
font-weight: normal;
color: #fff;
white-space: nowrap;
cursor:pointer;
}
.tagexperts-table th:first-child,
.tagexperts-table td:first-child {
width:10%;
text-align:center;
}
.tagexperts-table td, .tagexperts-table th {
padding: 1em 1.5em;
text-align: left;
}
.tagexperts-table tbody th {
background-color: #2ea879;
}
.tagexperts-table tbody tr:nth-child(2n-1) {
background-color: #f5f5f5;
transition: all .125s ease-in-out;
}
.tagexperts-table tbody tr:hover {
background-color: rgba(50,98,149,.3);
}
.thmbup {
font-size:20px;
}
.newtagform {
margin-top:40px;
}
</style>
';
return $qa_content;
}
}; // END q2apro_tagexperts
/*
Omit PHP closing tag to help avoid accidental output
*/