From c6361406b5b6b1cf0a83c348d5c7703746dc25b5 Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Fri, 25 Mar 2016 14:43:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=AF=84=E6=B5=8B=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/student_work_controller.rb | 118 +++++++++++++++++- .../_programing_work_show.html.erb | 25 ++-- .../users/new_user_commit_homework.html.erb | 82 ++++++------ app/views/users/user_commit_homework.html.erb | 90 ++++++------- config/routes.rb | 2 +- db/schema.rb | 17 +-- public/javascripts/application.js | 4 +- public/javascripts/homework.js | 66 +++++++++- public/stylesheets/courses.css | 1 + public/stylesheets/public.css | 1 + 10 files changed, 292 insertions(+), 114 deletions(-) diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb index aa77e2d51..dda3291a5 100644 --- a/app/controllers/student_work_controller.rb +++ b/app/controllers/student_work_controller.rb @@ -4,14 +4,14 @@ class StudentWorkController < ApplicationController include ApplicationHelper require 'bigdecimal' require "base64" - before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test,:set_score_rule,:forbidden_anonymous_comment,:delete_work,:new_student_work_project,:student_work_project,:cancel_relate_project,:search_course_students] + before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test,:program_test_ex,:set_score_rule,:forbidden_anonymous_comment,:delete_work,:new_student_work_project,:student_work_project,:cancel_relate_project,:search_course_students] before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work,:retry_work,:revise_attachment] before_filter :member_of_course, :only => [:new, :create, :show, :add_score, :praise_student_work] before_filter :author_of_work, :only => [:edit, :update, :destroy] before_filter :teacher_of_course, :only => [:student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :set_score_rule, :forbidden_anonymous_comment] before_filter :is_logged, :only => [:index] - ### + ### def program_test is_test = params[:is_test] == 'true' resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T')} @@ -48,13 +48,102 @@ class StudentWorkController < ApplicationController resultObj[:time] = student_work_test.created_at.to_s(:db) resultObj[:index] = student_work.student_work_tests.count end - end end render :json => resultObj end + $test_result = {} + $test_status = {} +#根据传入的tIndex确定是第几次测试 + def program_test_ex + is_test = params[:is_test] == 'true' + resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1} #保存每测试一次返回的结果 + + student_work = find_or_save_student_work(is_test) + + resultObj[:tcount] = @homework.homework_tests.size + + unless student_work + resultObj[:status] = 100 + else + if @homework.homework_type == 2 && @homework.homework_detail_programing + #找到第index个测试的输入输出 + index = params[:tIndex].to_i + resultObj[:tseq] = index + test = @homework.homework_tests[index - 1] + + #请求测试 + result = test_realtime_ex(test, params[:src]) + logger.debug result + + #-1 默认值 0全部正确并结束 2 超时 -2 编译错误 + resultObj[:status] = -1 + resultObj[:results] = result["results"][0] #本次测试结果 + resultObj[:error_msg] = result["error_msg"] #编译错误时的信息 + + if result["status"].to_i == -2 #编译错误 + resultObj[:status] = -2 + elsif result["results"][0]["status"].to_i == 2 + resultObj[:status] = 2 + end + + unless student_work.save + resultObj[:status] = 200 + else + + #索引 + work_id = student_work.id + + #测试第一个时初始化下全局变量 + if index == 1 + $test_result[work_id] = [] #保存本次测试的结果 输入输出 + $test_status[work_id] = 0 #保存本次测试的结果 正确还是错误 + end + + if result["status"].to_i == -2 + $test_result[work_id] = [result["error_msg"]] + $test_status[work_id] = -2 + else + #存下每次的结果 只有每次都为0才全部正确 + $test_status[work_id] = result["status"] != 0 ? result["status"]:$test_status[work_id] + $test_result[work_id][index - 1] = resultObj[:results] + end + + student_work.name = params[:title] + student_work.description = params[:src] + + if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") + student_work.late_penalty = @homework.late_penalty + else + student_work.late_penalty = 0 + end + + #超时或编译错误则直接返回了并存入数据库 + if resultObj[:status] == 2 || resultObj[:status] == -2 || index == @homework.homework_tests.size + if $test_status[work_id] == 0 + resultObj[:status] = 0 + end + + student_work_test = student_work.student_work_tests.build(status: $test_status[work_id], + results: $test_result[work_id],src: params[:src]) + student_work.save + resultObj[:time] = student_work_test.created_at.to_s(:db) + resultObj[:index] = student_work.student_work_tests.count + + $test_result[work_id] = nil + $test_status[work_id] = nil + end + + #渲染返回结果 + render :json => resultObj + end + end + end + end + + def index # 作业消息状态更新 @homework.course_messages.each do |homework_message| @@ -986,7 +1075,6 @@ class StudentWorkController < ApplicationController student_work end - def test_realtime(student_work, src) url = "#{Redmine::Configuration['judge_server']}api/realtime_test.json" @@ -1010,6 +1098,28 @@ class StudentWorkController < ApplicationController JSON.parse(res.body) end + def test_realtime_ex(test, src) + url = "#{Redmine::Configuration['judge_server']}api/realtime_test.json" + + factor = [] + factor << {input: test.input, output: test.output} + + solutions = { + src:src, + language:@homework.homework_detail_programing.language, + factor: factor + } + uri = URI(url) + body = solutions.to_json + res = Net::HTTP.new(uri.host, uri.port).start do |client| + request = Net::HTTP::Post.new(uri.path) + request.body = body + request["Content-Type"] = "application/json" + client.request(request) + end + JSON.parse(res.body) + end + #成绩计算 def set_final_score homework,student_work if homework && homework.homework_detail_manual diff --git a/app/views/student_work/_programing_work_show.html.erb b/app/views/student_work/_programing_work_show.html.erb index a343ae97a..241aa0c4c 100644 --- a/app/views/student_work/_programing_work_show.html.erb +++ b/app/views/student_work/_programing_work_show.html.erb @@ -53,27 +53,28 @@ <% else %>
<%=x["time_used"]%>微秒您的输出: -
<%=x["result"]%>+
<%=x["result"]%>正确输出: -
<%=x["output"]%>+
<%=x["output"]%>+ <% if x["status"].to_i == 2 %> + 耗时: +
<%=x["time_used"]%>毫秒+ <% end %> <% else %> - 测试正确! - 耗时: -
<%=x["time_used"]%>微秒+ 测试正确! + + <% end %>
<%=x["time_used"]%>微秒您的输出: -
<%=x["result"]%>+
<%=x["result"]%>正确输出: -
<%= x["output"] %>+
<%= x["output"] %>+ <% if x["status"].to_i == 2 %> + 耗时: +
<%=x["time_used"]%>毫秒+ <% end %>
<%=x["time_used"]%>微秒+ 测试正确! + + <% end %> diff --git a/app/views/users/user_commit_homework.html.erb b/app/views/users/user_commit_homework.html.erb index 90b22ad14..40fd9d241 100644 --- a/app/views/users/user_commit_homework.html.erb +++ b/app/views/users/user_commit_homework.html.erb @@ -2,44 +2,46 @@ @@ -98,26 +100,28 @@ <% else %>
<%=x["time_used"]%>微秒您的输出: -
<%=x["result"]%>+
<%=x["result"]%>正确输出: -
<%=x["output"]%>+
<%=x["output"]%>+ <% if x["status"].to_i == 2 %> + 耗时: +
<%=x["time_used"]%>毫秒+ <% end %>
<%=x["time_used"]%>微秒+ 测试正确! + + <% end %> diff --git a/config/routes.rb b/config/routes.rb index bde12fa59..ee79bdb2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -272,7 +272,7 @@ RedmineApp::Application.routes.draw do get 'absence_penalty_list' get 'evaluation_list' # post 'set_program_score' - post 'program_test' + post 'program_test_ex' post 'set_score_rule' end end diff --git a/db/schema.rb b/db/schema.rb index 2dad292f4..d738b90c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160225031230) do +ActiveRecord::Schema.define(:version => 20160311072819) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -432,9 +432,11 @@ ActiveRecord::Schema.define(:version => 20160225031230) do t.integer "resource_num" t.integer "journal_num" t.integer "journal_reply_num" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "total_score" + t.integer "homework_journal_num", :default => 0 + t.integer "news_num", :default => 0 end create_table "course_groups", :force => true do |t| @@ -506,6 +508,7 @@ ActiveRecord::Schema.define(:version => 20160225031230) do t.integer "is_excellent", :default => 0 t.integer "excellent_option", :default => 0 t.integer "is_copy", :default => 0 + t.integer "visits", :default => 0 end create_table "custom_fields", :force => true do |t| @@ -1282,6 +1285,7 @@ ActiveRecord::Schema.define(:version => 20160225031230) do t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.boolean "allow_guest_download", :default => true + t.integer "visits", :default => 0 end create_table "phone_app_versions", :force => true do |t| @@ -1441,6 +1445,7 @@ ActiveRecord::Schema.define(:version => 20160225031230) do t.integer "acts_count", :default => 0 t.integer "journals_count", :default => 0 t.integer "boards_reply_count", :default => 0 + t.integer "visits", :default => 0 end add_index "projects", ["lft"], :name => "index_projects_on_lft" @@ -1900,6 +1905,7 @@ ActiveRecord::Schema.define(:version => 20160225031230) do t.string "mail_notification", :default => "", :null => false t.string "salt", :limit => 64 t.integer "gid" + t.integer "visits", :default => 0 end add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id" @@ -2013,11 +2019,6 @@ ActiveRecord::Schema.define(:version => 20160225031230) do add_index "wikis", ["project_id"], :name => "wikis_project_id" - create_table "wlcs", :force => true do |t| - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - create_table "workflows", :force => true do |t| t.integer "tracker_id", :default => 0, :null => false t.integer "old_status_id", :default => 0, :null => false diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 55d3eefad..cd6ba1cdc 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1018,6 +1018,7 @@ function showNormalImage(id) { $(image).attr('src',_src); return; } + var element=$("").attr("href",image.attr('src')); image.wrap(element); $(image).parent().colorbox({rel:'nofollow', close: "关闭", returnFocus: false}); @@ -1173,5 +1174,4 @@ function getRootPath(){ // var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); var projectName=""; return(localhostPaht+projectName); -} - +} \ No newline at end of file diff --git a/public/javascripts/homework.js b/public/javascripts/homework.js index 7dd416dec..ca120df2f 100644 --- a/public/javascripts/homework.js +++ b/public/javascripts/homework.js @@ -31,9 +31,9 @@ $(function(){ if(!valid_form()){ return; } - + /* $.post( - '/student_work/program_test', + '/student_work/program_test_ex', {homework: homework_id, student_work_id: student_work_id, src: src, title: title, is_test: is_test}, function(data,status){ tested = true; @@ -44,7 +44,6 @@ $(function(){ if (typeof cb == 'function') {cb(data); return;} - var html=bt('t:result-list',data); $('.ProResult').prepend(html); @@ -56,6 +55,47 @@ $(function(){ } } ); + */ + //先测试一次并返回测试集个数及结果再判断是否需要继续进行测试 + var test_post = function(i){ + $.post( + '/student_work/program_test_ex', + {homework: homework_id, student_work_id: student_work_id, src: src, title: title, is_test: is_test,tIndex:i}, + function(data,status){ + var tSeq = data.tseq; + var tCount = data.tcount; + console.log("tSeq="+tSeq); + console.log("tCount="+tCount); + tested = true; + console.log(data); + if(data.index <=0){ + data.index = $('.ProResultTop').length+1; + } + + var html=bt('t:result-list',data); + $('.ProResult').prepend(html); + + if (data.status==0 && is_test != 'true') { + if (typeof cb == 'function') {cb(data); return;} + var r=confirm("答题正确,是否立刻提交?"); + if (r) { + $(".HomeWorkCon form").submit(); + } + return; + } + + //2 超时 -2 编译错误 测试结束 + if (data.status == 2 || data.status == -2 || tSeq >= tCount ){ + if (typeof cb == 'function') {cb(data); return;} + return; + } + + test_post(i+1); + } + ); + }; + + test_post(1); }; $('#test-program-btn').on('click', test_program); @@ -373,6 +413,10 @@ $(function(){ var src = ''; if(language==4){ src = '\ +//注意\n\ + //1:该程序每次运行的时间必须小于200毫秒,否则会超时,程序超时则接下来的测试集都不会测试了\n\ + //2:该程序每次运行使用的内存不能超过3200字节,否则会返回错误\n\ + //3:该程序每次运行输出的结果不能超过3200个字符(最多显示100个字符),否则会返回错误\n\ import java.io.*;\n\ import java.util.*;\n\ \n\ @@ -392,8 +436,12 @@ class Main\n\ '; } else if(language==1){ - src = '#include