This commit is contained in:
sw 2015-01-16 15:34:43 +08:00
commit d448599664
4 changed files with 138 additions and 53 deletions

View File

@ -24,6 +24,7 @@ class PollController < ApplicationController
render_403 render_403
else else
@can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin? @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 = @poll.poll_questions
@poll_questions = paginateHelper poll_questions,3 #分页 @poll_questions = paginateHelper poll_questions,3 #分页
respond_to do |format| respond_to do |format|
@ -169,7 +170,7 @@ class PollController < ApplicationController
def commit_answer def commit_answer
pq = PollQuestion.find(params[:poll_question_id]) pq = PollQuestion.find(params[:poll_question_id])
if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?) if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?)
render :text => 'failure' render :json => {:text => "failure"}
return return
end end
if pq.question_type == 1 if pq.question_type == 1
@ -182,9 +183,10 @@ class PollController < ApplicationController
end end
pv.poll_answer_id = params[:poll_answer_id] pv.poll_answer_id = params[:poll_answer_id]
if pv.save if pv.save
render :text => "ok" @percent = get_percent(@poll,User.current)
render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
else else
render :text => "failure" render :json => {:text => "failure"}
end end
elsif pq.question_type == 2 elsif pq.question_type == 2
pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id) pv = PollVote.find_by_poll_answer_id_and_user_id(params[:poll_answer_id],User.current.id)
@ -194,38 +196,64 @@ class PollController < ApplicationController
pv.poll_question_id = params[:poll_question_id] pv.poll_question_id = params[:poll_question_id]
pv.poll_answer_id = params[:poll_answer_id] pv.poll_answer_id = params[:poll_answer_id]
if pv.save if pv.save
render :text => "true" @percent = get_percent(@poll,User.current)
render :json => {:text => "true",:percent => format("%.2f" ,@percent)}
else else
render :text => "failure" render :json => {:text => "failure"}
end end
else else
if pv.delete if pv.delete
render :text => "false" @percent = get_percent(@poll,User.current)
render :json => {:text => "false" ,:percent => format("%.2f" ,@percent)}
else else
render :text => "failure" render :json => {:text => "failure"}
end end
end end
elsif pq.question_type == 3 || 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) pv = PollVote.find_by_poll_question_id_and_user_id(params[:poll_question_id],User.current.id)
if pv.nil? if pv.nil?
pv = PollVote.new if params[:vote_text].nil? || params[:vote_text].blank?
pv.user_id = User.current.id @percent = get_percent(@poll,User.current)
pv.poll_question_id = params[:poll_question_id] render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
end else
pv.vote_text = params[:vote_text] pv = PollVote.new
if pv.save pv.user_id = User.current.id
render :text => pv.vote_text pv.poll_question_id = params[:poll_question_id]
pv.vote_text = params[:vote_text]
if pv.save
@percent = get_percent(@poll,User.current)
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
else
render :json => {:text => "failure"}
end
end
else else
render :text => "failure" 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)}
else
render :json => {:text => "failure"}
end
else
pv.vote_text = params[:vote_text]
if pv.save
@percent = get_percent(@poll,User.current)
render :json => {:text => pv.vote_text,:percent => format("%.2f" ,@percent)}
else
render :json => {:text => "failure"}
end
end
end end
else else
render :text => "failure" render :json => {:text => "failure"}
end end
end end
#提交问卷 #提交问卷
def commit_poll def commit_poll
@uncomplete_question = get_uncomplete_question(@poll) @uncomplete_question = get_uncomplete_question(@poll,User.current)
if @uncomplete_question.count < 1 if @uncomplete_question.count < 1
pu = get_poll_user(@poll.id,User.current.id) pu = get_poll_user(@poll.id,User.current.id)
pu.user_id = User.current.id pu.user_id = User.current.id
@ -274,17 +302,41 @@ class PollController < ApplicationController
end end
#获取未完成的题目 #获取未完成的题目
def get_uncomplete_question poll def get_uncomplete_question poll,user
necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1") necessary_questions = poll.poll_questions.where("#{PollQuestion.table_name}.is_necessary = 1")
uncomplete_question = [] uncomplete_question = []
necessary_questions.each do |question| necessary_questions.each do |question|
if question.poll_votes.nil? || question.poll_votes.count < 1 answers = get_user_answer(question,user)
if answers.nil? || answers.count < 1
uncomplete_question << question uncomplete_question << question
end end
end end
uncomplete_question uncomplete_question
end end
#获取用户对某个问题的答案
def get_user_answer(question,user)
user_answer = question.poll_votes.where("#{PollVote.table_name}.user_id = #{user.id}")
user_answer
end
def get_complete_question(poll,user)
questions = poll.poll_questions
complete_question = []
questions.each do |question|
answers = get_user_answer(question,user)
if !(answers.nil? || answers.count < 1)
complete_question << question
end
end
complete_question
end
def get_percent poll,user
complete_count = get_complete_question(poll,user).count
(complete_count.to_f / poll.poll_questions.count.to_f)*100
end
#PollUser记录用户是否已提交问卷有对应的记录则已提交没有则新建一个 #PollUser记录用户是否已提交问卷有对应的记录则已提交没有则新建一个
def get_poll_user poll_id,user_id def get_poll_user poll_id,user_id
pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id) pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id)

View File

