Ejemplo n.º 1
0
/**
 * Generates rating results in CSV format.
 *
 * @param $file_name the file_name to save
 * @param $filters used to filter the report e.g. from_date, to_date, user_id etc...
 * @returns true if report successfully generated and written to file
 */
function mr_generare_csv_report($file_name, $filters)
{
    $rating_item_entries = Multi_Rating_API::get_rating_item_entries($filters);
    $header_row = __('Entry Id', 'multi-rating') . ', ' . __('Entry Date', 'multi-rating') . ', ' . __('Post Id', 'multi-rating') . ', ' . __('Post Title', 'multi-rating') . ', ' . __('Score Rating Result', 'multi-rating') . ', ' . __('Adjusted Score Rating Result', 'multi-rating') . ', ' . __('Total Max Option Value', 'multi-rating') . ', ' . __('Percentage Rating Result', 'multi-rating') . ', ' . __('Adjusted Percentage Rating Result', 'multi-rating') . ', ' . __('Star Rating Result', 'multi-rating') . ', ' . __('Adjusted Star Rating Result', 'multi-rating') . ', ' . __('User Id', 'multi-rating');
    $export_data_rows = array($header_row);
    // iterate all found rating item entries and create row in report
    if (count($rating_item_entries) > 0) {
        foreach ($rating_item_entries as $rating_item_entry) {
            $post_id = $rating_item_entry['post_id'];
            $rating_item_entry_id = $rating_item_entry['rating_item_entry_id'];
            $rating_items = Multi_Rating_API::get_rating_items(array('post' => $post_id, 'rating_item_entry_id' => $rating_item_entry_id));
            $rating_result = Multi_Rating_API::calculate_rating_item_entry_result($rating_item_entry_id, $rating_items);
            // FIXME some fields may have a comma (e.g. post title, comment etc..) causing formatting issues
            $current_row = $rating_item_entry_id . ', ' . $rating_item_entry['entry_date'] . ', ' . $post_id . ', ' . get_the_title($post_id) . ', ' . $rating_result['score_result'] . ', ' . $rating_result['adjusted_score_result'] . ', ' . $rating_result['total_max_option_value'] . ', ' . $rating_result['percentage_result'] . ', ' . $rating_result['adjusted_percentage_result'] . ', ' . $rating_result['star_result'] . ', ' . $rating_result['adjusted_star_result'] . ', ' . $rating_item_entry['user_id'];
            array_push($export_data_rows, $current_row);
        }
    }
    // write to file
    $file = null;
    try {
        $file = fopen($file_name, 'w');
        foreach ($export_data_rows as $row) {
            fputcsv($file, explode(',', $row));
        }
        fclose($file);
    } catch (Exception $e) {
        return false;
    }
    return true;
}
Ejemplo n.º 2
0
 /**
  * Calculates the rating result of a rating form for a post with filters for user_id
  *
  * @param array $params post_id, rating_items
  * @return rating result
  */
 public static function calculate_rating_result($params = array())
 {
     if (!isset($params['rating_items']) || !isset($params['post_id'])) {
         return;
     }
     $rating_items = $params['rating_items'];
     $post_id = $params['post_id'];
     $rating_item_entries = Multi_Rating_API::get_rating_item_entries(array('post_id' => $post_id));
     $score_result_total = 0;
     $adjusted_score_result_total = 0;
     $star_result_total = 0;
     $adjusted_star_result_total = 0;
     $percentage_result_total = 0;
     $adjusted_percentage_result_total = 0;
     $total_max_option_value = 0;
     $count_entries = count($rating_item_entries);
     // get max option value
     $total_max_option_value = 0;
     foreach ($rating_items as $rating_item) {
         $total_max_option_value += $rating_item['max_option_value'];
     }
     $count_entries = count($rating_item_entries);
     // process all entries for the post and construct a rating result for each post
     foreach ($rating_item_entries as $rating_item_entry) {
         $total_value = 0;
         // retrieve the entry values for each rating item
         $rating_item_entry_id = $rating_item_entry['rating_item_entry_id'];
         // do not pass rating items as not all entries may have all rating items
         // (e.g. rating items added or deleted)
         $rating_result = Multi_Rating_API::calculate_rating_item_entry_result($rating_item_entry_id, null);
         // this mean total max option value may also be different, so adjust the score result if it is as
         // this is out of the total max option value
         $adjustment = 1.0;
         if ($rating_result['total_max_option_value'] != $total_max_option_value) {
             $adjustment = $total_max_option_value / $rating_result['total_max_option_value'];
         }
         $score_result_total += $rating_result['score_result'] * $adjustment;
         $adjusted_score_result_total += $rating_result['adjusted_score_result'] * $adjustment;
         $star_result_total += $rating_result['star_result'];
         $adjusted_star_result_total += $rating_result['adjusted_star_result'];
         $percentage_result_total += $rating_result['percentage_result'];
         $adjusted_percentage_result_total += $rating_result['adjusted_percentage_result'];
     }
     $score_result = 0;
     $adjusted_score_result = 0;
     $star_result = 0;
     $adjusted_star_result = 0;
     $percentage_result = 0;
     $adjusted_percentage_result = 0;
     $overall_rating_result = 0;
     $overall_adjusted_rating_result = 0;
     if ($count_entries > 0) {
         // calculate 5 star result
         $score_result = round(doubleval($score_result_total) / $count_entries, 2);
         $adjusted_score_result = round(doubleval($adjusted_score_result_total) / $count_entries, 2);
         // calculate star result
         $star_result = round(doubleval($star_result_total) / $count_entries, 2);
         $adjusted_star_result = round(doubleval($adjusted_star_result_total) / $count_entries, 2);
         // calculate percentage result
         $percentage_result = round(doubleval($percentage_result_total) / $count_entries, 2);
         $adjusted_percentage_result = round(doubleval($adjusted_percentage_result_total) / $count_entries, 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, 'count' => $count_entries, 'post_id' => $post_id);
 }
    /**
     * Column default
     * 
     * @param $item
     * @param $column_name
     * @return 
     */
    function column_default($item, $column_name)
    {
        switch ($column_name) {
            case MR_Rating_Entry_Table::SHORTCODE_COLUMN:
                echo '[mr_rating_result post_id="' . $item[MR_Rating_Entry_Table::POST_ID_COLUMN] . '"]';
                break;
            case MR_Rating_Entry_Table::ENTRY_DATE_COLUMN:
                echo date('F j, Y, g:i a', strtotime($item[$column_name]));
                break;
            case MR_Rating_Entry_Table::CHECKBOX_COLUMN:
                return $item[$column_name];
                break;
            case MR_Rating_Results_Table::POST_ID_COLUMN:
                $post_id = $item[MR_Rating_Entry_Table::POST_ID_COLUMN];
                $temp_post_id = $post_id;
                // WPML get adjusted post id for active language, just for the string translation
                if (function_exists('icl_object_id')) {
                    $temp_post_id = icl_object_id($post_id, get_post_type($post_id), true, ICL_LANGUAGE_CODE);
                }
                $post_link = esc_html(get_the_title($temp_post_id));
                if (current_user_can('edit_post', $temp_post_id)) {
                    $post_link = "<a href='" . esc_url(get_edit_post_link($temp_post_id)) . "'>";
                    $post_link .= esc_html(get_the_title($temp_post_id)) . '</a>';
                }
                echo $post_link . ' (Id=' . $post_id . ')';
                break;
            case MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN:
            case MR_Rating_Entry_Table::IP_ADDRESS_COLUMN:
            case MR_Rating_Entry_Table::USER_ID_COLUMN:
                echo $item[$column_name];
                break;
            case MR_Rating_Entry_Table::RATING_RESULT_COLUMN:
                $rating_result = Multi_Rating_API::calculate_rating_item_entry_result($item[MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN], null);
                echo __('Star: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_star_result'], 2) . '/5</span><br />' . __('Score: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_score_result'], 2) . '/' . $rating_result['total_max_option_value'] . '</span><br />' . __('Percentage: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_percentage_result'], 2) . '%</span>';
                break;
            case MR_Rating_Entry_Table::ACTION_COLUMN:
                // do not need to pass post id and rating form id
                $url = '?page=mr_edit_rating&entry-id=' . $item[MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN];
                if (isset($_REQUEST['username'])) {
                    $url .= '&username='******'username'];
                }
                if (isset($_REQUEST['to-date'])) {
                    $url .= '&to-date=' . $_REQUEST['to-date'];
                }
                if (isset($_REQUEST['from-date'])) {
                    $url .= '&from-date=' . $_REQUEST['from-date'];
                }
                if (isset($_REQUEST['paged'])) {
                    $url .= '&paged=' . $_REQUEST['paged'];
                }
                ?>
				
				<a class="edit-rating-anchor" href="<?php 
                echo $url;
                ?>
