I want to be able to display a sortable table of nodes listing each node and the number of votes and average votes for that node. I'm trying to create this myself, but I'm stuck. Here you can find what I have done so far. Please let me know if I'm going about this in the correct way and what I need to do next to make it work.

Comments

Jesse Grosjean’s picture

Here's some code that will do this. I'm not sure how best to (or if it's really necessary) integrate with the voting.module, but for me at least it's quite useful.

  $header = array(
    array('data' => t('title'), 'field' => 'n.title'),
    array('data' => t('changed'), 'field' => 'n.changed', 'sort' => 'desc'),
    array('data' => t('rating'), 'field' => 'v.vavg'),
    array('data' => t('votes'), 'field' => 'v.vcount'),
  );

  $sql = "SELECT n.nid, n.title,"
       . " n.created, n.changed, count(v.vote) vcount, avg(v.vote) vavg"
       . " FROM {node} n"
       . " LEFT JOIN {votes} v ON n.nid = v.content_id"
       . " WHERE n.type = 'contribution'"
       . " GROUP BY n.nid, n.title, n.created, n.changed";

  $sql .= tablesort_sql($header);
  $result = pager_query(db_rewrite_sql($sql), 50);
    
  while ($row = db_fetch_object($result)) {
    $rows[] = array(l($row->title, "node/$row->nid"), date("Y-m-d", $row->changed), number_format($row->vavg, 1), $row->vcount);
  }
  
  if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
    $rows[] = array(array('data' => $pager, 'colspan' => 3));
  }

  $output .= theme('table', $header, $rows);
duaelfr’s picture

Status: Active » Closed (won't fix)

This version of Voting is not supported anymore. The issue is closed for this reason.
Please upgrade to a supported version and feel free to reopen the issue on the new version if applicable.

This issue has been automagically closed by a script.