From 6d82e79e7f231dcd7635991c0265d94a6f95d881 Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 15 Jan 2015 09:41:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=94=E9=A2=98=E7=9B=B8=E5=85=B3=E6=96=B9?= =?UTF-8?q?=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