Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Custom Five Star Module (multiple votes)

Parent Feed: 

This widget allows the Drupal Five Star module to accept inputs from multiple votes for various criteria and average it into one, It then displays the average of the multiple votes.

To create views sortable by the new widget (the average of five votes), hook_votingapi_results_alter() was used in a small custom module.

Below is a tutorial on how to implement the code.

Best practices using multiple votes criteria In node using FiveStar module in Drupal 6.

1. Enable Fivestar module, Voting API.
2. Go to edit content type and Enable Fivestar rating.
3. Add CCK field Type Fivestar Rating with “Fivestar rating” Select list widget.
4. Configure this CCK field: add Voting Axis: which is the multiple criteria separated by a comma. For example: first, second, third, fourth, fifth . Save field settings.
5. Create the template for node type. In this example it is node type is “teacher”. This will be node-teacher.tpl.php .

In the template paste this snippet:

$output = '';
$tags = array(
'first' = t('Communication'),
'second' = t(‘Availability'),
'third' = t(‘Skills'),
'fourth' = t(‘Efficiency'),
'fifth' = t(‘Personality'),
);
$i = 0;
foreach ($tags as $tag = $title) {
$votes = fivestar_get_votes('node', $node->nid, $tag);
if(!empty($votes['average']['value'])){
$i++;
}
$values = array(
'user' = isset($votes['user']['value']) ? $votes['user']['value'] : NULL,
'average' = isset($votes['average']['value']) ? $votes['average']['value'] : NULL,
'count' = isset($votes['count']['value']) ? $votes['count']['value'] : NULL,
);

if (user_access('rate content')) { /*check user access for voting in fivestar*/
$settings = array(
'stars' = 5,
'allow_clear' = TRUE,
'style' = 'average',
'text' = 'dual',
'content_type' = 'node',
'content_id' = $node->nid,
'tag' = $tag,
'autosubmit' = TRUE,
'title' = $title,
'feedback_enable' = TRUE,
'labels_enable' = TRUE,
'labels' = array(t('Poor'), t('Okay'), t('Good'), t('Great'), t('Awesome')),
);
$output .= drupal_get_form('fivestar_custom_widget', $values, $settings);
}
else {
$output .= theme_fivestar_static($values['average'], 5, $tag);
}
}

$reliability_rating = votingapi_select_results(array('content_id' = $node->nid, 'tag' =array('first', 'second', 'third', 'fourth', 'fifth' ), 'function' = 'average'));
foreach ($reliability_rating as $v) {
$a = $a + $v['value]; /*sum values*/
}
?>
print theme('fivestar_static', $a/$i, '5') ; /* print fivestar widget with general average result*/
?>
print $output; /*print 5 fivestar fields for votes with average result each*/
?>
Check out the module used in this social networking site for yoga teachers, (ignore debug output at top)

Sort By Votes

To create views sortable by votes here is a small custom module that uses hook_votingapi_results_alter().  This is a complete custom module so save this code in a .module file with it’s own .info file.
[php]
function mymodule_votingapi_results_alter(&$results, $content_type, $content_id) {
$vote_avg_sum = 0;
$vote_avg_count = 0;
$vote_tags = 0;

foreach($results as $tag = $data) {
if($tag != ‘vote’) {
$vote_avg_sum += $data[‘percent’][‘average’];
$vote_avg_count += $data[‘percent’][‘count’];
$vote_tags++;
}
}
if($vote_tags > 0) {
$results[‘vote’][‘percent’][‘average’] = $vote_avg_sum/$vote_tags;
$results[‘vote’][‘percent’][‘count’] = $vote_avg_count/$vote_tags;
}
}</code>

1.In views settings add relationships
“Node: Vote results ”

2. configure it
“Value type: No filtering” “Vote tag: Normal vote” “Aggregation function:No filtering”

3. Go to Sort criteria and add
“Vote results: Value”
choose Descending or Ascending.

4. Add the filters that you need.

5. Then you can add to your fields
“Vote results: Value”

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web