diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 0dfa4b1b8..1a6981d9f 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -924,7 +924,8 @@ class PollsController < ApplicationController @poll_commit_ids = @poll_users.commit_by_status(1).pluck(:user_id) #问卷提交用户的id @page = params[:page] || 1 @limit = params[:limit] || 10 - @poll_export_questions = @poll_questions.order("question_number ASC") + @poll_export_questions = @poll_questions + logger.info("################____________________#{@poll_questions.pluck(:id)}") @poll_questions = @poll_questions.page(@page).per(@limit) if params[:format] == "xlsx" @@ -936,11 +937,14 @@ class PollsController < ApplicationController respond_to do |format| format.xlsx{ polls_export_name = "#{current_user.real_name}_#{@course.name}_#{@poll.polls_name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}" - render xlsx: "#{polls_export_name.strip.first(30)}",template: "polls/commit_result.xlsx.axlsx",locals: { - poll_questions:@poll_export_questions, - poll:@poll, - poll_users: @poll_users, - poll_commit_ids:@poll_commit_ids} + polls_user_commit = poll_commit_result(@poll,@poll_export_questions,@poll_users,@poll_commit_ids) + render xlsx: "#{polls_export_name.strip.first(30)}",template: "polls/commit_result.xlsx.axlsx",locals: {polls_user_commit:polls_user_commit} + + # render xlsx: "#{polls_export_name.strip.first(30)}",template: "polls/commit_result.xlsx.axlsx",locals: { + # poll_questions:@poll_export_questions, + # poll:@poll, + # poll_users: @poll_users, + # poll_commit_ids:@poll_commit_ids} } end end @@ -1153,11 +1157,12 @@ class PollsController < ApplicationController end def get_questions_count - @poll_questions = @poll.poll_questions&.includes(:poll_answers,:poll_votes).order("question_number ASC") + @poll_questions = @poll.poll_questions.order("question_number ASC") @poll_questions_count = @poll_questions.size # 全部的题目数 - @poll_question_singles = @poll_questions.ques_count(1).all.size # 单选题 - @poll_question_doubles = @poll_questions.ques_count(2).all.size # 多选题 - @poll_question_mains = @poll_questions.ques_count(3).all.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 #主观题 + @poll_questions = @poll_questions&.includes(:poll_answers,:poll_votes).distinct end def check_poll_question_complete #commit_poll 的权限 @@ -1301,4 +1306,102 @@ class PollsController < ApplicationController end end + #问卷的统计结果的导出 + def poll_commit_result(poll,poll_questions,poll_users,poll_commit_ids) + obj_commit = [] + sub_commit = [] + user_commit = [] + poll_users_info = %w(序号) + poll_ques_titles = poll_questions.pluck(:question_title).map {|k| k.strip 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 = (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 + } + obj_commit.push(sheet_obj_commit) + else #主观题答案 + user_votes= [] + 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 : "--"] + user_votes.push(q_poll_vote) + end + sheet_sub_commit = { + sub_title: main_show_row, + sub_user_votes:user_votes + } + sub_commit.push(sheet_sub_commit) + end + end #each_with_index + + 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 + user_commit.push(all_user_cell) + end + + { + obj_commit:obj_commit, + poll_users_info:poll_users_info, + sub_commit:sub_commit, + user_commit:user_commit + } + end + end diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index 1892f3fa2..693149e8f 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -128,16 +128,4 @@ module PollsHelper } end - def poll_commit_result(poll,poll_questions,poll_users,poll_commit_ids) - 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 - - end end diff --git a/app/views/polls/commit_result.xlsx.axlsx b/app/views/polls/commit_result.xlsx.axlsx index 71585dae9..975668bfe 100644 --- a/app/views/polls/commit_result.xlsx.axlsx +++ b/app/views/polls/commit_result.xlsx.axlsx @@ -7,91 +7,130 @@ wb.styles do |s| wb.add_worksheet(:name => "统计结果") do |sheet| sheet.sheet_view.show_grid_lines = false - poll_users_info = %w(序号) - Rails.logger.info("#############_______________poll_questions.pluck(:question_title)_______####################{poll_questions.pluck(:question_title)}") + 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] - poll_ques_titles = poll_questions.pluck(:question_title).map {|k| strip_export_title(k) if k.present?} - Rails.logger.info("#############_______________poll_ques_titles_______####################{poll_ques_titles}") - 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 + #客观题的导出 + 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 [] - 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 + end - 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 = "--" + #主观题的导出 + if sub_commit.size > 0 + sub_commit.each do |sub| + main_sub_title = sub[:sub_tile] + 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 end - user_answer_array.push(u_answer) + sheet.add_row [] 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 + + sheet.add_row poll_user_info, :height =>15, :style => blue_cell + + if user_commit.size > 0 + user_commit.each do |com| + sheet.add_row com, :height =>15,:style => sz_all 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 + + #答题用户的导出 + + # 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