@ -412,6 +412,18 @@ class EcCoursesController < ApplicationController
end
end
# 导出课程目标
def export_ec_course_student_scores
targets = @ec_course . ec_course_targets . includes ( :ec_graduation_subitems )
filename = " #{ @year . year } 届 #{ @ec_course . name } _课程目标成绩_ #{ Time . now . strftime ( '%Y%m%d%H%M%S' ) } .xls "
respond_to do | format |
format . xls {
send_data ( export_target_scope_students ( targets ) , :type = > " text/excel;charset=utf-8; header=present " ,
:filename = > filename_for_content_disposition ( filename ) )
}
end
end
def destroy
@ec_course . destroy
@ec_courses = @year . ec_courses
@ -584,6 +596,130 @@ class EcCoursesController < ApplicationController
xls_report . string
end
def export_target_scope_students targets
xls_report = StringIO . new
book = Spreadsheet :: Workbook . new
targets . each_with_index do | t , index |
sheet1 = book . create_worksheet :name = > " 课程目标 #{ index + 1 } "
avg_score = t . ec_student_score_targets . average ( :score ) . to_f . round ( 2 )
course_evaluation_ids = t . ec_course_achievement_methods . pluck ( :ec_course_evaluation_id )
create_target_students_sheet sheet1 , t . ec_student_score_targets . includes ( :ec_year_student ) , avg_score , course_evaluation_ids
end
target_sheet = book . create_worksheet :name = > " 课程目标达成度 "
create_course_target_sheet target_sheet , targets
total_sheet = book . create_worksheet :name = > " 课程总体目标达成 "
create_total_target_students_sheet total_sheet
book . write xls_report
xls_report . string
end
def create_target_students_sheet sheet1 , student_score_targets , avg_score , course_evaluation_ids
font_size = Spreadsheet :: Format . new :size = > 14 , :weight = > :bold , :vertical_align = > :middle , :horizontal_align = > :center
center_style = Spreadsheet :: Format . new :vertical_align = > :middle , :horizontal_align = > :center
green_bg = Spreadsheet :: Format . new :top = > :thin , :bottom = > :thin , :left = > :thin , :right = > :thin , :vertical_align = > :middle , :horizontal_align = > :center ,
:pattern = > 1 , :pattern_fg_color = > :xls_color_34 , :size = > 11
titles = EcCourseEvaluation . where ( id : course_evaluation_ids ) . pluck ( :name )
sheet1 . row ( 0 ) . concat ( [ " 学号 " , " 姓名 " ] + titles + [ " 达成情况 " ] )
sheet1 . column ( 0 ) . width = 25
sheet1 . column ( 1 ) . width = 25
course_evaluation_ids . each_with_index do | ce , index |
sheet1 . column ( 2 + index ) . width = 15
end
sheet1 . column ( 2 + course_evaluation_ids . size ) . width = 15
sheet1 . row ( 0 ) . default_format = font_size
count_row = 1
student_score_targets . each do | sc_target |
sheet1 [ count_row , 0 ] = " #{ sc_target . ec_year_student & . student_id } "
sheet1 [ count_row , 1 ] = " #{ sc_target . ec_year_student & . name } "
source_score = sc_target . ec_year_student . ec_student_achievements . where ( ec_course_evaluation_id : course_evaluation_ids ) . pluck ( :score )
add_cell = 1
course_evaluation_ids . each_with_index do | ce , index |
sheet1 [ count_row , 1 + add_cell ] = " #{ source_score [ index ] } "
add_cell += 1
end
sheet1 [ count_row , 1 + add_cell ] = " #{ sc_target . score . to_f . round ( 2 ) } "
sheet1 . row ( count_row ) . default_format = center_style
if sc_target . score > = avg_score
sheet1 . row ( count_row ) . set_format ( 0 , green_bg )
sheet1 . row ( count_row ) . set_format ( 1 , green_bg )
end
count_row += 1
end
end
def create_course_target_sheet sheet1 , targets
font_size = Spreadsheet :: Format . new :size = > 14 , :weight = > :bold , :vertical_align = > :middle , :horizontal_align = > :center
center_style = Spreadsheet :: Format . new :vertical_align = > :middle , :horizontal_align = > :center
left_style = Spreadsheet :: Format . new :vertical_align = > :middle , :horizontal_align = > :left
titles = EcCourseEvaluation . where ( ec_course_id : targets . first . ec_course_id ) . pluck ( :name )
sheet1 . row ( 0 ) . concat ( [ " 序号 " , " 课程目标 " ] + titles + [ " 达成情况 " ] )
sheet1 . row ( 0 ) . default_format = font_size
count_row = 1
targets . each_with_index do | t , index |
total_score = [ ]
sheet1 [ count_row , 0 ] = " #{ index + 1 } "
sheet1 [ count_row , 1 ] = " #{ t . content } "
sheet1 . row ( count_row ) . set_format ( 1 , left_style )
@ec_course . ec_course_evaluations & . each_with_index do | evaluation , idx |
if evaluation . ec_course_achievement_methods . where ( ec_course_target_id : t . id ) . present?
avg_score = EcStudentAchievement . where ( ec_course_evaluation_id : evaluation . id ) . average ( :score ) . to_f . round ( 2 )
total_score . push ( avg_score )
sheet1 [ count_row , 2 + idx ] = " #{ avg_score } "
else
sheet1 [ count_row , 2 + idx ] = " "
end
end
sheet1 [ count_row , 2 + @ec_course . ec_course_evaluations . size ] = " #{ ( total_score . sum . to_f / t . ec_course_achievement_methods . size ) . to_f . round ( 2 ) } "
sheet1 . row ( count_row ) . default_format = center_style
count_row += 1
end
sheet1 . column ( 0 ) . width = 5
sheet1 . column ( 1 ) . width = 80
@ec_course . ec_course_evaluations . each_with_index do | ce , index |
sheet1 . column ( 2 + index ) . width = 15
end
sheet1 . column ( 2 + @ec_course . ec_course_evaluations . size ) . width = 15
end
def create_total_target_students_sheet sheet1
font_size = Spreadsheet :: Format . new :size = > 14 , :weight = > :bold , :vertical_align = > :middle , :horizontal_align = > :center
center_style = Spreadsheet :: Format . new :vertical_align = > :middle , :horizontal_align = > :center
green_bg = Spreadsheet :: Format . new :top = > :thin , :bottom = > :thin , :left = > :thin , :right = > :thin , :vertical_align = > :middle , :horizontal_align = > :center ,
:pattern = > 1 , :pattern_fg_color = > :xls_color_34 , :size = > 11
sheet1 . row ( 0 ) . concat ( [ " 学号 " , " 姓名 " , " 达成情况 " ] )
sheet1 . row ( 0 ) . default_format = font_size
count_row = 1
student_score_targets = @ec_course . ec_student_score_targets . select ( " ec_student_score_targets.*, AVG(ec_student_score_targets.score) as score " ) . group ( :ec_year_student_id )
avg_score = ( student_score_targets . map ( & :score ) . sum . to_f / student_score_targets . size ) . to_f . round ( 2 )
student_score_targets . each_with_index do | sc_target , index |
sheet1 [ count_row , 0 ] = " #{ sc_target . ec_year_student & . student_id } "
sheet1 [ count_row , 1 ] = " #{ sc_target . ec_year_student & . name } "
sheet1 [ count_row , 2 ] = " #{ sc_target . score . to_f . round ( 2 ) } "
sheet1 . row ( count_row ) . default_format = center_style
if sc_target . score > = avg_score
sheet1 . row ( count_row ) . set_format ( 0 , green_bg )
sheet1 . row ( count_row ) . set_format ( 1 , green_bg )
end
count_row += 1
end
sheet1 . column ( 0 ) . width = 25
sheet1 . column ( 1 ) . width = 25
@ec_course . ec_course_evaluations . each_with_index do | ce , index |
sheet1 . column ( 2 + index ) . width = 15
end
sheet1 . column ( 2 + @ec_course . ec_course_evaluations . size ) . width = 15
end
def find_ec_course
@ec_course = EcCourse . find ( params [ :id ] )
@year = @ec_course . ec_year