/[drupal]/contributions/modules/jrating/votingapi_views.inc
ViewVC logotype

Contents of /contributions/modules/jrating/votingapi_views.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Dec 9 11:55:37 2006 UTC (2 years, 11 months ago) by hickory
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7
File MIME type: text/x-php
adding files
1 <?php
2 /* $Id */
3
4 function votingapi_views_tables() {
5 $tables["votingapi_cache"] = array(
6 "name" => "votingapi_cache",
7 "provider" => "votingapi",
8 "join" => array(
9 "left" => array(
10 "table" => "node",
11 "field" => "nid"
12 ),
13 "right" => array(
14 "field" => "content_id"
15 ),
16 "extra" => array(
17 'content_type' => 'node',
18 ),
19 ),
20 "fields" => array(
21 "value" => array(
22 'name' => t("VotingAPI: Voting results"),
23 'sortable' => true,
24 'handler' => 'votingapi_views_value_display_handler',
25 'query_handler' => 'votingapi_views_value_query_handler',
26 'option' => array('#type' => 'vote_cache_properties'),
27 'addlfields' => array('value_type', 'tag', 'function', 'content_type', 'content_id'),
28 ),
29 "tag" => array(
30 'name' => t("VotingAPI: Vote tag"),
31 'sortable' => true,
32 'handler' => 'votingapi_views_tag_display_handler',
33 'addlfields' => array('value_type', 'value', 'function', 'content_type', 'content_id'),
34 ),
35 ),
36 "sorts" => array(
37 "value" => array('name' => "Voting result")
38 ),
39 "filters" => array(
40 "value_type" => array(
41 'name' => "VotingAPI: Result type",
42 'operator' => array("=" => "Is", "!=" => "Is Not"),
43 'list' => array(
44 'percent' => 'Percentage',
45 'points' => 'Points',
46 'option' => 'Options',
47 ),
48 'list-type' => "select",
49 'handler' => 'votingapi_views_nullable_field_handler',
50 'help' => "Filter by the type of result being calculated (Percentage, points, etc)",
51 ),
52 "tag" => array(
53 'name' => "VotingAPI: Result tag",
54 'operator' => array("=" => "Is", "!=" => "Is Not"),
55 'list' => "votingapi_cache_tags",
56 'list-type' => "select",
57 'handler' => 'votingapi_views_nullable_field_handler',
58 'help' => "Filter by the tag being voted on",
59 ),
60 "function" => array(
61 'name' => "VotingAPI: Result function",
62 'operator' => array("=" => "Is", "!=" => "Is Not"),
63 'list' => "votingapi_cache_functions",
64 'list-type' => "select",
65 'handler' => 'votingapi_views_nullable_field_handler',
66 'help' => "Filter by the function used to aggregate results (total number of votes, average vote, etc)",
67 ),
68 "value" => array(
69 'name' => t("VotingAPI: Has been voted on"),
70 'list' => array('NULL' => 'Voted'),
71 'list-type' => "select",
72 'operator' => array('IS NOT' => "Yes", 'IS' => "No"),
73 'handler' => 'votingapi_views_value_is_nullable_handler',
74 'help' => t("This will filter out nodes that have or have not been voted on."),
75 ),
76 ),
77 );
78
79 $tables["votingapi_vote"] = array(
80 "name" => "votingapi_vote",
81 "provider" => "votingapi",
82 "join" => array(
83 "left" => array(
84 "table" => "node",
85 "field" => "nid"
86 ),
87 "right" => array(
88 "field" => "content_id"
89 ),
90 "extra" => array(
91 'content_type' => 'node',
92 ),
93 ),
94 "fields" => array(
95 "value" => array(
96 'name' => t("VotingAPI: Individual vote value"),
97 'sortable' => true,
98 'query_handler' => 'votingapi_views_value_query_handler',
99 'handler' => 'votingapi_views_value_display_handler',
100 'option' => array('#type' => 'vote_properties'),
101 'sortable' => true,
102 ),
103 "tag" => array(
104 'name' => t("VotingAPI: Individual vote tag"),
105 'sortable' => true,
106 'handler' => 'votingapi_views_tag_display_handler',
107 'addlfields' => array('value_type', 'value', 'uid', 'content_type', 'content_id'),
108 ),
109 "timestamp" => array(
110 'name' => t("VotingAPI: Individual vote timestamp"),
111 'sortable' => true,
112 'handler' =>
113 array(
114 "views_handler_field_date_small"=>"As Short Date",
115 "views_handler_field_date"=>"As Medium Date",
116 "views_handler_field_date_large"=>"As Long Date",
117 "views_handler_field_since" => "As Time Ago"
118 ),
119 'sortable' => true,
120 'addlfields' => array('value_type', 'tag', 'uid', 'content_type', 'content_id'),
121 'help' => "Display the time the vote was cast.",
122 ),
123 "uid" => array(
124 'name' => t("VotingAPI: Individual vote user"),
125 'handler' => 'votingapi_views_handler_field_username',
126 'sortable' => true,
127 'uid' => "uid",
128 'addlfields' => array('uid', 'content_type', 'content_id'),
129 ),
130 "currentuservoted" => array(
131 'notafield' => true,
132 'name' => t("VotingAPI: Current user has voted"),
133 'handler' => 'votingapi_views_handler_field_current_user_voted',
134 'content_type' => "content_type",
135 'content_id' => "content_id",
136 'uid' => "uid",
137 'addlfields' => array('content_type', 'content_id'),
138 ),
139 ),
140 "sorts" => array(
141 "value" => array('name' => "Vote value")
142 ),
143 "filters" => array(
144 "value_type" => array(
145 'name' => "VotingAPI: Individual vote type",
146 'operator' => array("=" => "Is", "!=" => "Is Not"),
147 'list' => array(
148 'percent' => 'Percentage',
149 'points' => 'Points',
150 'option' => 'Custom',
151 ),
152 'list-type' => "select",
153 'handler' => 'votingapi_views_nullable_field_handler',
154 'help' => "Filter by the type of result being calculated (Percentage, points, etc)",
155 ),
156 "tag" => array(
157 'name' => "VotingAPI: Individual vote tag",
158 'operator' => array("=" => "Is", "!=" => "Is Not"),
159 'list' => "votingapi_vote_tags",
160 'list-type' => "select",
161 'handler' => 'votingapi_views_nullable_field_handler',
162 'help' => "Filter by the tag being voted on",
163 ),
164 "uid" => array(
165 'name' => "VotingAPI: Individual vote ",
166 'operator' => array("=" => "was cast by"),
167 'list' => array(NULL => "Current user"),
168 'list-type' => "select",
169 'handler' => 'votingapi_views_handler_filter_uid_voted',
170 ),
171 "currentuidtouched" => array(
172 'field' => 'uid',
173 'name' => "VotingAPI: Current user has voted",
174 'operator' => array('=' => "voted on by"),
175 'list' => "views_handler_filter_usercurrent",
176 'list-type' => 'select',
177 'handler' => "votingapi_views_handler_filter_uid_has_voted",
178 'help' => "This allows you to filter by whether or not the logged in user has voted on the node.",
179 ),
180 ),
181 );
182
183 return $tables;
184 }
185
186 function votingapi_views_value_query_handler($field, $fieldinfo, &$query) {
187 $table = $field['tablename'];
188
189 if (is_string($field['options'])) {
190 $options = unserialize($field['options']);
191 if (is_array($options)) {
192 foreach ($options as $column => $value) {
193 $query->add_where("$table.$column = '$value'");
194 }
195 }
196 }
197 }
198
199 function votingapi_views_handler_filter_uid_has_voted($op, $filter, $filterinfo, &$query) {
200 $query->add_where("votingapi_vote.uid = '%s'", $filter['value']);
201 }
202
203 function votingapi_views_handler_filter_uid_voted($op, $filter, $filterinfo, &$query) {
204 $query->add_where("votingapi_vote.uid $filter[operator] '%s' OR votingapi_vote.uid IS NULL", $filter['value']);
205 }
206
207
208 function votingapi_views_value_is_nullable_handler($op, $filter, $filterinfo, &$query) {
209 $tn = $query->add_table($filterinfo['table']);
210 $tname = $query->get_table_name($filterinfo['table'], $tn);
211 $query->add_where("$tname.value $filter[operator] NULL");
212 }
213
214
215 function votingapi_views_nullable_field_handler($op, $filter, $filterinfo, &$query) {
216 $tn = $query->add_table($filterinfo['table']);
217 $tname = $filter['field'];
218 $fvalue = $filter['value'];
219 $fop = $filter['operator'];
220 $query->add_where("$tname $fop '$fvalue' OR $tname IS NULL");
221 }
222
223 function votingapi_views_value_display_handler($op, $filter, $value, &$query) {
224 if (!isset($value)) {
225 return t('no votes');
226 }
227 $t = $filter['tablename'] . "_";
228 $q = (array)$query;
229
230 $vobj->value = check_plain($value);
231 $vobj->value_type = $q[$t . 'value_type'];
232 $vobj->content_type = $q[$t . 'content_type'];
233 $vobj->content_id = $q[$t . 'content_id'];
234 $vobj->tag = $q[$t . 'tag'];
235 if (isset($q[$t . 'function'])) {
236 $vobj->function = $q[$t . 'function'];
237 }
238 if (isset($q[$t . 'uid'])) {
239 $vobj->uid = $q[$t . 'uid'];
240 }
241 return votingapi_format_value($vobj);
242 }
243
244 function votingapi_views_tag_display_handler($op, $filter, $value, &$query) {
245 if (!isset($value)) {
246 return t('no votes');
247 }
248 $t = $filter['tablename'] . "_";
249 $q = (array)$query;
250
251 $vobj->value = $q[$t . 'value'];
252 $vobj->value_type = $q[$t . 'value_type'];
253 $vobj->content_type = $q[$t . 'content_type'];
254 $vobj->content_id = $q[$t . 'content_id'];
255 $vobj->tag = $q[$t . 'tag'];
256 if (isset($q[$t . 'function'])) {
257 $vobj->function = $q[$t . 'function'];
258 }
259 if (isset($q[$t . 'uid'])) {
260 $vobj->uid = $q[$t . 'uid'];
261 }
262 return votingapi_format_tag($vobj);
263 }
264
265
266 function votingapi_views_handler_field_username($fieldinfo, $fielddata, $value, $data) {
267 $uidfield = $fielddata['tablename'] . "_" . $fieldinfo['uid'];
268 $user = user_load(array('uid' => $data->$uidfield));
269 return theme('username', $user);
270 }
271
272 function votingapi_views_handler_field_current_user_voted($fieldinfo, $fielddata, $value, $data) {
273 $content_type_field = $fielddata['tablename'] . "_" . $fieldinfo['content_type'];
274 $content_id_field = $fielddata['tablename'] . "_" . $fieldinfo['content_id'];
275
276 return votingapi_get_user_votes($data->$content_type_field, $data->$content_id_field) ? t('Yes') : t('No');
277 }
278
279 function votingapi_views_default_views() {
280 $view = new stdClass();
281 $view->name = 'Node votes';
282 $view->description = 'Lists all votes for a node';
283 $view->access = array (
284 0 => '2',
285 );
286
287 $view->page = TRUE;
288 $view->disabled = TRUE;
289 $view->page_title = '%1';
290 $view->page_header = 'An overview of all votes cast for this piece of content.';
291 $view->page_header_format = '1';
292 $view->page_type = 'table';
293 $view->url = 'node/$arg/votes';
294 $view->use_pager = FALSE;
295 $view->nodes_per_page = '10';
296 $view->menu = TRUE;
297 $view->menu_title = 'votes';
298 $view->menu_tab = TRUE;
299 $view->menu_tab_default = FALSE;
300 $view->menu_weight = '';
301 $view->sort = array (
302 );
303 $view->argument = array (
304 array (
305 'type' => 'nid',
306 'argdefault' => '1',
307 'title' => '',
308 'options' => '',
309 ),
310 );
311 $view->field = array (
312 array (
313 'tablename' => 'votingapi_vote',
314 'field' => 'uid',
315 'label' => 'user',
316 'sortable' => '1',
317 ),
318 array (
319 'tablename' => 'votingapi_vote',
320 'field' => 'timestamp',
321 'label' => 'time',
322 'handler' => 'views_handler_field_since',
323 'sortable' => '1',
324 'defaultsort' => 'DESC',
325 ),
326 array (
327 'tablename' => 'votingapi_vote',
328 'field' => 'tag',
329 'label' => 'criteria',
330 'sortable' => '1',
331 ),
332 array (
333 'tablename' => 'votingapi_vote',
334 'field' => 'value',
335 'label' => 'vote',
336 'sortable' => '1',
337 ),
338 );
339 $view->filter = array (
340 );
341 $view->requires = array(votingapi_vote);
342 $views[$view->name] = $view;
343
344 $view = new stdClass();
345 $view->name = 'Moderation queue';
346 $view->description = 'Nodes in the moderation queue';
347 $view->access = array (
348 0 => '2',
349 );
350 $view->page = TRUE;
351 $view->disabled = TRUE;
352 $view->page_title = 'moderation queue';
353 $view->page_header = 'Content in the moderation queue can be voted on by the community. Depending on the results, content may be removed from the site or promoted to the front page.';
354 $view->page_header_format = '1';
355 $view->page_type = 'table';
356 $view->url = 'queue';
357 $view->use_pager = TRUE;
358 $view->nodes_per_page = '40';
359 $view->menu = TRUE;
360 $view->menu_title = 'moderation queue';
361 $view->menu_tab = FALSE;
362 $view->menu_tab_default = FALSE;
363 $view->menu_weight = '';
364 $view->sort = array (
365 );
366 $view->argument = array (
367 );
368 $view->field = array (
369 array (
370 'tablename' => 'node',
371 'field' => 'type',
372 'label' => 'type',
373 'sortable' => '1',
374 ),
375 array (
376 'tablename' => 'node',
377 'field' => 'title',
378 'label' => 'title',
379 'handler' => 'views_handler_field_nodelink_with_mark',
380 'sortable' => '1',
381 ),
382 array (
383 'tablename' => 'users',
384 'field' => 'name',
385 'label' => 'author',
386 'sortable' => '1',
387 ),
388 array (
389 'tablename' => 'node',
390 'field' => 'created',
391 'label' => 'created on',
392 'handler' => 'views_handler_field_since',
393 'sortable' => '1',
394 'defaultsort' => 'DESC',
395 ),
396 array (
397 'tablename' => 'votingapi_cache',
398 'field' => 'value',
399 'label' => 'current rating',
400 'sortable' => '1',
401 ),
402 );
403 $view->filter = array (
404 array (
405 'tablename' => 'node',
406 'field' => 'distinct',
407 'operator' => '=',
408 'options' => '',
409 'value' => '',
410 ),
411 array (
412 'tablename' => 'node',
413 'field' => 'moderate',
414 'operator' => '=',
415 'options' => '',
416 'value' => '1',
417 ),
418 array (
419 'tablename' => 'votingapi_cache',
420 'field' => 'tag',
421 'operator' => '=',
422 'options' => '',
423 'value' => 'vote',
424 ),
425 array (
426 'tablename' => 'votingapi_cache',
427 'field' => 'function',
428 'operator' => '=',
429 'options' => '',
430 'value' => 'average',
431 ),
432 );
433 $view->requires = array(node, users, votingapi_cache);
434 $views[$view->name] = $view;
435
436 return $views;
437 }
438
439
440 /*
441 * Implements custom form widgets used by VotingAPI for its
442 * Views integration. It's rather wicked.
443 */
444
445 function votingapi_elements() {
446 $type['vote_properties'] = array(
447 '#input' => TRUE,
448 '#process' => array('expand_vote_properties' => array()),
449 '#validate' => array('vote_properties_validate' => array()),
450 );
451 $type['vote_cache_properties'] = array(
452 '#input' => TRUE,
453 '#process' => array('expand_vote_cache_properties' => array()),
454 '#validate' => array('vote_cache_properties_validate' => array()),
455 );
456 return $type;
457 }
458
459
460 function vote_properties_validate($form) {
461 if (isset($form['value_type']['#value'])) {
462 $val['value_type'] = trim($form['value_type']['#value']);
463 }
464 if (isset($form['tag']['#value'])) {
465 $val['tag'] = trim($form['tag']['#value']);
466 }
467
468 form_set_value($form['tag'], NULL);
469 form_set_value($form['value_type'], NULL);
470 form_set_value($form, serialize($val));
471
472 return $form;
473 }
474
475
476 function expand_vote_properties($element) {
477 $element['#tree'] = TRUE;
478 $default_value = $element['#default_value'];
479 if (isset($default_value) && !is_array($default_value)) {
480 // we're in a funky situation where it's probably serialized.
481 $default_value = unserialize($default_value);
482 }
483
484 $element['value_type'] = array(
485 '#type' => 'select',
486 '#options' => votingapi_cache_value_types(),
487 '#default_value' => $default_value['value_type'],
488 '#spawned' => TRUE,
489 );
490
491 $element['tag'] = array(
492 '#type' => 'select',
493 '#options' => votingapi_cache_tags(),
494 '#default_value' => $default_value['tag'],
495 '#spawned' => TRUE,
496 );
497
498 return $element;
499 }
500
501 function theme_vote_properties($element) {
502 if ($element['#title'] || $element['#description']) {
503 return theme('form_element', $element['#title'], '<div class="container-inline">' . $element['#children'] . '</div>', $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
504 }
505 else {
506 return '<div class="container-inline">' . $element['#children'] . '</div>';
507 }
508 }
509
510 function vote_cache_properties_validate($form) {
511 if (isset($form['value_type']['#value'])) {
512 $val['value_type'] = trim($form['value_type']['#value']);
513 }
514 if (isset($form['tag']['#value'])) {
515 $val['tag'] = trim($form['tag']['#value']);
516 }
517 if (isset($form['function']['#value'])) {
518 $val['function'] = trim($form['function']['#value']);
519 }
520
521 form_set_value($form['tag'], NULL);
522 form_set_value($form['value_type'], NULL);
523 form_set_value($form['function'], NULL);
524 form_set_value($form, serialize($val));
525
526 return $form;
527 }
528
529
530 function expand_vote_cache_properties($element) {
531 $element['#tree'] = TRUE;
532 $default_value = $element['#default_value'];
533 if (isset($default_value) && !is_array($default_value)) {
534 // we're in a funky situation where it's probably serialized.
535 $default_value = unserialize($default_value);
536 }
537
538 $element['value_type'] = array(
539 '#type' => 'select',
540 '#options' => votingapi_cache_value_types(),
541 '#default_value' => $default_value['value_type'],
542 '#spawned' => TRUE,
543 );
544
545 $element['tag'] = array(
546 '#type' => 'select',
547 '#options' => votingapi_cache_tags(),
548 '#default_value' => $default_value['tag'],
549 '#spawned' => TRUE,
550 );
551
552 $element['function'] = array(
553 '#type' => 'select',
554 '#options' => votingapi_cache_functions(),
555 '#default_value' => $default_value['function'],
556 '#spawned' => TRUE,
557 );
558 return $element;
559 }
560
561 function theme_vote_cache_properties($element) {
562 if ($element['#title'] || $element['#description']) {
563 return theme('form_element', $element['#title'], '<div class="container-inline">' . $element['#children'] . '</div>', $element['#description'], $element['#id'], $element['#required'], form_get_error($element));
564 }
565 else {
566 return '<div class="container-inline">' . $element['#children'] . '</div>';
567 }
568 }

  ViewVC Help
Powered by ViewVC 1.1.2