class HomeworkCommonController < ApplicationController require 'net/http' require 'json' require "base64" layout "base_courses" include StudentWorkHelper before_filter :find_course, :only => [:index,:new,:create,:next_step] before_filter :find_homework, :only => [:edit,:update,:alert_anonymous_comment,:start_anonymous_comment,:stop_anonymous_comment,:destroy] before_filter :teacher_of_course, :only => [:new, :create, :edit, :update, :destroy, :start_anonymous_comment, :stop_anonymous_comment, :alert_anonymous_comment] before_filter :member_of_course, :only => [:index] def index homeworks = @course.homework_commons.order("created_at desc") @is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course)) @is_student = User.current.logged? && (User.current.admin? || (User.current.member_of_course?(@course) && !@is_teacher)) @homeworks = paginateHelper homeworks,20 respond_to do |format| format.html end end #新建作业,在个人作业列表创建作业 def new render_404 # @homework_type = "1" # # @homework = HomeworkCommon.new # @homework.safe_attributes = params[:homework_common] # @homework.late_penalty = 2 # @homework.end_time = (Time.now + 3600 * 24).strftime('%Y-%m-%d') # @homework.publish_time = Time.now.strftime('%Y-%m-%d') # # if @homework_type == "1" # #匿评作业相关属性 # @homework_detail_manual = HomeworkDetailManual.new # @homework_detail_manual.ta_proportion = 0.6 # @homework_detail_manual.absence_penalty = 2 # @homework_detail_manual.evaluation_num = 3 # @homework_detail_manual.evaluation_start = Time.now.strftime('%Y-%m-%d') # @homework_detail_manual.evaluation_end = (Time.now + 3600 * 24).strftime('%Y-%m-%d') # @homework.homework_detail_manual = @homework_detail_manual # elsif @homework_type == "2" # #编程作业相关属性 # @homework_detail_programing = HomeworkDetailPrograming.new # @homework.homework_detail_programing = @homework_detail_programing # end # respond_to do |format| # format.html # end end #新建作业,在个人作业列表创建作业 def create redirect_to user_homeworks_user_path(User.current.id) # if params[:homework_common] # homework = HomeworkCommon.new # homework.name = params[:homework_common][:name] # homework.description = params[:homework_common][:description] # homework.end_time = params[:homework_common][:end_time] # homework.publish_time = params[:homework_common][:publish_time] # homework.homework_type = params[:homework_common][:homework_type] # homework.late_penalty = params[:late_penalty] # homework.user_id = User.current.id # homework.course_id = @course.id # # homework.save_attachments(params[:attachments]) # render_attachment_warning_if_needed(homework) # # if homework.homework_type == 2 # homework_detail_programing = HomeworkDetailPrograming.new # homework_detail_programing.language = params[:language] # homework_detail_programing.standard_code = params[:standard_code] # homework_detail_programing.ta_proportion = params[:ta_proportion] || 0.6 # question = {title:homework.name,content:homework.description} # question[:input] = [] # question[:output] = [] # if params[:input] && params[:output] && params[:result] # params[:input].each do |k,v| # if params[:output].include? k # homework_test = HomeworkTest.new # homework_test.input = v # homework_test.output = params[:output][k] # homework_test.result = params[:result][k] # homework_test.error_msg = params[:error_msg] # homework.homework_tests << homework_test # question[:input] << homework_test.input # question[:output] << homework_test.output # end # end # end # # # uri = URI('http://192.168.80.21:8080/api/questions.json') # # body = question.to_json # # res = Net::HTTP.new(uri.host, uri.port).start do |client| # # request = Net::HTTP::Post.new(uri.path) # # request.body = body # # request["Content-Type"] = "application/json" # # client.request(request) # # end # # result = JSON.parse(res.body) # # homework_detail_programing.question_id = result["id"] if result["status"] && result["status"] == 0 # # homework.homework_detail_programing = homework_detail_programing # else # #匿评作业相关属性 # homework_detail_manual = HomeworkDetailManual.new # homework_detail_manual.ta_proportion = params[:ta_proportion] || 0.6 # homework_detail_manual.comment_status = 1 # homework_detail_manual.evaluation_start = params[:evaluation_start] # homework_detail_manual.evaluation_end = params[:evaluation_end] # homework_detail_manual.evaluation_num = params[:evaluation_num] # homework_detail_manual.absence_penalty = params[:absence_penalty] # homework.homework_detail_manual = homework_detail_manual # end # # if homework.save # homework_detail_programing.save if homework_detail_programing # homework_detail_manual.save if homework_detail_manual # respond_to do |format| # format.html { # flash[:notice] = l(:notice_successful_create) # redirect_to homework_common_index_path(:course => @course.id) # } # end # return # end # end # # respond_to do |format| # format.html { # flash[:notice] = l(:notice_failed_create) # redirect_to new_homework_common_path(:course => @course.id) # } # end end def edit @user = User.current @is_in_course = params[:is_in_course] respond_to do |format| format.html{render :layout => 'new_base_user'} end end def update if params[:homework_common] @homework.name = params[:homework_common][:name] @homework.description = params[:homework_common][:description] @homework.end_time = params[:homework_common][:end_time] || Time.now @homework.course_id = params[:course_id] @homework.save_attachments(params[:attachments]) render_attachment_warning_if_needed(@homework) #编程作业相关属性 if @homework.homework_type == 2 @homework.homework_detail_programing ||= HomeworkDetailPrograming.new @homework_detail_programing = @homework.homework_detail_programing @homework_detail_programing.language = params[:language_type].to_i @homework.homework_tests.delete_all inputs = params[:program][:input] if Array === inputs inputs.each_with_index do |val, i| @homework.homework_tests << HomeworkTest.new( input: val, output: params[:program][:output][i] ) end end end if @homework.save @homework_detail_manual.save if @homework_detail_manual @homework_detail_programing.save if @homework_detail_programing if params[:is_in_course] == "1" redirect_to homework_common_index_path(:course => @course.id) else redirect_to user_homeworks_user_path(User.current.id) end end end end def destroy if @homework.destroy respond_to do |format| format.html { if params[:is_in_course] == "1" redirect_to homework_common_index_path(:course => @course.id) else redirect_to user_homeworks_user_path(User.current.id) end } end end end #开启匿评 #statue 1:启动成功,2:启动失败,作业总数大于等于2份时才能启动匿评,3:已开启匿评,请务重复开启,4:没有开启匿评的权限 def start_anonymous_comment @statue = 4 and return unless User.current.admin? || User.current.allowed_to?(:as_teacher,@course) @statue = 5 and return if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") if @homework_detail_manual.comment_status == 1 student_works = @homework.student_works if student_works && student_works.size >= 2 student_works.each_with_index do |work, index| user = work.user n = @homework_detail_manual.evaluation_num n = n < student_works.size ? n : student_works.size - 1 assigned_homeworks = get_assigned_homeworks(student_works, n, index) assigned_homeworks.each do |h| student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id) student_works_evaluation_distributions.save end end @homework_detail_manual.update_column('comment_status', 2) @statue = 1 # 匿评开启消息邮件通知 send_message_anonymous_comment(@homework, m_status = 2) Mailer.send_mail_anonymous_comment_open(@homework).deliver else @statue = 2 end else @statue = 3 end end #关闭匿评 def stop_anonymous_comment @homework_detail_manual.update_column('comment_status', 3) #计算缺评扣分 work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" @homework.student_works.each do |student_work| absence_penalty_count = student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - student_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * @homework_detail_manual.absence_penalty : 0 student_work.save end # 匿评关闭消息邮件通知 send_message_anonymous_comment(@homework, m_status = 3) Mailer.send_mail_anonymous_comment_close(@homework).deliver respond_to do |format| format.js end end # 开启/关闭匿评消息通知 def send_message_anonymous_comment(homework, m_status ) # status 标记匿评状态 1为关闭 0为开启 course = @homework.course course.student.each do |st| @homework.course_messages << CourseMessage.new(:user_id => st.student_id, :course_id => course.id, :viewed => false, :status => m_status) end end #提示 def alert_anonymous_comment @cur_size = 0 @totle_size = 0 if @homework_detail_manual.comment_status == 1 @totle_size = @course.student.count @cur_size = @homework.student_works.size elsif @homework_detail_manual.comment_status == 2 @homework.student_works.map { |work| @totle_size += work.student_works_evaluation_distributions.count} @cur_size = 0 @homework.student_works.map { |work| @cur_size += work.student_works_scores.where(:reviewer_role => 3).count} end @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) respond_to do |format| format.js end end def programing_test test = {language:params[:language],src:Base64.encode64(params[:src]),input:[params[:input]],output:[params[:output]]} @index = params[:index] uri = URI('http://192.168.80.21:8080/api/realtime.json') body = test.to_json res = Net::HTTP.new(uri.host, uri.port).start do |client| request = Net::HTTP::Post.new(uri.path) request.body = body request["Content-Type"] = "application/json" client.request(request) end result = JSON.parse(res.body) @err_msg = result["compile_error_msg"] result["results"].each do |re| @result = re["status"] end end private #获取课程 def find_course @course = Course.find params[:course] rescue render_404 end #获取作业 def find_homework @homework = HomeworkCommon.find params[:id] @homework_detail_manual = @homework.homework_detail_manual @homework_detail_programing = @homework.homework_detail_programing @course = @homework.course rescue render_404 end #是不是课程的老师 def teacher_of_course render_403 unless User.current.allowed_to?(:as_teacher,@course) || User.current.admin? end #当前用户是不是课程的成员 def member_of_course render_403 unless @course.is_public || User.current.member_of_course?(@course) || User.current.admin? end def get_assigned_homeworks(student_works, n, index) student_works += student_works student_works[index + 1 .. index + n] end end