Merge branch 'guange_homework' of http://repository.trustie.net/xianbo/trustie2 into guange_homework

This commit is contained in:
sw 2015-09-09 09:11:30 +08:00
commit 068b03cd8f
5 changed files with 62 additions and 23 deletions

View File

@ -42,9 +42,6 @@ class StudentWorkController < ApplicationController
end
end
render :json => resultObj
end
@ -165,24 +162,23 @@ class StudentWorkController < ApplicationController
def create
if params[:student_work]
stundet_work = StudentWork.find(params[:student_work_id]) if params[:student_work_id]
stundet_work ||= StudentWork.new
stundet_work.name = params[:student_work][:name]
stundet_work.description = params[:student_work][:description]
stundet_work.project_id = params[:student_work][:project_id]
stundet_work.homework_common_id = @homework.id
stundet_work.user_id = User.current.id
stundet_work.save_attachments(params[:attachments])
student_work = StudentWork.find(params[:student_work_id]) if params[:student_work_id]
student_work ||= StudentWork.new
student_work.name = params[:student_work][:name]
student_work.description = params[:student_work][:description]
student_work.project_id = params[:student_work][:project_id]
student_work.homework_common_id = @homework.id
student_work.user_id = User.current.id
student_work.save_attachments(params[:attachments])
if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d")
stundet_work.late_penalty = @homework.late_penalty
student_work.late_penalty = @homework.late_penalty
else
stundet_work.late_penalty = 0
student_work.late_penalty = 0
end
render_attachment_warning_if_needed(stundet_work)
if stundet_work.save
if @homework.homework_type == 2 && @homework.homework_detail_programing #编程作业,学生提交作品后计算系统得分
end
render_attachment_warning_if_needed(student_work)
if student_work.save
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_create)
@ -624,7 +620,7 @@ class StudentWorkController < ApplicationController
end
def find_or_save_student_work(is_test)
student_work = @homework.student_works.where(user_id: User.current.id).first
student_work = StudentWork.where(homework_common_id: @homework.id, user_id: User.current.id).first
if student_work.nil?
@homework.student_works.build(
name: params[:title],
@ -633,6 +629,7 @@ class StudentWorkController < ApplicationController
is_test: is_test
)
unless @homework.save
logger.debug @homework.errors.full_messages
else
student_work = @homework.student_works.where(user_id: User.current.id).first
end

View File

@ -368,8 +368,15 @@ class UsersController < ApplicationController
end
def user_commit_homework
flash[:notice] = l(:notice_successful_create)
redirect_to student_work_index_url(:homework => params[:homework])
homework = HomeworkCommon.find(params[:homework])
student_work = homework.student_works.where(user_id: User.current.id).first
if student_work
student_work.save
flash[:notice] = l(:notice_successful_create)
redirect_to student_work_index_url(:homework => params[:homework])
else
render_403
end
end
def user_new_homework

View File

@ -56,6 +56,10 @@ class HomeworkCommon < ActiveRecord::Base
Mailer.run.homework_added(self)
end
def is_program_homework?
self.homework_type == 2 && self.homework_detail_programing
end
delegate :language_name, :to => :homework_detail_programing
end

View File

@ -10,10 +10,32 @@ class StudentWork < ActiveRecord::Base
has_many :student_work_tests, order: 'id desc'
before_destroy :delete_praise
before_save :set_program_score, :set_src
acts_as_attachable
def delete_praise
PraiseTread.where("praise_tread_object_id = #{self.id} AND praise_tread_object_type = 'StudentWork'").destroy_all
end
def last_test
student_work_tests.order('id desc').first
end
private
def set_program_score
if homework_common.is_program_homework? #编程作业,学生提交作品后计算系统得分
#根据最后一次测试计算得分
unless last_test
self.system_score = 0
else
self.system_score = last_test.test_score
end
end
end
def set_src
self.description = last_test.src if last_test
end
end

View File

@ -34,10 +34,19 @@ class StudentWorkTest < ActiveRecord::Base
def test_score
if self.status.to_i == 0
format("%.1f",100.0 / self.student_work.homework_common.homework_tests.count)
else
100
elsif self.results.empty?
0
else
get_success_count * 100 / self.results.count
end
end
private
def get_success_count
self.results.inject(0) do |sum, result|
sum += (result["status"].to_i == 0 ? 1 : 0)
end || 0
end
end