/**
  * Calculates the rating item entry result.
  *
  * @param int $rating_item_entry_id
  * @param array $rating_items optionally used to save an additional call to the database if the
  * rating items have already been loaded
  */
 public static function calculate_rating_item_entry_result($rating_item_entry_id, $rating_items = null)
 {
     if ($rating_items == null) {
         $rating_items = Multi_Rating_API::get_rating_items(array('rating_item_entry_id' => $rating_item_entry_id));
     }
     global $wpdb;
     $query = 'SELECT * FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME . ' WHERE rating_item_entry_id = ' . esc_sql($rating_item_entry_id);
     $rating_item_entry_value_rows = $wpdb->get_results($query);
     $total_max_option_value = 0;
     $total_rating_item_result = 0;
     $total_adjusted_rating_item_result = 0;
     $star_result = 0;
     $adjusted_star_result = 0;
     $score_result = 0;
     $adjusted_score_result = 0;
     $percentage_result = 0;
     $adjusted_percentage_result = 0;
     $rating_result = 0;
     $total_weight = Multi_Rating_API::get_total_weight($rating_items);
     // use the rating items to determine total max option value
     // we do not use the entry values in case some rating items can be added/deleted
     $count_rating_items = 0;
     $total_adjusted_max_option_value = 0;
     foreach ($rating_items as $rating_item) {
         //if ($rating_item['exclude_result'] == false) {
         $total_max_option_value += $rating_item['max_option_value'];
         $total_adjusted_max_option_value += $rating_item['max_option_value'] * $rating_item['weight'];
         $count_rating_items++;
         //}
     }
     foreach ($rating_item_entry_value_rows as $rating_item_entry_value_row) {
         $rating_item_id = $rating_item_entry_value_row->rating_item_id;
         // check rating item is available, if it's been deleted it wont be included in rating result
         if (isset($rating_items[$rating_item_id]) && isset($rating_items[$rating_item_id]['max_option_value'])) {
             //if ($rating_items[$rating_item_id]['exclude_result'] == true) {
             //	continue;
             //}
             // add value and max option values
             $value = $rating_item_entry_value_row->value;
             $max_option_value = $rating_items[$rating_item_id]['max_option_value'];
             if ($value > $max_option_value) {
                 $value = $max_option_value;
             }
             // make adjustments to the rating for weights
             $weight = $rating_items[$rating_item_id]['weight'];
             // score result
             $score_result += intval($value);
             $adjusted_score_result += $value * $weight;
         } else {
             continue;
             // skip
         }
     }
     // FIXME if rating item has been deleted, this returns division by zero error
     if (count($rating_item_entry_value_rows) > 0) {
         // calculate 5 star result
         $star_result = round(doubleval($score_result) / doubleval($total_max_option_value) * 5, 2);
         $adjusted_star_result = round(doubleval($adjusted_score_result) / doubleval($total_adjusted_max_option_value) * 5, 2);
         // calculate percentage result
         $percentage_result = round(doubleval($score_result) / doubleval($total_max_option_value) * 100, 2);
         $adjusted_percentage_result = round(doubleval($adjusted_score_result) / doubleval($total_adjusted_max_option_value) * 100, 2);
         // calculate adjusted score result relative to max value
         $adjusted_score_result = round(doubleval($adjusted_score_result) / doubleval($total_adjusted_max_option_value) * $total_max_option_value, 2);
     }
     return array('adjusted_star_result' => $adjusted_star_result, 'star_result' => $star_result, 'total_max_option_value' => $total_max_option_value, 'adjusted_score_result' => $adjusted_score_result, 'score_result' => $score_result, 'percentage_result' => $percentage_result, 'adjusted_percentage_result' => $adjusted_percentage_result);
 }