From 6d82e79e7f231dcd7635991c0265d94a6f95d881 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 09:41:19 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=AD=94=E9=A2=98=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 22 +++++++++++++++++- app/helpers/poll_helper.rb | 35 +++++++++++++++++++++++++++++ app/views/poll/commit_answer.js.erb | 3 +++ app/views/poll/show.html.erb | 22 ++++++++++++++++-- config/routes.rb | 1 + 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 app/helpers/poll_helper.rb create mode 100644 app/views/poll/commit_answer.js.erb diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index 88bbfff46..3ea1934f5 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -3,7 +3,7 @@ class PollController < ApplicationController before_filter :find_container, :only => [:new,:create, :index] before_filter :is_member_of_course, :only => [:index,:show] before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy] - + include PollHelper def index if @course @is_teacher = User.current.allowed_to?(:as_teacher,@course) @@ -86,6 +86,26 @@ class PollController < ApplicationController end + #提交答案 + def commit_answer + @pv = get_poll_vote(User.current.id,params[:poll_question_id]) + if params[:poll_vote][:poll_answer_id] + @pv.poll_answer_id = params[:poll_vote][:poll_answer_id] + end + if params[:poll_vote][:vote_text] + @pv.vote_text = params[:poll_vote][:vote_text] + end + @pv_saved = false + if @pv.save + @pv_saved = true + end + #respond_to do |format| + # format.js + # format.json + #end + render :text => "ok" + end + private def find_poll_and_course @poll = Poll.find params[:id] diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb new file mode 100644 index 000000000..202f33eaa --- /dev/null +++ b/app/helpers/poll_helper.rb @@ -0,0 +1,35 @@ +# encoding: utf-8 +# +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module PollHelper + def get_poll_vote(user_id,poll_question_id) + pq = PollQuestion.find(poll_question_id) + if pq.question_type == 1 + pv = PollVote.find_by_poll_question_id_and_user_id(poll_question_id,user_id) + if pv.nil? + pv = PollVote.new + pv.user_id = user_id + pv.poll_question_id = poll_question_id + pv + else + pv + end + end + end +end \ No newline at end of file diff --git a/app/views/poll/commit_answer.js.erb b/app/views/poll/commit_answer.js.erb new file mode 100644 index 000000000..b698d8658 --- /dev/null +++ b/app/views/poll/commit_answer.js.erb @@ -0,0 +1,3 @@ +<% if @pv_saved %> +$('#poll_vote_poll_answer_id_<%= @pv.poll_answer_id %>').checked = true; +<% end %> \ No newline at end of file diff --git a/app/views/poll/show.html.erb b/app/views/poll/show.html.erb index 26daa8fc5..08dae18b2 100644 --- a/app/views/poll/show.html.erb +++ b/app/views/poll/show.html.erb @@ -4,6 +4,7 @@ 问卷调查_问卷页面 <%= stylesheet_link_tag 'polls', :media => 'all' %> + <%= javascript_heads %> @@ -33,20 +34,37 @@
+ <%= form_tag(commit_answer_poll_path(@poll),:remote => true,:id => 'form_'+ pq.id.to_s) do %> <% pq.poll_answers.each do |pa| %> <% end %>
+ <% end %>
<% elsif pq.question_type == 2 %> diff --git a/config/routes.rb b/config/routes.rb index af0b75ae7..931b9f919 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,7 @@ RedmineApp::Application.routes.draw do member do get 'statistics_result' get 'add_mc' + post 'commit_answer' end end From d2fadfda085f17d35cad7c7787f52cf5e49060b6 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 10:11:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=8D=95=E9=80=89=E9=A2=98=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E7=AD=94=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 8 ++++---- app/views/poll/show.html.erb | 17 +++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index fcc1eef61..f83ff7cb9 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -113,11 +113,11 @@ class PollController < ApplicationController #提交答案 def commit_answer @pv = get_poll_vote(User.current.id,params[:poll_question_id]) - if params[:poll_vote][:poll_answer_id] - @pv.poll_answer_id = params[:poll_vote][:poll_answer_id] + if params[:poll_answer_id] + @pv.poll_answer_id = params[:poll_answer_id] end - if params[:poll_vote][:vote_text] - @pv.vote_text = params[:poll_vote][:vote_text] + if params[:vote_text] + @pv.vote_text = params[:vote_text] end @pv_saved = false if @pv.save diff --git a/app/views/poll/show.html.erb b/app/views/poll/show.html.erb index 08dae18b2..86815f80a 100644 --- a/app/views/poll/show.html.erb +++ b/app/views/poll/show.html.erb @@ -42,21 +42,22 @@ From d684acf5273c038cba31ac51c412e1cc93015952 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 11:54:30 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A4=9A=E9=80=89=E9=A2=98=E7=AD=94?= =?UTF-8?q?=E9=A2=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 51 ++++++++++++++++++++------ app/helpers/poll_helper.rb | 19 ++++------ app/views/poll/show.html.erb | 59 +++++++++++++++++++++--------- 3 files changed, 89 insertions(+), 40 deletions(-) diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index f83ff7cb9..1f1f67ca9 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -112,22 +112,51 @@ class PollController < ApplicationController #提交答案 def commit_answer - @pv = get_poll_vote(User.current.id,params[:poll_question_id]) - if params[:poll_answer_id] - @pv.poll_answer_id = params[:poll_answer_id] - end - if params[:vote_text] - @pv.vote_text = params[:vote_text] - end - @pv_saved = false - if @pv.save - @pv_saved = true + pq = PollQuestion.find(params[:poll_question_id]) + if pq.question_type == 1 + #单选题 + 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 + render :text => "ok" + else + render :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] + pv.poll_answer_id = params[:poll_answer_id] + if pv.save + render :text => "true" + else + render :text => "failure" + end + else + if pv.delete + render :text => "false" + else + render :text => "failure" + end + end + elsif pq.question_type == 3 + elsif pq.question_type == 4 + end + + #respond_to do |format| # format.js # format.json #end - render :text => "ok" + end private diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb index 202f33eaa..d612cbb97 100644 --- a/app/helpers/poll_helper.rb +++ b/app/helpers/poll_helper.rb @@ -18,18 +18,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module PollHelper - def get_poll_vote(user_id,poll_question_id) - pq = PollQuestion.find(poll_question_id) - if pq.question_type == 1 - pv = PollVote.find_by_poll_question_id_and_user_id(poll_question_id,user_id) - if pv.nil? - pv = PollVote.new - pv.user_id = user_id - pv.poll_question_id = poll_question_id - pv - else - pv - end + #判断选项是否被选中 + def answer_be_selected?(answer,user) + pv = answer.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id} ") + if !pv.nil? && pv.count > 0 + true + else + false end end end \ No newline at end of file diff --git a/app/views/poll/show.html.erb b/app/views/poll/show.html.erb index 86815f80a..2dadbd2d3 100644 --- a/app/views/poll/show.html.erb +++ b/app/views/poll/show.html.erb @@ -34,7 +34,7 @@
- <%= form_tag(commit_answer_poll_path(@poll),:remote => true,:id => 'form_'+ pq.id.to_s) do %> +
<% pq.poll_answers.each do |pa| %> @@ -57,7 +57,7 @@ }); } - <%= radio_button "poll_vote","poll_answer_id",pa.id,:class=>"ur_radio",:onclick =>"click_#{pa.id}(this);return false;" %> + <%= radio_button "poll_vote","poll_answer_id",pa.id,:class=>"ur_radio",:onclick =>"click_#{pa.id}(this);return false;",:checked => answer_be_selected?(pa,User.current) %> <%= pa.answer_text %> @@ -65,7 +65,7 @@ <% end %>
- <% end %> +
<% elsif pq.question_type == 2 %> @@ -80,20 +80,45 @@
- - - <% pq.poll_answers.each do |pa| %> - - - - <% end %> - -
- -
+
+ + + <% pq.poll_answers.each do |pa| %> + + + + <% end %> + +
+ +
+
<% elsif pq.question_type == 3 %> From 77dc9607df4fe7147ce6aa536f40315f6b633e79 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 17:36:30 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AE=80=E7=AD=94=E9=A2=98=E7=AD=94?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poll_controller.rb | 57 +++++++++++++++++++++++++-- app/helpers/poll_helper.rb | 10 +++++ app/views/poll/_commit_alert.html.erb | 3 ++ app/views/poll/commit_poll.js.erb | 10 +++++ app/views/poll/show.html.erb | 49 ++++++++++++++++++++--- config/routes.rb | 1 + 6 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 app/views/poll/_commit_alert.html.erb create mode 100644 app/views/poll/commit_poll.js.erb diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index 1f1f67ca9..531cff906 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -1,5 +1,5 @@ class PollController < ApplicationController - before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question] + before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll] before_filter :find_container, :only => [:new,:create, :index] before_filter :is_member_of_course, :only => [:index,:show] before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy] @@ -146,8 +146,20 @@ class PollController < ApplicationController render :text => "failure" end end - elsif pq.question_type == 3 - elsif pq.question_type == 4 + 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 = PollVote.new + pv.user_id = User.current.id + pv.poll_question_id = params[:poll_question_id] + end + pv.vote_text = params[:vote_text] + if pv.save + render :text => pv.vote_text + else + render :text => "failure" + end + else end @@ -159,6 +171,24 @@ class PollController < ApplicationController end + #提交问卷 + def commit_poll + @uncomplete_question = get_uncomplete_question(@poll) + if @uncomplete_question.count < 1 + pu = get_poll_user(@poll.id,User.current.id) + pu.user_id = User.current.id + pu.poll_id = @poll.id + if pu.save + #redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course') + end + else + + end + respond_to do |format| + format.js + end + end + private def find_poll_and_course @poll = Poll.find params[:id] @@ -187,4 +217,25 @@ class PollController < ApplicationController def is_course_teacher render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course)) end + + #获取未完成的题目 + def get_uncomplete_question poll + necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1") + uncomplete_question = [] + necessary_questions.each do |question| + if question.poll_votes.nil? || question.poll_votes.count < 1 + uncomplete_question << question + end + end + uncomplete_question + end + + #PollUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个 + def get_poll_user poll_id,user_id + pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id) + if pu.nil? + pu = PollUser.new + end + pu + end end \ No newline at end of file diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb index d612cbb97..1c9ecd40f 100644 --- a/app/helpers/poll_helper.rb +++ b/app/helpers/poll_helper.rb @@ -27,4 +27,14 @@ 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) + if pv.nil? + '' + else + pv.vote_text + end + end end \ No newline at end of file diff --git a/app/views/poll/_commit_alert.html.erb b/app/views/poll/_commit_alert.html.erb new file mode 100644 index 000000000..007ae5ba7 --- /dev/null +++ b/app/views/poll/_commit_alert.html.erb @@ -0,0 +1,3 @@ +
+ shaksdkfdks +
diff --git a/app/views/poll/commit_poll.js.erb b/app/views/poll/commit_poll.js.erb new file mode 100644 index 000000000..e0b9960b9 --- /dev/null +++ b/app/views/poll/commit_poll.js.erb @@ -0,0 +1,10 @@ +$('#ajax-modal').html('<%= escape_javascript(render :partial => 'commit_alert') %>'); +showModal('ajax-modal', '513px'); +$('#ajax-modal').css('height','200px'); +$('#ajax-modal').siblings().remove(); +$('#ajax-modal').before("" + + ""); +$('#ajax-modal').parent().removeClass("alert_praise"); +$('#ajax-modal').parent().css("top","50%").css("left","20%"); +$('#ajax-modal').parent().css("position","absolute"); +$('#ajax-modal').parent().addClass("alert_box"); \ No newline at end of file diff --git a/app/views/poll/show.html.erb b/app/views/poll/show.html.erb index 2dadbd2d3..2db65ec97 100644 --- a/app/views/poll/show.html.erb +++ b/app/views/poll/show.html.erb @@ -4,7 +4,12 @@ 问卷调查_问卷页面 <%= stylesheet_link_tag 'polls', :media => 'all' %> - <%= javascript_heads %> + @@ -133,9 +138,27 @@
- + +
- + <% elsif pq.question_type == 4 %>
  • @@ -149,7 +172,23 @@
    - + +
    <%= get_anwser_vote_text(pq.id,User.current.id) %>
  • @@ -163,7 +202,7 @@
    - 提交 + <%= link_to "提交",commit_poll_poll_path(@poll), :method => :post,:class => "ur_button",:format => 'js',:remote=>true %>
    答题已完成 0%
    diff --git a/config/routes.rb b/config/routes.rb index 28aa7452c..2f1414a48 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,7 @@ RedmineApp::Application.routes.draw do get 'statistics_result' post 'commit_answer' post 'create_poll_question' + post 'commit_poll' end end