2014-05-16 11:02:25 +08:00
class HomeworkAttachController < ApplicationController
2014-06-19 15:55:42 +08:00
include CoursesHelper
2014-05-20 09:59:33 +08:00
###############################
2014-06-05 17:39:48 +08:00
#判断当前角色权限时需先找到当前操作的project
2014-06-17 15:06:31 +08:00
before_filter :find_course_by_bid_id , :only = > [ :new ]
before_filter :find_course_by_hoemwork_id , :only = > [ :edit , :update , :destroy , :show , :add_homework_users , :destory_homework_users ]
2014-06-05 17:39:48 +08:00
#判断当前角色是否有操作权限
#勿删 before_filter :authorize, :only => [:new,:edit,:update,:destroy]
2014-06-17 15:06:31 +08:00
def find_course_by_bid_id
2014-06-05 17:39:48 +08:00
@bid = Bid . find ( params [ :id ] )
2014-06-17 15:06:31 +08:00
@course = @bid . courses [ 0 ]
2014-06-05 17:39:48 +08:00
rescue ActiveRecord :: RecordNotFound
render_404
end
2014-06-17 15:06:31 +08:00
def find_course_by_hoemwork_id
2014-06-05 17:39:48 +08:00
@homework = HomeworkAttach . find ( params [ :id ] )
2014-06-17 15:06:31 +08:00
@course = @homework . bid . courses [ 0 ]
2014-06-05 17:39:48 +08:00
end
2014-06-11 17:41:06 +08:00
#获取作业的成员
def get_homework_member homework
@hoemwork_users = users_for_homework ( @homework )
@members = members_for_homework ( @homework , @hoemwork_users , params [ :q ] )
@members = paginateHelper @members , 10
end
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-06-11 15:28:13 +08:00
#作业添加成员(参与人员)
def add_homework_users
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( @homework . bid . courses . first )
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
if params [ :membership ]
if params [ :membership ] [ :user_ids ]
attrs = params [ :membership ] . dup
user_ids = attrs . delete ( :user_ids )
user_ids . each do | user_id |
@homework . homework_users . build ( :user_id = > user_id )
end
2014-06-11 15:28:13 +08:00
end
2014-05-21 14:10:11 +08:00
end
2014-06-11 17:34:57 +08:00
@homework . save
2014-06-11 17:41:06 +08:00
get_homework_member @homework
2014-06-11 17:34:57 +08:00
respond_to do | format |
format . js
end
else
render_403 :message = > :notice_not_authorized
2014-06-11 15:28:13 +08:00
end
end
#作业删除成员(参与人员)
def destory_homework_users
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( @homework . bid . courses . first )
2014-06-11 17:34:57 +08:00
homework_user = @homework . homework_users . where ( " user_id = #{ params [ :user_id ] } " ) . first
homework_user . destroy
2014-06-11 17:41:06 +08:00
get_homework_member @homework
2014-06-11 17:34:57 +08:00
respond_to do | format |
format . js
end
else
render_403 :message = > :notice_not_authorized
2014-06-11 15:28:13 +08:00
end
2014-05-21 14:10:11 +08:00
end
2014-05-20 09:59:33 +08:00
def create
2014-06-04 16:23:43 +08:00
bid = Bid . find params [ :bid_id ]
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( bid . courses . first ) # modify by nwb
2014-06-06 10:28:37 +08:00
if bid . homeworks . where ( " user_id = ? " , User . current ) . count == 0
user_id = params [ :user_id ]
bid_id = params [ :bid_id ]
2014-06-19 17:49:18 +08:00
if params [ :homework_attach ]
if params [ :homework_attach ] [ :project_id ]
project_id = params [ :homework_attach ] [ :project_id ]
else
project_id = 0
end
else
project_id = 0
end
2014-06-06 10:28:37 +08:00
sta = 0
name = params [ :new_form ] [ :name ]
description = params [ :new_form ] [ :description ]
options = {
:user_id = > user_id ,
:state = > sta ,
:name = > name ,
:description = > description ,
2014-06-18 10:07:19 +08:00
:bid_id = > bid_id ,
:project_id = > project_id
2014-06-06 10:28:37 +08:00
}
2014-05-20 09:59:33 +08:00
2014-06-04 16:23:43 +08:00
2014-06-06 10:28:37 +08:00
#@homework_list = @bid.homeworks
2014-06-03 15:47:00 +08:00
2014-06-06 10:28:37 +08:00
@homework = HomeworkAttach . new ( options )
@homework . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @homework )
2014-05-21 14:10:11 +08:00
2014-06-06 10:28:37 +08:00
if @homework . save
respond_to do | format |
2014-06-17 15:06:31 +08:00
format . html { redirect_to course_for_bid_path @homework . bid }
2014-06-06 10:28:37 +08:00
format . json { head :no_content }
end
else
render_403 :message = > :notice_not_authorized
2014-05-21 14:10:11 +08:00
end
2014-06-04 09:11:06 +08:00
else
2014-06-06 10:28:37 +08:00
render_403 :message = > :notice_has_homework
2014-05-20 09:59:33 +08:00
end
2014-06-04 16:23:43 +08:00
else
2014-06-06 10:28:37 +08:00
render_403 :message = > :notice_not_authorized
2014-06-04 16:23:43 +08:00
end
2014-05-20 09:59:33 +08:00
end
def new
2014-05-21 14:10:11 +08:00
@bid = Bid . find ( params [ :id ] )
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( @bid . courses . first ) #nwb
2014-06-11 15:28:13 +08:00
#该课程的学生的集合(新建不实现功能:添加成员)
#@members = @bid.courses.first.members.joins(:member_roles).where("member_roles.role_id IN (:role_id) and user_id <> #{User.current.id}", {:role_id => [5, 10]})
2014-06-10 17:33:31 +08:00
2014-06-11 15:28:13 +08:00
#@members = paginateHelper @members,10
2014-06-10 08:47:21 +08:00
#@all_user = []
#@bid.courses.first.members.each do |member|
# @all_user << member.user
#end
2014-06-04 16:23:43 +08:00
@homework = HomeworkAttach . new
2014-06-10 08:47:21 +08:00
#@homework_user = members_for_homework(@homework) + User.current
#@members = @all_user - @homework_user
2014-06-04 16:23:43 +08:00
respond_to do | format |
format . html # new.html.erb
format . json { render json : @homework }
end
else
render_403 :message = > :notice_not_authorized
2014-05-20 09:59:33 +08:00
end
end
2014-06-11 15:28:13 +08:00
#获取作业成员的集合
2014-06-10 17:33:31 +08:00
def get_homework_member_list
2014-06-11 15:54:17 +08:00
@homework = HomeworkAttach . find ( params [ :bid_id ] )
2014-06-11 15:28:13 +08:00
course = @homework . bid . courses . first
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( course )
2014-06-11 17:41:06 +08:00
get_homework_member @homework
2014-06-10 17:33:31 +08:00
else
raise " error "
end
respond_to do | format |
format . js
end
end
2014-06-10 08:47:21 +08:00
#获取指定作业的所有成员
2014-06-11 15:28:13 +08:00
def users_for_homework homework
homework . nil? ? [ ] : ( homework . users + [ homework . user ] )
end
#获取可选成员列表
#homework: 作业
#users: 该作业所有成员
2014-06-12 14:43:51 +08:00
#q:模糊匹配的用户的昵称
2014-06-11 15:54:17 +08:00
def members_for_homework homework , users , q
homework . bid . courses . first . members . joins ( :member_roles ) . where ( " member_roles.role_id IN (:role_id) and user_id not in (:users) " , { :role_id = > [ 5 , 10 ] , :users = > users } ) . joins ( :user ) . where ( " users.login like '% #{ q } %' " )
2014-06-10 08:47:21 +08:00
end
2014-05-20 09:59:33 +08:00
def edit
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( @homework . bid . courses . first )
2014-06-11 15:28:13 +08:00
#@members = @homework.bid.courses.first.members.joins(:member_roles).where("member_roles.role_id IN (:role_id)", {:role_id => [5, 10]})
2014-06-11 17:41:06 +08:00
get_homework_member @homework
2014-06-04 16:23:43 +08:00
else
render_403 :message = > :notice_not_authorized
end
2014-05-20 09:59:33 +08:00
end
def update
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
2014-06-11 15:28:13 +08:00
course = @homework . bid . courses . first
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( course )
2014-06-04 16:23:43 +08:00
name = params [ :homework_name ]
description = params [ :homework_description ]
2014-06-19 17:49:18 +08:00
if params [ :homework_attach ]
if params [ :homework_attach ] [ :project_id ]
project_id = params [ :homework_attach ] [ :project_id ]
else
project_id = 0
end
else
project_id = 0
end
2014-06-04 16:23:43 +08:00
@homework . name = name
@homework . description = description
2014-06-17 18:49:21 +08:00
@homework . project_id = project_id
2014-06-04 16:23:43 +08:00
if params [ :attachments ]
@homework . save_attachments ( params [ :attachments ] )
end
if @homework . save
respond_to do | format |
2014-06-17 15:06:31 +08:00
format . html { redirect_to course_for_bid_path @homework . bid }
2014-06-04 16:23:43 +08:00
format . json { head :no_content }
end
else
2014-06-03 16:47:00 +08:00
end
2014-06-04 09:11:06 +08:00
else
2014-06-04 16:23:43 +08:00
render_403 :message = > :notice_not_authorized
2014-06-03 16:47:00 +08:00
end
2014-05-20 09:59:33 +08:00
end
def destroy
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
2014-06-17 18:49:21 +08:00
if User . current . admin? || User . current == @homework . user
2014-06-04 16:23:43 +08:00
if @homework . destroy
respond_to do | format |
2014-06-17 15:06:31 +08:00
format . html { redirect_to course_for_bid_path @homework . bid }
2014-06-04 16:23:43 +08:00
format . json { head :no_content }
end
else
2014-06-03 16:47:00 +08:00
end
2014-06-04 09:11:06 +08:00
else
2014-06-04 16:23:43 +08:00
render_403 :message = > :notice_not_authorized
2014-06-03 16:47:00 +08:00
end
2014-05-20 09:59:33 +08:00
end
2014-05-16 11:02:25 +08:00
#显示作业信息
def show
2014-06-11 17:34:57 +08:00
#@homework = HomeworkAttach.find(params[:id])
2014-06-17 15:06:31 +08:00
if User . current . admin? || User . current . member_of_course? ( @homework . bid . courses . first )
2014-06-04 16:23:43 +08:00
# 打分统计
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-06-05 14:00:18 +08:00
#是否已经进行过评价
@has_evaluation = stars_reates . where ( " rater_id = ? " , User . current ) . count > 0
#是否开启互评功能
@is_evaluation = @homework . bid . is_evaluation == 1 || @homework . bid . is_evaluation == nil
2014-06-04 16:23:43 +08:00
@limit = 10
@jours = @homework . journals_for_messages . where ( " is_comprehensive_evaluation is null " ) . order ( " created_on DESC " )
@feedback_count = @jours . count
@feedback_pages = Paginator . new @feedback_count , @limit , params [ 'page' ]
@offset || = @feedback_pages . offset
@jour = @jours [ @offset , @limit ]
@comprehensive_evaluation = @homework . journals_for_messages . where ( " is_comprehensive_evaluation is not null " ) . order ( " created_on DESC " )
2014-06-19 15:55:42 +08:00
@totle_score = score_for_homework @homework
@teaher_score = teacher_score_for_homework @homework
2014-06-04 16:23:43 +08:00
else
render_403 :message = > :notice_not_authorized
2014-05-16 11:02:25 +08:00
end
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 ] )
2014-05-27 20:01:21 +08:00
@add_jour = @homework . addjours User . current . id , params [ :new_form ] [ :user_message ] , 0 , params [ :is_comprehensive_evaluation ]
@jours = @homework . journals_for_messages . where ( " is_comprehensive_evaluation is null " ) . 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-27 20:01:21 +08:00
@comprehensive_evaluation = @homework . journals_for_messages . where ( " is_comprehensive_evaluation is not null " ) . order ( " created_on DESC " )
2014-06-19 17:00:40 +08:00
@totle_score = score_for_homework @homework
@teaher_score = teacher_score_for_homework @homework
2014-05-17 11:15:27 +08:00
respond_to do | format |
format . js
end
2014-06-26 17:49:23 +08:00
end
#教师综评
def comprehensive_evaluation_jour
@homework = HomeworkAttach . find ( params [ :jour_id ] )
@add_jour = @homework . addjours User . current . id , params [ :new_form ] [ :user_message ] , 0 , params [ :is_comprehensive_evaluation ]
respond_to do | format |
format . html { redirect_to homework_attach_path @homework }
format . json { head :no_content }
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