class HomeworkAttachController < ApplicationController layout "course_base" include CoursesHelper include HomeworkAttachHelper helper :words ############################### before_filter :can_show_course,except: [] #判断当前角色权限时需先找到当前操作的project before_filter :find_course_by_bid_id, :only => [:new] before_filter :find_bid_and_course,:only => [:get_not_batch_homework,:get_batch_homeworks,:get_homeworks,:get_homework_jours, :get_student_batch_homework, :get_my_homework] before_filter :find_course_by_hoemwork_id, :only => [:edit,:update,:destroy,:show,:add_homework_users,:destory_homework_users, :praise_homework] #判断当前角色是否有操作权限 #勿删 before_filter :authorize, :only => [:new,:edit,:update,:destroy] #@cur_type: #1.未批作业列表 #2.已批作业列表 #3.全部作业列表 #4.匿评作业列表 #根据此字段判断关闭homework_attach的show界面后是否要调用js刷新页面 #获取未批作业列表 def get_not_batch_homework sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" if sort == 't_socre' order_by = "t_score #{direction}" elsif sort == 's_socre' order_by = "s_score #{direction}" elsif sort == 'time' order_by = "created_at #{direction}" end teachers = find_course_teachers @course all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT * FROM (SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN #{teachers}) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN #{teachers}) AS s_score FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY #{order_by}) AS table1 WHERE table1.t_score IS NULL") @cur_page = params[:page] || 1 @cur_type = 1 @homework_list = paginateHelper all_homework_list,10 @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js end end #获取已评作业列表 def get_batch_homeworks sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" if sort == 't_socre' order_by = "t_score #{direction}" elsif sort == 's_socre' order_by = "s_score #{direction}" elsif sort == 'time' order_by = "created_at #{direction}" end teachers = find_course_teachers @course all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT * FROM (SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN #{teachers} and stars IS NOT NULL) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN #{teachers}) AS s_score FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY #{order_by}) AS table1 WHERE table1.t_score IS NOT NULL") @cur_page = params[:page] || 1 @cur_type = 2 @homework_list = paginateHelper all_homework_list,10 @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js end end #获取所有作业列表 def get_homeworks sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" if sort == 't_socre' order_by = "t_score #{direction}" elsif sort == 's_socre' order_by = "s_score #{direction}" elsif sort == 'time' order_by = "created_at #{direction}" end teachers = find_course_teachers @course all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id in #{teachers}) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id not in #{teachers}) AS s_score FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY #{order_by}") @cur_page = params[:page] || 1 @cur_type = 3 @homework_list = paginateHelper all_homework_list,10 @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js end end #获取学生匿评列表 def get_student_batch_homework @is_student_batch_homework = true teachers = find_course_teachers @course all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN #{teachers}) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN #{teachers}) AS s_score, (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id = #{User.current.id}) AS m_score FROM homework_attaches INNER JOIN homework_evaluations ON homework_evaluations.homework_attach_id = homework_attaches.id WHERE homework_attaches.bid_id = #{@bid.id} AND homework_evaluations.user_id = #{User.current.id} ORDER BY m_score DESC") @cur_page = params[:page] || 1 @cur_type = 4 @homework_list = paginateHelper all_homework_list,10 respond_to do |format| format.js end end #获取我的作业 def get_my_homework @is_my_homework = true teachers = find_course_teachers @course all_homework_list = HomeworkAttach.find_by_sql("SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN #{teachers}) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN #{teachers}) AS s_score FROM homework_attaches WHERE homework_attaches.bid_id = #{@bid.id} AND homework_attaches.user_id = #{User.current.id}") #如果我没有创建过作业,就检索我是否参与了某个作业 if all_homework_list.empty? all_homework_list = HomeworkAttach.find_by_sql("SELECT homework_attaches.*, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN #{teachers}) AS t_score, (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN #{teachers}) AS s_score FROM homework_attaches INNER JOIN homework_users ON homework_users.homework_attach_id = homework_attaches.id WHERE homework_attaches.bid_id = #{@bid.id} AND homework_users.user_id = #{User.current.id}") end @cur_page = params[:page] || 1 @homework_list = paginateHelper all_homework_list,10 respond_to do |format| format.js end end #获取作业的留言列表 def get_homework_jours @user = @bid.author @jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') @jour = paginateHelper @jours,10 @state = false respond_to do |format| format.js end end #为作业点赞 def praise_homework pt = PraiseTread.new pt.user_id = User.current.id pt.praise_tread_object_id = @homework.id pt.praise_tread_object_type = "HomeworkAttach" pt.praise_or_tread = 1 if pt.save respond_to do |format| format.js end else render_404 end end #获取作业的成员 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 def index @homeworks = HomeworkAttach.all respond_to do |format| format.html # index.html.erb format.json { render json: @homeworks } end end #作业添加成员(参与人员) def add_homework_users if User.current.admin? || User.current.member_of_course?(@homework.bid.courses.first) #@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 end end @homework.save get_homework_member @homework respond_to do |format| format.js end else render_403 :message => :notice_not_authorized end end #作业删除成员(参与人员) def destory_homework_users #@homework = HomeworkAttach.find(params[:id]) if User.current.admin? || User.current.member_of_course?(@homework.bid.courses.first) homework_user = @homework.homework_users.where("user_id = #{params[:user_id]}").first homework_user.destroy get_homework_member @homework @homework_list = [] @is_my_homework = true respond_to do |format| format.js end else render_403 :message => :notice_not_authorized end end def create bid = Bid.find params[:bid_id] if User.current.admin? || User.current.member_of_course?(bid.courses.first) # modify by nwb if bid.homeworks.where("user_id = ?",User.current).empty? user_id = params[:user_id] bid_id = params[:bid_id] 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 sta = 0 name = params[:homework_attach][:name] description = params[:homework_attach][:description] options = { :user_id => user_id, :state => sta, :name => name, :description => description, :bid_id => bid_id, :project_id => project_id } @homework = HomeworkAttach.new(options) @homework.save_attachments(params[:attachments]) render_attachment_warning_if_needed(@homework) if @homework.save respond_to do |format| format.html { redirect_to course_for_bid_url @homework.bid } format.json { head :no_content } end else render_403 :message => :notice_not_authorized end else render_403 :message => :notice_has_homework end else render_403 :message => :notice_not_authorized end end def new @bid = Bid.find(params[:id]) if User.current.admin? || User.current.member_of_course?(@bid.courses.first) @homework = HomeworkAttach.new respond_to do |format| format.html # new.html.erb format.json { render json: @homework } end else render_403 :message => :notice_not_authorized end end #获取作业成员的集合 def get_homework_member_list @homework = HomeworkAttach.find(params[:bid_id]) course = @homework.bid.courses.first if User.current.admin? || User.current.member_of_course?(course) get_homework_member @homework else raise "error" end respond_to do |format| format.js end end #获取指定作业的所有成员 def users_for_homework homework homework.nil? ? [] : (homework.users + [homework.user]) end #获取可选成员列表 #homework:作业 #users:该作业所有成员 #q:模糊匹配的用户的昵称 def members_for_homework homework,users,q unpartin_users = homework.bid.courses.first.members.where("user_id not in (:users)", {:users => users}).joins(:user).where("users.login like '%#{q}%'") canpartin_users = [] unpartin_users.each do |m| if m.user.allowed_to?(:paret_in_homework,homework.bid.courses.first) canpartin_users << m end end canpartin_users end def edit if User.current.admin? || User.current.member_of_course?(@homework.bid.courses.first) get_homework_member @homework else render_403 :message => :notice_not_authorized end end def update #@homework = HomeworkAttach.find(params[:id]) course = @homework.bid.courses.first if User.current.admin? || User.current.member_of_course?(course) name = params[:homework_name] description = params[:homework_description] @homework.name = name @homework.description = description @homework.project_id = params[:project_id] || 0 if params[:attachments] @homework.save_attachments(params[:attachments]) end if @homework.save respond_to do |format| format.html { redirect_to course_for_bid_url @homework.bid } format.json { head :no_content } end else end else render_403 :message => :notice_not_authorized end end def destroy if User.current.admin? || User.current == @homework.user if @homework.destroy #respond_to do |format| # format.html { redirect_to course_for_bid_url @homework.bid } # format.json { head :no_content } #end @homework_list = [] @is_my_homework = true respond_to do |format| format.js end else end else render_403 :message => :notice_not_authorized end end #显示作业信息 def show if User.current.admin? || User.current.member_of_course?(@homework.bid.courses.first) # 打分统计 #stars_reates = @homework. rates(:quality) #是否已经进行过评价 temp = HomeworkAttach.find_by_sql("SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = #{@homework.id} AND rater_id = #{User.current.id}").first @m_score = temp.nil? ? 0:temp.stars @has_evaluation = @homework.has_rated?( User.current,:quality) #@jours留言 is null条件用以兼容历史数据 @jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC") @cur_page = params[:cur_page] || 1 @cur_type = params[:cur_type] || 5 @jour = paginateHelper @jours,5 teachers = searchTeacherAndAssistant @course @comprehensive_evaluation = [] #JourForMessage的is_comprehensive_evaluation字段: #1:老师评价 #2:学生评价 #3 || null:学生留言 teachers.each do|teacher| temp = @homework.journals_for_messages.where("is_comprehensive_evaluation = 1 and user_id = #{teacher.user_id}").order("created_on DESC").first @comprehensive_evaluation << temp if temp end annymous_users = @homework.homework_evaluations.map { |homework_evaluation| homework_evaluation.user.id}.join(',') unless annymous_users.nil? || annymous_users == "" @anonymous_comments = @homework.journals_for_messages.where("is_comprehensive_evaluation = 2 and user_id in (#{annymous_users})").order("created_on DESC") end #@totle_score = score_for_homework @homework #@teaher_score = teacher_score_for_homework @homework #is_student = is_cur_course_student @homework.bid.courses.first is_teacher = is_course_teacher User.current,@homework.bid.courses.first @is_anonymous_comments = @homework.bid.comment_status == 1 #是否开启了匿评 #@is_comprehensive_evaluation 1:教师评论,2:学生匿评,3:学生留言 if User.current.member_of_course?(@homework.bid.courses.first) if is_teacher @is_comprehensive_evaluation = 1 #教师评论 else if is_student && @is_anonymous_comments && !@has_evaluation #是学生且开启了匿评且未进行评分 @is_comprehensive_evaluation = 2 #匿评 else #是学生未开启匿评或者已经进行评分 @is_comprehensive_evaluation = 3 #留言 end end else @is_comprehensive_evaluation = 3 end respond_to do |format| format.html format.js end else render_403 :message => :notice_not_authorized end end #删除留言 def destroy_jour @homework = HomeworkAttach.find(params[:jour_id]) @course=@homework.bid.courses.first @journal_destroyed = JournalsForMessage.find(params[:object_id]) @is_comprehensive_evaluation = @journal_destroyed.is_comprehensive_evaluation @journal_destroyed.destroy if @is_comprehensive_evaluation == 3 @jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC") @jour = paginateHelper @jours,5 elsif @is_comprehensive_evaluation == 2 annymous_users = @homework.homework_evaluations.map { |homework_evaluation| homework_evaluation.user.id}.join(',') unless annymous_users.nil? || annymous_users.count == "" @anonymous_comments = @homework.journals_for_messages.where("is_comprehensive_evaluation = 2 and user_id in (#{annymous_users})").order("created_on DESC") end elsif @is_comprehensive_evaluation == 1 teachers = searchTeacherAndAssistant @course @comprehensive_evaluation = [] teachers.each do|teacher| temp = @homework.journals_for_messages.where("is_comprehensive_evaluation = 1 and user_id = #{teacher.user_id}").order("created_on DESC").first @comprehensive_evaluation << temp if temp end end respond_to do |format| format.js end end #添加留言 def addjours @homework = HomeworkAttach.find(params[:jour_id]) #保存评分 @homework.rate(params[:stars_value],User.current.id,:quality) #保存评论 @add_jour = @homework.addjours User.current.id, params[:new_form][:user_message],0,params[:is_comprehensive_evaluation] if @add_jour.is_comprehensive_evaluation == 3 @jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC") @jour = paginateHelper @jours,5 elsif @add_jour.is_comprehensive_evaluation == 2 annymous_users = @homework.homework_evaluations.map { |homework_evaluation| homework_evaluation.user.id}.join(',') unless annymous_users.nil? || annymous_users == "" @anonymous_comments = @homework.journals_for_messages.where("is_comprehensive_evaluation = 2 and user_id in (#{annymous_users})").order("created_on DESC") end elsif @add_jour.is_comprehensive_evaluation == 1 teachers = searchTeacherAndAssistant @homework.bid.courses.first @comprehensive_evaluation = [] teachers.each do|teacher| temp = @homework.journals_for_messages.where("is_comprehensive_evaluation = 1 and user_id = #{teacher.user_id}").order("created_on DESC").first @comprehensive_evaluation << temp if temp end end is_teacher = is_course_teacher User.current,@homework.bid.courses.first @is_anonymous_comments = @homework.bid.comment_status == 1 #是否开启了匿评 #@is_comprehensive_evaluation 1:教师评论,2:学生匿评,3:学生留言 if User.current.member_of_course?(@homework.bid.courses.first) if is_teacher @is_comprehensive_evaluation = 1 #教师评论 else if is_student && @is_anonymous_comments && !@has_evaluation #是学生且开启了匿评且未进行评分 @is_comprehensive_evaluation = 2 #匿评 else #是学生未开启匿评或者已经进行评分 @is_comprehensive_evaluation = 3 #留言 end end else @is_comprehensive_evaluation = 3 end respond_to do |format| format.js end 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_url @homework } format.json { head :no_content } end end #获取指定作业的平均得分 def score end #添加回复 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 private #验证是否显示课程 def can_show_course @first_page = FirstPage.find_by_page_type('project') if @first_page.show_course == 2 render_404 end end def find_bid_and_course @bid = Bid.find(params[:bid_id]) @course = @bid.courses.first rescue ActiveRecord::RecordNotFound render_404 end def find_course_by_bid_id @bid = Bid.find(params[:id]) @course = @bid.courses.first rescue ActiveRecord::RecordNotFound render_404 end def find_course_by_hoemwork_id @homework = HomeworkAttach.find(params[:id]) @bid = @homework.bid @course = @bid.courses.first rescue ActiveRecord::RecordNotFound render_404 end #获取课程的老师列表 def find_course_teachers course teachers = "(" teacher_members = searchTeacherAndAssistant(course) teacher_members.each do |member| if member == teacher_members.last teachers += member.user_id.to_s + ")" else teachers += member.user_id.to_s + "," end end teachers end #获取作业教师评分所占比例 def get_teacher_proportion bid if bid.proportion teacher_proportion = bid.proportion * 1.0 / 100 else teacher_proportion = 1.0 end teacher_proportion end end