From b5ab3785d93d368e90941ac717d994e0a6aa34f8 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Thu, 4 Jul 2019 14:34:33 +0800 Subject: [PATCH] poll export youhua --- app/controllers/polls_controller.rb | 33 +++---- app/views/polls/commit_result.xlsx.axlsx | 117 ++--------------------- 2 files changed, 24 insertions(+), 126 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index e6ada3098..ee2b061ba 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -146,7 +146,7 @@ class PollsController < ApplicationController def edit ActiveRecord::Base.transaction do begin - @poll_questions = @poll.poll_questions.order("question_number ASC") + rescue Exception => e uid_logger_error(e.message) tip_exception("页面请求失败!") @@ -183,7 +183,7 @@ class PollsController < ApplicationController else @is_teacher_or = 0 end - @poll_questions = @poll.poll_questions&.includes(:poll_answers).order("question_number ASC") + rescue Exception => e uid_logger_error(e.message) tip_exception("没有权限") @@ -868,7 +868,6 @@ class PollsController < ApplicationController @user_poll_status = 0 #可编辑 end - # @answer_user = User.find_by(id:@poll_current_user_id) @answer_status = [] question_answered = 0 @@ -925,7 +924,6 @@ class PollsController < ApplicationController @page = params[:page] || 1 @limit = params[:limit] || 10 @poll_export_questions = @poll_questions - @poll_questions = @poll_questions.page(@page).per(@limit) if params[:format] == "xlsx" if @user_course_identity > Course::ASSISTANT_PROFESSOR @@ -1151,7 +1149,7 @@ class PollsController < ApplicationController def get_questions_count @poll_questions = @poll.poll_questions.order("question_number ASC") - @poll_questions_count = @poll_questions.size # 全部的题目数 + @poll_questions_count = @poll_questions&.size # 全部的题目数 @poll_question_singles = @poll_questions.ques_count(1).size # 单选题 @poll_question_doubles = @poll_questions.ques_count(2).size # 多选题 @poll_question_mains = @poll_questions.ques_count(3).size #主观题 @@ -1301,7 +1299,6 @@ class PollsController < ApplicationController #问卷的统计结果的导出 def poll_commit_result(poll,poll_questions,poll_users,poll_commit_ids) - obj_commit = [] sub_commit = [] user_commit = [] poll_users_info = %w(序号) @@ -1315,30 +1312,28 @@ class PollsController < ApplicationController poll_users_info = poll_users_info + user_info + poll_ques_titles poll_questions.each do |q| if q.question_type != 3 #问题不为主观题 - question_vote_user = q.poll_votes.find_current_vote("user_id",poll_commit_ids).size #该问题的有效填写量 + question_vote_user = q.poll_votes.find_current_vote("user_id",poll_commit_ids)&.size #该问题的有效填写量 sheet_row = ["第#{q.question_number}题"] #选择题答案选项的数组 sheet_answer_row = ["小计"] #选择题回答的答案人数,数组 sheet_answer_percent = ["比例"] - sheet_answer_useful = ["有效填写人次",question_vote_user] + sheet_answer_useful = ["本题有效填写人次",question_vote_user] q.poll_answers.each do |a| #问卷的答案选项 - answer_users_count = a.poll_votes.find_current_vote("user_id",poll_commit_ids).size + answer_users_count = a.poll_votes.find_current_vote("user_id",poll_commit_ids)&.size answer_percent = (question_vote_user == 0) ? "0.0%" : "#{((answer_users_count / question_vote_user.to_f)*100).round(1).to_s}%" sheet_row.push(a.answer_text) sheet_answer_row.push(answer_users_count) sheet_answer_percent.push(answer_percent) end - sheet_obj_commit = { - sheet_row:sheet_row, - sheet_answer_row:sheet_answer_row, - sheet_answer_percent:sheet_answer_percent, - sheet_answer_useful:sheet_answer_useful + sheet_sub_commit = { + sub_title: sheet_row, + sub_user_votes:[sheet_answer_row,sheet_answer_percent,sheet_answer_useful] } - obj_commit.push(sheet_obj_commit) + sub_commit.push(sheet_sub_commit) else #主观题答案 user_votes= [] - main_show_row = ["第#{q.question_number}题", q.question_title] + main_show_row = ["第#{q.question_number}题", q.question_title ] q.poll_votes.each_with_index do |v,index| #主观题的答案 - q_poll_vote = [(index+1),v.vote_text.present? ? v.vote_text : "--"] + q_poll_vote = [(index+1), v.vote_text.present? ? v.vote_text : "--"] user_votes.push(q_poll_vote) end sheet_sub_commit = { @@ -1347,9 +1342,10 @@ class PollsController < ApplicationController } sub_commit.push(sheet_sub_commit) end + end #each_with_index - poll_users.includes(user: :user_extension).each_with_index do |u,index| + poll_users.includes(user: [:user_extension,:poll_votes]).each_with_index do |u,index| u_user = u.user user_answer_array = [] poll_questions.each do |q| @@ -1390,7 +1386,6 @@ class PollsController < ApplicationController end { - obj_commit:obj_commit, poll_users_info:poll_users_info, sub_commit:sub_commit, user_commit:user_commit diff --git a/app/views/polls/commit_result.xlsx.axlsx b/app/views/polls/commit_result.xlsx.axlsx index 016c6e34c..4391e0e8f 100644 --- a/app/views/polls/commit_result.xlsx.axlsx +++ b/app/views/polls/commit_result.xlsx.axlsx @@ -7,30 +7,19 @@ wb.styles do |s| wb.add_worksheet(:name => "统计结果") do |sheet| sheet.sheet_view.show_grid_lines = false - obj_commit = polls_user_commit[:obj_commit] || [] - sub_commit = polls_user_commit[:sub_commit] || [] - poll_user_info = polls_user_commit[:poll_users_info] || [] - user_commit = polls_user_commit[:user_commit] || [] + sub_commit = polls_user_commit[:sub_commit] + poll_user_info = polls_user_commit[:poll_users_info] + user_commit = polls_user_commit[:user_commit] - #客观题的导出 - if obj_commit&.size > 0 - obj_commit.each do |obj| - sheet.add_row obj[:sheet_row], :height =>15,:style => blue_cell - sheet.add_row obj[:sheet_answer_row], :height =>15, :style => sz_all - sheet.add_row obj[:sheet_answer_percent], :height =>15, :style => sz_all - sheet.add_row obj[:sheet_answer_useful], :height =>15, :style => sz_all - sheet.add_row [] - end - end - - #主观题的导出 if sub_commit&.size > 0 sub_commit.each do |sub| - main_sub_title = sub[:sub_tile] + main_sub_title = sub[:sub_title] main_sub_content = sub[:sub_user_votes] sheet.add_row main_sub_title,:height =>15, :style => blue_cell - main_sub_content.each do |con| - sheet.add_row con,:height =>15, :style => sz_all + if main_sub_content.present? + main_sub_content.each do |con| + sheet.add_row con,:height =>15, :style => sz_all + end end sheet.add_row [] end @@ -44,93 +33,7 @@ wb.styles do |s| end end sheet.column_widths *([25]*sheet.column_info.count) - sheet.column_info.first.width = 10 - - #答题用户的导出 + sheet.column_info.first.width = 15 - # sheet.sheet_view.show_grid_lines = false - # poll_users_info = %w(序号) - # poll_ques_titles = poll_questions.pluck(:question_title).map {|k| strip_export_title(k) if k.present?} - # poll_un_anony = poll.un_anonymous - # if poll_un_anony #是否匿名,默认为false - # user_info = %w(登陆名 真实姓名 邮箱 学号) - # else - # user_info = [] - # end - # poll_users_info = poll_users_info + user_info + poll_ques_titles - # poll_questions.each do |q| - # if q.question_type != 3 #问题不为主观题 - # question_vote_user = q.poll_votes.find_current_vote("user_id",poll_commit_ids).size #该问题的有效填写量 - # sheet_row = ["第#{q.question_number}题"] #选择题答案选项的数组 - # sheet_answer_row = ["小计"] #选择题回答的答案人数,数组 - # sheet_answer_percent = ["比例"] - # sheet_answer_useful = ["有效填写人次",question_vote_user] - # q.poll_answers.each do |a| #问卷的答案选项 - # answer_users_count = a.poll_votes.find_current_vote("user_id",poll_commit_ids).size - # answer_percent = number_to_percentage((answer_users_count.to_f / question_vote_user.to_f)*100,precision:1) - # sheet_row.push(a.answer_text) - # sheet_answer_row.push(answer_users_count) - # sheet_answer_percent.push(answer_percent.to_s) - # end - # sheet.add_row sheet_row, :height =>15,:style => blue_cell - # sheet.add_row sheet_answer_row, :height =>15, :style => sz_all - # sheet.add_row sheet_answer_percent, :height =>15, :style => sz_all - # sheet.add_row sheet_answer_useful, :height =>15, :style => sz_all - # #合并单元格,但无法填充style - # # sheet.merge_cells (Axlsx::cell_r(1,sheet.rows.last.row_index) + ':' + Axlsx::cell_r(sheet_row.count-1,sheet.rows.last.row_index)) - # # sheet.rows[sheet.rows.last.row_index].style = sz_all - # sheet.add_row [] - # else #主观题答案 - # main_show_row = ["第#{q.question_number}题",q.question_title] - # sheet.add_row main_show_row,:height =>15, :style => blue_cell - # q.poll_votes.each_with_index do |v,index| #主观题的答案 - # sheet.add_row [(index+1),v.vote_text.present? ? v.vote_text : "--"],:height =>15, :style => sz_all - # end - # sheet.add_row [], :style => sz_all - # end - # end #each_with_index - # - # sheet.add_row poll_users_info, :height =>15, :style => blue_cell - # poll_users.includes(user: :user_extension).each_with_index do |u,index| - # u_user = u.user - # user_answer_array = [] - # poll_questions.each do |q| - # user_poll_votes = u_user.poll_votes.find_current_vote("poll_question_id",q.id) - # if user_poll_votes.present? - # user_poll_answer_ids = user_poll_votes.pluck(:poll_answer_id).reject(&:blank?) - # user_poll_vote_texts = user_poll_votes.pluck(:vote_text).reject(&:blank?) - # if user_poll_answer_ids.count > 0 - # answer_content = q.poll_answers.find_answer_by_custom("id",user_poll_answer_ids) - # if user_poll_answer_ids.count >1 - # u_answer = answer_content.pluck(:answer_text).join(";") - # else - # u_answer = answer_content.first.answer_text - # end - # elsif user_poll_vote_texts.count > 0 - # if user_poll_vote_texts.count > 1 - # u_answer = user_poll_vote_texts.join(";") - # else - # u_answer = user_poll_vote_texts.first - # end - # else - # u_answer = "--" - # end - # else - # u_answer = "--" - # end - # user_answer_array.push(u_answer) - # end - # user_cell = [index+1] - # if poll_un_anony - # user_login = u_user.login - # user_name = u_user.real_name.present? ? u_user.real_name : "--" - # user_student_id = u_user.student_id.present? ? u_user.student_id : "--" - # user_cell += [user_login,user_name, u_user.mail, user_student_id] - # end - # all_user_cell = user_cell + user_answer_array - # sheet.add_row all_user_cell, :height =>15,:style => sz_all - # end - # sheet.column_widths *([25]*sheet.column_info.count) - # sheet.column_info.first.width = 10 end #add_worksheet -end \ No newline at end of file +end