-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtr.module
407 lines (365 loc) · 12.7 KB
/
qtr.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/*
* @file
* qtr module
*/
require_once 'qtr.inc';
require_once 'qtr.api.inc';
function qtr_menu() {
$items = array();
/* $items['qtr/demo'] = array(
'title' => 'QTR demonstration',
'description' => 'Running QTR on sample data',
'page callback' => 'qtr_demo',
'access arguments' => array('administer QTR'),
);*/
$items['admin/config/user-interface/qtr'] = array(
'title' => 'QTR',
'description' => 'QTR settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('qtr_run_form'),
'access arguments' => array('administer QTR'),
);
$items['admin/config/user-interface/qtr/run'] = array(
'title' => 'Run',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10
);
$items['admin/config/user-interface/qtr/Parameters'] = array(
'title' => 'Parameters',
'page callback' => 'drupal_get_form',
'page arguments' => array('qtr_settings'),
'access arguments' => array('administer QTR'),
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/user-interface/qtr/item'] = array(
'title' => 'Item',
'page callback' => 'drupal_get_form',
'page arguments' => array('qtr_set_form'),
'access arguments' => array('administer QTR'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function qtr_permission() {
return array(
// 'access QTR demonstration' => array('title' => t('Access QTR demonstration')),
'administer QTR' => array('title' => t('Administer QTR')),
);
}
function qtr_run_form($form, &$form_state) {
$form['description'] = array(
'#markup' => '<p>' . t('You can specify !cron frequency to run the QTR. Click the button run now:',
array('!cron' => l(t('cron'), 'admin/config/system/cron'))) . '</p>',
);
$form['run'] = array(
'#type' => 'submit',
'#value' => t('Run QTR'),
'#submit' => array('qtr_demo'),
);
return $form;
}
function qtr_set_form($form, &$form_state) {
$form= array();
$posttype = array();
$default = array();
$types=qtr_get_itemtype();
if ($types) {
foreach ($types as $type) {
$default[]=$type->item_type;
}
}
foreach (node_type_get_types() as $type => $type_obj) {
$posttype[$type] = $type_obj->name;
}
$form['content_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Content type to be considered:'),
'#options' => $posttype,
'#default_value' => $default,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function qtr_set_form_submit($form, &$form_state) {
$result = array();
$types = $form_state['values']['content_type'];
foreach ($types as $type) {
if ($type)
$result[] = array('item_type' => $type);
}
qtr_update_itemtype($result);
drupal_set_message(t('The settings have been saved.'));
}
function qtr_settings($form, &$form_state) {
$form['params'] = array(
'#type' => 'fieldset',
'#title' => t('Parameters'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['params']['qtr_delta'] = array(
'#type' => 'textfield',
'#title' => t('Delta'),
'#description' => t('N.B.: if the effective number of AGENTS/ITEMS is lower (i.e. if there are gaps in the input file), the renormalization of the algorithm has to change. Better to have no gaps!'),
'#default_value' => variable_get('qtr_delta', 0.00000000001),
);
$actions = qtr_get_actiontype();
if ($actions) {
foreach ($actions as $action) {
$form['params']['qtr_w_' . $action->action] = array(
'#type' => 'textfield',
'#title' => t('Weight of %action action', array('%action' => $action->action)),
'#default_value' => $action->weight,
);
}
}
$form['params']['qtr_decay'] = array(
'#type' => 'select',
'#title' => t('Time-decay of scores'),
'#options' => array(0 => 'no decay', 1 => 'power-decay', 2 => 'exponential decay', 3 => 'theta-decay'),
'#default_value' => variable_get('qtr_decay', 0),
);
$form['params']['qtr_tau0'] = array(
'#type' => 'textfield',
'#title' => t('Time scale of the decay (day)'),
'#default_value' => variable_get('qtr_tau0', 50),
);
$form['params']['qtr_renorm_q'] = array(
'#type' => 'textfield',
'#title' => t('Renormalization of quality'),
'#default_value' => variable_get('qtr_renorm_q', 0),
);
$form['params']['qtr_renorm_r'] = array(
'#type' => 'textfield',
'#title' => t('Renormalization of reputation'),
'#default_value' => variable_get('qtr_renorm_r', 0),
);
$form['params']['qtr_renorm_t'] = array(
'#type' => 'textfield',
'#title' => t('Renormalization of trust'),
'#default_value' => variable_get('qtr_renorm_t', 0),
);
$form['params']['qtr_resc_q'] = array(
'#type' => 'textfield',
'#title' => t('Rescaled quality'),
'#default_value' => variable_get('qtr_resc_q', 0),
);
$form['params']['qtr_resc_r'] = array(
'#type' => 'textfield',
'#title' => t('Rescaled reputation'),
'#default_value' => variable_get('qtr_resc_r', 0),
);
$form['params']['qtr_resc_t'] = array(
'#type' => 'textfield',
'#title' => t('Rescaled trust'),
'#default_value' => variable_get('qtr_resc_t', 0),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function qtr_settings_submit($form, &$form_state) {
$params = $form_state['values'];
variable_set('qtr_delta', $params['qtr_delta']);
variable_set('qtr_decay', $params['qtr_decay']);
variable_set('qtr_tau0', $params['qtr_tau0']);
variable_set('qtr_renorm_q', $params['qtr_renorm_q']);
variable_set('qtr_renorm_r', $params['qtr_renorm_r']);
variable_set('qtr_renorm_t', $params['qtr_renorm_t']);
variable_set('qtr_resc_q', $params['qtr_resc_q']);
variable_set('qtr_resc_r', $params['qtr_resc_t']);
variable_set('qtr_resc_t', $params['qtr_resc_t']);
$actions = qtr_get_actiontype();
if ($actions) {
foreach ($actions as $action) {
qtr_update_actionweight($action->action, $params['qtr_w_' . $action->action]);
}
}
drupal_set_message(t('The settings have been saved.'));
}
function qtr_demo() {
qtr_from_db();
return '';
}
function qtr_from_db($trust = NULL) {
$agent = $item = NULL;
// _qtr_init($agent, $item, 50, 1000);
_qtr_read($agent, $item, $trust);
$start = strtotime('now');
if (count($agent)&&count($item)) {
_qtr_calculate($agent, $item, $trust, $start);
qtr_write_values('qtr_reputation', $agent);
qtr_write_values('qtr_quality', $item);
}
//write the result of reputation of user and quality of paper into files
//$path_agents = drupal_get_path('module', 'qtr') . "/agent_file";
//$path_items = drupal_get_path('module', 'qtr') . "/item_file";
//_qtr_save($agent, $item, $path_agents,$path_items);
}
/*
* Implemens hook_block_info
*/
function qtr_block_info() {
$blocks['popular'] = array(
'info' => t('Highlighted contents'),
'region' => 'sidebar_first',
'status' => TRUE
);
return $blocks;
}
/**
* Implementation of hook_init().
*/
function qtr_init() {
//$_moduleName_='user_block';
//drupal_add_css(drupal_get_path('module', $_moduleName_) . '/' . $_moduleName_ . '.css');
drupal_add_css(drupal_get_path('module', 'qtr') . '/qtr.css');
drupal_add_js(drupal_get_path('module', 'qtr') . '/qtr.js');
}
/*
* Implements hook_block_configure
*/
function qtr_block_configure($delta = '') {
if ($delta == 'popular') {
$posttype = array();
$default = array();
foreach (node_type_get_types() as $type => $type_obj) {
$posttype[$type] = $type_obj->name;
if (variable_get('popular_block_' . $type, 0)) {
$default[] = $type;
}
}
$form['content_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Content type to display'),
'#options' => $posttype,
'#default_value' => $default,
);
return $form;
}
}
/*
* Implements hook_block_save
*/
function qtr_block_save($delta, $edit = array()) {
if ($delta == 'popular') {
foreach (node_type_get_types() as $type => $type_obj) {
if (variable_get('popular_block_' . $type, 0)) {
variable_del('popular_block_' . $type);
}
}
foreach ($edit['content_type'] as $type) {
variable_set('popular_block_' . $type, $type);
}
}
}
/*
* Implements hook_block_view
*/
function qtr_block_view($delta = '') {
if ($delta == 'popular') {
$contents = '<div class="title_block_head">
<h2>Top papers</h2>
<div id="tab_menu">
<a href="javascript:;" id="cont-1" class="tabLink active_tab">Top value</a>
<a href="javascript:;" id="cont-2" class="tabLink ">Most downloads</a>
<a href="javascript:;" id="cont-3" class="tabLink ">Most votes</a>
<div class="clear"></div>
</div>
</div>';
$contents .= '<div class="content_box">
<div class="tabcontent" id="cont-1-1">
<ul class="red_arrow_li">';
$showlist = qtr_get_topquality(5);
if ($showlist) {
foreach ($showlist as $list) {
$contents .= '<li><a href="' . url('node/' . $list->nid) . '" target="_blank">' . $list->title . "</a></li>";
}
}
else {
$contents .='No polular content yet.';
}
$contents .= '</ul>
<div class="right_align">
<a href="' . url('papers/popular') . '" class="red_see_more2">Show more</a>
</div>
</div>
<div class="tabcontent hide" id="cont-2-1"><ul class="red_arrow_li">';
$showlist = qtr_get_topitems(5, 2, 30);
if ($showlist) {
foreach ($showlist as $list) {
$contents .= '<li><a href="' . url('node/' . $list->nid) . '" target="_blank">' . $list->title . '</a> <span class="grey_text3">(' . $list->count . " downloads)</span></li>";
}
}
else {
$contents .='No polular content yet.';
}
$contents .= '</ul><div class="right_align">
<a href="' . url('papers/popular', array('query' => array('order' => 'download'))) . '" class="red_see_more2">Show more</a>
</div></div><div class="tabcontent hide" id="cont-3-1"><ul class="red_arrow_li">';
$showlist = qtr_get_topitems(5, 1, 30);
if ($showlist) {
foreach ($showlist as $list) {
$contents .= '<li><a href="' . url('node/' . $list->nid) . '" target="_blank">' . $list->title . '</a> <span class="grey_text3">(' . $list->count . " votes)</span></li>";
}
}
else {
$contents .= 'No polular content yet.';
}
$contents .= '</ul><div class="right_align">
<a href="' . url('papers/popular', array('query' => array('order' => 'vote'))) . ' "class="red_see_more2">Show more</a>
</div></div></div>';
/* $showlist = qtr_get_topquality(5);
$blocks['subject'] = 'Highlighted contents';
$contents='<ul class="option">
<span class="optionlink optionlinkon" id="byquality"><a
href="javascript:PopByQuality()"><span class="Clink">top papers</span></a>
</span>
<span class="optionlink optionlinkoff" id="bydownload"><a
href="javascript:PopByBownload()"><span class="Clink">most downloads</span></a>
</span>
</ul>';
$contents .= '<div class="Visible" id="quality"><ul>';
if ($showlist) {
foreach ($showlist as $list) {
$contents .= '<li><a href="'.url('node/' . $list->nid) . '" target="_blank">' . $list->title . "</a></li>";
}
} else {
$contents .='No polular content yet.';
}
$contents .= '</ul><a href="'.url('papers').'" class="more"><strong>show more </strong></a></div>';
$showlist = qtr_get_topitems(5,2,30);
$contents .= '<div class="listsHidden" id="download"><ul>';
if ($showlist) {
foreach ($showlist as $list) {
$contents .= '<li><a href="'.url('node/' . $list->nid) . '" target="_blank">' . $list->title . "</a> (".$list->count." downloads)</li>";
}
} else {
$contents .='No polular content yet.';
}
$contents .= '</ul><a href="'.url('papers').'" class="more"><strong>show more </strong></a></div>';*/
global $user;
$blocks['content'] = $contents;
return $blocks;
}
}
/*
* Implements hook_cron
*
* Automatically run QTR
*/
function qtr_cron() {
if (variable_get('qtr_updatetime', 0)+24*3600<strtotime('now')) {
variable_set('qtr_updatetime', strtotime('now')-50);
qtr_demo();
}
}