diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb index 1af832b79..fb4c39931 100644 --- a/app/controllers/student_work_controller.rb +++ b/app/controllers/student_work_controller.rb @@ -3,13 +3,43 @@ class StudentWorkController < ApplicationController include StudentWorkHelper require 'bigdecimal' require "base64" - before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list] + before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test] before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work] before_filter :member_of_course, :only => [:index, :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] protect_from_forgery :except => :set_program_score + ### + def program_test + resultObj = {status: 0, results: [], error_msg: ''} + student_work = find_or_save_student_work + unless student_work + resultObj[:status] = 100 + else + if @homework.homework_type == 2 && @homework.homework_detail_programing + result = test_realtime(student_work, params[:src]) + logger.debug result + resultObj[:status] = result["status"] + resultObj[:results] = result["results"] + resultObj[:error_msg] = result["error_msg"] + results = result["results"] + if result["status"].to_i == -2 #编译错误 + results = [result["error_msg"]] + end + student_work.student_work_tests.build(status: result["status"], results: results) + unless student_work.save + resultObj[:status] = 200 + else + resultObj[:status] = result["status"].to_i + end + + end + end + + render :json => resultObj + end + def index #设置作业对应的forge_messages表的viewed字段 query_student_work = @homework.course_messages @@ -102,14 +132,9 @@ class StudentWorkController < ApplicationController end def new - student_work = @homework.student_works.where("user_id = ?",User.current.id).first - if student_work.nil? - @stundet_work = StudentWork.new - respond_to do |format| - format.html - end - else - render_403 + @student_work = @homework.student_works.where("user_id = ?",User.current.id).first + if @student_work.nil? + @student_work = StudentWork.new end end @@ -584,4 +609,44 @@ class StudentWorkController < ApplicationController book.write xls_report xls_report.string end + + def find_or_save_student_work + student_work = @homework.student_works.where(user_id: User.current.id).first + if student_work.nil? + @homework.student_works.build( + name: params[:title], + description: params[:src], + user_id: User.current.id + ) + unless @homework.save + else + student_work = @homework.student_works.where(user_id: User.current.id).first + end + end + student_work + end + + + def test_realtime(student_work, src) + url = "http://192.168.41.130:3000/api/realtime_test.json" + + factor = [] + @homework.homework_tests.each do |test| + factor << {input: test.input, output: test.output} + end + 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 end \ No newline at end of file diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index 217c7d770..1c066495f 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -56,4 +56,6 @@ class HomeworkCommon < ActiveRecord::Base Mailer.run.homework_added(self) end + delegate :language_name, :to => :homework_detail_programing + end diff --git a/app/models/homework_detail_programing.rb b/app/models/homework_detail_programing.rb index 4c7134806..e5938d091 100644 --- a/app/models/homework_detail_programing.rb +++ b/app/models/homework_detail_programing.rb @@ -2,4 +2,8 @@ class HomeworkDetailPrograming < ActiveRecord::Base attr_accessible :language, :standard_code, :homework_common_id belongs_to :homework_common + + def language_name + %W(c c++).at(self.language.to_i - 1) + end end diff --git a/app/models/homework_test.rb b/app/models/homework_test.rb index df2848194..b4d8a4262 100644 --- a/app/models/homework_test.rb +++ b/app/models/homework_test.rb @@ -2,5 +2,4 @@ class HomeworkTest < ActiveRecord::Base attr_accessible :input, :output, :homework_common_id,:result,:error_msg belongs_to :homework_common - has_many :student_work_test end diff --git a/app/models/student_work.rb b/app/models/student_work.rb index b95c11c11..6e39c954a 100644 --- a/app/models/student_work.rb +++ b/app/models/student_work.rb @@ -7,7 +7,7 @@ class StudentWork < ActiveRecord::Base has_many :student_works_evaluation_distributions, :dependent => :destroy has_many :student_works_scores, :dependent => :destroy belongs_to :project - has_many :student_work_test + has_many :student_work_tests, order: 'id desc' before_destroy :delete_praise diff --git a/app/models/student_work_test.rb b/app/models/student_work_test.rb index 413528b82..00484a820 100644 --- a/app/models/student_work_test.rb +++ b/app/models/student_work_test.rb @@ -1,12 +1,12 @@ # encoding: utf-8 class StudentWorkTest < ActiveRecord::Base - attr_accessible :student_work_id, :homework_test_id, :result, :error_msg - - belongs_to :homework_test + attr_accessible :student_work_id, :results, :status belongs_to :student_work + serialize :results, Array + def status_to_s - case self.result.to_i + case self.status.to_i when -1 '编译出错' when -2 @@ -33,10 +33,11 @@ class StudentWorkTest < ActiveRecord::Base end def test_score - if self.result.to_i == 0 + if self.status.to_i == 0 format("%.1f",100.0 / self.student_work.homework_common.homework_tests.count) else 0 end end + end diff --git a/app/views/student_work/new.html.erb b/app/views/student_work/new.html.erb index c72f9d422..a7d3332b3 100644 --- a/app/views/student_work/new.html.erb +++ b/app/views/student_work/new.html.erb @@ -1,3 +1,8 @@ +<% content_for :header_tags do %> + <%= javascript_include_tag 'homework' %> +<% end %> + +<% if @homework.homework_type == 1 %>