示例#1
0
 /**
  * Displays the rating result
  * 
  * @param unknown_type $atts
  * @return void|string
  */
 public static function display_rating_result($params = array())
 {
     $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS);
     $custom_text_settings = (array) get_option(Multi_Rating::CUSTOM_TEXT_SETTINGS);
     $style_settings = (array) get_option(Multi_Rating::STYLE_SETTINGS);
     $font_awesome_version = $style_settings[Multi_Rating::FONT_AWESOME_VERSION_OPTION];
     $icon_classes = MR_Utils::get_icon_classes($font_awesome_version);
     $use_custom_star_images = $style_settings[Multi_Rating::USE_CUSTOM_STAR_IMAGES];
     $image_width = $style_settings[Multi_Rating::CUSTOM_STAR_IMAGE_WIDTH];
     $image_height = $style_settings[Multi_Rating::CUSTOM_STAR_IMAGE_HEIGHT];
     extract(wp_parse_args($params, array('post_id' => null, 'no_rating_results_text' => $custom_text_settings[Multi_Rating::NO_RATING_RESULTS_TEXT_OPTION], 'show_rich_snippets' => false, 'show_title' => false, 'show_count' => true, 'echo' => true, 'result_type' => Multi_Rating::STAR_RATING_RESULT_TYPE, 'class' => '', 'before_count' => '(', 'after_count' => ')')));
     if (is_string($show_rich_snippets)) {
         $show_rich_snippets = $show_rich_snippets == 'true' ? true : false;
     }
     if (is_string($show_title)) {
         $show_title = $show_title == 'true' ? true : false;
     }
     if (is_string($show_count)) {
         $show_count = $show_count == 'true' ? true : false;
     }
     if (is_string($echo)) {
         $echo = $echo == 'true' ? true : false;
     }
     // get the post id
     global $post;
     if (!isset($post_id) && isset($post)) {
         $post_id = $post->ID;
     } else {
         if (!isset($post) && !isset($post_id)) {
             return;
             // No post Id available to display rating form
         }
     }
     // WPML get original post id for default language to get rating results
     $temp_post_id = $post_id;
     if (function_exists('icl_object_id')) {
         global $sitepress;
         $temp_post_id = icl_object_id($post_id, get_post_type($post_id), true, $sitepress->get_default_language());
     }
     $rating_result = Multi_Rating_API::get_rating_result($temp_post_id);
     $rating_result['post_id'] = $post_id;
     // set back to adjusted for WPML
     ob_start();
     mr_get_template_part('rating-result', null, true, array('no_rating_results_text' => $no_rating_results_text, 'show_rich_snippets' => $show_rich_snippets, 'show_title' => $show_title, 'show_date' => false, 'show_count' => $show_count, 'no_rating_results_text' => $no_rating_results_text, 'result_type' => $result_type, 'class' => $class . ' rating-result-' . $post_id, 'rating_result' => $rating_result, 'before_count' => $before_count, 'after_count' => $after_count, 'post_id' => $post_id, 'ignore_count' => false, 'preserve_max_option' => false, 'icon_classes' => $icon_classes, 'use_custom_star_images' => $use_custom_star_images, 'image_width' => $image_width, 'image_height' => $image_height));
     $html = ob_get_contents();
     ob_end_clean();
     $html = apply_filters('mr_template_html', $html);
     if ($echo == true) {
         echo $html;
     }
     return $html;
 }
 /**
  * Tests rating result for two rating items in a rating form. Checks for 1 entry 
  * and then 2 entries
  * 
  * @group func
  */
 public function test_rating_result3()
 {
     global $wpdb;
     $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 1', 'max_option_value' => 5));
     $rating_item_id1 = $wpdb->insert_id;
     $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 2', 'max_option_value' => 3));
     $rating_item_id2 = $wpdb->insert_id;
     $user_id = $this->factory->user->create();
     $post_id = $this->factory->post->create(array('post_title' => 'Test Post'));
     /*
      * Entry 1
      * 3.125 stars, 5/8 score, 62.5% (0.625)
      */
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME, array('post_id' => $post_id, 'user_id' => $user_id), array('%d', '%d'));
     $rating_entry_id = $wpdb->insert_id;
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id1, 'value' => 4), array('%d', '%d', '%d'));
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id2, 'value' => 1), array('%d', '%d', '%d'));
     $rating_result = Multi_Rating_API::get_rating_result($post_id);
     // 3.125 stars, 5/8 score, 62.5% (.625)
     $this->assertEquals(1, $rating_result['count']);
     // FIXME count_entries
     $this->assertEquals(3.13, $rating_result['adjusted_star_result']);
     $this->assertEquals(5, $rating_result['adjusted_score_result']);
     $this->assertEquals(8, $rating_result['total_max_option_value']);
     $this->assertEquals(62.5, $rating_result['adjusted_percentage_result']);
     $this->assertEquals($post_id, $rating_result['post_id']);
     /*
      * Entry 2 anonymous
      * 5 stars, 8/8 score and 100%
      */
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME, array('post_id' => $post_id, 'user_id' => 0), array('%d', '%d'));
     $rating_entry_id = $wpdb->insert_id;
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id1, 'value' => 5), array('%d', '%d', '%d'));
     $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id2, 'value' => 3), array('%d', '%d', '%d'));
     delete_post_meta($post_id, Multi_Rating::RATING_RESULTS_POST_META_KEY);
     /*
      * Rating result
      */
     $rating_result = Multi_Rating_API::get_rating_result($post_id);
     // 4.06 stars ( ( 3.13 + 5 )  / 2 = 4.065 ), 6.5 ( ( 5 + 8 ) / 2 ), 81.25 ( 0.8125)
     $this->assertEquals(2, $rating_result['count']);
     // FIXME count_entries
     $this->assertEquals(4.07, $rating_result['adjusted_star_result']);
     $this->assertEquals(6.5, $rating_result['adjusted_score_result']);
     $this->assertEquals(8, $rating_result['total_max_option_value']);
     $this->assertEquals(81.25, $rating_result['adjusted_percentage_result']);
     $this->assertEquals($post_id, $rating_result['post_id']);
 }