添加作品列表导出excel功能

This commit is contained in:
z9hang 2014-12-30 14:29:30 +08:00
parent 4f5fe52485
commit 899ac6f654
14 changed files with 103 additions and 34 deletions

View File

@ -16,6 +16,8 @@ gem "coderay", "~> 1.0.6"
gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
gem "builder", "3.0.0"
gem 'acts-as-taggable-on', '2.4.1'
gem 'spreadsheet'
gem 'ruby-ole'
group :development do
gem 'better_errors', path: 'lib/better_errors'

View File

@ -192,6 +192,7 @@ GEM
rails (>= 3.2.0)
sass-rails
rmagick (2.13.2)
ruby-ole (1.2.11.7)
ruby-openid (2.1.8)
rubyzip (1.1.6)
sass (3.3.10)
@ -214,6 +215,8 @@ GEM
spork (0.9.2)
spork-testunit (0.0.8)
spork (>= 0.6.0)
spreadsheet (1.0.0)
ruby-ole (>= 1.0)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
@ -272,11 +275,13 @@ DEPENDENCIES
rails (= 3.2.13)
rich (= 1.4.6)
rmagick (>= 2.0.0)
ruby-ole
ruby-openid (~> 2.1.4)
sass-rails (~> 3.2.3)
seems_rateable!
selenium-webdriver (~> 2.42.0)
shoulda (~> 3.5.0)
spork-testunit (~> 0.0.8)
spreadsheet
therubyracer
uglifier (>= 1.0.3)

View File

@ -498,6 +498,7 @@ class BidsController < ApplicationController
(SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score
FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY s_score DESC,created_at ASC) AS table1
WHERE table1.t_score IS NULL")
@not_batch_homework = true
@cur_type = 1
else
all_homework_list = HomeworkAttach.find_by_sql("SELECT homework_attaches.*,

View File

@ -21,6 +21,7 @@ class HomeworkAttachController < ApplicationController
#获取未批作业列表
def get_not_batch_homework
@not_batch_homework = true
sort, direction = params[:sort] || "s_socre", params[:direction] || "desc"
get_not_batch_homework_list sort,direction, @bid.id
@cur_page = params[:page] || 1
@ -28,12 +29,17 @@ class HomeworkAttachController < ApplicationController
@direction = direction == 'asc'? 'desc' : 'asc'
respond_to do |format|
format.js
format.xls {
send_data(homework_to_xls(@all_homework_list), :type => "text/excel;charset=utf-8; header=present",
:filename => "not_rated_homework_#{Time.now.strftime("%Y%m%d")}.xls")
}
end
end
#获取已评作业列表
def get_batch_homeworks
sort, direction = params[:sort] || "s_socre", params[:direction] || "desc"
@is_batch_homeworks = true
if sort == 't_socre'
order_by = "t_score #{direction}"
elsif sort == 's_socre'
@ -53,33 +59,18 @@ class HomeworkAttachController < ApplicationController
@direction = direction == 'asc'? 'desc' : 'asc'
respond_to do |format|
format.js
format.csv {
send_data(homework_to_csv(all_homework_list), :type => 'text/csv; header=present', :filename => 'issues.csv')
format.xls {
send_data(homework_to_xls(all_homework_list), :type => "text/excel;charset=utf-8; header=present",
:filename => "been_rated_homework_#{Time.now.strftime("%Y%m%d")}.xls")
}
end
end
def homework_to_csv items
encoding = l(:general_csv_encoding)
columns = ["student_id","user_name","login","student_num","mail","work_name","teacher_score","ni_score","commit_time"]
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
# csv header fields
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c, encoding) }
# csv lines
items.each do |homework|
csv << [homework.user.id,Redmine::CodesetUtil.from_utf8(homework.user.lastname.to_s + homework.user.firstname.to_s, encoding),Redmine::CodesetUtil.from_utf8(homework.user.login, encoding),
Redmine::CodesetUtil.from_utf8(homework.user.user_extensions.student_id, encoding),Redmine::CodesetUtil.from_utf8(homework.user.mail, encoding),Redmine::CodesetUtil.from_utf8(homework.name, encoding),
Redmine::CodesetUtil.from_utf8((homework.t_score.nil? || (homework.t_score && homework.t_score.to_i == 0)) ? l(:label_without_score) : format("%.2f",homework.t_score), encoding),
Redmine::CodesetUtil.from_utf8( homework.s_score.nil? ? l(:label_without_score) : format("%.2f",homework.s_score), encoding),Redmine::CodesetUtil.from_utf8(format_time(homework.created_at), encoding)]
end
end
export
end
#获取所有作业列表
def get_homeworks
@is_all_homeworks = true
sort, direction = params[:sort] || "s_socre", params[:direction] || "desc"
if sort == 't_socre'
order_by = "t_score #{direction}"
@ -99,6 +90,10 @@ class HomeworkAttachController < ApplicationController
@direction = direction == 'asc'? 'desc' : 'asc'
respond_to do |format|
format.js
format.xls {
send_data(homework_to_xls(all_homework_list), :type => "text/excel;charset=utf-8; header=present",
:filename => "all_homework_#{Time.now.strftime("%Y%m%d")}.xls")
}
end
end
@ -582,18 +577,65 @@ class HomeworkAttachController < ApplicationController
elsif sort == 'time'
order_by = "created_at #{direction}"
end
all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT * FROM (SELECT homework_attaches.*,
@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 is_teacher_score = 1) AS t_score,
(SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score
FROM homework_attaches WHERE bid_id = #{bid_id}
ORDER BY #{order_by}) AS table1
WHERE table1.t_score IS NULL")
@homework_list = paginateHelper all_homework_list,10
@homework_list = paginateHelper @all_homework_list,10
end
#获取指定作业的所有成员
def users_for_homework homework
homework.nil? ? [] : (homework.users + [homework.user])
end
def homework_to_csv items
encoding = l(:general_csv_encoding)
columns = ["student_id","user_name","login","student_num","mail","work_name","teacher_score","ni_score","commit_time"]
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
# csv header fields
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c, encoding) }
# csv lines
items.each do |homework|
csv << [homework.user.id,Redmine::CodesetUtil.from_utf8(homework.user.lastname.to_s + homework.user.firstname.to_s, encoding),Redmine::CodesetUtil.from_utf8(homework.user.login, encoding),
Redmine::CodesetUtil.from_utf8(homework.user.user_extensions.student_id, encoding),Redmine::CodesetUtil.from_utf8(homework.user.mail, encoding),Redmine::CodesetUtil.from_utf8(homework.name, encoding),
Redmine::CodesetUtil.from_utf8((homework.t_score.nil? || (homework.t_score && homework.t_score.to_i == 0)) ? l(:label_without_score) : format("%.2f",homework.t_score), encoding),
Redmine::CodesetUtil.from_utf8( homework.s_score.nil? ? l(:label_without_score) : format("%.2f",homework.s_score), encoding),Redmine::CodesetUtil.from_utf8(format_time(homework.created_at), encoding)]
end
end
export
end
def homework_to_xls items
xls_report = StringIO.new
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => "homework"
blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
sheet1.row(0).default_format = blue
sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_homework_name),
l(:excel_t_score),l(:excel_n_score),l(:excel_commit_time)])
count_row = 1
items.each do |homework|
sheet1[count_row,0]=homework.user.id
sheet1[count_row,1] = homework.user.lastname.to_s + homework.user.firstname.to_s
sheet1[count_row,2] = homework.user.login
sheet1[count_row,3] = homework.user.user_extensions.student_id
sheet1[count_row,4] = homework.user.mail
sheet1[count_row,5] = homework.name
sheet1[count_row,6] =(homework.t_score.nil? || (homework.t_score && homework.t_score.to_i == 0)) ? l(:label_without_score) : format("%.2f",homework.t_score)
sheet1[count_row,7] = homework.s_score.nil? ? l(:label_without_score) : format("%.2f",homework.s_score)
sheet1[count_row,8] = format_time(homework.created_at)
count_row += 1
end
book.write xls_report
xls_report.string
end
end

View File

@ -59,7 +59,7 @@
:locals => {:homeworks => @homework_list,
:homework_count => @obj_count,
:remote => false,
:is_student_batch_homework => @is_student_batch_homework}%>
:not_batch_homework => @not_batch_homework,:is_student_batch_homework => @is_student_batch_homework}%>
</div>
</div>
</div>

