Merge branch 'Poll' of http://repository.trustie.net/xianbo/trustie2 into Poll
Conflicts: app/controllers/poll_controller.rb
This commit is contained in:
commit
c4bedfb369
|
@ -1,9 +1,9 @@
|
||||||
class PollController < ApplicationController
|
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 :find_container, :only => [:new,:create, :index]
|
||||||
before_filter :is_member_of_course, :only => [:index,:show]
|
before_filter :is_member_of_course, :only => [:index,:show]
|
||||||
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
|
before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy]
|
||||||
|
include PollHelper
|
||||||
def index
|
def index
|
||||||
if @course
|
if @course
|
||||||
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
@is_teacher = User.current.allowed_to?(:as_teacher,@course)
|
||||||
|
@ -138,6 +138,85 @@ class PollController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#提交答案
|
||||||
|
def commit_answer
|
||||||
|
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 || 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
|
||||||
|
|
||||||
|
|
||||||
|
#respond_to do |format|
|
||||||
|
# format.js
|
||||||
|
# format.json
|
||||||
|
#end
|
||||||
|
|
||||||
|
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
|
private
|
||||||
def find_poll_and_course
|
def find_poll_and_course
|
||||||
@poll = Poll.find params[:id]
|
@poll = Poll.find params[:id]
|
||||||
|
@ -166,4 +245,25 @@ class PollController < ApplicationController
|
||||||
def is_course_teacher
|
def is_course_teacher
|
||||||
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
|
render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course))
|
||||||
end
|
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
|
end
|
|
@ -0,0 +1,40 @@
|
||||||
|
# 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 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
|
||||||
|
|
||||||
|
#获取文本题答案
|
||||||
|
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
|
|
@ -0,0 +1,3 @@
|
||||||
|
<div id="popbox">
|
||||||
|
shaksdkfdks
|
||||||
|
</div>
|
|
@ -0,0 +1,3 @@
|
||||||
|
<% if @pv_saved %>
|
||||||
|
$('#poll_vote_poll_answer_id_<%= @pv.poll_answer_id %>').checked = true;
|
||||||
|
<% end %>
|
|
@ -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("<span style='float: right;cursor:pointer;padding-left: 513px;'>" +
|
||||||
|
"<a href='#' onclick='hidden_atert_form();'><img src='/images/bid/close.png' width='26px' height='26px' /></a></span>");
|
||||||
|
$('#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");
|
|
@ -4,6 +4,12 @@
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
<title>问卷调查_问卷页面</title>
|
<title>问卷调查_问卷页面</title>
|
||||||
<%= stylesheet_link_tag 'polls', :media => 'all' %>
|
<%= stylesheet_link_tag 'polls', :media => 'all' %>
|
||||||
|
<script type="text/javascript">
|
||||||
|
function hidden_atert_form(cur_page,cur_type)
|
||||||
|
{
|
||||||
|
hideModal($("#popbox"));
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -33,20 +39,38 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_inputs">
|
<div class="ur_inputs">
|
||||||
|
<form>
|
||||||
<table class="ur_table" >
|
<table class="ur_table" >
|
||||||
<tbody>
|
<tbody>
|
||||||
<% pq.poll_answers.each do |pa| %>
|
<% pq.poll_answers.each do |pa| %>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<label >
|
<label >
|
||||||
<input class="ur_radio" type="radio" value="新建选项" >
|
<script>
|
||||||
<%= pa.answer_text %>
|
function click_<%= pa.id %>(obj)
|
||||||
|
{
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "<%= commit_answer_poll_path(@poll) %>",
|
||||||
|
data: {
|
||||||
|
poll_answer_id: <%= pa.id %>,
|
||||||
|
poll_question_id: <%= pq.id %>
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
obj.checked = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<%= 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 %>
|
||||||
</label>
|
</label>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<% elsif pq.question_type == 2 %>
|
<% elsif pq.question_type == 2 %>
|
||||||
|
@ -61,20 +85,45 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_inputs">
|
<div class="ur_inputs">
|
||||||
<table class="ur_table" >
|
<form>
|
||||||
<tbody>
|
<table class="ur_table" >
|
||||||
<% pq.poll_answers.each do |pa| %>
|
<tbody>
|
||||||
<tr>
|
<% pq.poll_answers.each do |pa| %>
|
||||||
<td>
|
<tr>
|
||||||
<label >
|
<td>
|
||||||
<input class="ur_checkbox" type="checkbox" value="新建选项" >
|
<label >
|
||||||
<%= pa.answer_text %>
|
<script>
|
||||||
</label>
|
function click_<%= pa.id %>(obj)
|
||||||
</td>
|
{
|
||||||
</tr>
|
$.ajax({
|
||||||
<% end %>
|
type: "post",
|
||||||
</tbody>
|
url: "<%= commit_answer_poll_path(@poll) %>",
|
||||||
</table>
|
data: {
|
||||||
|
poll_answer_id: <%= pa.id %>,
|
||||||
|
poll_question_id: <%= pq.id %>
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
if(data == "true")
|
||||||
|
{
|
||||||
|
obj.checked = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
obj.checked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<input class="ur_checkbox" type="checkbox" onclick="click_<%= pa.id %>(this);return false;" <%= answer_be_selected?(pa,User.current) ? "checked":"" %>>
|
||||||
|
<%= pa.answer_text %>
|
||||||
|
</label>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<% elsif pq.question_type == 3 %>
|
<% elsif pq.question_type == 3 %>
|
||||||
|
@ -89,9 +138,27 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_inputs">
|
<div class="ur_inputs">
|
||||||
<input class="ur_text ur_textbox" type="text" size="" maxlength=""value="">
|
<script>
|
||||||
|
function onblur_<%= pq.id %>(obj)
|
||||||
|
{
|
||||||
|
$(window).unbind('beforeunload');
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "<%= commit_answer_poll_path(@poll) %>",
|
||||||
|
data: {
|
||||||
|
poll_question_id: <%= pq.id %> ,
|
||||||
|
vote_text: obj.value
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
// obj.value = data;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<input class="ur_text ur_textbox" type="text" size="" maxlength="" value="<%= get_anwser_vote_text(pq.id,User.current.id) %>" onblur="onblur_<%= pq.id %>(this);">
|
||||||
</div>
|
</div>
|
||||||
</li><!--当行输入 end-->
|
</li><!--单行输入 end-->
|
||||||
<% elsif pq.question_type == 4 %>
|
<% elsif pq.question_type == 4 %>
|
||||||
<!-- 多行文字-->
|
<!-- 多行文字-->
|
||||||
<li class="ur_question_item textarea">
|
<li class="ur_question_item textarea">
|
||||||
|
@ -105,7 +172,23 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_inputs">
|
<div class="ur_inputs">
|
||||||
<textarea class="ur_textbox" rows="5" cols="60"></textarea>
|
<script>
|
||||||
|
function onblur_<%= pq.id %>(obj)
|
||||||
|
{
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "<%= commit_answer_poll_path(@poll) %>",
|
||||||
|
data: {
|
||||||
|
poll_question_id: <%= pq.id %> ,
|
||||||
|
vote_text: obj.innerHTML
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
// obj.value = data;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<div contenteditable='true' class="ur_textbox" style="min-height: 150px;width: 100%" onblur="onblur_<%= pq.id %>(this);"><%= get_anwser_vote_text(pq.id,User.current.id) %></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li><!--多行输入 end-->
|
</li><!--多行输入 end-->
|
||||||
|
@ -119,7 +202,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_buttons" style="width: 100px;">
|
<div class="ur_buttons" style="width: 100px;">
|
||||||
<a href="#" class="ur_button" >提交</a>
|
<%= link_to "提交",commit_poll_poll_path(@poll), :method => :post,:class => "ur_button",:format => 'js',:remote=>true %>
|
||||||
</div>
|
</div>
|
||||||
<div class="cl"></div>
|
<div class="cl"></div>
|
||||||
<div class="ur_progress_text">答题已完成 <strong class="ur_progress_number">0%</strong> </div>
|
<div class="ur_progress_text">答题已完成 <strong class="ur_progress_number">0%</strong> </div>
|
||||||
|
|
|
@ -61,7 +61,9 @@ RedmineApp::Application.routes.draw do
|
||||||
resources :poll do
|
resources :poll do
|
||||||
member do
|
member do
|
||||||
get 'statistics_result'
|
get 'statistics_result'
|
||||||
|
post 'commit_answer'
|
||||||
post 'create_poll_question'
|
post 'create_poll_question'
|
||||||
|
post 'commit_poll'
|
||||||
end
|
end
|
||||||
collection do
|
collection do
|
||||||
delete 'delete_poll_question'
|
delete 'delete_poll_question'
|
||||||
|
|
Loading…
Reference in New Issue