2014-10-23 11:30:34 +08:00
# fq
class BidsController < ApplicationController
#Added by young
menu_item l ( :label_homework ) , :only = > [ :edit , :udpate ]
menu_item :respond
menu_item :course , :only = > :show_courseEx
menu_item :project , :only = > [ :show_project , :show_results , :new_submit_homework ]
menu_item :homework_respond , :only = > :homework_respond
menu_item :homework_statistics , :only = > :homework_statistics
2014-11-12 17:07:27 +08:00
menu_item :edit , :only = > :edit
2014-10-23 11:30:34 +08:00
2015-01-17 19:00:32 +08:00
2014-10-23 11:30:34 +08:00
before_filter :can_show_course , only : [ ]
before_filter :can_show_contest , only : [ ]
#Ended by young
before_filter :find_bid , :only = > [ :show , :show_project , :create , :destroy , :more , :back , :add , :delete , :new , :show_results , :set_reward , :add_homework , :fork , :create_fork ,
:show_course , :show_courseEx , :show_bid_project , :show_bid_user , :join_in_contest , :unjoin_in_contest , :new_join , :show_participator , :settings ]
# 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 , ]
#before_filter :memberAccess, only: :show_project
helper :watchers
helper :attachments
include AttachmentsHelper
include ApplicationHelper
include BidsHelper
helper :projects
helper :words
helper :welcome
helper :project_score
def find_project_by_bid_id
@bid = Bid . find ( params [ :id ] )
@project = @bid . courses [ 0 ]
rescue ActiveRecord :: RecordNotFound
render_404
end
def homework_ajax_modal
@bid = Bid . find_by_id ( params [ :id ] )
# find_bid
respond_to do | format |
format . js
end
end
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 = 1
@homework . is_evaluation = params [ :bid ] [ :is_evaluation ]
@homework . parent_id = @bid . id
@homework . save_attachments ( params [ :attachments ] || ( params [ :bid ] && params [ :bid ] [ :uploads ] ) )
# @bid.
if @homework . save
HomeworkForCourse . create ( :course_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
2014-11-02 10:20:28 +08:00
redirect_to course_for_bid_path ( @homework )
2014-10-23 11:30:34 +08:00
else
@bid . safe_attributes = params [ :bid ]
@courses = [ ]
@membership = User . current . coursememberships . all #(:conditions => Project.visible_condition(User.current))
@membership . each do | membership |
@courses << membership . course
end
render :action = > 'fork'
end
end
def show
@user = @bid . author
@jours = @bid . journals_for_messages . where ( 'm_parent_id IS NULL' ) . order ( 'created_on DESC' )
@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 |
layout_file = ''
case @bid . reward_type
when 3
html_title ( l ( :label_question_student ) )
layout_file = 'base_homework'
when 1
layout_file = 'base_bids'
else
layout_file = 'base_contest'
end
format . html {
render :layout = > layout_file
}
format . api
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
# added by bai 增加了参与者和竞赛设置
def show_participator
render :layout = > 'base_contest'
end
def settings
if @bid . author . id == User . current . id
if @bid . reward_type == 2
@contest = Bid . find_by_reward_type ( @bid . reward_type )
render :layout = > 'base_contest'
end
else
render_403 :message = > :notice_not_contest_setting_authorized
end
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 )
if membership . user . allowed_to? ( :quote_project , membership . project )
@option << membership . project
end
end
end
# a = [1]
# @project = Project.where("id in []", a)
@user = @bid . author
@bidding_project = @bid . biding_projects . all
if params [ :student_id ] . present?
@temp = [ ]
@bidding_project . each do | pro |
if pro . project && pro . project . project_status
if / #{ params [ :student_id ] } / =~ pro . user . user_extensions . student_id
@temp << pro
end
end
@temp
end
@bidding_project = @temp
else
#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
end
if @bid . homework_type == 1
@homework = HomeworkAttach . new
#@homework_list = @bid.homeworks
#增加作业按评分排序,
@homework_list = @bid . homeworks . eager_load ( :rate_averages , :user , :attachments ) . order ( 'seems_rateable_cached_ratings.avg DESC' ) . order ( " #{ HomeworkAttach . table_name } .created_at ASC " )
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
@homework_list = @temp
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
# 显示作业课程
# add by nwb
def show_courseEx
2014-11-01 15:06:03 +08:00
2014-10-23 11:30:34 +08:00
if ( User . current . logged? && ( User . current . member_of_course? ( @bid . courses . first ) || User . current . admin? ) )
# flash[:notice] = ""
@membership = User . current . coursememberships . all ( :conditions = > Course . visible_condition ( User . current ) )
@user = @bid . author
@bidding_project = @bid . biding_projects . all
if params [ :student_id ] . present?
@temp = [ ]
@bidding_project . each do | pro |
if pro . project && pro . project . project_status
if / #{ params [ :student_id ] } / =~ pro . user . user_extensions . student_id
@temp << pro
end
end
@temp
end
@bidding_project = @temp
else
#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
end
if @bid . homework_type
@homework = HomeworkAttach . new
2014-10-31 15:28:04 +08:00
@is_teacher = is_course_teacher ( User . current , @bid . courses . first )
2014-11-01 11:02:13 +08:00
if @is_teacher
2014-12-29 16:59:22 +08:00
all_homework_list = HomeworkAttach . find_by_sql ( " SELECT * FROM (SELECT homework_attaches.*,
2014-12-31 11:18:22 +08:00
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0 , 1 ) AS t_score ,
2014-12-29 16:03:21 +08:00
( SELECT AVG ( stars ) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 0 ) AS s_score
2014-10-30 17:27:07 +08:00
FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY s_score DESC,created_at ASC) AS table1
WHERE table1 . t_score IS NULL " )
2014-12-30 14:29:30 +08:00
@not_batch_homework = true
2014-11-04 10:41:34 +08:00
@cur_type = 1
2014-11-01 11:02:13 +08:00
else
2014-12-29 16:59:22 +08:00
all_homework_list = HomeworkAttach . find_by_sql ( " SELECT homework_attaches.*,
2014-12-31 11:18:22 +08:00
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0 , 1 ) AS t_score ,
2014-12-29 16:03:21 +08:00
( SELECT AVG ( stars ) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 0 ) AS s_score ,
2014-12-29 16:59:22 +08:00
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND rater_id = #{User.current.id} AND is_teacher_score = #{@is_teacher ? 1 : 0}) AS m_score
2014-11-01 11:02:13 +08:00
FROM homework_attaches
INNER JOIN homework_evaluations ON homework_evaluations . homework_attach_id = homework_attaches . id
2014-11-08 13:43:29 +08:00
WHERE homework_attaches . bid_id = #{@bid.id} AND homework_evaluations.user_id = #{User.current.id} ORDER BY s_score DESC")
2014-11-01 16:19:04 +08:00
@is_student_batch_homework = true
2014-11-04 10:41:34 +08:00
@cur_type = 4
2014-11-01 11:02:13 +08:00
end
2014-10-30 15:12:45 +08:00
2014-11-04 10:41:34 +08:00
@cur_page = params [ :page ] || 1
2014-10-30 17:27:07 +08:00
@homework_list = paginateHelper all_homework_list , 10
2014-11-01 15:06:03 +08:00
@jours_count = @bid . journals_for_messages . where ( 'm_parent_id IS NULL' ) . count
2014-10-23 11:30:34 +08:00
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
@homework_list = @temp
end
end
respond_to do | format |
if @bid . reward_type == 3
format . html {
html_title ( l ( :label_homework_info ) )
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
else
render_403 :message = > :notice_not_authorized
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 . find ( params [ :bid ] )
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 . now [ :notice ] = l ( :label_bidding_succeed )
elsif @bid . reward_type == 2
flash . now [ :notice ] = l ( :label_bidding_contest_succeed )
else @bid . reward_type == 3
flash . now [ :notice ] = l ( :label_bidding_homework_succeed )
end
# end
end
else
if @bid . reward_type == 3
flash . now [ :error ] = l ( :label_bidding_homework_fail )
else
flash . now [ :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 delete
binding_project = params [ :binding_project ]
if can_delete_project_homework ( BidingProject . find ( binding_project ) , User . current )
if BidingProject . delete ( binding_project )
redirect_to project_for_bid_url
else
render_403 ;
end
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 ]
@m = 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 . where ( 'm_parent_id IS NULL' ) . order ( 'created_on DESC' )
2014-11-01 15:06:03 +08:00
@jour = paginateHelper @jours , 10
2014-10-23 11:30:34 +08:00
@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{ <pre>((.| \ s)*?)</pre> }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 . password = params [ :bid ] [ :password ] #added by bai
@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_url ( @bid )
else
@bid . safe_attributes = params [ :bid ]
render :action = > 'new_bid'
end
end
# added by bai
def update_contest
@bid = Bid . find ( params [ :id ] )
@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 . password = params [ :bid ] [ :password ]
@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_url ( @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_url ( @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 . is_evaluation = params [ :bid ] [ :is_evaluation ]
@bid . proportion = params [ :bid ] [ :proportion ]
2014-11-05 10:06:20 +08:00
@bid . evaluation_num = params [ :bid ] [ :evaluation_num ]
2014-11-19 10:34:36 +08:00
@bid . open_anonymous_evaluation = params [ :bid ] [ :open_anonymous_evaluation ]
2014-10-23 11:30:34 +08:00
@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 = 1
@bid . save_attachments ( params [ :attachments ] || ( params [ :bid ] && params [ :bid ] [ :uploads ] ) )
# @bid.
if @bid . save
HomeworkForCourse . create ( :course_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
2014-11-05 10:49:00 +08:00
redirect_to course_homework_url ( params [ :course_id ] )
2014-10-23 11:30:34 +08:00
else
@bid . safe_attributes = params [ :bid ]
@homework = @bid
@course = Course . find_by_id ( params [ :course_id ] )
@course_id = @course . id
#respond_to do |format|
# format.html { redirect_to new_homework_course_path(params[:course_id]),:layout => 'base_courses'}
# format.api { render_validation_errors(@bid) }
#end
render file : 'courses/new_homework' , layout : 'base_courses'
end
end
# modify by nwb\
# 编辑作业
def edit
@bid = Bid . find ( params [ :bid_id ] )
2014-12-04 15:24:19 +08:00
if ( User . current . admin? || User . current . allowed_to? ( :as_teacher , @bid . courses . first ) )
2014-10-23 11:30:34 +08:00
@course_id = params [ :course_id ]
respond_to do | format |
format . html {
@course = Course . find ( params [ :course_id ] )
@user = User . find ( User . current . id )
render :layout = > 'base_courses'
}
end
else
render_403
end
end
def update
@bid = Bid . find ( params [ :id ] )
@course = @bid . courses . first #Project.find(params[:course_id])
@bid . name = params [ :bid ] [ :name ]
@bid . description = params [ :bid ] [ :description ]
@bid . is_evaluation = params [ :bid ] [ :is_evaluation ]
@bid . proportion = params [ :bid ] [ :proportion ]
2014-11-05 10:06:20 +08:00
@bid . evaluation_num = params [ :bid ] [ :evaluation_num ]
2014-11-19 10:34:36 +08:00
@bid . open_anonymous_evaluation = params [ :bid ] [ :open_anonymous_evaluation ]
2014-10-23 11:30:34 +08:00
@bid . reward_type = 3
@bid . deadline = params [ :bid ] [ :deadline ]
@bid . budget = 0
2014-12-02 17:22:46 +08:00
#@bid.author_id = User.current.id
2014-10-23 11:30:34 +08:00
@bid . commit = 0
@bid . homework_type = 1
@bid . save_attachments ( params [ :attachments ] || ( params [ :bid ] && params [ :bid ] [ :uploads ] ) )
if @bid . save
flash [ :notice ] = l ( :label_update_homework_succeed )
redirect_to course_homework_url ( @course )
else
@bid . safe_attributes = params [ :bid ]
render :action = > 'edit' , :layout = > 'base_courses'
end
end
def new_submit_homework
#render html to prepare create submit homework
find_bid
2014-12-04 15:24:19 +08:00
find_bid
2014-10-23 11:30:34 +08:00
render :layout = > 'base_homework'
end
def add_homework
if User . current . logged? && ( ! Member . where ( 'user_id = ? and project_id = ?' , User . current . id , @bid . courses . first . id ) . first . nil? && ( Member . where ( 'user_id = ? and project_id = ?' , User . current . id , @bid . courses . first . id ) . first . roles & Role . where ( 'id = ? or id = ? or id =?' , 5 , 10 , 7 ) ) . size > 0 )
# homework = HomeworkAttach.create(:bid_id => @bid.id, :user_id => User.current.id)
# homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads]))
if hadcommittedhomework ( User . current . id , @bid . id ) == true
@homework_flag = l ( :label_bidding_homework_committed )
else
@homework = HomeworkAttach . new
@homework . safe_attributes = params [ :homeworkattach ]
@homework . bid_id = @bid . id
@homework . user_id = User . current . id
@homework . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @homework )
@homework_flag = if @homework . save
l ( :label_bidding_homework_succeed )
else
l ( :label_bidding_homework_failed )
end
if @homework . attachments . empty?
@homework . delete
#flash[:error] = l(:no_attachmens_allowed)
@homework_flag = l ( :no_attachmens_allowed )
# else
end
end
end
@homework_list = @bid . homeworks
respond_to do | format |
format . html {
#redirect_to project_for_bid_path, notice: @homework_flag.to_s
flash [ :notice ] = @homework_flag . to_s
redirect_back_or_default ( project_for_bid_path )
}
format . js
end
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
2014-11-02 14:38:43 +08:00
# 启动匿评
def start_anonymous_comment
@bid = Bid . find ( params [ :id ] )
2014-11-06 15:50:39 +08:00
@course = @bid . courses . first
2014-11-04 15:41:10 +08:00
if ( @bid . comment_status == 0 )
2014-11-04 14:53:49 +08:00
homeworks = @bid . homeworks
2014-11-05 09:16:06 +08:00
if ( homeworks && homeworks . size > = 2 )
2014-11-04 21:43:14 +08:00
homeworks . each_with_index do | homework , index |
user = homework . user
2014-11-05 10:06:20 +08:00
n = @bid . evaluation_num
2014-11-04 21:43:14 +08:00
n = n < homeworks . size ? n : homeworks . size - 1
assigned_homeworks = get_assigned_homeworks ( homeworks , n , index )
assigned_homeworks . each do | h |
@homework_evaluation = HomeworkEvaluation . new ( user_id : user . id , homework_attach_id : h . id )
@homework_evaluation . save
2014-11-04 14:53:49 +08:00
end
2014-11-02 14:38:43 +08:00
end
2014-11-04 21:43:14 +08:00
@bid . update_column ( 'comment_status' , 1 )
2014-11-04 14:53:49 +08:00
@statue = 1
else
@statue = 2
2014-11-02 14:38:43 +08:00
end
2014-11-04 14:53:49 +08:00
else
@statue = 3
2014-11-02 14:38:43 +08:00
end
respond_to do | format |
format . js
end
end
def stop_anonymous_comment
@bid = Bid . find ( params [ :id ] )
@bid . update_column ( 'comment_status' , 2 )
2014-11-07 10:02:13 +08:00
respond_to do | format |
format . js
end
end
def alert_anonymous_comment
@bid = Bid . find params [ :id ]
@course = @bid . courses . first
if @bid . comment_status == 0
@totle_size = searchStudent ( @course ) . size
@cur_size = @bid . homeworks . size
elsif @bid . comment_status == 1
@totle_size = 0
@bid . homeworks . map { | homework | @totle_size += homework . homework_evaluations . count }
@cur_size = 0
2014-12-31 10:09:00 +08:00
@bid . homeworks . map { | homework | @cur_size += homework . rates ( :quality ) . where ( " seems_rateable_rates.is_teacher_score = 0 " ) . count }
2014-11-07 10:02:13 +08:00
end
2014-11-07 10:37:10 +08:00
@percent = format ( " %.2f " , ( @cur_size . to_f / ( @totle_size == 0 ? 1 : @totle_size ) ) * 100 )
2014-11-02 14:38:43 +08:00
respond_to do | format |
format . js
end
end
2014-10-23 11:30:34 +08:00
private
2014-11-04 21:43:14 +08:00
def get_assigned_homeworks ( homeworks , n , index )
homeworks += homeworks
homeworks [ index + 1 .. index + n ]
end
2014-10-23 11:30:34 +08:00
def find_bid
if params [ :id ]
@bid = Bid . find ( params [ :id ] , :include = > [ { :homeworks = > :user } ] )
@user = @bid . author
end
rescue
render_404
end
def memberAccess
# 是课程,则判断当前用户是否参加了课程
return true if current_user . admin?
#return 0 if @bid.courses.first.project_type == Project::ProjectType_project
currentUser = User . current
render_403 unless currentUser . member_of_course? ( @bid . courses . first )
end
#验证是否显示课程
def can_show_course
@first_page = FirstPage . find_by_page_type ( 'project' )
if @first_page . show_course == 2
render_404
end
end
#验证是否显示竞赛
def can_show_contest
@first_page = FirstPage . find_by_page_type ( 'project' )
if @first_page . show_contest == 2
render_404
end
end
end