Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds functions to account for activity deletion. Adds functions that …

…allow for accurate testing of whether a user has already left a rating.
  • Loading branch information...
commit 6d74553c17bb4c8392b4831e06e9b4f5d5dda675 1 parent b0a596a
@boonebgorges authored
View
59 bp-group-reviews.php
@@ -22,7 +22,8 @@ function __construct() {
add_action( 'wp_head', array( $this, 'maybe_previous_data' ), 999 );
add_action( 'wp_print_styles', array( $this, 'load_styles' ) );
add_action( 'wp', array( $this, 'grab_cookie' ) );
- add_filter( 'bp_has_activities', array( $this, 'activities_template_data' ) );
+ add_filter( 'bp_has_activities', array( $this, 'activities_template_data' ) );
+ add_action( 'bp_activity_action_delete_activity', array( $this, 'delete_activity' ), 10, 2 );
}
function includes() {
@@ -71,6 +72,7 @@ function setup_globals() {
global $bp;
$bp->group_reviews->slug = BP_GROUP_REVIEWS_SLUG;
+ $bp->group_rewiews->allow_multiples = apply_filters( 'bpgr_allow_multiples_default', false );
$image_types = array(
'star',
@@ -127,6 +129,9 @@ function activities_template_data( $has_activities ) {
}
$activity_ids = implode( ',', $activity_ids );
+ if ( empty( $activity_ids ) )
+ return $has_activities;
+
$sql = apply_filters( 'bpgr_activities_data_sql', $wpdb->prepare( "SELECT activity_id, meta_value AS rating FROM {$bp->activity->table_name_meta} WHERE activity_id IN ({$activity_ids}) AND meta_key = 'bpgr_rating'" ) );
$ratings_raw = $wpdb->get_results( $sql, ARRAY_A );
@@ -171,6 +176,58 @@ function group_is_reviewable( $group_id ) {
return apply_filters( 'bpgr_group_is_reviewable', $is_reviewable, $group_id );
}
+
+ function delete_activity( $activity_id, $user_id ) {
+ $activity = new BP_Activity_Activity( $activity_id );
+ $group_id = $activity->item_id;
+
+ // First check whether the current user has more than one group review
+ $args = array(
+ 'user_id' => $user_id,
+ 'type' => 'review',
+ 'item_id' => $group_id
+ );
+
+ if ( bp_has_activities() ) {
+ if ( bp_get_activity_count() > 1 ) {
+ // If so, then remove the user from the list of reviewers
+ $has_posted = (array)groups_get_groupmeta( $group_id, 'posted_review' );
+
+ // Because of a previous bug, we have to remove *all* instances
+ $keys = array_keys( $has_posted, $user_id );
+ foreach( (array)$keys as $key ) {
+ unset( $has_posted[$key] );
+ }
+ $has_posted = array_values( $has_posted );
+
+ groups_update_groupmeta( $group_id, 'posted_review', $has_posted );
+ }
+ }
+
+ // Next, remove the rating from the list of ratings
+ $group_ratings = groups_get_groupmeta( $group_id, 'bpgr_ratings' );
+ if ( !empty( $group_ratings[$activity_id] ) )
+ unset( $group_ratings[$activity_id] );
+ groups_update_groupmeta( $group_id, 'bpgr_ratings', $group_ratings );
+
+ // Then recalculate the total number of ratings and the average
+ // Pull the composite scores and recalculate
+ if ( !$rating = groups_get_groupmeta( $group_id, 'bpgr_rating' ) )
+ $avg_score = 0;
+ if ( !$how_many = groups_get_groupmeta( $group_id, 'bpgr_how_many_ratings' ) )
+ $how_many = 0;
+
+ // In order to account for recording errors, we will recalculate based on data
+ $raw_score = 0;
+ foreach( $group_ratings as $score ) {
+ $raw_score += (int)$score;
+ }
+ $how_many = count( $group_ratings );
+ $rating = $how_many === 0 ? 0 : $raw_score / $how_many;
+
+ groups_update_groupmeta( $group_id, 'bpgr_how_many_ratings', $how_many );
+ groups_update_groupmeta( $group_id, 'bpgr_rating', $rating );
+ }
}
endif;
View
4 includes/classes.php
@@ -38,7 +38,9 @@ function bp_group_reviews_extension() {
bp_core_add_message( "Your review was posted successfully!" );
$has_posted = groups_get_groupmeta( $bp->groups->current_group->id, 'posted_review' );
- $has_posted[] = (int)$bp->loggedin_user->id;
+ if ( !in_array( (int)$bp->loggedin_user->id, (array)$has_posted ) ) {
+ $has_posted[] = (int)$bp->loggedin_user->id;
+ }
groups_update_groupmeta( $bp->groups->current_group->id, 'posted_review', $has_posted );
if ( (int)$_POST['rating'] < 0 )
View
33 includes/templatetags.php
@@ -85,8 +85,6 @@ function bpgr_get_plugin_rating_html( $rating, $num_ratings = 0 ) {
}
function bpgr_get_review_rating_html( $rating ) {
- global $bp;
-
$star1 = '<img src="' . bpgr_get_star_off_img() . '" alt="' . __( '1 star', 'bpgr' ) . '" />';
$star2 = '<img src="' . bpgr_get_star_off_img() . '" alt="' . __( '2 stars', 'bpgr' ) . '" />';
$star3 = '<img src="' . bpgr_get_star_off_img() . '" alt="' . __( '3 stars', 'bpgr' ) . '" />';
@@ -120,8 +118,6 @@ function bpgr_is_group_reviews() {
function bpgr_has_written_review() {
global $bp;
- return false;
-
$has_posted = groups_get_groupmeta( $bp->groups->current_group->id, 'posted_review' );
return in_array( $bp->loggedin_user->id, (array) $has_posted );
@@ -195,6 +191,35 @@ function bpgr_get_review_rating( $review_id = false ) {
return apply_filters( 'bpgr_review_rating', $rating, $review_id );
}
+function bpgr_allow_multiple_reviews() {
+ global $bp;
+
+ $allow_multiples = !empty( $bp->group_reviews->allow_multiples ) ? true : false;
+
+ return apply_filters( 'bpgr_allow_multiple_reviews', $allow_multiples );
+}
+
+function bpgr_user_previous_review_args() {
+ $args = array(
+ 'user_id' => bp_loggedin_user_id(),
+ 'type' => 'review',
+ 'item_id' => bp_get_group_id(),
+ 'max' => 1
+ );
+
+ return apply_filters( 'bpgr_user_previous_review_args', $args );
+}
+
+function bpgr_activity_date_recorded() {
+ echo bpgr_get_activity_date_recorded();
+}
+ function bpgr_get_activity_date_recorded() {
+ $date = bp_get_activity_date_recorded();
+
+ $format = get_option( 'date_format' );
+
+ return apply_filters( 'bpgr_get_activity_date_recorded', date( $format, strtotime( $date ) ), $date );
+ }
?>
Please sign in to comment.
Something went wrong with that request. Please try again.