diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index 2e7d95478..cbd8fb12d 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -85,10 +85,22 @@ class PollController < ApplicationController end def statistics_result + @poll = Poll.find(params[:id]) + poll_questions = @poll.poll_questions + @poll_questions = paginateHelper poll_questions, 5 respond_to do |format| format.html{render :layout => 'base_courses'} end end + + def get_poll_totalcount poll_question + @total_questions_count = poll_question.poll_votes.count + end + + def get_poll_everycount poll_answer + @every_answer_count = poll_answer.poll_votes.count + end + #添加题目 def create_poll_question @@ -182,20 +194,26 @@ class PollController < ApplicationController #单选题 pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id) if pv.nil? + #尚未答该题,添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] end + #修改该题对应答案 pv.poll_answer_id = params[:poll_answer_id] if pv.save + #保存成功返回成功信息及当前以答题百分比 @percent = get_percent(@poll,User.current) render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)} else + #返回失败信息 render :json => {:text => "failure"} end elsif pq.question_type == 2 + #多选题 pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id) if pv.nil? + #尚未答该题,添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] @@ -207,6 +225,7 @@ class PollController < ApplicationController render :json => {:text => "failure"} end else + #pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案 if pv.delete @percent = get_percent(@poll,User.current) render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)} @@ -215,12 +234,16 @@ class PollController < ApplicationController end end elsif pq.question_type == 3 || pq.question_type == 4 + #单行文本,多行文本题 pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id) if pv.nil? + #pv为空之前尚未答题,添加答案 if params[:vote_text].nil? || params[:vote_text].blank? + #用户提交空答案,视作不作答 @percent = get_percent(@poll,User.current) render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)} else + #添加答案 pv = PollVote.new pv.user_id = User.current.id pv.poll_question_id = params[:poll_question_id] @@ -233,7 +256,9 @@ class PollController < ApplicationController end end else + #pv不为空说明用户之前已作答 if params[:vote_text].nil? || params[:vote_text].blank? + #用户提交空答案,视为删除答案 if pv.delete @percent = get_percent(@poll,User.current) render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)} @@ -241,6 +266,7 @@ class PollController < ApplicationController render :json => {:text => "failure"} end else + #用户修改答案 pv.vote_text = params[:vote_text] if pv.save @percent = get_percent(@poll,User.current) @@ -339,7 +365,11 @@ class PollController < ApplicationController def get_percent poll,user complete_count = get_complete_question(poll,user).count - (complete_count.to_f / poll.poll_questions.count.to_f)*100 + if poll.poll_questions.count == 0 + return 0 + else + return (complete_count.to_f / poll.poll_questions.count.to_f)*100 + end end #PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个 diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 5d43a4e20..5ff9a3f17 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -116,8 +116,8 @@ class ProjectsController < ApplicationController joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").joins("LEFT JOIN #{ProjectScore.table_name} ON #{Project.table_name}.id = #{ProjectScore.table_name}.project_id"). where("#{Project.table_name}.project_type = ? ", Project::ProjectType_project) - @project_count = @projects_all.count - @project_pages = Paginator.new @project_count, per_page_option, params['page'] + @poll_questions_count = @projects_all.count + @poll_questions_pages = Paginator.new @project_count, per_page_option, params['page'] #gcm activity count diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb index 7816d8e37..1a8fabae3 100644 --- a/app/helpers/poll_helper.rb +++ b/app/helpers/poll_helper.rb @@ -27,7 +27,7 @@ module PollHelper false end end - + #获取文本题答案 def get_anwser_vote_text(question_id,user_id) pv = PollVote.find_by_poll_question_id_and_user_id(question_id,user_id) @@ -47,4 +47,20 @@ module PollHelper true end end + + #统计答题百分比 + def statistics_result_percentage(e, t) + return e*100/t + end + + def options_show p + case p + when 1 + "单选题" + when 2 + "多选题" + else + "问答题" + end + end end \ No newline at end of file diff --git a/app/views/poll/_choice_show.html.erb b/app/views/poll/_choice_show.html.erb new file mode 100644 index 000000000..16971d5ad --- /dev/null +++ b/app/views/poll/_choice_show.html.erb @@ -0,0 +1,28 @@ +
<%= l(:label_poll_options) %> | +<%= l(:label_poll_subtotal) %> | +<%= l(:label_poll_proportion) %> | +
<%= poll_answer.answer_text %> | +<%= poll_answer.poll_votes.count %> | +
+
+
+
+ <%= statistics_result_percentage(poll_answer.poll_votes.count, @poll.users.count) %>% |
+
<%= l(:label_poll_valid_commit) %> | +<%= poll_question.poll_votes.count %> | ++ |
选项 | -小计 | -比例 | -
第一选项 | -24 | - 75% |
-
第二选项 | -1 | -3.13% |
-
第三选项 | -1 | -3.13% |
-
本题有效填写人次 | -26 | -- |
选项 | -小计 | -比例 | -
第一选项 | -24 | - 75% |
-
第二选项 | -1 | -3.13% |
-
第三选项 | -1 | -3.13% |
-
本题有效填写人次 | -26 | -- |
选项 | -小计 | -比例 | -
第一选项 | -24 | - 75% |
-
第二选项 | -1 | -3.13% |
-
第三选项 | -1 | -3.13% |
-
本题有效填写人次 | -26 | -- |
选项 | -小计 | -比例 | -
第一选项 | -24 | - 75% |
-
第二选项 | -1 | -3.13% |
-
第三选项 | -1 | -3.13% |
-
本题有效填写人次 | -26 | -- |
选项 | -小计 | -比例 | -
第一选项 | -24 | - 75% |
-
第二选项 | -1 | -3.13% |
-
第三选项 | -1 | -3.13% |
-
本题有效填写人次 | -26 | -- |