From 834362491ee354299b1e4a5499b04a3c0510457d Mon Sep 17 00:00:00 2001 From: differentreality Date: Fri, 16 Aug 2013 12:46:49 +0300 Subject: [PATCH] star voting system for proposals --- app/assets/images/star-bright.png | Bin 0 -> 1181 bytes app/assets/images/star-glow.png | Bin 0 -> 1015 bytes app/assets/images/star.png | Bin 0 -> 620 bytes app/assets/javascripts/osem.js | 6 +++ app/assets/stylesheets/osem.css | 17 +++++++ app/controllers/admin/events_controller.rb | 15 ++++++ app/models/event.rb | 17 ++++++- app/models/person.rb | 2 + app/models/vote.rb | 10 ++++ app/views/admin/events/_proposal.html.haml | 7 ++- app/views/admin/events/_voting.html.haml | 47 ++++++++++++++++++ app/views/admin/events/index.html.haml | 12 +++++ app/views/admin/events/vote.js.erb | 1 + config/routes.rb | 1 + db/migrate/20130815085420_create_votes.rb | 15 ++++++ ...815201317_add_rating_to_call_for_papers.rb | 5 ++ ...1511_add_rating_desc_to_call_for_papers.rb | 5 ++ 17 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 app/assets/images/star-bright.png create mode 100644 app/assets/images/star-glow.png create mode 100644 app/assets/images/star.png create mode 100644 app/models/vote.rb create mode 100644 app/views/admin/events/_voting.html.haml create mode 100644 app/views/admin/events/vote.js.erb create mode 100644 db/migrate/20130815085420_create_votes.rb create mode 100644 db/migrate/20130815201317_add_rating_to_call_for_papers.rb create mode 100644 db/migrate/20130815201511_add_rating_desc_to_call_for_papers.rb diff --git a/app/assets/images/star-bright.png b/app/assets/images/star-bright.png new file mode 100644 index 0000000000000000000000000000000000000000..2dbc8ed36445f19350c03d96a707fa8faeebf411 GIT binary patch literal 1181 zcmV;O1Y-M%P)P000>X1^@s6#OZ}&00006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru-3Si}E+{($WS;;402y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00ad|L_t(Y$L*ERZ&Xzj$3OSJ zH#6;<>GTJ*1qwpAv`PR8CN5y5h%qK$!p4PhW8Ao8p@|!H>%y2I7(+tTgyyNmmQn(swf-dx!#Agc%=c#ya8MSz6H)IrIzl3_`d(D5aNXA zdHtT}Q79B}T^A`OQS*Cx^Pl2~4o?+Jy{k z+C#5gBWeCj7S_Q!q)~uV-p%G`KEtHR>JTBssd~MBHHsoLyz3-ZZYNxp}b3at^ch%q@319;>6iH%F6QC~F1yseZvao=TBN}V#se3coJq&o{%gN`7x z2zLXHH-*!`6`NUX+QTFvNoO8oL2G?+$BrEzuC|QsDt{ZKBXhdjfGkG1{gCr8NkAH| zAe|hpw+)+W%*qmJu*7ZO9C#308F3th=`|775G}W9-MB(89>5(tOq`C)hn#bSMbK4V$-3I$PZ0ntwjhy)STw`AK#;M^$dgk`#^vYLazCW zRVtOsagy0??HikR=dHEYhGA%HwVJL}Dq*cwOM)P<)><3gyk?jE^ET}DY^74Y_#o~2 zq;+$#73B8N>N?EM&eCW!&c$*3f|Rlx$MM01g@v!@=H~RWF3~IPqTOmCrFgK*===U* vUlRbmuy0LXv^rPO);*Tc{sfFTuk1W@3+00000NkvXXu0mjfu@WZ# literal 0 HcmV?d00001 diff --git a/app/assets/images/star-glow.png b/app/assets/images/star-glow.png new file mode 100644 index 0000000000000000000000000000000000000000..b605587a45672085a08cb580e8944619401a8041 GIT binary patch literal 1015 zcmVP000>X1^@s6#OZ}&00006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru-3Si}E-_zk%wGTi02y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00Uo1L_t(Y$JLfkXdG1>$3HW( zo9QN#?2bt`AqgZrqyz&h6a+y8J&0CNM6H(`yoNvzqEHenqS%6|hk_-F7Y|-kuqY@9 z9)brEOGB##Ym8Gjv)eSA&7aNgWOrxZ%=0k2xI)5a6$}30!SBI?&-=dL?|t9Xlv4cH zp>r1^)=V77X}9(SFy1o%96$(hbV!?c#Wc-#W3j)Md!Bb5nCnPMGY;CB<2Wi=h1c$t=Ynj!dt+Lz!Sg&z_@MO-L`GRM~^`7P;|n%8!&r~ za-~K-pRWKp;Adb7xGaQl4};9=y8d2IPY;Pi0@E~!#bRih23Hp0m(L(M0OyR zg+d1Q_n;>Y{gW{9Fif2!=p81L$rOYTgNL{9moGzDgzi4*wV-zrLJf*{U^feX9sC;f zje^+^)e5XF!^#qTeGcASJwoB1{}iMFrCl)Mu;aq|PZ2-{5J=eDgg>rA?mA$AzYjGZ z=9b%b*J3uCb?WsxocaPP5;ii>HwHlfVFOSPB3LLPJryY|6=8mvFp$-$tc)qph2OZZ1rPO>jnt*tE|kjZ4SJN4u&_dIP000>X1^@s6#OZ}&00006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru-3Si}E)$9mjk^E<02y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00GoVL_t(Y$JLfWi_=gL$A2$P z8@mlOVQU2`EDYW}c<}DgZy;X$0!8p3dRbvFqI+2nf>J+#XZI_3^sd(iY9eUY9#R@C z?aO;!iUw_M7HfTF>ts|NVnZ?Q>j#nkS_2X__7U6p7_3hrCP1xIL?nN48vg4Ox8gz0o_)s z_1ldg2vADhi=ybpI*97``^CKv+yZU_*MNgC4EMq?T>9{2GGRCzegi%M?}4|#sffg@ zAa@tYD5d`RmeyLd*34!zrqd}|mZ7zNEFwqCARjuN&edA2cCMbt z^PIt8@Kr=E?z9}nt$YU}47RMj1$}b>0000 '/schedule', :action => :index redirect_to(admin_conference_events_path(:conference_id => @conference.short_title), :notice => "Updated state") end + + def vote + event = Event.find(params[:id]) + + if votes = current_user.person.votes.find_by_event_id(params[:id]) + votes.update_attributes(:rating => params[:rating]) + else + @myvote = event.votes.build + @myvote.person = current_user.person + @myvote.rating = params[:rating] + @myvote.save + end + redirect_to admin_conference_event_path(@conference.short_title, event) + end end diff --git a/app/models/event.rb b/app/models/event.rb index c735cfb7..b5aafd29 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -9,8 +9,10 @@ class Event < ActiveRecord::Base has_many :event_attachments, :dependent => :destroy has_many :people, :through => :event_people has_many :speakers, :through => :event_people, :source => :person, :conditions => {"event_people.event_role" => "speaker"} + has_many :votes + has_many :voters, :through => :votes, :source => :person belongs_to :event_type - + has_and_belongs_to_many :registrations belongs_to :track @@ -62,6 +64,19 @@ class Event < ActiveRecord::Base end end + def voted?(event, person) + event.votes.where("person_id = ?", person).first + end + + def average_rating + @total_rating = 0 + self.votes.each do |vote| + @total_rating = @total_rating + vote.rating + end + @total = self.votes.size + number_with_precision(@total_rating / @total.to_f, :precision => 2, :strip_insignificant_zeros => true) + end + def submitter result = self.event_people.where(:event_role => "submitter").first if !result.nil? diff --git a/app/models/person.rb b/app/models/person.rb index ea61ef2c..a149bf23 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -8,6 +8,8 @@ class Person < ActiveRecord::Base has_many :event_people, :dependent => :destroy has_many :events, :through => :event_people, :uniq => true has_many :registrations, :dependent => :destroy + has_many :votes, :dependent => :destroy + has_many :voted_events, :through => :votes, :source => :events validates :first_name, :presence => true validates :last_name, :presence => true diff --git a/app/models/vote.rb b/app/models/vote.rb new file mode 100644 index 00000000..051561ae --- /dev/null +++ b/app/models/vote.rb @@ -0,0 +1,10 @@ +class Vote < ActiveRecord::Base + attr_accessible :rating + + belongs_to :person + belongs_to :event + + delegate :first_name, :to => :person + delegate :last_name, :to => :person + delegate :public_name, :to => :person +end \ No newline at end of file diff --git a/app/views/admin/events/_proposal.html.haml b/app/views/admin/events/_proposal.html.haml index cc2fb7c0..6c16f814 100644 --- a/app/views/admin/events/_proposal.html.haml +++ b/app/views/admin/events/_proposal.html.haml @@ -142,7 +142,10 @@ %td = link_to "Delete", conference_proposal_event_attachment_path(@conference.short_title, @event.id, a.id), :method => :delete, :confirm => "Are you sure you want to delete this attachment? It's permanant!" - +.row-fluid + = link_to "Votes (#{@event.voters.length})", "#", :id => "votes-link" + #votes-div + = render :partial => 'voting' .row-fluid = link_to "Comments (#{@comment_count})", "#", :id => "event-comment-link" #comments-div @@ -156,4 +159,4 @@ %br - @comments.each do |comment| %div - = raw format_comments(comment) + = raw format_comments(comment) \ No newline at end of file diff --git a/app/views/admin/events/_voting.html.haml b/app/views/admin/events/_voting.html.haml new file mode 100644 index 00000000..a100170d --- /dev/null +++ b/app/views/admin/events/_voting.html.haml @@ -0,0 +1,47 @@ +%table#myeventratings{:style=>"width: 20%"} + %tr + %td + %b You voted: + %td + - @conference.call_for_papers.rating.times do |counter| + - voted = @event.voted?(@event, current_user.person) + - if voted && voted.rating == counter+1 + = link_to "", vote_admin_conference_event_path(@conference.short_title, @event, :rating => counter+1), :remote => true, :id =>"label#{counter+1}", :class => "myrating", :voted => true + - else + = link_to "", vote_admin_conference_event_path(@conference.short_title, @event, :rating => counter+1), :remote => true, :id =>"label#{counter+1}", :class => "myrating" + %br + + - if @ratings.length > 0 + - @ratings.each do |rate| + - unless rate.person_id == current_user.person.id + %tr + %td + = rate.first_name + = rate.last_name + %td + - @conference.call_for_papers.rating.times do |counter| + - voted = @event.voted?(@event, rate.person) + - if voted && voted.rating == counter+1 + = label_tag "label#{counter+1}", "", :class => "othersrating", :voted => true + = javascript_tag "$('label[voted=true]').prevAll().andSelf().addClass('bright');" + - else + = label_tag "label#{counter+1}", "", :class => "othersrating" +:javascript + $(function () { + var checkedId = $("a[voted='true']").attr('id'); + $('a[id=' + checkedId + ']').prevAll().andSelf().addClass('bright'); + }); + + $(".myrating").hover( + function() { // mouseover + $(this).prevAll().andSelf().addClass('glow'); + }, + function() { // mouseout + $(this).siblings().andSelf().removeClass('glow'); + } + ); + + $(".myrating").click(function() { + $(this).siblings().removeClass("bright"); + $(this).prevAll().andSelf().addClass("bright"); + }); \ No newline at end of file diff --git a/app/views/admin/events/index.html.haml b/app/views/admin/events/index.html.haml index eafc6af1..d053ea7c 100644 --- a/app/views/admin/events/index.html.haml +++ b/app/views/admin/events/index.html.haml @@ -115,6 +115,18 @@ = event.id %td =link_to event.title, admin_conference_event_path(@conference.short_title, event) + - if event.average_rating.to_f > 0 + Rating: (#{event.average_rating}/#{@conference.call_for_papers.rating}) + - else + Rating: (0/#{@conference.call_for_papers.rating}) + + - @conference.call_for_papers.rating.times do |counter| + - if event.average_rating.to_f.round == counter+1 + = label_tag "label_rating", "", :class => "avgrating", :avgrate => true + = javascript_tag "$('label[avgrate=true]').prevAll().andSelf().addClass('bright');" + - else + = label_tag "label_rating", "", :class => "avgrating" + - if event.submitter.registrations.count < 1 - bgcolor="#F7819F" - else diff --git a/app/views/admin/events/vote.js.erb b/app/views/admin/events/vote.js.erb new file mode 100644 index 00000000..38ffd622 --- /dev/null +++ b/app/views/admin/events/vote.js.erb @@ -0,0 +1 @@ +$('table#myeventratings').replaceWith("<%= escape_javascript(render :partial => 'voting') %>"); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 0cbeab15..a5451c60 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,7 @@ Osem::Application.routes.draw do post :comment put :update_state put :update_track + get :vote end resource :speaker, :only => [:edit, :update] end diff --git a/db/migrate/20130815085420_create_votes.rb b/db/migrate/20130815085420_create_votes.rb new file mode 100644 index 00000000..1b4460a7 --- /dev/null +++ b/db/migrate/20130815085420_create_votes.rb @@ -0,0 +1,15 @@ +class CreateVotes < ActiveRecord::Migration + def up + create_table :votes do |t| + t.references :person + t.references :event + t.integer :rating + + t.timestamps + end + end + + def down + drop_table :votes + end +end diff --git a/db/migrate/20130815201317_add_rating_to_call_for_papers.rb b/db/migrate/20130815201317_add_rating_to_call_for_papers.rb new file mode 100644 index 00000000..fd1a3527 --- /dev/null +++ b/db/migrate/20130815201317_add_rating_to_call_for_papers.rb @@ -0,0 +1,5 @@ +class AddRatingToCallForPapers < ActiveRecord::Migration + def change + add_column :call_for_papers, :rating, :integer + end +end diff --git a/db/migrate/20130815201511_add_rating_desc_to_call_for_papers.rb b/db/migrate/20130815201511_add_rating_desc_to_call_for_papers.rb new file mode 100644 index 00000000..386a3064 --- /dev/null +++ b/db/migrate/20130815201511_add_rating_desc_to_call_for_papers.rb @@ -0,0 +1,5 @@ +class AddRatingDescToCallForPapers < ActiveRecord::Migration + def change + add_column :call_for_papers, :rating_desc, :text + end +end