There is an included votingapi_views.inc and the readme says to replace the existing on from votingapi. However with v5 there is no votingapi_views.inc as it hasn't been ported yet. Is there a way to get this file to work or is it left over from 4.7?

many thanks
Andy

Comments

hickory’s picture

Status: Active » Fixed

The one that's included should work, if you move it to the votingapi folder and make sure it's included. I'm going to look at making a separate views.inc for jrating soon as well.

AstralP’s picture

Many thanks, for anyone else needing this just add this line to votingapi.module at the top underneath the define statements:

include_once('votingapi_views.inc');

thanks again for the great module.

schnizZzla’s picture

I always try to add/change code only if really necessary.
In this case i changed the file name and saved it to

sites\all\modules\views\modules\views_votingapi.inc

It will be included automatically. No code change necessary.

I needed also to edit/save the content type that included a voting field, before the votingapi views were accessible.

hickory’s picture

That last bit probably just cleared the cache, so the views were updated.

novon’s picture

How does one use this in application?

novon’s picture

Okay, I included the file, I try to add a node by rating view or any new view with votingapi in the fields and I get this:

Fatal error: Call to undefined function: votingapi_cache_value_types() in /home/suevcom/public_html/modules/votingapi/votingapi_views.inc on line 540

hickory’s picture

There are all kinds of problems with VotingAPI's Views integration, and there's supposed to be a separate module in development. Until then, I'm going to make a separate views_jrating.inc that should cover this.

Nikkol’s picture

Any update to this? I'd like to at least show the rating results in a view, but whenevery I try to add that field I get errors similar to the others above. I followed schnizZzla's suggestion rather than changing any code.

hickory’s picture

It's really up to the VotingAPI author, or anyone else who wants to volunteer, to write the Views integration for that module. I might have time to write a separate views file for jrating (like fivestar has), but can't guarantee it as I don't personally need it.

Nikkol’s picture

I might look at doing something; although I'm totally new to drupal and to php in general. So what do others do if they want a list of the top rated nodes? (It seems silly to have the ability to rate without having the ability to easily analyze that data, which could be done by viewing the nodes differently.)

bcn’s picture

I recycled some code from the fivestar module (which is a good module also, but i didn't like how the css file was loading on every page even if it wasn't needed), and created a simple jrating_views.inc.

I'm sure that this probably isn't the best way to do this, but i needed to get something working today. I haven't tested the filters either... Use at your own risk!!!

<?php
// $Id: jrating_views.inc,v .1 2007/02/27
// This is just Nate Haug and Jeff Eaton's five-star code adapted for jrating

function jrating_views_tables() {

  $tables["jrating_results"] = array(
    "name" => "votingapi_cache",
    "provider" => "votingapi",
    "join" => array(
      "left" => array(
        "table" => "node",
        "field" => "nid"
      ),
      "right" => array(
        "field" => "content_id"
      ),
      "extra" => array(
        'content_type' => 'node',
        'value_type' => 'percent',
        'tag' => 'vote',
        'function' => 'average',
      ),
    ),
    "fields" => array(
      "value" => array(
        'name' => t("Jrating: Vote results"),
        'sortable' => true,
        'handler' => 'jrating_views_value_display_handler',
      ),
    ),
    "sorts" => array(
      "value" => array('name' => "Jrating: Vote results")
    ),
    "filters" => array(
      "value" => array(
        'name' => t("Jrating: Average vote"),
        'operator' => views_handler_operator_gtlt(),
        'handler' => 'jrating_views_nullable_field_handler',
        'help' => t("Filter by the value of a vote."),
      ),
    ),
  );

  return $tables;
}


function jrating_views_value_display_handler($op, $filter, $value, &$query) {
  if (!isset($value)) {
    return t('No votes');
  }
  else {
    return theme('jrating_static', $value);
  }
}

Also, I had to add the following two bits to the jrating.module:

if (module_exists('views')) {
  include_once(drupal_get_path('module', 'jrating') . '/jrating_views.inc');
}

And,

/**   BORROWED from Nate Haug and Jeff Eaton's Fivestar Module ;-)!!!
 * Display a plain HTML VIEW ONLY version of the widget
 * with the specified rating
 *
 * @param $rating
 *   The desired rating to display out of 100 (i.e. 80 is 4 out of 5 stars)
 * @param $stars
 *   The total number of stars this rating is out of
 * @return
 *   A themed HTML string representing the star widget
 *
*/
function theme_jrating_static($rating, $stars = 5) {
  $output = '';
  $output .= '<div class="jrating-widget-static">';
  for ($n=1; $n <= $stars; $n++) {
    $star_value = ceil((100/$stars) * $n);
    $next_star_value = ceil((100/$stars) * ($n+1));
    if ($rating > $star_value && $rating < $next_star_value) {
      $percent = (1 - ($next_star_value - $rating) / ($next_star_value - $star_value)) * 100;
      drupal_add_css(drupal_get_path('module', 'jrating') . '/jrating.css');
      $output .= '<div class="star"><span class="on" style="width: '.$percent.'%">' . $n . '</span></div>';
    }
    elseif ($rating >= $star_value) {
      $output .= '<div class="star"><span class="on">' . $n . '</span></div>';
    }
    else {
      $output .= '<div class="star"><span class="off">' . $n . '</span></div>';
    }
  }
  $output .= '</div>';
  return $output;
}

and lastly, I added this css to jrating.css:

/** Static widget css from Nate Haug and Jeff Eaton's five-star module
*
*/
div.jrating-widget-static .star span.on{background:transparent url(images/star.gif) no-repeat scroll 0pt -32px;display:block;height:100%;width:100%}

div.jrating-widget-static .star span.off{background:transparent url(images/star.gif) no-repeat scroll 0pt 0;display:block;height:100%;width:100%}

div.jrating-widget-static{display:block; width:85px;}

div.jrating-widget-static .star{float:left;height:15px;overflow:hidden;text-indent:-999em;width:17px}

hickory’s picture

Assigned: Unassigned » hickory
Category: support » feature
Status: Fixed » Needs review

Thanks noahb. I've committed a version of this to the DRUPAL-5 branch, but haven't checked all the filters and fields fully yet. I don't think the default view is working at the moment either.

bcn’s picture

You're right, that the default view doesn't seem to be working either... To be honest i didn't even check it, as i just was in a rush to get a custom view working... When i have a bit of time, i'll check to see what's up with the default view.

levelos’s picture

Hey - I just wanted to follow up on this. So is it NOT currently possible to use voting API fields with views in 5.x?

hickory’s picture

Status: Needs review » Fixed

votingapi_views.inc (in the VotingAPI package) has now been updated by the maintainer for Drupal 5.

Nikkol’s picture

I tried this new .inc file and I'm not sure it is working the way it should. I get duplicate results. One for the vote and one for the number of votes. Is this how it's suppose to work? Also, how can I show the stars in the view?

hickory’s picture

Status: Fixed » Closed (fixed)

Please post any further issues about votingapi views integration to the votingapi module issue queue.