This commit is contained in:
cxt 2016-10-28 15:42:45 +08:00
parent 8d1673d0ea
commit 4d88fe727b
12 changed files with 186 additions and 68 deletions

View File

@ -44,8 +44,7 @@ class PollController < ApplicationController
else
@can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin?
@percent = get_percent(@poll,User.current)
poll_questions = @poll.poll_questions
@poll_questions = paginateHelper poll_questions,5 #分页
@poll_questions = @poll.poll_questions
@left_nav_type = 7
respond_to do |format|
format.html {render :layout => 'base_courses'}
@ -209,6 +208,8 @@ class PollController < ApplicationController
#@poll = @poll_question.poll
@poll_question.is_necessary = params[:is_necessary]=="true" ? 1 : 0
@poll_question.question_title = params[:poll_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title]
@poll_question.max_choices = params[:max_choices].to_i || 0
@poll_question.min_choices = params[:min_choices].to_i || 0
################处理选项
if params[:question_answer]
@poll_question.poll_answers.each do |answer|
@ -325,6 +326,10 @@ class PollController < ApplicationController
pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id)
if pv.nil?
#尚未答该题,添加答案
count = PollVote.where("poll_question_id = #{params[:poll_question_id].to_i} and user_id = #{User.current.id}").count
if pq.max_choices != 0 && count >= pq.max_choices
render :json => {:text => "over"}
else
pv = PollVote.new
pv.user_id = User.current.id
pv.poll_question_id = params[:poll_question_id]
@ -336,6 +341,7 @@ class PollController < ApplicationController
else
render :json => {:text => "failure"}
end
end
else
#pv不为空则当前选项之前已被选择再次点击则是不再选择该项故删除该答案
if params[:vote_text]

View File

@ -87,4 +87,21 @@ module PollHelper
s.html_safe
end
#问卷的多选题上下限
def min_or_max_choices_option pq
type = []
count = pq.poll_answers.count
option = []
option << '不限'
option << 0
type << option
for i in 1 .. count do
option = []
option << i
option << i
type << option
end
type
end
end

View File

@ -33,18 +33,6 @@
</div>
<div class="ur_editor_content">
<ul id="poll_answers_<%=poll_question.id%>">
<li class='ur_item'>
<div class="fl mr30">
<label>下限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<select class="poll-multiple-limit"></select>
<span>(可选)答题时最少选几项</span>
</div>
<div class="fl">
<label>上限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<select class="poll-multiple-limit"></select>
<span>(可选)答题时最多选几项</span>
</div>
</li>
<% poll_question.poll_answers.reorder("answer_position").each do |poll_answer| %>
<% if poll_answer.answer_text != '' %>
<li class='ur_item new_answer'>
@ -64,6 +52,22 @@
<% end%>
</ul>
<ul>
<li class='ur_item'>
<div class="fl mr30">
<label>下限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<%= select_tag :min_choices,options_for_select(min_or_max_choices_option(poll_question),poll_question.min_choices), {:id=>"min_choices", :class=>"poll-multiple-limit"} %>
<span class="fontGrey2">(可选)答题时最少选几项</span>
</div>
</li>
<div class="cl"></div>
<li class='ur_item'>
<div class="fl mr30">
<label>上限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<%= select_tag :max_choices,options_for_select(min_or_max_choices_option(poll_question),poll_question.max_choices), {:id=>"max_choices", :class=>"poll-multiple-limit"} %>
<span class="fontGrey2">(可选)答题时最多选几项</span><span class="c_red ml10" id="choices_notice"></span>
</div>
</li>
<div class="cl"></div>
<li class="ur_item">
<div class="dash-block new-question" onclick='add_single_answer($(this));'>新建选项</div>
</li>

View File

@ -32,7 +32,12 @@
<li class='ur_item'>
<div class="fl mr30">
<label>下限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<select name="min_choices" class="poll-multiple-limit"></select>
<select name="min_choices" class="poll-multiple-limit">
<option value="0">不限</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<span class="fontGrey2">(可选)答题时最少选几项</span>
</div>
</li>
@ -40,8 +45,13 @@
<li class='ur_item'>
<div class="fl mr30">
<label>上限<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label>
<select name="max_choices" class="poll-multiple-limit"></select>
<span class="fontGrey2">(可选)答题时最多选几项</span>
<select name="max_choices" class="poll-multiple-limit">
<option value="0">不限</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<span class="fontGrey2">(可选)答题时最多选几项</span><span class="c_red ml10" id="choices_notice"></span>
</div>
</li>
<div class="cl"></div>
@ -72,5 +82,4 @@
add_poll_question_new($(this));
});
});
multiLimit();
</script>

View File

