# fq class BidsController < ApplicationController #Added by young menu_item l(:label_homework), :only => [:edit, :udpate] menu_item :respond menu_item :project, :only => [:show_project,:show_results] menu_item :homework_respond, :only => :homework_respond 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] before_filter :require_login,:only => [:set_reward, :destroy, :add, :new, ] helper :watchers helper :attachments include AttachmentsHelper helper :projects def index @project_type = params[:project_type] # Modified by nie # @requirement_title = "4" @offset, @limit = api_offset_and_limit({:limit => 10}) if @project_type == '1' @bids = Bid.visible.where('reward_type = ?', 3) # elsif # @bids = Bid.visible.where('reward_type = ? or reward_type = ?', 4) else @bids = Bid.visible.where('reward_type = ?', 1) end @bids = @bids.like(params[:name]) if params[:name].present? @bid_count = @bids.count @bid_pages = Paginator.new @bid_count, @limit, params['page'] @offset ||= @bid_pages.reverse_offset #added by nie if params[:bid_sort_type].present? case params[:bid_sort_type] when '0' unless @offset == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.offset(@offset).limit(limit).all.reverse end @s_state = 0 when '1' unless @offset == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse end @s_state = 1 when '2' unless @offset == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse end @s_state = 0 end else unless @offset == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse end @s_state = 1 end #end end #huang def contest # Modified by nie # @requirement_title = "4" @offset, @limit = api_offset_and_limit({:limit => 10}) @bids = Bid.visible.where('reward_type = ?', 2) # elsif # @bids = Bid.visible.where('reward_type = ? or reward_type = ?', 4) @bids = @bids.like(params[:name]) if params[:name].present? @bid_count = @bids.count @bid_pages = Paginator.new @bid_count, @limit, params['page'] @offset ||= @bid_pages.reverse_offset #added by nie if params[:contest_sort_type].present? case params[:contest_sort_type] when '0' unless @offset == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.offset(@offset).limit(limit).all.reverse end @s_state = 0 when '1' unless @offset == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse end @s_state = 1 when '2' unless @offset == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.offset(@offset).limit(@limit).all.reverse end @s_state = 0 end else unless @offset == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse else limit = @bid_count % @limit limit = @limit if limit == 0 @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse end @s_state = 1 end #end end def fork @courses = [] @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) @membership.each do |membership| if membership.project.project_type == 1 @courses << membership.project end end end def create_fork @homework = Bid.new @homework.name = params[:bid][:name] @homework.description = params[:bid][:description] @homework.reward_type = 3 # @bid.budget = params[:bid][:budget] @homework.deadline = params[:bid][:deadline] @homework.budget = 0 @homework.author_id = User.current.id @homework.commit = 0 @homework.homework_type = params[:bid][:homework_type] @homework.parent_id = @bid.id @homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) # @bid. if @homework.save HomeworkForCourse.create(:project_id => params[:course], :bid_id => @homework.id) unless @bid.watched_by?(User.current) if @bid.add_watcher(User.current) flash[:notice] = l(:label_bid_succeed) end end redirect_to respond_path(@homework) else @bid.safe_attributes = params[:bid] @courses = [] @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) @membership.each do |membership| if membership.project.project_type == 1 @courses << membership.project end end render :action => 'fork' end end def show @user = @bid.author @jours = @bid.journals_for_messages.reverse @limit = 10 @feedback_count = @jours.count @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] @offset ||= @feedback_pages.offset @jour = @jours[@offset, @limit] @state = false respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end def show_course bids = Bid.where('parent_id = ?', @bid.id) @courses = [] for bid in bids @courses << bid.courses.first end respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end def show_bid_project bids = Bid.where('parent_id = ?', @bid.id) @projects = [] for bid in bids @projects += bid.biding_projects end respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end def show_bid_user bids = Bid.where('parent_id = ?', @bid.id) @users = [] for bid in bids for project in bid.projects @users += project.users end end respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end def show_project # flash[:notice] = "" @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) @option = [] @membership.each do |membership| unless(membership.project.project_type==1) membership.member_roles.each{|role| if(role.role_id == 3) @option << membership.project end } end end # a = [1] # @project = Project.where("id in []", a) @user = @bid.author @bidding_project = @bid.biding_projects.all #added by nie @temp = [] @bidding_project.each do |pro| if pro.project && pro.project.project_status @temp << pro end @temp end if @temp.size > 0 @bidding_project = @temp.sort {|a,b| b.project.project_status.grade <=> a.project.project_status.grade} end #ended if @bid.homework_type == 1 @homework = HomeworkAttach.new @homework_list = @bid.homeworks end respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end ##### by huang def show_project_homework # flash[:notice] = "" @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) @option = [] @membership.each do |membership| end # a = [1] # @project = Project.where("id in []", a) @user = @bid.author @bidding_project = @bid.biding_projects respond_to do |format| if @bid.reward_type == 3 format.html { render :layout => 'base_homework' } elsif @bid.reward_type == 1 format.html { render :layout => 'base_bids' } else format.html { render :layout => 'base_contest' } end format.api end end ###添加应标项目 def add project = Project.where('name = ?', params[:bid]).first bid_message = params[:bid_for_save][:bid_message] if BidingProject.where("project_id = ? and bid_id = ?", project.id, @bid.id).size == 0 if BidingProject.cerate_bidding(@bid.id, project.id, bid_message) # added by bai type ==1 需求,type==2 竞赛, type==3 作业 if @bid.reward_type == 1 flash[:notice] = l(:label_bidding_succeed) elsif @bid.reward_type == 2 flash[:notice] = l(:label_bidding_contest_succeed) else @bid.reward_type == 3 flash[:notice] = l(:label_bidding_homework_succeed) end # end end else if @bid.reward_type == 3 flash[:error] = l(:label_bidding_homework_fail) else flash[:error] = l(:label_bidding_fail) end end @bidding_project = @bid.biding_projects respond_to do |format| # format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} # format.html format.html { redirect_to :back } format.js #format.api { render_api_ok } end end ## 新建留言 def create if params[:bid_message][:message].size>0 if params[:reference_content] message = params[:bid_message][:message] + "\n" + params[:reference_content] else message = params[:bid_message][:message] end refer_user_id = params[:bid_message][:reference_user_id].to_i @bid.add_jour(User.current, message, refer_user_id) end @user = @bid.author @jours = @bid.journals_for_messages.reverse @limit = 10 @feedback_count = @jours.count @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] @offset ||= @feedback_pages.offset @jour = @jours[@offset, @limit] @bid.set_commit(@feedback_count) respond_to do |format| format.js #format.api { render_api_ok } end end ##删除留言 def destroy @user = @bid.author if User.current.admin? || User.current.id == @user.id JournalsForMessage.delete_message(params[:object_id]) end @jours = @bid.journals_for_messages.reverse @limit = 10 @feedback_count = @jours.count @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] @offset ||= @feedback_pages.offset @jour = @jours[@offset, @limit] @bid.set_commit(@feedback_count) # if a_message.size > 5 # @message = a_message[-5, 5] # else # @message = a_message # end # @message_count = a_message.count respond_to do |format| # format.html format.js #format.api { render_api_ok } end end #删除作业 #by xianbo def homework_destroy @bid_to_destroy = Bid.find params[:course_id] (render_403; return false) unless User.current.admin?||User.current.id==@bid_to_destroy.author_id @bid_to_destroy.destroy respond_to do |format| format.html { redirect_to :back } format.js #format.api { render_api_ok } end end #end by xianbo ##引用 def new @jour = JournalsForMessage.find(params[:journal_id]) if params[:journal_id] if @jour user = @jour.user text = @jour.notes else user = @bid.author text = @bid.description end # Replaces pre blocks with [...] text = text.to_s.strip.gsub(%r{
((.|\s)*?)}m, '[...]') @content = "> #{ll(User.current.language, :text_user_wrote, user)}\n> " @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" @id = user.id rescue ActiveRecord::RecordNotFound render_404 end ##新建需求 def new_bid @bid = Bid.new @bid.safe_attributes = params[:bid] end #huang def create_contest @bid = Bid.new @bid.name = params[:bid][:name] @bid.description = params[:bid][:description] @bid.reward_type = 2 @bid.budget = params[:bid][:budget] @bid.deadline = params[:bid][:deadline] @bid.author_id = User.current.id @bid.commit = 0 if @bid.save unless @bid.watched_by?(User.current) if @bid.add_watcher(User.current) flash[:notice] = l(:label_bid_succeed) end end redirect_to respond_path(@bid) else @bid.safe_attributes = params[:bid] render :action => 'new_bid' end end #huang def new_contest @bid = Bid.new @bid.safe_attributes = params[:bid] end def create_bid @bid = Bid.new @bid.name = params[:bid][:name] @bid.description = params[:bid][:description] @bid.reward_type = 1 @bid.budget = params[:bid][:budget] @bid.deadline = params[:bid][:deadline] @bid.author_id = User.current.id @bid.commit = 0 if @bid.save unless @bid.watched_by?(User.current) if @bid.add_watcher(User.current) flash[:notice] = l(:label_bid_succeed) end end redirect_to respond_path(@bid) else @bid.safe_attributes = params[:bid] render :action => 'new_bid' end end def create_homework @bid = Bid.new @bid.name = params[:bid][:name] @bid.description = params[:bid][:description] @bid.reward_type = 3 # @bid.budget = params[:bid][:budget] @bid.deadline = params[:bid][:deadline] @bid.budget = 0 @bid.author_id = User.current.id @bid.commit = 0 @bid.homework_type = params[:bid][:homework_type] @bid.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) # @bid. if @bid.save HomeworkForCourse.create(:project_id => params[:course_id], :bid_id => @bid.id) unless @bid.watched_by?(User.current) if @bid.add_watcher(User.current) flash[:notice] = l(:label_bid_succeed) end end redirect_to respond_path(@bid) else @bid.safe_attributes = params[:bid] render :action => 'new_bid' end end def edit @project_id = params[:project_id] @bid = Bid.find(params[:bid_id]) respond_to do |format| format.html { @project = Project.find(params[:project_id]) @user= User.find(User.current.id) render :layout => 'base_courses' } end end def update @bid = Bid.find(params[:id]) if @bid.update_attributes(params[:bid]) flash[:notice] = l(:label_update_homework_succeed) @project = Project.find(params[:course_id]) redirect_to project_homework_path(@project) else @bid.safe_attributes = params[:bid] render :action => 'edit' end end def add_homework # homework = HomeworkAttach.create(:bid_id => @bid.id, :user_id => User.current.id) # homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) @homework = HomeworkAttach.new @homework.bid_id = @bid.id @homework.user_id = User.current.id @homework.save_attachments(params[:attachments]) @homework.save @homework_list = @bid.homeworks end # 作业统计 def homework_statistics @course = @bid.courses.first @member = [] @course.memberships.each do |member| unless (member.roles && Role.where('id = ? ', 3)).empty? @member.push member end end if @bid.homework_type = 1 @student = User.where("id in (select DISTINCT user_id from #{HomeworkAttach.table_name} where bid_id = ? )", @bid.id) @homework_type = true else @homework_type = false end @user = @bid.author render :layout => 'base_homework' end def homework_respond @user = @bid.author render :layout => 'base_homework' end def more @jour = @bid.journals_for_messages @jour.each_with_index {|j,i| j.indice = i+1} @state = true respond_to do |format| format.html { redirect_to :back } format.js #format.api { render_api_ok } end end def back @jour = @bid.journals_for_messages @jour.each_with_index {|j,i| j.indice = i+1} @state = false respond_to do |format| format.html { redirect_to :back } format.js #format.api { render_api_ok } end end #added by william #used to set the bidding project reward def set_reward @b_p = nil @biding_project_id = nil if params[:set_reward][:reward]&&((User.current.id==@bid.author_id)||User.current.admin) # @bid_id = params[:id] @biding_project_id = params[:set_reward][:b_id] @b_p = BidingProject.find_by_id(@biding_project_id) # 把字段存进表中 @b_p.update_reward(params[:set_reward][:reward].to_s) end respond_to do |format| format.js end end # added by william # used to manage the bid and end the bid def manage end private def find_bid if params[:id] @bid = Bid.find(params[:id]) @user = @bid.author end rescue render_404 end end