TPI单选多选题

This commit is contained in:
daiao 2017-09-14 17:30:58 +08:00
parent 7bb99cee44
commit 2c7e5d1f38
8 changed files with 134 additions and 122 deletions

View File

@ -296,55 +296,41 @@ class GamesController < ApplicationController
end
# 选择题评测(选择题不需要重新评测)
# score 获得金币数 tag_count 技能数 right用户是否全对 answer_right全部选择题的正确性
def evaluating_choice
if false
@game_challenge = @game.challenge
user_answer = params[:answer]
correct = true
@challenges = Challenge.where(:shixun_id => @myshixun.shixun_id)
@shixun = @myshixun.shixun
score = 0
tag_count = 0
if !user_answer.blank?
if user_answer.split("").count != @game_challenge.challenge_questions.where(:right_key => true).count
correct = false
else
user_answer.split("").each do |answer|
question = @game_challenge.challenge_questions.where(:position => answer.to_i).first
unless question.right_key
correct = false
break
@right = true
answer_right = []
@game_challenge.challenge_chooses.each_with_index do |choose, index|
correct = (params[:answer][index] == choose.standard_answer) ? true : false
if choose.choose_outputs.blank?
ChooseOutputs.create(:challenge_choose_id => choose.id, :user_id => User.current.id, :answer => params[:answer][index], :correct => correct)
end
if @shixun.status > 1 && !@game.answer_open
if correct
score += choose.score
tag_count += choose.challenge_tags.count
end
elsif @shixun.status > 1 && @game.answer_open
score -= choose.score
end
else
correct = false
unless correct
@right = false
end
answer_right << correct
end
@game.update_attributes(:status => 2, :end_time => Time.now)
had_done = @game.had_done
shixun = @myshixun.shixun
if shixun.status > 1 && !@game.answer_open
if correct
#User.current.update_attributes(:grade => (User.current.grade + @game.challenge.score), :experience => (User.current.experience + @game.challenge.score))
reward_grade(@game.user, @game.id, 'Game', @game_challenge.score)
reward_experience(@game.user, @game.id, 'Game', @game_challenge.score)
@game.update_attribute(:final_score, @game_challenge.score)
score = @game_challenge.score
tag_count = @game_challenge.challenge_tags.count
else
score = "+0"
tag_count = 0
end
elsif shixun.status > 1 && @game.answer_open
score = -@game_challenge.score.to_i
tag_count = 0
else
score = "+0"
tag_count = 0
end
if @game.outputs.blank?
Output.create(:game_id => @game.id, :actual_output => user_answer, :result => correct)
end
render :json => {correct: correct, had_done: had_done, :grade => User.where(:id => User.current.id).first.grade, score: score, tag_count: tag_count}
@had_done = @game.had_done
reward_grade(@game.user, @game.id, 'Game', score)
reward_experience(@game.user, @game.id, 'Game', score)
@game.update_attribute(:final_score, score)
respond_to do |format|
format.js{redirect_to myshixun_game_path(@game, :myshixun_id => @myshixun, :choose => 1)}
end
end

View File

@ -28,6 +28,27 @@ class Challenge < ActiveRecord::Base
str
end
def choose_correct_num
num = 0
self.challenge_chooses.each do |choose|
outputs = ChooseOutputs.where(:challenge_choose_id => choose.id).first
if outputs.nil?
num = nil
else
num += (outputs.correct ? 1 : 0)
end
end
return num
end
def choose_score
score = 0
self.challenge_chooses.each do |choose|
score += choose.score
end
return score
end
def next_challenge
challenge_count = Challenge.where(:shixun_id => self.shixun_id).count
render_404 if self.position ==challenge_count

View File

@ -22,10 +22,10 @@
<p class="ml10 mb10 ">
<span>
<% if choose.category == 1 %>
<input type="radio" name="answer[]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-radio ml5">
<input type="radio" <%= (choose.choose_outputs.try(:answer) == (i + 65).chr) ? "checked" : "" %> name="answer[<%= index + 1 %>]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-radio ml5">
<% else %>
<!--多选-->
<input type="checkbox" name="answer[]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-checkbox ml5">
<input type="checkbox" <%= (!choose.choose_outputs.try(:answer).nil? && (choose.choose_outputs.try(:answer).include? ((i + 65).chr))) ? "checked" : "" %> name="answer[]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-checkbox ml5">
<% end %>
<label for="result_<%= index %>_<%= i %>" style="top: 0px"><span><%= (question.position + 65).chr %></span>.<%= question.option_name %></label>
</span>
@ -61,7 +61,7 @@
</div>
<!------------------------------------------------------>
<div class="split-panel--second -layout -vertical -flex -relative -bg-black -flex-basic60" id="games_valuation_contents">
<%= render :partial => 'games/game_choose_results' %>
<%= render :partial => 'games/game_choose_results', :locals => { :game_challenge => @game_challenge} %>
</div>
</div>
@ -129,7 +129,6 @@
<input type="radio" name="answer[]" value="<%= question.position %>" id="result_<%= question.position %>" class="ml-3 mr5 magic-radio ml5">
<!--多选-->
<input type="checkbox" name="answer[]" value="<%= question.position %>" id="result_<%= question.position %>" class="ml-3 mr5 magic-checkbox ml5">
<input type="hidden" name="answer" value="<%= question.position %>">
<div class="-layout-h -center break_word">
<span class="mr10"><%= (question.position + 65).chr %>.</span>
<div class="markdown">

