Advertisement
wizonesolutions

votingapi custom average with array example

Mar 19th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. function mymod_get_votes($type, $cid, $tag = 'vote', $uid = NULL) {
  2.   global $user;
  3.  
  4. $percent_threshold = variable_get('mymod_percent', 50);
  5. $number_threshold = variable_get('mymod_number', 5);
  6. $unpublished = 0;
  7.  
  8.   $criteria = array(
  9.     'entity_type' => $type,
  10.     'entity_id' => $cid,
  11.     'value_type' => 'points',//new addition - links to view. specifies that it is in points now, rather than percent.
  12.     'tag' => $tag,
  13.   );
  14.  
  15.   $votes = array(
  16.     'average' => array(),
  17.     'count' => array(),
  18.     'user' => array(),
  19.   );
  20.  
  21. /**  $vount = array(
  22.   'val' => array()
  23.   );
  24. */
  25.    
  26.   $results = votingapi_select_results($criteria);
  27.  
  28. // wizonesolutions START ----------------
  29. foreach ($results as $result) {
  30.   $content_id = $result['content_id'];
  31.   if ($result['function'] == 'sum') { // Add as many of these as you need for however many functions you are using
  32.     $values[$content_id]['sum'] = $result['value'];
  33.   }
  34.   elseif ($result['function'] = 'count') {
  35.     $values[$content_id]['count'] = $result['value'];
  36.   }
  37.   // Add more elseifs if using other functions
  38.   $values[$content_id]['result'] = $result; // Save the original value of $result for our
  39. }
  40.  
  41.   foreach ($results as $result) {
  42.     if ($result['function'] == 'average') {
  43.       $votes['average'] = $result;
  44.     }
  45.     if ($result['function'] == 'points') {
  46.       $votes['count'] = $result;
  47.     }
  48.   }
  49.   //dsm(print_r($vount['val']));
  50.  // dsm(print_r($percent_threshold));
  51.    
  52.  
  53.   foreach ($values as $value){ // changed this too
  54.       $val_x = $value['sum'];
  55.       dsm(print_r('And the sum for node '));
  56.       dsm(print_r($value['result']['content_id']));
  57.       dsm(print_r(' is:'));
  58.       dsm(print_r($value['sum']));
  59.       dsm(print_r('; '));
  60.       dsm(print_r(' percentage '));
  61.       dsm(print_r (round($percentout)));
  62.       //dsm(print_r($val_x));
  63.    
  64.   $vount = $value['count'];
  65.       dsm(print_r('Hello friend! The count for node '));
  66.       dsm(print_r($value['result']['content_id']));
  67.       dsm(print_r(' is:'));
  68.       dsm(print_r($value['count']));
  69.       dsm(print_r('; '));
  70.       }
  71. // wizonesolutions END ------------------
  72.         if ($val_x<0){
  73.         $poscount = ($vount+$val_x)/2;//note that we add here because it is a negative value, so adding is actually subtracting. Get the count, subtract the negative value, divide by two. You're now left with only the positive votes.
  74.         $percentout = ($poscount/$vount)*100; //now just use the positive votes like a normal percentage sum.
  75.         }
  76.         elseif($val_x>0){
  77.         $percentout = $val_x/$vount*100;
  78.         }
  79.        
  80.         else{
  81.         $percentout = 50;
  82.         }
  83.     }  //foreach end
  84.  
  85.   } // main function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement