cirandas.net

ref: master

plugins/organization_ratings/controllers/organization_ratings_plugin_profile_controller.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class OrganizationRatingsPluginProfileController < ProfileController
  include RatingsHelper
  helper :ratings

  def new_rating
    @rating_available = user_can_rate_now?
    @users_ratings = get_ratings(profile.id).paginate(
                      :per_page => env_organization_ratings_config.per_page,
                      :page => params[:npage]
                    )
    if request.post?
      if @rating_available
        create_new_rate
      else
        session[:notice] = _("You can not vote on this %s") % _(profile.class.name)
      end
    end
  end

  private

  def user_can_rate_now?
    return false unless user
    ratings = OrganizationRating.where(
      :organization_id => profile.id,
      :person_id => user.id
    )

    return false if (!ratings.empty? && env_organization_ratings_config.vote_once)

    if ratings.empty?
      true
    else
      elapsed_time_since_last_rating = Time.zone.now - ratings.last.created_at
      elapsed_time_since_last_rating > env_organization_ratings_config.cooldown.hours
    end
  end

  def create_new_rate
    @rating = OrganizationRating.new(params[:organization_rating])
    @rating.person = current_user.person
    @rating.organization = profile
    @rating.value = params[:organization_rating_value] if params[:organization_rating_value]

    if @rating.save
      @plugins.dispatch(:organization_ratings_plugin_rating_created, @rating, params)
      create_rating_comment(@rating)
      session[:notice] = _("%s successfully rated!") % profile.name
      redirect_to profile.url
    end
  end

  def create_rating_comment(rating)
    if params[:comments].present? && params[:comments][:body].present?
      comment_task = CreateOrganizationRatingComment.create!(
        params[:comments].merge(
          :requestor => rating.person,
          :organization_rating_id => rating.id,
          :target => rating.organization
        )
      )
      comment_task.finish unless env_organization_ratings_config.are_moderated
    end
  end

  def permission
    :manage_memberships
  end
end