@ -171,18 +171,6 @@ function insert_MCQ(quest_type,quest_id){
'<div class="ur_editor_content">'+
'<ul>'+
'<li class="ur_item">'+
'<div class="fl mr30">' +
'<label>下限<span class="ur_index"></span>&nbsp;&nbsp;&nbsp;</label>' +
'<select class="poll-multiple-limit"></select>' +
'<span>(可选)答题时最少选几项</span>' +
'</div>' +
'<div class="fl">'+
'<label>上限<span class="ur_index"></span>&nbsp;&nbsp;&nbsp;</label>' +
'<select class="poll-multiple-limit"></select>' +
'<span>(可选)答题时最多选几项</span>' +
'</div>' +
'</li>' +
'<li class="ur_item">'+
'<label>选项<span class="ur_index"></span>&nbsp;&nbsp;&nbsp;</label>'+
'<input maxlength="200" type="text" name="question_answer[0]" placeholder="输入选项内容"/>'+
'<a class="icon_remove" title="删除"" onclick="remove_single_answer($(this))"></a>'+
@ -203,6 +191,32 @@ function insert_MCQ(quest_type,quest_id){
'</ul>'+
'<ul>'+
'<li class="ur_item">'+
'<div class="fl mr30">'+
'<label>下限<span class="ur_index"></span>&nbsp;&nbsp;&nbsp;</label>'+
'<select name="min_choices" class="poll-multiple-limit">'+
'<option value="0">不限</option>'+
'<option value="1">1</option>'+
'<option value="2">2</option>'+
'<option value="3">3</option>'+
'</select>'+
'<span class="fontGrey2">(可选)答题时最少选几项</span>'+
'</div>'+
'</li>'+
'<div class="cl"></div>'+
'<li class="ur_item">'+
'<div class="fl mr30">'+
'<label>上限<span class="ur_index"></span>&nbsp;&nbsp;&nbsp;</label>' +
'<select name="max_choices" class="poll-multiple-limit">'+
'<option value="0">不限</option>'+
'<option value="1">1</option>'+
'<option value="2">2</option>'+
'<option value="3">3</option>'+
'</select>'+
'<span class="fontGrey2">(可选)答题时最多选几项</span><span class="c_red ml10" id="choices_notice"></span>'+
'</div>' +
'</li>' +
'<div class="cl"></div>'+
'<li class="ur_item">'+
'<div class="dash-block new-question" onclick="add_single_answer($(this));">新建选项</div>'+
'</li>'+
'<div class="cl"></div>'+
@ -396,7 +410,6 @@ function insert_MCQ(quest_type,quest_id){
return;
}
}
}
//添加标题时确定按钮
@ -408,7 +421,13 @@ function insert_MCQ(quest_type,quest_id){
doc.one('click', function(){
add_poll_question($(this));
});
}else if(doc.parent().parent().find("select[name='min_choices']").length > 0 && doc.parent().parent().find("select[name='min_choices']").find("option:selected").val() != '0' && doc.parent().parent().find("select[name='max_choices']").length > 0 && doc.parent().parent().find("select[name='max_choices']").find("option:selected").val() != '0' && parseInt(doc.parent().parent().find("select[name='min_choices']").find("option:selected").val()) > parseInt(doc.parent().parent().find("select[name='max_choices']").find("option:selected").val())){
doc.parent().parent().find("#choices_notice").html("下限不能大于上限");
doc.one('click', function(){
add_poll_question($(this));
});
}else{
doc.parent().parent().find("#choices_notice").html("");
doc.parent().parent().parent().submit();
}
}
@ -421,7 +440,14 @@ function insert_MCQ(quest_type,quest_id){
doc.one('click', function(){
add_poll_question_new($(this));
});
}else{
}else if(doc.parent().parent().find("select[name='min_choices']").length > 0 && doc.parent().parent().find("select[name='min_choices']").find("option:selected").val() != '0' && doc.parent().parent().find("select[name='max_choices']").length > 0 && doc.parent().parent().find("select[name='max_choices']").find("option:selected").val() != '0' && parseInt(doc.parent().parent().find("select[name='min_choices']").find("option:selected").val()) > parseInt(doc.parent().parent().find("select[name='max_choices']").find("option:selected").val())){
doc.parent().parent().find("#choices_notice").html("下限不能大于上限");
doc.one('click', function(){
add_poll_question_new($(this));
});
}
else{
doc.parent().parent().find("#choices_notice").html("");
doc.parent().parent().parent().submit();
}
}
@ -429,7 +455,14 @@ function insert_MCQ(quest_type,quest_id){
function edit_poll_question(doc,id)
{
var title = $.trim($("#poll_questions_title_" + id).val());
if(title.length == 0){alert("标题不能为空");}else{doc.parent().parent().parent().submit();}
if(title.length == 0){
alert("标题不能为空");
}else if(doc.parent().parent().find("select[name='min_choices']").length > 0 && doc.parent().parent().find("select[name='min_choices']").find("option:selected").val() != '0' && doc.parent().parent().find("select[name='max_choices']").length > 0 && doc.parent().parent().find("select[name='max_choices']").find("option:selected").val() != '0' && parseInt(doc.parent().parent().find("select[name='min_choices']").find("option:selected").val()) > parseInt(doc.parent().parent().find("select[name='max_choices']").find("option:selected").val())){
doc.parent().parent().find("#choices_notice").html("下限不能大于上限");
}else{
doc.parent().parent().find("#choices_notice").html("");
doc.parent().parent().parent().submit();
}
}
//问卷头
@ -471,11 +504,13 @@ function insert_MCQ(quest_type,quest_id){
// doc.parent().before("<li class='ur_item new_answer'><label>选项<span class='ur_index'></span>&nbsp;&nbsp;&nbsp;</label><input maxlength='200' type='text' name='question_answer["+new Date().getTime()+"]' placeholder='输入选项内容'/>" +
// "<a class='icon_remove' title='删除' onclick='remove_single_answer($(this))'></a>"+
// "</li><div class='cl'></div>");
$(".poll-multiple-limit option").remove();
multiLimit();
var count = doc.parent().parent().prev().children('li').length;
multiLimit(doc.parent().siblings("li").children("div").children("select:first"), count);
multiLimit(doc.parent().siblings("li").children("div").children("select:last"), count);
}
function remove_single_answer(doc)
{
var ul = doc.parent().parent();
if(doc.parent().siblings("li.new_answer").length == 0)
{
alert("至少有一个选项");
@ -484,17 +519,27 @@ function insert_MCQ(quest_type,quest_id){
{
doc.parent().remove();
}
$(".poll-multiple-limit option").remove();
multiLimit();
var count = ul.children('li').length;
multiLimit(ul.siblings("ul").children("li").children("div").children("select:first"), count);
multiLimit(ul.siblings("ul").children("li").children("div").children("select:last"), count);
}
//多选题答题限制数实时更新
function multiLimit(){
var upperLimit = $(".questionContainer .new_answer").size();
$(".poll-multiple-limit").append("<option value='0'></option>");
for(var i = 1; i <= upperLimit; i++){
$(".poll-multiple-limit").append("<option value='"+ i +"'>" + i + "</option>");
function multiLimit(doc, count){
var upperLimit = $(".ur_editor_content .new_answer").size();
var option_count = doc.children('option').length;
if (option_count == 0){
doc.append("<option value='"+ 0 +"'>" + '' + "</option>");
}
var option_count = doc.children('option').length;
if(count > option_count - 1){
for(var i = option_count; i <= count; i++){
doc.append("<option value='"+ i +"'>" + i + "</option>");
}
} else if(count < option_count - 1){
for(var i = count + 1; i <= option_count - 1; i++){
doc.children("option[value='" + i + "']").remove();
}
}
}
@ -507,8 +552,9 @@ function insert_MCQ(quest_type,quest_id){
"</li><div class='cl'></div>");
}
$(".poll-multiple-limit option").remove();
multiLimit();
var count = doc.parent().parent().prev().children('li').length;
multiLimit(doc.parent().siblings("li").children("div").children("select:first"), count);
multiLimit(doc.parent().siblings("li").children("div").children("select:last"), count);
}
function poll_cancel()
{

View File

@ -12,11 +12,13 @@
<% if poll_question.question_number < count %>
<%= link_to('', {:controller => 'poll', :action => 'update_question_num', :id => poll.id, :ques_id => poll_question.id, :opr => 'down'},:remote => true, :method => 'post', :class => "poll-down mb8", :title => '下移') %>
<% end %>
<a href="javascript:void(0);" class="poll-add mb8"></a>
<div class="poll-add mb8 pr">
<ul class="poll-add-menu fontGrey3">
<li><a href="javascript:void(0);" onclick=" dismiss('<%=type %>',<%=poll_question.id%>);insert_MC('<%=type %>',<%=poll_question.id%>);">单选题</a></li>
<li><a href="javascript:void(0);" onclick=" dismiss('<%=type %>',<%=poll_question.id%>);insert_MCQ('<%=type %>',<%=poll_question.id%>);">多选题</a></li>
<li><a href="javascript:void(0);" onclick=" dismiss('<%=type %>',<%=poll_question.id%>);insert_SINGLE('<%=type %>',<%=poll_question.id%>);">主观题</a></li>
</ul>
</div>
<%= link_to("", delete_poll_question_poll_index_path(:poll_question => poll_question.id),
method: :delete, :confirm => l(:text_are_you_sure), :remote => true, :class => "poll-delete", :title => "删除") %>

View File

@ -40,9 +40,9 @@
</div>
<script>
$(".poll-add").mouseover(function(){
$(this).next().show();
$(this).children('ul').show();
}).mouseout(function(){
$(this).next().hide();
$(this).children('ul').hide();
});
$(".poll-add-menu").mouseover(function(){
$(this).show();

View File

@ -50,9 +50,9 @@
</div>
<script>
$(".poll-add").mouseover(function(){
$(this).next().show();
$(this).children('ul').show();
}).mouseout(function(){
$(this).next().hide();
$(this).children('ul').hide();
});
$(".poll-add-menu").mouseover(function(){
$(this).show();

View File

@ -23,9 +23,9 @@
</div>
<script>
$(".poll-add").mouseover(function(){
$(this).next().show();
$(this).children('ul').show();
}).mouseout(function(){
$(this).next().hide();
$(this).children('ul').hide();
});
$(".poll-add-menu").mouseover(function(){
$(this).show();

View File

@ -20,9 +20,9 @@
</div>
<script>
$(".poll-add").mouseover(function(){
$(this).next().show();
$(this).children('ul').show();
}).mouseout(function(){
$(this).next().hide();
$(this).children('ul').hide();
});
$(".poll-add-menu").mouseover(function(){
$(this).show();

View File

@ -118,6 +118,7 @@
<% elsif pq.max_choices != 0 %>
答题时最多选<%=pq.max_choices %>项
<% end %>
<span class="c_red ml10" id="mcq_notice_<%=pq.id %>"></span>
</p>
<% end %>
</div>
@ -161,10 +162,16 @@
if (dataObj.text == "ok") {
obj.checked = true;
$(obj).next('input').removeAttr("disabled");
$("#mcq_notice_<%=pq.id %>").html("");
}
else {
obj.checked = false;
$(obj).next('input').attr("disabled", "disabled");
if(dataObj.text == "over"){
$("#mcq_notice_<%=pq.id %>").html("该题最多只能选择<%=pq.max_choices %>个选项");
}else{
$("#mcq_notice_<%=pq.id %>").html("");
}
}
var span = $('#percent');
span.html(dataObj.percent);
@ -298,7 +305,8 @@
<div class="ur_buttons" style="width: 77px;">
<% if @poll.polls_status == 2 %>
<%= link_to l(:button_submit), commit_poll_poll_path(@poll), :method => :post, :class => "BlueCirBtn", :format => 'js', :remote => true %>
<a href="javascript:void(0)" class="BlueCirBtn" onclick="commit_poll();"><%=l(:button_submit) %></a>
<%#= link_to l(:button_submit), commit_poll_poll_path(@poll), :method => :post, :class => "BlueCirBtn", :format => 'js', :remote => true %>
<% end %>
</div>
<div class="cl"></div>
@ -311,3 +319,29 @@
</div>
<!--问卷内容end-->
</div>
<script>
function check_mcq(){
var result = true;
<% @poll.poll_questions.where("question_type = 2").each do |pq| %>
$("#mcq_notice_<%=pq.id %>").html("");
<% if pq.min_choices != 0 %>
var count = $("#show_poll_questions_<%= pq.id %>").find("input[type='checkbox']:checked").length;
if(count < <%=pq.min_choices %>) {
$("#mcq_notice_<%=pq.id %>").html("该题最少选择<%=pq.min_choices %>个选项");
result = false;
} else{
$("#mcq_notice_<%=pq.id %>").html("");
}
<% end %>
<% end %>
if(!result){
alert("您的多选题答题不符合要求,请检查后再提交");
}
return result;
}
function commit_poll(){
if(check_mcq()){
$.post('<%=commit_poll_poll_path(@poll) %>');
}
}
</script>

View File

@ -574,7 +574,7 @@ a:hover.blueCir{ background:#3598db; color:#fff;}
.poll-delete:hover{ background:url(/images/course/icons.png) -17px -343px no-repeat;}
.poll-edit{ background:url(/images/course/icons.png) 0px -272px no-repeat; width:16px; height:27px; display:block;float:right;}
.poll-edit:hover{ background:url(/images/course/icons.png) -21px -272px no-repeat;}
.poll-add-menu {border:1px solid #eaeaea; background:#fff; padding:5px 8px; width:50px; left:15px; top:110px; position:absolute; z-index:9999; line-height:2; box-shadow: 0px 2px 8px rgba(146, 153, 169, 0.5); display:none;}
.poll-add-menu {border:1px solid #eaeaea; background:#fff; padding:5px 8px; width:50px; left:0px; top:20px; position:absolute; z-index:9999; line-height:2; box-shadow: 0px 2px 8px rgba(146, 153, 169, 0.5); display:none;}
.poll-multiple-limit {width:70px; outline: none;}
/*黄色边框的提示信息 */