730 lines
34 KiB
Ruby
730 lines
34 KiB
Ruby
class HomeworkCommonController < ApplicationController
|
||
require 'net/http'
|
||
require 'json'
|
||
require "base64"
|
||
layout "base_courses"
|
||
|
||
include StudentWorkHelper
|
||
before_filter :find_course, :only => [:index,:new,:create]
|
||
before_filter :find_homework, :only => [:edit,:update,:alert_anonymous_comment,:start_anonymous_comment,:stop_anonymous_comment,:destroy,:start_evaluation_set,
|
||
:set_evaluation_attr,:score_rule_set,:alert_forbidden_anonymous_comment,:alert_open_student_works,:open_student_works,
|
||
:set_score_open,:alert_score_open_modal,:add_to_homework_bank,:publish_homework]
|
||
before_filter :teacher_of_course, :only => [:new, :create, :edit, :update, :destroy, :start_anonymous_comment, :stop_anonymous_comment, :alert_anonymous_comment,
|
||
:start_evaluation_set,:set_evaluation_attr,:score_rule_set,:alert_forbidden_anonymous_comment,:alert_open_student_works,
|
||
:open_student_works,:add_to_homework_bank,:publish_homework]
|
||
before_filter :member_of_course, :only => [:index]
|
||
|
||
def index
|
||
#unless params[:page]
|
||
# update_homework_time(@course.homework_commons)
|
||
#end
|
||
search = "%#{params[:search].to_s.strip.downcase}%"
|
||
@new_homework = HomeworkCommon.new
|
||
@new_homework.homework_detail_manual = HomeworkDetailManual.new
|
||
@new_homework.course = @course
|
||
@page = params[:page] ? params[:page].to_i + 1 : 0
|
||
@is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course))
|
||
if @is_teacher
|
||
#@homeworks = @course.homework_commons.order("created_at desc").limit(10).offset(@page * 10)
|
||
@homework_commons = @course.homework_commons.where("name like '%#{search}%'").order("created_at desc")
|
||
else
|
||
#@homeworks = @course.homework_commons.where("publish_time <= '#{Date.today}'").order("created_at desc").limit(10).offset(@page * 10)
|
||
@homework_commons = @course.homework_commons.where("name like '%#{search}%' and publish_time <= '#{Time.now}'").order("created_at desc")
|
||
end
|
||
#update_homework_time @homework_commons
|
||
@is_student = User.current.logged? && (User.current.admin? || (User.current.member_of_course?(@course) && !@is_teacher))
|
||
@is_new = params[:is_new]
|
||
|
||
@homeworks = paginateHelper @homework_commons,10
|
||
#设置at已读
|
||
ids = @homeworks.inject([]) do |ids, homework|
|
||
jids = homework.journals_for_messages.map(&:id)
|
||
jids ? ids + jids : ids
|
||
|
||
# homework.delay.set_jour_viewed
|
||
end
|
||
unless ids.empty?
|
||
User.current.at_messages.where(viewed: false,
|
||
at_message_type: 'JournalsForMessage',
|
||
at_message_id: ids).update_all(viewed: true)
|
||
end
|
||
|
||
@left_nav_type = 3
|
||
respond_to do |format|
|
||
format.js
|
||
format.html
|
||
end
|
||
end
|
||
|
||
#新建作业,在个人作业列表创建作业
|
||
def new
|
||
render_404
|
||
end
|
||
|
||
#新建作业,在个人作业列表创建作业
|
||
def create
|
||
redirect_to user_homeworks_user_path(User.current.id)
|
||
end
|
||
|
||
def edit
|
||
@user = User.current
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_manage = params[:is_manage]
|
||
if @hw_status != 1
|
||
@left_nav_type = 3
|
||
respond_to do |format|
|
||
format.html{render :layout => 'base_courses'}
|
||
end
|
||
else
|
||
respond_to do |format|
|
||
format.html{render :layout => 'base_course_community'}
|
||
end
|
||
end
|
||
end
|
||
|
||
def update
|
||
if params[:homework_common]
|
||
is_update = @homework.name != params[:homework_common][:name] || @homework.description != params[:homework_common][:description] || params[:homework_type].to_i != @homework.homework_type
|
||
if params[:homework_type].to_i == 2
|
||
is_update = is_update || params[:language_type] != @homework.homework_detail_programing.language
|
||
if !is_update && params[:sample] && params[:program]
|
||
is_update = params[:sample][:input] != @homework.homework_samples.map(&:input) || params[:sample][:output] != @homework.homework_samples.map(&:output) || params[:program][:input] != @homework.homework_tests.map(&:input)|| params[:program][:output] != @homework.homework_tests.map(&:output)
|
||
end
|
||
elsif params[:homework_type].to_i == 3
|
||
base_on_project = params[:base_on_project] ? 1 : 0
|
||
is_update = is_update || params[:min_num].to_i != @homework.homework_detail_group.min_num || params[:max_num].to_i != @homework.homework_detail_group.max_num || base_on_project != @homework.homework_detail_group.base_on_project
|
||
end
|
||
@homework.is_update = @homework.is_update || is_update
|
||
|
||
@homework.name = params[:homework_common][:name]
|
||
@homework.description = params[:homework_common][:description]
|
||
homework_detail_manual = @homework.homework_detail_manual || HomeworkDetailManual.new
|
||
if params[:homework_common][:end_time] != ""
|
||
if params[:homework_common][:publish_time] == ""
|
||
@homework.publish_time = Time.now
|
||
else
|
||
@homework.publish_time = params[:homework_common][:publish_time]
|
||
end
|
||
param_end_time = Time.parse(params[:homework_common][:end_time]).to_s
|
||
homework_end_time = @homework.end_time ? @homework.end_time.to_s : ''
|
||
if homework_end_time != param_end_time
|
||
if homework_end_time > param_end_time
|
||
@homework.student_works.where("work_status = 1").each do |st|
|
||
if param_end_time < st.commit_time
|
||
st.late_penalty = @homework.late_penalty
|
||
st.work_status = 2
|
||
st.save
|
||
#更新CourseHomeworkStatistics中学生的迟交作品数
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, st.user_id)
|
||
course_statistics.update_attribute('late_commit_work_num', course_statistics.late_commit_work_num + 1) if course_statistics
|
||
end
|
||
end
|
||
else
|
||
@homework.student_works.where("work_status = 2").each do |st|
|
||
if param_end_time >= Time.parse(st.commit_time.to_s).strftime("%Y-%m-%d")
|
||
st.late_penalty = 0
|
||
st.work_status = 1
|
||
st.save
|
||
#更新CourseHomeworkStatistics中学生的迟交作品数
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, st.user_id)
|
||
course_statistics.update_attribute('late_commit_work_num', (course_statistics.late_commit_work_num - 1) < 0 ? 0 : (course_statistics.late_commit_work_num - 1)) if course_statistics
|
||
end
|
||
end
|
||
end
|
||
end
|
||
@homework.end_time = params[:homework_common][:end_time] || Time.now
|
||
|
||
status = false
|
||
if @homework.publish_time > Time.now && homework_detail_manual.comment_status == 1
|
||
homework_detail_manual.comment_status = 0
|
||
@homework.student_works.destroy_all
|
||
#更新CourseHomeworkStatistics中每个学生的未交作品数
|
||
@homework.course.student.each do |student|
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, student.student_id)
|
||
course_statistics.update_attribute('un_commit_work_num', (course_statistics.un_commit_work_num - 1) < 0 ? 0 : (course_statistics.un_commit_work_num - 1)) if course_statistics
|
||
end
|
||
elsif @homework.publish_time <= Time.now && homework_detail_manual.comment_status == 0
|
||
homework_detail_manual.comment_status = 1
|
||
create_works_list @homework if params[:homework_type].to_i != 3
|
||
status = true
|
||
#更新CourseHomeworkStatistics中每个学生的未交作品数
|
||
@homework.course.student.each do |student|
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, student.student_id)
|
||
course_statistics.update_attribute('un_commit_work_num', course_statistics.un_commit_work_num + 1) if course_statistics
|
||
end
|
||
end
|
||
|
||
eval_start = homework_detail_manual.evaluation_start
|
||
if eval_start.nil? || (Time.parse(eval_start.to_s) <= @homework.end_time && homework_detail_manual.comment_status <= 1)
|
||
homework_detail_manual.evaluation_start = (@homework.end_time + 7*24*60*60).strftime("%Y-%m-%d")
|
||
homework_detail_manual.evaluation_end = homework_detail_manual.evaluation_start + 7
|
||
end
|
||
end
|
||
|
||
if params[:homework_type] && params[:homework_type].to_i != @homework.homework_type
|
||
if @homework.homework_type == 2
|
||
@homework.homework_detail_programing.destroy if @homework.homework_detail_programing
|
||
@homework.homework_tests.destroy_all
|
||
elsif @homework.homework_type == 3
|
||
@homework.homework_detail_group.destroy if @homework.homework_detail_group
|
||
create_works_list @homework if homework_detail_manual.comment_status > 0 && @homework.student_works.empty?
|
||
end
|
||
if params[:homework_type].to_i == 3
|
||
@homework.student_works.destroy_all
|
||
end
|
||
@homework.homework_type = params[:homework_type].to_i
|
||
end
|
||
anonymous = params[:homework_common][:anonymous_comment] ? params[:homework_common][:anonymous_comment].to_i : 1
|
||
if anonymous != @homework.anonymous_comment
|
||
if anonymous == 1
|
||
homework_detail_manual.ta_proportion = @homework.homework_type == 2 ? 0.4 : 1.0
|
||
else
|
||
homework_detail_manual.ta_proportion = @homework.homework_type == 2 ? 0.3 : 0.6
|
||
end
|
||
end
|
||
|
||
@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 if params[:language_type]
|
||
if anonymous != @homework.anonymous_comment
|
||
if anonymous == 1
|
||
@homework_detail_programing.ta_proportion = 0.6
|
||
else
|
||
@homework_detail_programing.ta_proportion = 0.5
|
||
end
|
||
end
|
||
|
||
@homework.homework_samples.delete_all if params[:sample]
|
||
sample_inputs = params[:sample][:input] if params[:sample]
|
||
if Array === sample_inputs
|
||
sample_inputs.each_with_index do |val, i|
|
||
@homework.homework_samples << HomeworkSample.new(
|
||
input: val,
|
||
output: params[:sample][:output][i]
|
||
)
|
||
end
|
||
end
|
||
|
||
@homework.homework_tests.delete_all if params[:program]
|
||
inputs = params[:program][:input] if params[:program]
|
||
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.homework_type == 3 && @homework.student_works.has_committed.count == 0
|
||
@homework.homework_detail_group ||= HomeworkDetailGroup.new
|
||
@homework_detail_group = @homework.homework_detail_group
|
||
@homework_detail_group.min_num = params[:min_num].to_i if params[:min_num]
|
||
@homework_detail_group.max_num = params[:max_num].to_i if params[:max_num]
|
||
@homework_detail_group.base_on_project = params[:base_on_project] ? 1 : 0
|
||
end
|
||
|
||
if anonymous != @homework.anonymous_comment
|
||
@homework.student_works.where("work_status != 0").each do |student_work|
|
||
student_work.save
|
||
end
|
||
end
|
||
|
||
@homework.anonymous_comment = params[:homework_common][:anonymous_comment] ? params[:homework_common][:anonymous_comment].to_i : 1
|
||
if @homework.save
|
||
homework_detail_manual.save if homework_detail_manual
|
||
@homework_detail_programing.save if @homework_detail_programing
|
||
@homework_detail_group.save if @homework_detail_group
|
||
|
||
# if @homework.homework_type != 3 && homework_detail_manual.comment_status == 1 && status
|
||
# create_works_list @homework
|
||
# end
|
||
|
||
@hw_status = params[:hw_status].to_i
|
||
if params[:is_manage] == "1"
|
||
redirect_to user_manage_homeworks_user_path(User.current.id)
|
||
elsif params[:is_manage] == "2"
|
||
redirect_to user_receive_homeworks_user_path(User.current.id)
|
||
elsif @hw_status == 1
|
||
redirect_to user_course_community_path(User.current.id)
|
||
elsif @hw_status == 2
|
||
redirect_to course_path(@course.id)
|
||
elsif @hw_status == 5
|
||
redirect_to student_work_index_url(:homework => @homework.id)
|
||
else
|
||
redirect_to homework_common_index_path(:course => @course.id)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
def destroy
|
||
if @homework.destroy
|
||
#更新CourseHomeworkStatistics中每个学生的未交作品数、已交作品数、迟交作品数
|
||
hw_count = @course.homework_commons.includes(:homework_detail_manual).where("homework_detail_manuals.comment_status > 0").count
|
||
homework_ids = @course.homework_commons.empty? ? "(-1)" : "(" + @course.homework_commons.map{|hw| hw.id}.join(",") + ")"
|
||
student_works = StudentWork.where("homework_common_id in #{homework_ids} and work_status !=0")
|
||
is_eva_homeworks = @course.homework_commons.includes(:homework_detail_manual).where("homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 2")
|
||
is_eva_student_works = StudentWork.where(:homework_common_id => is_eva_homeworks.map{|hw| hw.id})
|
||
has_eva_homeworks = @course.homework_commons.includes(:homework_detail_manual).where("homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 3")
|
||
has_eva_student_works = StudentWork.where(:homework_common_id => has_eva_homeworks.map{|hw| hw.id})
|
||
@course.student.each do |student|
|
||
user = student.student
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, student.student_id)
|
||
if user && course_statistics
|
||
committed_work_num = user.student_works.where("homework_common_id in #{homework_ids} and work_status != 0").count
|
||
un_commit_work_num = (hw_count - committed_work_num) < 0 ? 0 : (hw_count - committed_work_num)
|
||
late_commit_work_num = user.student_works.where("homework_common_id in #{homework_ids} and work_status = 2").count
|
||
absence_evaluation_work_num = user.student_works_evaluation_distributions.where(:student_work_id => has_eva_student_works.map(&:id)).count -
|
||
user.student_works_scores.where(:reviewer_role => 3, :student_work_id => has_eva_student_works.map(&:id)).group_by(&:student_work_id).count
|
||
absence_evaluation_work_num = absence_evaluation_work_num < 0 ? 0 : absence_evaluation_work_num
|
||
un_evaluation_work_num = user.student_works_evaluation_distributions.where(:student_work_id => is_eva_student_works.map(&:id)).count -
|
||
user.student_works_scores.where(:reviewer_role => 3, :student_work_id => is_eva_student_works.map(&:id)).group_by(&:student_work_id).count
|
||
un_evaluation_work_num = un_evaluation_work_num < 0 ? 0 : un_evaluation_work_num
|
||
appeal_num = user.student_works_scores.where(:student_work_id => student_works.map(&:id), :appeal_status => 3).count
|
||
average_score = user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first.score : 0
|
||
total_score = user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first.score : 0
|
||
course_statistics.update_attributes(:committed_work_num => committed_work_num, :un_commit_work_num => un_commit_work_num,
|
||
:late_commit_work_num => late_commit_work_num, :absence_evaluation_work_num => absence_evaluation_work_num, :un_evaluation_work_num => un_evaluation_work_num,
|
||
:appeal_num => appeal_num, :average_score => average_score, :total_score => total_score)
|
||
end
|
||
end
|
||
|
||
respond_to do |format|
|
||
format.html {
|
||
@hw_status = params[:hw_status].to_i
|
||
if @hw_status == 1
|
||
redirect_to user_path(User.current.id)
|
||
elsif @hw_status == 2
|
||
redirect_to course_path(@course.id)
|
||
else
|
||
redirect_to homework_common_index_path(:course => @course.id)
|
||
end
|
||
}
|
||
end
|
||
end
|
||
end
|
||
|
||
#加入到题库
|
||
def add_to_homework_bank
|
||
if params[:type] && (params[:type].to_i == 1 || params[:type].to_i == 3)
|
||
if params[:type].to_i == 3
|
||
old_banks = HomeworkBank.where(:homework_common_id => @homework.id)
|
||
unless old_banks.blank?
|
||
old_banks.each do |bank|
|
||
bank.update_attributes(:quotes => (bank.quotes - 1) > 0 ? (bank.quotes - 1) : 0)
|
||
end
|
||
end
|
||
end
|
||
homework_bank = add_to_homework_bank_f @homework
|
||
homework_bank.save
|
||
@homework.update_attributes(:homework_bank_id => homework_bank.id)
|
||
elsif params[:type].to_i == 2
|
||
homework_bank = HomeworkBank.where(:id => @homework.homework_bank_id).first
|
||
if homework_bank
|
||
homework_bank.update_attributes(:name => @homework.name, :description => @homework.description, :homework_type => @homework.homework_type)
|
||
if @homework.homework_type == 2 && @homework.homework_detail_programing
|
||
homework_bank.update_attributes(:language => @homework.homework_detail_programing.language)
|
||
homework_bank.homework_bank_tests.destroy_all
|
||
@homework.homework_tests.each_with_index do |homework_test|
|
||
homework_bank.homework_bank_tests << HomeworkBankTest.new(
|
||
test_type: 1,
|
||
input: homework_test.input,
|
||
output: homework_test.output
|
||
)
|
||
end
|
||
@homework.homework_samples.each_with_index do |homework_test|
|
||
homework_bank.homework_bank_tests << HomeworkBankTest.new(
|
||
test_type: 0,
|
||
input: homework_test.input,
|
||
output: homework_test.output
|
||
)
|
||
end
|
||
elsif @homework.homework_type == 3 && @homework.homework_detail_group
|
||
homework_bank.update_attributes(:min_num => @homework.homework_detail_group.min_num, :max_num => @homework.homework_detail_group.max_num, :base_on_project => @homework.homework_detail_group.base_on_project)
|
||
end
|
||
end
|
||
end
|
||
if @homework.is_update
|
||
@homework.update_attributes(:is_update => 0)
|
||
end
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher, @course)
|
||
end
|
||
|
||
def publish_homework
|
||
if @homework.homework_detail_manual.comment_status > 0
|
||
@status = 1
|
||
elsif @homework.publish_time.nil?
|
||
@status = 2
|
||
else
|
||
homework_detail_manual = @homework.homework_detail_manual
|
||
homework_detail_manual.update_column('comment_status', 1)
|
||
@homework.update_attributes(:publish_time => Time.now)
|
||
@status = 3
|
||
if @homework.homework_type != 3
|
||
create_works_list @homework
|
||
end
|
||
#更新CourseHomeworkStatistics中每个学生的未交作品数
|
||
@course.student.each do |student|
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, student.student_id)
|
||
course_statistics.update_attribute('un_commit_work_num', course_statistics.un_commit_work_num + 1) if course_statistics
|
||
end
|
||
|
||
@course.members.each do |m|
|
||
@homework.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => @course.id, :viewed => false, :status => nil)
|
||
end
|
||
if @homework.course_acts.size == 0
|
||
@homework.course_acts << CourseActivity.new(:user_id => @homework.user_id,:course_id => @homework.course_id)
|
||
end
|
||
end
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher, @course)
|
||
end
|
||
|
||
# 成绩公开范围弹框
|
||
def alert_score_open_modal
|
||
if params[:user_activity_id]
|
||
@user_activity_id = params[:user_activity_id]
|
||
else
|
||
@user_activity_id = -1
|
||
end
|
||
@hw_status = params[:hw_status].to_i
|
||
end
|
||
|
||
def set_score_open
|
||
@homework.update_attribute('score_open', params[:score_open].to_i)
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher, @course)
|
||
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 @homework.end_time >= Time.now
|
||
if @homework_detail_manual.comment_status == 1
|
||
student_works = @homework.student_works.has_committed
|
||
if student_works && student_works.size >= 2
|
||
if @homework.homework_type == 3
|
||
student_work_projects = @homework.student_work_projects.where("student_work_id is not null")
|
||
student_work_projects.each_with_index do |pro_work, pro_index|
|
||
n = @homework_detail_manual.evaluation_num
|
||
n = (n < student_works.size && n != -1) ? n : student_works.size - 1
|
||
work_index = -1
|
||
student_works.each_with_index do |stu_work, stu_index|
|
||
if stu_work.id.to_i == pro_work.student_work_id.to_i
|
||
work_index = stu_index
|
||
end
|
||
end
|
||
assigned_homeworks = get_assigned_homeworks(student_works, n, work_index)
|
||
assigned_homeworks.each do |h|
|
||
student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: pro_work.user_id, student_work_id: h.id)
|
||
student_works_evaluation_distributions.save
|
||
end
|
||
|
||
#更新CourseHomeworkStatistics中该学生的待匿评数
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, pro_work.user_id)
|
||
course_statistics.update_attribute('un_evaluation_work_num', course_statistics.un_evaluation_work_num + n) if course_statistics
|
||
end
|
||
else
|
||
student_works.each_with_index do |work, index|
|
||
user = work.user
|
||
n = @homework_detail_manual.evaluation_num
|
||
n = (n < student_works.size && n != -1) ? 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
|
||
|
||
#更新CourseHomeworkStatistics中该学生的待匿评数
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, user.id)
|
||
course_statistics.update_attribute('un_evaluation_work_num', course_statistics.un_evaluation_work_num + n) if course_statistics
|
||
end
|
||
end
|
||
@homework_detail_manual.update_column('comment_status', 2)
|
||
@homework_detail_manual.update_column('evaluation_start', Date.today)
|
||
@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
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher, @course)
|
||
end
|
||
|
||
#关闭匿评
|
||
def stop_anonymous_comment
|
||
@homework_detail_manual.update_column('comment_status', 3)
|
||
@homework_detail_manual.update_column('evaluation_end', Date.today)
|
||
# 计算缺评扣分
|
||
work_ids = "(" + @homework.student_works.has_committed.map(&:id).join(",") + ")"
|
||
|
||
# 参与匿评的缺评计算
|
||
@homework.student_works.where("work_status != 0").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} and reviewer_role = 3").count
|
||
student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * @homework_detail_manual.absence_penalty : 0
|
||
student_work.save
|
||
|
||
#更新CourseHomeworkStatistics中该学生的待匿评数和缺评数
|
||
course_statistics = CourseHomeworkStatistics.find_by_course_id_and_user_id(@homework.course_id, student_work.user_id)
|
||
course_statistics.update_attribute('un_evaluation_work_num', (course_statistics.un_evaluation_work_num - absence_penalty_count) < 0 ? 0 : (course_statistics.un_evaluation_work_num - absence_penalty_count)) if course_statistics
|
||
course_statistics.update_attribute('absence_evaluation_work_num', course_statistics.absence_evaluation_work_num + absence_penalty_count) if course_statistics
|
||
end
|
||
|
||
# 未参与匿评的缺评计算
|
||
if @homework_detail_manual.no_anon_penalty == 0
|
||
all_dis_eva = StudentWorksEvaluationDistribution.where("student_work_id IN #{work_ids}")
|
||
has_sw_count = all_dis_eva.select("distinct user_id").count
|
||
anon_count = all_dis_eva.count / has_sw_count
|
||
@homework.student_works.where("work_status != 0").each do |student_work|
|
||
if student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count == 0
|
||
student_work.absence_penalty = @homework_detail_manual.absence_penalty * anon_count
|
||
student_work.save
|
||
end
|
||
end
|
||
end
|
||
|
||
# 匿评关闭消息邮件通知
|
||
send_message_anonymous_comment(@homework, m_status = 3)
|
||
# Mailer.send_mail_anonymous_comment_close(@homework).deliver
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher, @course)
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
end
|
||
|
||
# 开启/关闭匿评消息通知
|
||
def send_message_anonymous_comment(homework, m_status )
|
||
# status 标记匿评状态 1为关闭 0为开启
|
||
course = homework.course
|
||
course.members.each do |m|
|
||
@homework.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => m_status)
|
||
end
|
||
end
|
||
#提示
|
||
def alert_anonymous_comment
|
||
@cur_size = 0
|
||
@totle_size = 0
|
||
@work_size = 0
|
||
if @homework_detail_manual.comment_status == 1
|
||
@totle_size = @course.student.count
|
||
@cur_size = @homework.student_works.where("work_status != 0").size
|
||
@work_size = @homework.student_works.has_committed.size
|
||
elsif @homework_detail_manual.comment_status == 2
|
||
@homework.student_works.has_committed.map { |work| @totle_size += work.student_works_evaluation_distributions.count}
|
||
@cur_size = 0
|
||
@homework.student_works.has_committed.map { |work| @cur_size += work.student_works_scores.select("distinct user_id").where(:reviewer_role => 3).count}
|
||
end
|
||
@percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100)
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
end
|
||
|
||
def alert_forbidden_anonymous_comment
|
||
if params[:user_activity_id]
|
||
@user_activity_id = params[:user_activity_id]
|
||
else
|
||
@user_activity_id = -1
|
||
end
|
||
@hw_status = params[:hw_status].to_i
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
end
|
||
|
||
def open_student_works
|
||
if @homework.is_open == 0
|
||
@homework.update_column('is_open', 1)
|
||
else
|
||
@homework.update_column('is_open', 0)
|
||
end
|
||
@user_activity_id = params[:user_activity_id]
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher,@course)
|
||
end
|
||
|
||
def alert_open_student_works
|
||
if params[:user_activity_id]
|
||
@user_activity_id = params[:user_activity_id]
|
||
else
|
||
@user_activity_id = -1
|
||
end
|
||
@hw_status = params[:hw_status].to_i
|
||
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
|
||
|
||
#启动匿评参数设置
|
||
def start_evaluation_set
|
||
if params[:user_activity_id]
|
||
@user_activity_id = params[:user_activity_id]
|
||
else
|
||
@user_activity_id = -1
|
||
end
|
||
@hw_status = params[:hw_status].to_i
|
||
end
|
||
|
||
#设置匿评参数
|
||
def set_evaluation_attr
|
||
if @homework_detail_manual
|
||
unless params[:evaluation_start].to_s == @homework_detail_manual.evaluation_start.to_s
|
||
@homework_detail_manual.evaluation_start = params[:evaluation_start]
|
||
end
|
||
|
||
unless @homework_detail_manual.evaluation_end.to_s == params[:evaluation_end].to_s
|
||
@homework_detail_manual.evaluation_end = params[:evaluation_end]
|
||
end
|
||
|
||
evaluation_num = @homework_detail_manual.evaluation_num
|
||
if params[:evaluation_limit].to_i == 1
|
||
@homework_detail_manual.evaluation_num = params[:evaluation_num]
|
||
@homework_detail_manual.absence_penalty = evaluation_num == -1 ? 5 : @homework_detail_manual.absence_penalty
|
||
else
|
||
@homework_detail_manual.evaluation_num = -1
|
||
@homework_detail_manual.absence_penalty = 0
|
||
end
|
||
@homework_detail_manual.save
|
||
@user_activity_id = params[:user_activity_id].to_i
|
||
@hw_status = params[:hw_status].to_i
|
||
@is_teacher = User.current.admin? || User.current.allowed_to?(:as_teacher,@course)
|
||
end
|
||
end
|
||
|
||
#评分设置
|
||
def score_rule_set
|
||
if params[:user_activity_id]
|
||
@user_activity_id = params[:user_activity_id]
|
||
else
|
||
@user_activity_id = -1
|
||
end
|
||
@hw_status = params[:hw_status].to_i
|
||
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
|
||
@homework_detail_group = @homework.homework_detail_group
|
||
@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==1 || 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
|
||
|
||
def update_homework_time homeworks
|
||
unless homeworks.nil?
|
||
homeworks.each do |h|
|
||
if h.homework_type == 3 && h.homework_detail_group.base_on_project == 1
|
||
student_works = h.student_work_projects.where("is_leader = 1 && project_id != -1")
|
||
time = h.updated_at
|
||
unless student_works.nil?
|
||
student_works.each do |s|
|
||
project = Project.find s.project_id
|
||
unless project.nil? && project.gpid.nil?
|
||
project_time=project.updated_on
|
||
project_time=ForgeActivity.where("project_id=?",project.id).last.updated_at if ForgeActivity.where("project_id=?",project.id).last
|
||
if time.strftime('%Y-%m-%d %H:%M:%S') < project_time.strftime('%Y-%m-%d %H:%M:%S')
|
||
time = project_time
|
||
end
|
||
begin
|
||
# gitlab端获取默认分支
|
||
g = Gitlab.client
|
||
default_branch = g.project(project.gpid).default_branch
|
||
changesets = g.commits(project.gpid, :ref_name => default_branch)
|
||
changesets_latest_coimmit = changesets[0]
|
||
unless changesets[0].blank?
|
||
if time.strftime('%Y-%m-%d %H:%M:%S') <changesets_latest_coimmit.created_at.strftime('%Y-%m-%d %H:%M:%S')
|
||
time = changesets_latest_coimmit.created_at
|
||
end
|
||
end
|
||
rescue
|
||
end
|
||
end
|
||
end
|
||
end
|
||
s_time = time
|
||
if time.strftime('%Y-%m-%d %H:%M:%S') > h.updated_at.strftime('%Y-%m-%d %H:%M:%S')
|
||
h.update_column('updated_at', s_time)
|
||
course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", 'HomeworkCommon', h.id).first
|
||
if course_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > course_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S'))
|
||
course_activity.update_column('updated_at', s_time)
|
||
end
|
||
user_activity = UserActivity.where("act_type=? and act_id =?", 'HomeworkCommon', h.id).first
|
||
if user_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > user_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S'))
|
||
user_activity.update_column('updated_at', s_time)
|
||
end
|
||
org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", 'HomeworkCommon', h.id).first
|
||
if org_activity && (time.strftime('%Y-%m-%d %H:%M:%S') > org_activity.updated_at.strftime('%Y-%m-%d %H:%M:%S'))
|
||
org_activity.update_column('updated_at', s_time)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|