diff --git a/app/controllers/bids_controller.rb b/app/controllers/bids_controller.rb index b44c2ba5..6c5ff168 100644 --- a/app/controllers/bids_controller.rb +++ b/app/controllers/bids_controller.rb @@ -8,7 +8,10 @@ class BidsController < ApplicationController menu_item :homework_statistics, :only => :homework_statistics #Ended by young before_filter :find_bid, :only => [:show, :show_project, :create,:destroy,:more,:back,:add,:new,:show_results,:set_reward, :add_homework, :fork, :create_fork, - :show_course, :show_bid_project, :show_bid_user] + :show_course, :show_bid_project, :show_bid_user, :join_in_contest, :unjoin_in_contest, :new_join] + # added by fq + before_filter :require_login, :only => [:join_in_contest, :unjoin_in_contest] + # end before_filter :require_login,:only => [:set_reward, :destroy, :add, :new, ] helper :watchers @@ -209,6 +212,41 @@ class BidsController < ApplicationController end end + + def join_in_contest + if @bid.reward_type == 2 && params[:course_password] == @bid.password + JoinInContest.create(:user_id => User.current.id, :bid_id => @bid.id) + @state = 0 + else + @state = 1 + end + + respond_to do |format| + # format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + # TO_DO + format.js { render :partial => 'set_join', :locals => {:user => User.current, :object_id => params[:id]} } + end + end + + def unjoin_in_contest + + joined = JoinInContest.where('bid_id = ? and user_id = ?', @bid.id, User.current.id) + + joined.each do |join| + join.delete + end + + respond_to do |format| + # format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + format.js { render :partial => 'set_join', :locals => {:user => User.current, :object_id => params[:id]} } + end + end + + def new_join + # added by fq + + + end def show_course bids = Bid.where('parent_id = ?', @bid.id) @@ -338,13 +376,13 @@ class BidsController < ApplicationController @homework = HomeworkAttach.new @homework_list = @bid.homeworks if params[:student_id].present? - @temp = [] - @homework_list.each do |pro| - if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id - @temp << pro - end - @temp - end + @temp = [] + @homework_list.each do |pro| + if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id + @temp << pro + end + @temp + end @homework_list = @temp end end @@ -536,6 +574,7 @@ class BidsController < ApplicationController @bid.reward_type = 2 @bid.budget = params[:bid][:budget] @bid.deadline = params[:bid][:deadline] + @bid.password = params[:bid][:password] #added by bai @bid.author_id = User.current.id @bid.commit = 0 if @bid.save diff --git a/app/helpers/watchers_helper.rb b/app/helpers/watchers_helper.rb index 2fdb4fd1..e05ef6eb 100644 --- a/app/helpers/watchers_helper.rb +++ b/app/helpers/watchers_helper.rb @@ -56,9 +56,9 @@ module WatchersHelper url_f = try_join_path(:object_id => course.id) method = joined ? 'delete' : 'post' if joined - link_to text, url_t, :remote => true, :method => method, :id => 'join', :confirm => l(:text_are_you_sure_out), :class => []+options + link_to text, url_t, :remote => true, :method => method, :id => "#{course.id}", :confirm => l(:text_are_you_sure_out), :class => []+options else - link_to text, url_f, :remote => true, :method => method, :id => 'join', :class => []+options + link_to text, url_f, :remote => true, :method => method, :id => "#{course.id}", :class => []+options end end @@ -76,6 +76,25 @@ module WatchersHelper end end + #added by bai + def join_in_contest(bid, user, options=[]) + if bid.reward_type == 2 + return '' unless user && user.logged? + joined = user.join_in_contest?(bid) + text = joined ? l(:label_exit_contest) : l(:label_join_contest) + url_t = join_in_contest_path(:id => bid.id) + url_f = try_join_in_contest_path(:id => bid.id) + # url = join_in_contest_path(:id => bid.id) + method = joined ? 'delete' : 'post' + if joined + link_to text, url_t, :remote => true, :method => method, :id => "#{bid.id}", :confirm => l(:text_are_you_sure_out), :class => []+options + else + link_to text, url_f, :remote => true, :method => method, :id => "#{bid.id}", :class => []+options + end + end + end + + # Returns the css class used to identify watch links for a given +object+ def watcher_css(objects) objects = Array.wrap(objects) diff --git a/app/models/bid.rb b/app/models/bid.rb index 3bad65f3..bb7868ef 100644 --- a/app/models/bid.rb +++ b/app/models/bid.rb @@ -1,6 +1,6 @@ ####by fq class Bid < ActiveRecord::Base - attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type + attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type, :password include Redmine::SafeAttributes belongs_to :author, :class_name => 'User', :foreign_key => :author_id @@ -13,6 +13,7 @@ class Bid < ActiveRecord::Base has_many :homework_for_courses, :dependent => :destroy has_many :courses, :through => :homework_for_courses, :source => :project has_many :homeworks, :class_name => 'HomeworkAttach', :dependent => :destroy + has_many :join_in_contests, :dependent => :destroy # has_many :fork_homework, :class_name => 'Bid', :conditions => "#{Bid.table_name}.parent_id = #{id}" @@ -63,7 +64,8 @@ class Bid < ActiveRecord::Base 'budget', 'deadline', 'homework_type', - 'reward_type' + 'reward_type', + 'password' # safe_attributes 'name', diff --git a/app/models/join_in_contest.rb b/app/models/join_in_contest.rb new file mode 100644 index 00000000..d80a096a --- /dev/null +++ b/app/models/join_in_contest.rb @@ -0,0 +1,10 @@ +class JoinInContest < ActiveRecord::Base + attr_accessible :bid_id, :user_id + + belongs_to :user + belongs_to :bid + + validates_presence_of :user_id, :bid_id + + +end diff --git a/app/models/user.rb b/app/models/user.rb index 40e142e9..48bda02a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -90,6 +90,7 @@ class User < Principal has_many :students_for_courses has_many :courses, :through => :students_for_courses, :source => :project has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + has_many :join_in_contests, :dependent => :destroy ##### ######added by nie @@ -167,6 +168,16 @@ class User < Principal self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id, :status => true) end + # 判断用户是否加入了竞赛中 fq + def join_in_contest?(bid) + joined = JoinInContest.where('user_id = ? and bid_id =?', self.id, bid.id) + if joined.size > 0 + true + else + false + end + end + ### fq def join_in?(course) joined = StudentsForCourse.where('student_id = ? and course_id = ?', self.id, course.id) diff --git a/app/views/bids/_form_contest.html.erb b/app/views/bids/_form_contest.html.erb index b7e4dc5c..448af2e8 100644 --- a/app/views/bids/_form_contest.html.erb +++ b/app/views/bids/_form_contest.html.erb @@ -24,6 +24,9 @@
<%= f.text_field :name, :required => true, :size => 60, :style => "width:490px;", :maxlength => Bid::NAME_LENGTH_LIMIT, :placeholder => "#{l(:label_contest_name)}" %>
<%= f.text_area :description, :rows => 8, :class => 'wiki-edit', :style => "font-size:small;width:490px;margin-left:10px;", :maxlength => Bid::DESCRIPTION_LENGTH_LIMIT, :placeholder => "#{l(:label_contest_description)}" %>
+ +<%= f.text_field :password, :size => 60, :style => "width:488px;margin-left: 10px;" %>
+<%= f.text_field :budget, :required => true, :size => 60, :style => "width:350px;", :placeholder => l(:label_bids_reward_what) %> diff --git a/app/views/bids/_new_join.html.erb b/app/views/bids/_new_join.html.erb new file mode 100644 index 00000000..bbe09133 --- /dev/null +++ b/app/views/bids/_new_join.html.erb @@ -0,0 +1,59 @@ + + + +
+ | + + <%= text_field_tag 'course_password', nil, :size => 45 %> + +
<%= l(:label_setup_time) %> *
<%= text_field_tag :setup_time, @course.setup_time, :placeholder => "在此选择开课日期" %>
<%= calendar_for('setup_time')%>
@@ -53,11 +58,12 @@
<% end %>
-
-
|