View File

@ -84,57 +84,25 @@
// 选择题提交评测
// answer 选择的答案
function choice_submmit(){
var answer = "";
var nBtn = $(".card-check");
var choice = nBtn.find("input[name='answer']");
for(var i = 0; i < choice.length; i++){
answer += $(choice[i]).val();
}
var user_answer = [];
var input_answer = $("input[name='user_answer']");
var lens = input_answer.length;
for(i = 0; i < lens; i++ ){
var answer = $(input_answer[i]).val();
if(answer == ""){
notice_sure_box("答案不能为空");
notice_sure_box("第"+ (i+1) + "题未答,请确认");
return;
}
user_answer.push(answer)
}
console.log(user_answer);
$("#code_test").html("<a class='task-btn mt8 mr15'>评测中..</a>");
$.ajax({
type: "post",
type: "POST",
url: '<%= evaluating_choice_myshixun_game_path(@game, :myshixun_id => @myshixun) %>',
data: {answer: answer},
dataType: "json",
data: {answer:user_answer},
dataType: "script",
success: function(data){
console.log(data);
clearInterval(cm);
var icon = $("#game_status_<%= @game_challenge.id %>"); // 实训列表的icon
icon.find("i").attr("class", "fa fa-unlock fr font-18 mt5 color-light-green w20_center");
icon.find("a:last").attr("title", "已完成");
$("#code_test").remove();
if(data.correct){
$("#correct_tip").show();
$("#user_grade").html(data.grade);
$("#shixun_exp_<%=@game_challenge.id %>").html("经验值<span class='color-light-green mr5'>+"+ data.score + "</span>");
$("#shixun_grade_<%=@game_challenge.id %>").html("金币<span class='color-light-green mr5'>+"+ data.score + "</span>");
$("#shixun_tag_<%=@game_challenge.id %>").html("技能标签<span class='color-light-green mr5'>+"+ data.tag_count + "</span>");
if( data.had_done == 0 ) {
$("#code_estimate").html("<a href='<%= next_step_myshixun_game_path(@game, :myshixun_id => @myshixun) %>' class='task-btn task-btn-blue mr15 mt8' id='next_step' data-remote='true'>下一关</a>");
var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game=> @game, :myshixun => @myshixun, :had_done => 0}) %>";
}else{
$("#code_estimate").html("<a href='<%= shixun_path(@myshixun.shixun) %>' class='task-btn task-btn-blue mt8 mr15' >退出实训</a>");
var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game => @game, :myshixun => @myshixun, :had_done => 1}) %>";
}
// 传递具体屏幕的宽高,实现不同浏览器与不同分辨率都能撑满屏幕
pop_box_new2(htmlvalue, window.innerWidth, window.innerHeight);
}else{
$("#error_tip").show();
var span_class = parseInt(data.score) < 0 ? "u-color-light-red" : "color-light-green";
$("#shixun_exp_<%=@game_challenge.id %>").html("经验值<span class='color-light-green mr5'>+0</span>");
$("#shixun_grade_<%=@game_challenge.id %>").html("金币<span class='"+ span_class +" mr5'>"+ data.score + "</span>");
$("#shixun_tag_<%=@game_challenge.id %>").html("技能标签<span class='color-light-green mr5'>+0</span>");
if(0 == data.had_done){
$("#code_estimate").html("<a href='<%= next_step_myshixun_game_path(@game, :myshixun_id => @myshixun) %>' class='task-btn task-btn-blue mr15 mt8' id='next_step' data-remote='true'>下一关</a>");
}else if(1 == data.had_done ){
$("#code_estimate").html("<a href='<%= shixun_path(@myshixun.shixun) %>' class='task-btn task-btn-blue mt8 mr15' >退出实训</a>");
}
}
},
error: function(){
console.log("错了");

View File

@ -1,6 +1,3 @@
<%= content_for(:header_tags) do %>
<%= javascript_include_tag 'baiduTemplate', 'jquery.datetimepicker.js' %>
<% end %>
<ul id="blacktab_nav">
<li class="blacktab_con blacktab_hover">
<a href="javascript:void(0);" class="tab_type tab_color">测试结果</a>
@ -14,25 +11,41 @@
<div class="fit -scroll">
<div class="-layout-v -fit">
<div class="-flex -scroll task-padding16">
<% unless game_challenge.choose_correct_num == nil %>
<% if game_challenge.choose_correct_num != game_challenge.challenge_chooses.count %>
<p class="-text-danger mb10">
<i class="fa fa-exclamation-circle font-16" ></i><span class="ml5 mr5 -text-danger">3/5</span>
<i class="fa fa-exclamation-circle font-16" ></i><span class="ml5 mr5 -text-danger"><%= game_challenge.choose_correct_num %>/<%= game_challenge.challenge_chooses.count %></span>错误
</p>
<% else %>
<p class="color-light-green mb10">
<i class="fa fa-check-circle font-16" ></i><span class="ml5 mr5">5/5</span> 全部通过
<i class="fa fa-check-circle font-16" ></i><span class="ml5 mr5"><%= game_challenge.challenge_chooses.count %>/<%= game_challenge.challenge_chooses.count %></span> 全部通过
</p>
<% @game_challenge.challenge_chooses.each_with_index do |choose, index| %>
<% end %>
<% end %>
<% game_challenge.challenge_chooses.each_with_index do |choose, index| %>
<div class="-task-ces-box mb15 clearfix">
<div class="-task-ces-top" onclick="toggle_test_case('0', '<%= index %>')" style="cursor:pointer">
<i class="fa fa-caret-right mr5 font-16" ></i>
<span class="font-14">题目<%= index + 1 %></span>
<%# outputs = ChooseOutputs.where(:challenge_choose_id => choose.id).first %>
<% unless choose.choose_outputs.blank? %>
<% if choose.choose_outputs.try(:answer) != choose.standard_answer %>
<i class="fa fa-exclamation-circle -text-danger fr mt8 ml5" ></i>
<% else %>
<i class="fa fa-check-circle color-light-green fr mt8 ml5 f14" ></i>
<% end %>
<% else %>
<i class="fa fa-lock fr mt8" ></i>
<% end %>
</div>
<div class="-task-ces-info undis" id="test_case_<%= index %>">
<ul class=" font-14">
<% if choose.choose_outputs.blank? %>
<li><span class="ml30">尚未提交,暂不支持查看</span></li>
<% else %>
<li><span class="-task-ces-info-left color-blue">正确选项:</span><span><%= choose.standard_answer %></span></li>
<li><span class="-task-ces-info-left color-blue">你的选项:</span><span class="color-orange">789</span></li>
<li><span class="-task-ces-info-left color-blue">你的选项:</span><span class="color-orange"><%= choose.choose_outputs.try(:answer).nil? ? "无" : choose.choose_outputs.try(:answer) %></span></li>
<% end %>
</ul>
</div>
</div>

View File

@ -27,7 +27,12 @@
<li id="tab_nav_4">
<%= link_to '评论', shixun_discuss_shixun_path(@shixun), :class => "tab_type", :style => "font-size: 16px", :remote => true %>
</li>
<% if @st == 0 %>
<span class="btn-cir-big fr mt8 mr15" >经验值:<%= @game_challenge.score %></span>
<% else %>
<span class="btn-cir-big fr mt8 mr15" >经验值:<%= @game_challenge.choose_score %></span>
<% end %>
</ul>
<div class="cl"></div>
<div class="-flex -relative greytab-inner" >

View File

@ -10,6 +10,18 @@
$("#all_task_show").html("<%= j (render :partial => "games_list") %>");
$("#all_task_show").hide();
fadein = 0;
// 选择题
<% if params[:choose] == "1" %>
<% if @had_done == 0 %>
$("#code_estimate").html("<a href='<%= next_step_myshixun_game_path(@game, :myshixun_id => @myshixun) %>' class='task-btn task-btn-blue mr15 mt8' id='next_step' data-remote='true'>下一关</a>");
var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game=> @game, :myshixun => @myshixun, :had_done => 0}) %>";
<% else %>
$("#code_estimate").html("<a href='<%= shixun_path(@myshixun.shixun) %>' class='task-btn task-btn-blue mt8 mr15' >退出实训</a>");
var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game => @game, :myshixun => @myshixun, :had_done => 1}) %>";
<% end %>
// 传递具体屏幕的宽高,实现不同浏览器与不同分辨率都能撑满屏幕
pop_box_new2(htmlvalue, window.innerWidth, window.innerHeight);
<% end %>
<% else %>
$("#all_task_show").html("<%= j (render :partial => "games_list") %>");
$("#all_task_tab").trigger("click"); // 模拟全部任务点击事件

View File

@ -0,0 +1,8 @@
class ModifyCorrectTypeForChooseOutputs < ActiveRecord::Migration
def up
change_column :choose_outputs, :correct, :boolean
end
def down
end
end