@ -3,7 +3,7 @@
<h3 style="font-weight: normal;color: green">提交成功!</h3> <h3 style="font-weight: normal;color: green">提交成功!</h3>
<%= link_to "确定", poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course'),:class => 'commit'%> <%= link_to "确定", poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course'),:class => 'commit'%>
<% elsif status == 1 %> <% elsif status == 1 %>
<h3 style="font-weight: normal;color: red">您还有尚未作答的题目请完成后在提交!</h3> <h3 style="font-weight: normal;color: red">您还有尚未作答的必答题目请完成后再提交!</h3>
<%= link_to "确定", "javascript:void(0)",:onclick => 'hidden_atert_form();',:class => 'commit'%> <%= link_to "确定", "javascript:void(0)",:onclick => 'hidden_atert_form();',:class => 'commit'%>
<% else %> <% else %>
<h3 style="font-weight: normal;color: red">发生未知错误,请检查您的网络。</h3> <h3 style="font-weight: normal;color: red">发生未知错误,请检查您的网络。</h3>

View File

@ -11,34 +11,46 @@
<div class="cl"></div> <div class="cl"></div>
<div class="polls_list"> <div class="polls_list">
<% @polls.each do |poll|%> <% @polls.each do |poll|%>
<ul id="polls_<%= poll.id %>">
<li> <% unless !@is_teacher && poll.polls_status != 2 %>
<% if has_commit_poll?(poll.id ,User.current) %> <ul id="polls_<%= poll.id %>">
<sapn class="polls_title fl"> <%= poll.polls_name %></sapn> <li>
<% else %> <% if @is_teacher %>
<%= link_to poll.polls_name, poll_path(poll.id), :class => "polls_title fl" %> <% if has_commit_poll?(poll.id ,User.current) %>
<% end %> <sapn class="polls_title fl"> <%= poll.polls_name %></sapn>
</li> <% else %>
<li> <%= link_to poll.polls_name, poll_path(poll.id), :class => "polls_title fl" %>
<%if @is_teacher%> <% end %>
<%= link_to l(:label_statistical_results), statistics_result_poll_path(poll.id), :class => "pollsbtn fl ml10"%> <% else %>
<% end%> <% if has_commit_poll?(poll.id ,User.current) && poll.polls_status == 2 %>
</li> <sapn class="polls_title fl"> <%= poll.polls_name %></sapn>
<li> <% elsif (!has_commit_poll?(poll.id ,User.current)) && poll.polls_status == 2 %>
<% if @is_teacher%> <%= link_to poll.polls_name, poll_path(poll.id), :class => "polls_title fl" %>
<%= link_to(l(:button_delete), poll, <% end %>
method: :delete, :confirm => l(:text_are_you_sure), :remote => true, :class => "polls_de fr ml20 mr10") %> <% end %>
<% end%> </li>
</li> <li>
<li> <%if @is_teacher%>
<% if @is_teacher%> <%= link_to l(:label_statistical_results), statistics_result_poll_path(poll.id), :class => "pollsbtn fl ml10"%>
<%= link_to l(:button_edit), edit_poll_path(poll.id), :class => "polls_de fr ml20"%> <% end%>
<% end%> </li>
</li> <li>
<li class="polls_date fr"> <% if @is_teacher && poll.polls_status == 1 #新建状态的问卷可删除%>
<%= format_time poll.created_at%> <%= link_to(l(:button_delete), poll,
</li> method: :delete, :confirm => l(:text_are_you_sure), :remote => true, :class => "polls_de fr ml20 mr10") %>
</ul> <% end%>
</li>
<li>
<% if @is_teacher && poll.polls_status == 1 #新建状态的问卷可编辑%>
<%= link_to l(:button_edit), edit_poll_path(poll.id), :class => "polls_de fr ml20"%>
<% end%>
</li>
<li class="polls_date fr">
<%= format_time poll.created_at%>
</li>
</ul>
<% end %>
<div class="cl"></div> <div class="cl"></div>
<% end%> <% end%>

View File

@ -72,7 +72,10 @@
poll_question_id: <%= pq.id %> poll_question_id: <%= pq.id %>
}, },
success: function (data) { success: function (data) {
obj.checked = true; var dataObj = eval(data);
obj.checked = true;
var span = $('#percent');
span.html(dataObj.percent);
} }
}); });
} }
@ -118,7 +121,8 @@
poll_question_id: <%= pq.id %> poll_question_id: <%= pq.id %>
}, },
success: function (data) { success: function (data) {
if(data == "true") var dataObj = eval(data);
if(dataObj.text == "true")
{ {
obj.checked = true; obj.checked = true;
} }
@ -126,6 +130,8 @@
{ {
obj.checked = false; obj.checked = false;
} }
var span = $('#percent');
span.html(dataObj.percent);
} }
}); });
} }
@ -165,6 +171,9 @@
vote_text: obj.value vote_text: obj.value
}, },
success: function (data) { success: function (data) {
var dataObj = eval(data);
var span = $('#percent');
span.html(dataObj.percent);
// obj.value = data; // obj.value = data;
} }
}); });
@ -198,7 +207,17 @@
vote_text: obj.innerHTML vote_text: obj.innerHTML
}, },
success: function (data) { success: function (data) {
// obj.value = data; var dataObj = eval(data);
if(dataObj.text != 'failure')
{
var span = $('#percent');
span.html(dataObj.percent);
}
else
{
alert("error");
}
} }
}); });
} }
@ -217,10 +236,12 @@
</ul> </ul>
<div class="cl"></div> <div class="cl"></div>
<div class="ur_buttons" style="width: 100px;"> <div class="ur_buttons" style="width: 100px;">
<%= link_to "提交",commit_poll_poll_path(@poll), :method => :post,:class => "ur_button",:format => 'js',:remote=>true %> <% if @poll.polls_status == 2 %>
<%= link_to "提交",commit_poll_poll_path(@poll), :method => :post,:class => "ur_button",:format => 'js',:remote=>true %>
<% end %>
</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"><span id="percent"><%= format "%.2f" ,@percent %></span>%</strong> </div>
</div> </div>