/** * Tests getting two rating item for a rating form but three rating items exist in db * * @group func */ public function test_get_rating_items2() { 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; $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 3', 'max_option_value' => 5)); $rating_item_id3 = $wpdb->insert_id; $rating_items = Multi_Rating_API::get_rating_items(); $this->assertEquals(3, count($rating_items)); }
/** * Edits a rating and redirect back to the entries page if successful */ function mr_edit_rating() { // get the entry id $entry_id = null; if (isset($_GET['entry-id'])) { $entry_id = $_GET['entry-id']; } else { if (isset($_POST['entry-id'])) { $entry_id = $_POST['entry-id']; } } $rating_items = Multi_Rating_API::get_rating_items(array('rating_item_entry_id' => $entry_id)); global $wpdb; // get post id $post_id_query = 'SELECT rie.post_id as post_id' . ' FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME . ' AS rie' . ' WHERE rating_item_entry_id = "' . $entry_id . '"'; $post_id = $wpdb->get_var($post_id_query, 0, 0); if ($post_id == null) { echo '<div class="error"><p>' . __('An error occured', 'multi-rating') . '</p></div>'; return; } foreach ($rating_items as $rating_item) { $rating_item_id = $rating_item['rating_item_id']; $rating_item_value = isset($_POST['rating-item-' . $rating_item_id]) ? $_POST['rating-item-' . $rating_item_id] : null; if ($rating_item_value != null) { $query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME . ' WHERE rating_item_entry_id = "' . $entry_id . '" AND rating_item_id = "' . $rating_item_id . '"'; $rows = $wpdb->get_col($query, 0); if ($rows[0] == 0) { $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $entry_id, 'rating_item_id' => $rating_item_id, 'value' => $rating_item_value), array('%d', '%d', '%d')); } else { $wpdb->update($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('value' => $rating_item_value), array('rating_item_entry_id' => $entry_id, 'rating_item_id' => $rating_item_id)); } } } $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS); $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, null); } // redirect back to entries page $entries_page = 'admin.php?page=mr_rating_results&tab=mr_entries&entry-id=' . $entry_id . '&post-id=' . $post_id; if (isset($_REQUEST['username'])) { $entries_page .= '&username='******'username']; } if (isset($_REQUEST['to-date'])) { $entries_page .= '&to-date=' . $_REQUEST['to-date']; } if (isset($_REQUEST['from-date'])) { $entries_page .= '&from-date=' . $_REQUEST['from-date']; } if (isset($_REQUEST['comments-only'])) { $entries_page .= '&comments-only=' . $_REQUEST['comments-only']; } if (isset($_REQUEST['paged'])) { $entries_page .= '&paged=' . $_REQUEST['paged']; } wp_redirect($entries_page); exit; }
/** * 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; }
/** * Displays the rating form * * @param unknown_type $params */ public static function display_rating_form($params = array()) { $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS); $custom_text_settings = (array) get_option(Multi_Rating::CUSTOM_TEXT_SETTINGS); $position_settings = (array) get_option(Multi_Rating::POSITION_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]; extract(wp_parse_args($params, array('post_id' => null, 'title' => $custom_text_settings[Multi_Rating::RATING_FORM_TITLE_TEXT_OPTION], 'before_title' => '<h4>', 'after_title' => '</h4>', 'submit_button_text' => $custom_text_settings[Multi_Rating::SUBMIT_RATING_FORM_BUTTON_TEXT_OPTION], 'echo' => true, 'class' => ''))); // 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 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()); } MR_Rating_Form::$sequence++; $rating_items = Multi_Rating_API::get_rating_items(array()); ob_start(); mr_get_template_part('rating-form', null, true, array('title' => $title, 'before_title' => $before_title, 'after_title' => $after_title, 'submit_button_text' => $submit_button_text, 'class' => $class, 'post_id' => $post_id, 'rating_items' => $rating_items, 'icon_classes' => $icon_classes, 'use_custom_star_images' => $use_custom_star_images)); $html = ob_get_contents(); ob_end_clean(); $html = apply_filters('mr_template_html', $html); if ($echo == true) { echo $html; } return $html; }
/** * 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; }
/** * Default column * @param unknown_type $item * @param unknown_type $column_name * @return unknown|mixed */ function column_default($item, $column_name) { $post_id = $item[MR_Rating_Results_Table::POST_ID_COLUMN]; switch ($column_name) { case MR_Rating_Results_Table::SHORTCODE_COLUMN: echo '[mr_rating_result post_id="' . $post_id . '"]'; break; case MR_Rating_Results_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_Results_Table::ACTION_COLUMN: ?> <a class="view-rating-result-entries-anchor" href="?page=<?php echo Multi_Rating::RATING_RESULTS_PAGE_SLUG; ?> &tab=<?php echo Multi_Rating::ENTRIES_TAB; ?> &post-id=<?php echo $post_id; ?> "><?php _e('View Entries', 'multi-rating'); ?> </a> <?php break; case MR_Rating_Results_Table::ENTRIES_COUNT_COLUMN: global $wpdb; $query = $query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME . ' WHERE post_id = "' . $post_id . '"'; $rows = $wpdb->get_col($query, 0); echo $rows[0]; break; case MR_Rating_Results_Table::RATING_RESULT_COLUMN: $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)); $entries = $rating_result['count']; if ($entries != 0) { 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>'; } else { _e('None', 'multi-rating'); } break; case Rating_Item_Entry_Table::CHECKBOX_COLUMN: return $item[$column_name]; break; default: return print_r($item, true); } }