2014-05-16 11:02:25 +08:00
class HomeworkAttachController < ApplicationController
2014-05-20 09:59:33 +08:00
###############################
def index
@homeworks = HomeworkAttach . all
respond_to do | format |
format . html # index.html.erb
format . json { render json : @homeworks }
end
end
2014-05-21 14:10:11 +08:00
def add_users users
if users != nil && users . count > 0
users . each do | user |
@homework . homework_users . build ( :user_id = > user . id )
@homework . save
end
end
end
2014-05-20 09:59:33 +08:00
def create
2014-05-21 14:10:11 +08:00
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 )
user_id = params [ :user_id ]
bid_id = params [ :bid_id ]
sta = 0
name = params [ :new_form ] [ :name ]
description = params [ :new_form ] [ :description ]
options = {
:user_id = > user_id ,
:state = > sta ,
:name = > name ,
:description = > description ,
:bid_id = > bid_id
}
2014-05-20 09:59:33 +08:00
2014-05-21 14:10:11 +08:00
@homework = HomeworkAttach . new ( options )
@homework . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @homework )
@homework . save
respond_to do | format |
if @homework . save
format . html { redirect_to @homework , notice : 'Post was successfully created.' }
format . json { render json : @homework , status : :created , location : @homework }
else
format . html { render action : " new " }
format . json { render json : @homework . errors , status : :unprocessable_entity }
end
2014-05-20 09:59:33 +08:00
end
end
end
def new
@homework = HomeworkAttach . new
2014-05-21 14:10:11 +08:00
@bid = Bid . find ( params [ :id ] )
2014-05-20 09:59:33 +08:00
respond_to do | format |
format . html # new.html.erb
format . json { render json : @homework }
end
end
def edit
@homework = HomeworkAttach . find ( params [ :id ] )
end
def update
@homework = HomeworkAttach . find ( params [ :id ] )
respond_to do | format |
if @post . update_attributes ( params [ :homework ] )
format . html { redirect_to @homework , notice : 'Homework was successfully updated.' }
format . json { head :no_content }
else
format . html { render action : " edit " }
format . json { render json : @homework . errors , status : :unprocessable_entity }
end
end
end
def destroy
2014-05-21 14:10:11 +08:00
@homework = HomeworkAttach . find ( params ( [ :id ] ) )
@homework . destroy
respond_to do | format |
format . html { render @homework }
format . json { render json : @homework }
end
2014-05-20 09:59:33 +08:00
end
2014-05-16 11:02:25 +08:00
#显示作业信息
def show
@homework = HomeworkAttach . find ( params [ :id ] )
# 打分统计
stars_reates = @homework .
rates ( :quality )
stars_reates_count = stars_reates . count == 0 ? 1 : stars_reates . count
stars_status = stars_reates . select ( " stars, count(*) as scount " ) .
group ( " stars " )
@stars_status_map = Hash . new ( 0 . 0 )
stars_status . each do | star_status |
percent = ( star_status . scount * 1 . 0 / stars_reates_count ) * 100 . to_f
percent_m = format ( " %.2f " , percent )
@stars_status_map [ " star #{ star_status . stars . to_i } " . to_sym ] =
percent_m . to_s + " % "
end
2014-05-17 11:15:27 +08:00
@limit = 10
2014-05-22 20:01:26 +08:00
@jours = @homework . journals_for_messages . order ( " created_on DESC " )
2014-05-17 11:15:27 +08:00
@feedback_count = @jours . count
@feedback_pages = Paginator . new @feedback_count , @limit , params [ 'page' ]
@offset || = @feedback_pages . offset
@jour = @jours [ @offset , @limit ]
2014-05-16 11:02:25 +08:00
end
2014-05-17 13:49:27 +08:00
#删除留言
2014-05-20 09:59:33 +08:00
def destroy_jour
2014-05-17 11:15:27 +08:00
@journal_destroyed = JournalsForMessage . delete_message ( params [ :object_id ] )
2014-05-20 08:48:42 +08:00
#@homework = HomeworkAttach.find(params[:id])
#@jours = @homework.journals_for_messages.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]
2014-05-17 11:15:27 +08:00
respond_to do | format |
format . js
end
end
2014-05-17 13:49:27 +08:00
#添加留言
2014-05-17 11:15:27 +08:00
def addjours
@homework = HomeworkAttach . find ( params [ :jour_id ] )
@homework . addjours User . current . id , params [ :new_form ] [ :user_message ] , 0
@jours = @homework . journals_for_messages . order ( " created_on DESC " )
2014-05-22 20:01:26 +08:00
@limit = 10
@feedback_count = @jours . count
@feedback_pages = Paginator . new @feedback_count , @limit , params [ 'page' ]
@offset || = @feedback_pages . offset
@jour = @jours [ @offset , @limit ]
2014-05-17 11:15:27 +08:00
respond_to do | format |
format . js
end
2014-05-16 11:02:25 +08:00
end
2014-05-17 13:49:27 +08:00
#获取指定作业的平均得分
def score
#stars_reates = @homework.rates(:quality)
#percent = 0
#stars_reates.each do |star_reates|
# percent = percent + star_reates.stars
#end
#stars_reates_count = stars_reates.count == 0 ? 1 : stars_reates.count
#result = percent * 1.0 / stars_reates_count
#result
end
2014-05-17 15:39:18 +08:00
#添加回复
def add_jour_reply
parent_id = params [ :reference_id ]
author_id = User . current . id
reply_user_id = params [ :reference_user_id ]
reply_id = params [ :reference_message_id ] # 暂时不实现
content = params [ :user_notes ]
options = { :user_id = > author_id ,
:m_parent_id = > parent_id ,
:m_reply_id = > reply_id ,
:reply_id = > reply_user_id ,
:notes = > content ,
:is_readed = > false }
@jfm = JournalsForMessage . new ( options )
@jfm . save
respond_to do | format |
format . js {
@save_succ = true if @jfm . errors . empty?
}
end
end
2014-05-16 11:02:25 +08:00
end