educoder/app/controllers/challenges_controller.rb

305 lines
10 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: utf-8
class ChallengesController < ApplicationController
layout "base_shixun"
before_filter :check_authentication
before_filter :find_shixun, :only => [:index, :new, :create, :destroy, :challenge_build, :update_evaluation]
before_filter :find_challenge, :only => [:show, :edit, :update, :challenge_build, :index_up, :index_down, :destroy, :update_evaluation]
before_filter :build_challege_from_params, :only => [:new, :create]
before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :edit, :new, :create]
before_filter :allowed_view, :only => [:show]
before_filter :query_challeges, :only => [:show, :edit, :update, :update_evaluation]
before_filter :find_shixun_language, :only => [:show, :new, :edit]
include ApplicationHelper
def new
# 顶部导航
@st = params[:st].to_i
@project_menu_type = 11
respond_to do |format|
format.html { render :action => 'new', :layout => 'base_shixun' }
end
end
def create
ActiveRecord::Base.transaction do
begin
challenge_count = @shixun.challenges.count
@challenge = Challenge.new(params[:challenge])
@challenge.position = challenge_count + 1
@challenge.shixun = @shixun
@challenge.user = User.current
@challenge.st = params[:st].to_i
if @challenge.save!
if params[:st].to_i != 0
#创建选项
params[:question][:cnt].each_with_index do |test, index|
answer = params[:choice][:answer][index] == "0" ? false : true
ChallengeQuestion.create(:option_name => test, :challenge_id => @challenge.id, :position => index, :right_key => answer)
end
end
end
# 实训是否需要重启
myshixuns = @shixun.myshixuns
unless myshixuns.blank?
myshixuns.each do |myshixun|
shixun_modify = ShixunModify.where(:shixun_id => @shixun.id, :myshixun_id => myshixun.id).first
if shixun_modify.nil?
ShixunModify.create(:shixun_id => @shixun.id, :myshixun_id => myshixun.id, :status => 1)
else
shixun_modify.update_attribute(:status, 1)
end
end
end
redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun)
rescue Exception => e
flash[:error] = "#{e.message}"
redirect_to new_shixun_challenge_path(:shixun_id => @shixun, :st => params[:st].to_i)
raise ActiveRecord::Rollback
end
end
=begin
ActiveRecord::Base.transaction do
begin
@challenge.save!
if params[:test_set][:hidden].length > 0
params[:test_set][:input].each_with_index do |input, index|
params[:test_set][:hidden][index].to_i == 0 ? open = 1 : open = 0
unless (input[index] == params[:test_set][:output][index] && input[index].nil?)
TestSet.create(:challenge_id => @challenge.id, :input => input, :output => params[:test_set][:output][index], :is_public => open, :position => (index + 1))
end
end
end
unless params[:knowledge].blank?
params[:knowledge][:input].each do |input|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
end
end
# 更新shixun公共测试用例
add_shixun_modify_status(@shixun, 1)
respond_to do |format|
format.html {redirect_to shixun_challenge_path(@challenge, :shixun_id => @shixun)}
end
rescue Exception => e
flash[:error] = "#{e.message}"
redirect_to new_shixun_challenge_path(:shixun_id => @shixun)
raise ActiveRecord::Rollback
end
end
=end
end
# 处理新建challenge返回的pipeline片段
def fragment_from_jenkins
end
def index
# 顶部导航
@challenges = @shixun.challenges.order("position desc")
respond_to do |format|
format.js
format.html
end
rescue ActiveRecord::RecordNotFound
render_404
end
def show
challenge_num = Challenge.where(:shixun_id => @shixun).count
challenge_pos = @challenge.position
if challenge_pos < challenge_num
@next_challenge = Challenge.where(:shixun_id => @shixun, :position => challenge_pos + 1).first
end
@prev_challenge = Challenge.where(:shixun_id => @shixun, :position => challenge_pos - 1).first if challenge_pos - 1 > 0
respond_to do |format|
format.html
format.js
end
end
def edit
@st = @challenge.st
challenge_num = Challenge.where(:shixun_id => @shixun).count
challenge_pos = @challenge.position
if challenge_pos < challenge_num
@next_challenge = Challenge.where(:shixun_id => @shixun, :position => challenge_pos+1).first
end
@prev_challenge = Challenge.where(:shixun_id => @shixun, :position => challenge_pos - 1).first if challenge_pos - 1 > 0
@tab = params[:tab].blank? ? 1 : params[:tab].to_i
@editor = params[:editor] # 编辑模式
end
def destroy
@challenge.destroy
respond_to do |format|
format.html{redirect_to shixun_challenges_path(@shixun)}
end
end
def update
ActiveRecord::Base.transaction do
@challenge.update_attributes(params[:challenge])
if (params[:tab] == "1" || params[:tab].nil?) && @challenge.st != 0
begin
@challenge.challenge_questions.delete_all
params[:question][:cnt].each_with_index do |test, index|
answer = params[:choice][:answer][index] == "0" ? false : true
ChallengeQuestion.create(:option_name => test, :challenge_id => @challenge.id, :position => index, :right_key => answer)
end
redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun, :tab => params[:tab])
rescue Exception => e
flash[:error] = "#{e.message}"
format.html{redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun)}
raise ActiveRecord::Rollback
end
else
redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun, :tab => params[:tab])
end
end
end
def update_evaluation
ActiveRecord::Base.transaction do
@challenge.update_attributes(params[:challenge])
begin
if params[:tab].to_i == 2
@test_sets.delete_all unless @test_sets.blank?
if params[:test_set][:hidden].length > 0
params[:test_set][:input].each_with_index do |input, index|
unless (input[index] == params[:test_set][:output][index] && input[index].nil?)
params[:test_set][:hidden][index].to_i == 0 ? open = 1 : open = 0
TestSet.create(:challenge_id => @challenge.id, :input => input, :output => params[:test_set][:output][index], :is_public => open, :position => (index + 1))
end
end
end
# 向jenkins端发送请求构建公共测试用例
add_shixun_modify_status(@shixun, 1)
elsif params[:tab].to_i == 5
@challenge_tags.delete_all unless @challenge_tags.blank?
unless params[:knowledge].blank?
params[:knowledge][:input].each do |input|
ChallengeTag.create(:name => input, :challenge_id => @challenge.id)
end
end
end
redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun, :tab => params[:tab])
rescue Exception => e
flash[:error] = "#{e.message}"
redirect_to edit_shixun_challenge_path(@challenge, :shixun_id => @shixun, :tab => params[:tab])
raise ActiveRecord::Rollback
end
end
end
def index_down
next_challenge = @challenge.next_challenge
position = @challenge.position
@challenge.update_attribute(:position, (position + 1))
next_challenge.update_attribute(:position, next_challenge.position - 1)
@challenges = @shixun.challenges
# 向jenkins端发送请求构建公共测试用例
add_shixun_modify_status(@shixun, 1)
end
def index_up
position = @challenge.position
last_challenge = @challenge.last_challenge
@challenge.update_attribute(:position, (position - 1))
last_challenge.update_attribute(:position, last_challenge.position + 1)
@challenges = @shixun.challenges
# 向jenkins端发送请求构建公共测试用例
add_shixun_modify_status(@shixun, 1)
end
# build job 只负责发送请求
def challenge_build
gitUrl = git_repository_url(@shixun, "Shixun")
gitUrl = Base64.encode64(gitUrl)
taskId = params[:id]
jobName = @shixun.forked_form
step = @challenge.position
jenkins_shixuns = Redmine::Configuration['jenkins_shixuns']
if @challenge.status == 0
params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"}
uri = URI("#{jenkins_shixuns}/jenkins-exec/api/buildJob")
res = uri_exec uri, params
# @challenge.update_attribute(:status, 1)
end
respond_to do |format|
format.js
end
end
private
def count_input input, output
if input.length == 0 && output.length == 0
result = 0
else
if input.length > output.length
end
end
end
def tpi_manager_allowed
unless (User.current.manager_of_shixun?(@shixun) || User.current.admin?)
render_403
end
end
def allowed_view
unless (allow_to_view_challenge(@challenge, @shixun) || User.current.manager_of_shixun?(@shixun))
render_403
end
end
def build_challege_from_params
if params[:id].blank?
@challenge = Challenge.new
@challenge.shixun = @shixun
else
@challenge = @shixun.challenges.find(params[:id])
end
@challenge.shixun = @shixun
@challenge.user ||= User.current
end
def query_challeges
@test_sets = @challenge.test_sets
@challenge_tags = @challenge.challenge_tags
end
def find_challenge
@challenge = Challenge.find(params[:id])
@shixun = @challenge.shixun
rescue ActiveRecord::RecordNotFound
render_404
end
# Find project of id params[:id]
def find_shixun
shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id])
@shixun = Shixun.find_by_identifier(shixun_id)
if @shixun.nil?
render_404
return
else
if (@shixun.status == 0 && !(User.current.manager_of_shixun?(@shixun) || User.current.admin?))
render_403
return
end
end
rescue ActiveRecord::RecordNotFound
render_404
end
def find_shixun_language
language = @shixun.language
@language = language_switch language
end
end