"><?php 
                _e('Edit Rating', 'multi-rating');
                ?>
</a>
				
				<?php 
                break;
            default:
                return print_r($item, true);
        }
    }
Ejemplo n.º 4
0
 /**
  * Saves a rating form entry.
  */
 public static function save_rating()
 {
     $ajax_nonce = $_POST['nonce'];
     if (wp_verify_nonce($ajax_nonce, Multi_Rating::ID . '-nonce')) {
         global $wpdb;
         $rating_items = $_POST['ratingItems'];
         $post_id = $_POST['postId'];
         $ip_address = MR_Utils::get_ip_address();
         $entry_date_mysql = current_time('mysql');
         $sequence = isset($_POST['sequence']) ? $_POST['sequence'] : '';
         // WPML get original pst id for default language
         if (function_exists('icl_object_id')) {
             global $sitepress;
             $post_id = icl_object_id($post_id, get_post_type($post_id), true, $sitepress->get_default_language());
         }
         $data = array('sequence' => $sequence, 'post_id' => $post_id);
         $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS);
         $custom_text_settings = (array) get_option(Multi_Rating::CUSTOM_TEXT_SETTINGS);
         // get user id
         global $wp_roles;
         $user = wp_get_current_user();
         $user_id = $user->ID;
         // stores any validation results, custom validation results can be added through filters
         $validation_results = array();
         $validation_results = MR_Utils::validate_save_rating_restricton($validation_results, $post_id);
         $validation_results = MR_Utils::validate_rating_item_required($validation_results, $rating_items);
         $validation_results = apply_filters('mr_after_rating_form_validation_save', $validation_results, $data);
         if (MR_Utils::has_validation_error($validation_results)) {
             echo json_encode(array('status' => 'error', 'data' => $data, 'validation_results' => $validation_results));
             die;
         }
         // everything is OK so now insert the rating form entry and entry values into the database tables
         $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME, array('post_id' => $post_id, 'entry_date' => $entry_date_mysql, 'ip_address' => $ip_address, 'user_id' => $user_id), array('%d', '%s', '%s', '%d'));
         $rating_entry_id = $wpdb->insert_id;
         foreach ($rating_items as $rating_item) {
             $rating_item_id = $rating_item['id'];
             $rating_item_value = $rating_item['value'];
             $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_id, 'value' => $rating_item_value), array('%d', '%d', '%d'));
         }
         // Set cookie if restriction type is used
         foreach ($general_settings[Multi_Rating::SAVE_RATING_RESTRICTION_TYPES_OPTION] as $save_rating_restriction_type) {
             if ($save_rating_restriction_type == 'cookie') {
                 if (!headers_sent()) {
                     $save_rating_restriction_hours = $general_settings[Multi_Rating::SAVE_RATING_RESTRICTION_HOURS_OPTION];
                     setcookie(Multi_Rating::POST_SAVE_RATING_COOKIE . '-' . $post_id, true, time() + 60 * 60 * $save_rating_restriction_hours, COOKIEPATH, COOKIE_DOMAIN, false, true);
                 }
                 break;
             }
         }
         $rating_items = Multi_Rating_API::get_rating_items(array('post_id' => $post_id));
         $rating_result = Multi_Rating_API::calculate_rating_result(array('post_id' => $post_id, 'rating_items' => $rating_items));
         $rating_results_cache = $general_settings[Multi_Rating::RATING_RESULTS_CACHE_OPTION];
         if ($rating_results_cache == true) {
             // update rating results cache
             update_post_meta($post_id, Multi_Rating::RATING_RESULTS_POST_META_KEY, $rating_result);
         }
         $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];
         $rating_results_position = get_post_meta($post_id, Multi_Rating::RATING_RESULTS_POSITION_POST_META, true);
         $position_settings = (array) get_option(Multi_Rating::POSITION_SETTINGS);
         // use default rating results position
         if ($rating_results_position == '') {
             $rating_results_position = $position_settings[Multi_Rating::RATING_RESULTS_POSITION_OPTION];
         }
         ob_start();
         mr_get_template_part('rating-result', null, true, array('no_rating_results_text' => '', 'show_rich_snippets' => false, 'show_title' => false, 'show_date' => false, 'show_count' => true, 'result_type' => Multi_Rating::STAR_RATING_RESULT_TYPE, 'class' => 'rating-result-' . $post_id . ' ' . $rating_results_position . ' mr-filter', 'rating_result' => $rating_result, 'before_count' => '(', 'after_count' => ')', 'post_id' => $post_id, 'ignore_count' => false, 'preserve_max_option' => false, 'before_date' => '', 'after_date' => '', '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();
         $data['html'] = $html;
         // if the custom text does not contain %, then there's no need to substitute the message
         $message = $custom_text_settings[Multi_Rating::RATING_FORM_SUBMIT_SUCCESS_MESSAGE_OPTION];
         if (strpos($message, '%') !== false) {
             $message = MR_Utils::substitute_message($message, $user, Multi_Rating_API::calculate_rating_item_entry_result($rating_entry_id, $rating_items));
         }
         $data['rating_result'] = $rating_result;
         $data['hide_rating_form'] = $general_settings[Multi_Rating::HIDE_RATING_FORM_AFTER_SUBMIT_OPTION];
         echo json_encode(array('status' => 'success', 'data' => $data, 'message' => $message, 'validation_results' => $validation_results));
     }
     die;
 }