View File

@ -1,7 +1,9 @@
<% is_teacher = is_course_teacher(User.current,@bid.courses.first) %>
<% is_my_homework ||= false %>
<% is_student_batch_homework ||= false %>
<% is_batch_homeworks ||= false %>
<% not_batch_homework ||= false %>
<% is_all_homeworks ||= false %>
<% unless is_my_homework || is_student_batch_homework %>
<div class="code_list">
<span>
@ -9,6 +11,9 @@
(<font color="#CC0000">
<%= homework_count%>
</font>)
<%= link_to l(:label_export_excel), get_batch_homeworks_homework_attach_index_path(:bid_id => @bid.id,:format => 'xls'),:class=>'xls' if is_batch_homeworks%>
<%= link_to l(:label_export_excel), get_not_batch_homework_homework_attach_index_path(:bid_id => @bid.id,:format => 'xls'),:class=>'xls' if not_batch_homework%>
<%= link_to l(:label_export_excel), get_homeworks_homework_attach_index_path(:bid_id => @bid.id,:format => 'xls'),:class=>'xls' if is_all_homeworks%>
</span>
<span class="fr">
按&nbsp;
@ -63,10 +68,4 @@
<span style="color: red;">
<%= anonymous_comment_notice(@bid, @bid.courses.first) %>
</span>
<% other_formats_links do |f| %>
<%= f.link_to 'CSV', :url => params %>
<% end %>
<!--<div>
导出 <a>csv</a>
</div> -->
<% end %>

View File

@ -1,4 +1,4 @@
$('#tbc_01').html('<%= escape_javascript(render(:partial => 'homeworks_list',
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true} )) %>');
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true,:is_batch_homeworks => @is_batch_homeworks} )) %>');
for(var i=1;i<=4;i++){$("#tb_"+i).removeClass().addClass("normaltab");}
$("#tb_2").removeClass().addClass("hovertab");

View File

@ -1,5 +1,5 @@
$('#tbc_01').html('<%= escape_javascript(render(:partial => 'homeworks_list',
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true} )) %>');
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true,:is_all_homeworks => @is_all_homeworks} )) %>');
for(var i=1;i<=4;i++){$("#tb_"+i).removeClass().addClass("normaltab");}
$("#tb_3").removeClass().addClass("hovertab");

View File

@ -1,4 +1,4 @@
$('#tbc_01').html('<%= escape_javascript(render(:partial => 'homeworks_list',
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true} )) %>');
:locals => {:homeworks => @homework_list, :homework_count => @obj_count, :bid => @bid, :remote => true,:not_batch_homework => @not_batch_homework} )) %>');
for(var i=1;i<=4;i++){$("#tb_"+i).removeClass().addClass("normaltab");}
$("#tb_1").removeClass().addClass("hovertab");

View File

@ -1,4 +1,5 @@
# Add new mime types for use in respond_to blocks:
Mime::SET << Mime::CSV unless Mime::SET.include?(Mime::CSV)
Mime::Type.register "text/excel", :xls

View File

@ -2090,6 +2090,16 @@ zh:
label_contest_delete: 删除竞赛
label_noawards_current: 暂未评奖
excel_user_id: 学生id
excel_user_name: 用户名
excel_nickname: 昵称
excel_student_id: 学号
excel_mail: 电子邮箱
excel_homework_name: 作品名
excel_t_score: 教师评分
excel_n_score: 匿名评分
excel_commit_time: 提交时间
label_export_excel: 导出Excel
label_softapplication: 应用软件
label_attending_contest: 参加竞赛

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -1431,3 +1431,11 @@ ul.contest-notification-list li span{
.softapplications-div table td{
word-break: break-all;
}
a.xls {
background: url('../images/icon_excel.gif') no-repeat scroll 1px 50% transparent;
padding: 2px 0px 3px 16px;
font-family:微软雅黑 !important;
font-size: 12px !important;
color: #136b3b !important;
}

View File

@ -6,6 +6,7 @@ ul,li{ list-style-type:none}
.cl{ clear:both; overflow:hidden; }
#resource a{ text-decoration:none; text-align:center; }
a:hover{ text-decoration:underline;}
/**** 常用***/
.f_l{ float:left;}
.f_r{ float:right;}