From 076c241d109465b12e8e8ae5116f553a43354e79 Mon Sep 17 00:00:00 2001 From: cxt Date: Sat, 1 Apr 2017 10:55:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=AE=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/challenges_controller.rb | 69 +- app/controllers/games_controller.rb | 73 +- app/controllers/shixuns_controller.rb | 84 +- app/controllers/users_controller.rb | 11 +- app/helpers/application_helper.rb | 58 +- app/models/challenge.rb | 1 + app/models/game.rb | 9 +- app/models/myshixun.rb | 4 +- app/models/shixun.rb | 20 +- app/views/admin/users.html.erb | 3 +- app/views/challenges/_content_list.html.erb | 23 +- app/views/challenges/_form.html.erb | 140 +- app/views/challenges/show.html.erb | 38 +- app/views/games/_desc_full_show.html.erb | 73 +- app/views/games/_exec_results.html.erb | 113 +- .../games/_extend_file_edit_form.html.erb | 13 +- app/views/games/_extend_test_output.html.erb | 43 + app/views/games/_file_edit_form.html.erb | 59 +- app/views/games/_game_show.html.erb | 105 +- app/views/games/_games_list.html.erb | 43 +- .../games/_myshixun_breadcrumbs.html.erb | 21 +- .../_myshixun_extend_repository.html.erb | 27 +- app/views/games/_myshixun_repository.html.erb | 45 +- app/views/games/_pass_game_show.html.erb | 15 + app/views/games/_repository.html.erb | 53 +- app/views/games/entry.html.erb | 14 - app/views/games/entry.js.erb | 9 +- app/views/games/file_edit.js.erb | 12 +- app/views/games/index.js.erb | 3 +- app/views/games/outputs_show.js.erb | 9 +- app/views/games/show.html.erb | 66 +- app/views/games/show.js.erb | 14 +- app/views/layouts/_logined_header.html.erb | 3 + app/views/layouts/base_myshixun.html.erb | 61 +- app/views/layouts/base_shixun.html.erb | 1 + app/views/myshixuns/_myshixun_top.html.erb | 8 +- app/views/shixuns/_form.html.erb | 4 + app/views/shixuns/_settings_edit.html.erb | 4 + .../shixuns/_shixun_breadcrumbs.html.erb | 6 - app/views/users/user_shixuns.html.erb | 5 + config/routes.rb | 6 +- ...170328064900_rename_parent_id_in_shixun.rb | 8 + db/migrate/20170328065349_modify_shixun.rb | 10 + db/migrate/20170328065735_modify_myshixun.rb | 8 + db/migrate/20170328081152_modify_games.rb | 15 + ...20170328082148_add_challenge_id_to_game.rb | 5 + ...0170330084904_change_type_to_chanllenge.rb | 9 + .../20170331013652_add_language_to_shixuns.rb | 5 + db/schema.rb | 73 +- lib/redmine/scm/adapters/gitlab_adapter.rb | 2 +- public/assets/codemirror/codemirror.js | 2 +- public/images/task/icons-flower.png | Bin 0 -> 36120 bytes public/images/task/nodata.jpg | Bin 0 -> 35857 bytes public/images/task/task-close.png | Bin 0 -> 16051 bytes public/images/task/task-su-btn.png | Bin 0 -> 27799 bytes public/images/task/task-success.png | Bin 0 -> 51091 bytes public/images/task/task-success02.png | Bin 0 -> 74902 bytes public/javascripts/application.js | 2490 ----------------- public/stylesheets/css/bigdata-popup.css | 15 +- public/stylesheets/css/common.css | 2 +- .../stylesheets/css/cssPlus-v.0.2-build.css | 1 + public/stylesheets/css/taskstyle.css | 49 +- 62 files changed, 758 insertions(+), 3304 deletions(-) create mode 100644 app/views/games/_extend_test_output.html.erb create mode 100644 app/views/games/_pass_game_show.html.erb delete mode 100644 app/views/games/entry.html.erb create mode 100644 app/views/users/user_shixuns.html.erb create mode 100644 db/migrate/20170328064900_rename_parent_id_in_shixun.rb create mode 100644 db/migrate/20170328065349_modify_shixun.rb create mode 100644 db/migrate/20170328065735_modify_myshixun.rb create mode 100644 db/migrate/20170328081152_modify_games.rb create mode 100644 db/migrate/20170328082148_add_challenge_id_to_game.rb create mode 100644 db/migrate/20170330084904_change_type_to_chanllenge.rb create mode 100644 db/migrate/20170331013652_add_language_to_shixuns.rb create mode 100644 public/images/task/icons-flower.png create mode 100644 public/images/task/nodata.jpg create mode 100644 public/images/task/task-close.png create mode 100644 public/images/task/task-su-btn.png create mode 100644 public/images/task/task-success.png create mode 100644 public/images/task/task-success02.png delete mode 100644 public/javascripts/application.js create mode 100644 public/stylesheets/css/cssPlus-v.0.2-build.css diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index d3b236a01..5a5b50179 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -3,18 +3,14 @@ class ChallengesController < ApplicationController layout "base_shixun" before_filter :find_shixun, :only => [:index, :new, :create, :destroy, :challenge_build] before_filter :find_challenge, :only => [:show, :edit, :update, :challenge_build, :index_up, :index_down, :destroy] - before_filter :authorize_tpi, :only => [:new, :create] before_filter :build_challege_from_params, :only => [:new, :create] - before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :show, :edit, :new] + before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :show, :edit, :new, :create] before_filter :query_challeges, :only => [:show, :edit, :update] - # before_filter :is_members, :only => [:show, :edit] + before_filter :find_shixun_language, :only => [:show, :new, :edit] include ApplicationHelper def new - unless @shixun.parent_id.nil? - return render_403 - end # 顶部导航 @project_menu_type = 11 respond_to do |format| @@ -91,9 +87,9 @@ class ChallengesController < ApplicationController respond_to do |format| if @challenge.update_attributes(params[:challenge]) ActiveRecord::Base.transaction do - @challenge_samples.destroy unless @challenge_samples.blank? - @test_sets.destroy unless @test_sets.blank? - @challenge_tags.destroy unless @challenge_tags.blank? + @challenge_samples.delete_all unless @challenge_samples.blank? + @test_sets.delete_all unless @test_sets.blank? + @challenge_tags.delete_all unless @challenge_tags.blank? if params[:sample][:input].length > 0 params[:sample][:input].each_with_index do |value, index| unless (value == params[:sample][:output][index] && value.blank?) @@ -125,7 +121,6 @@ class ChallengesController < ApplicationController def index_down - # @challenges = @shixun.challenges next_challenge = @challenge.next_challenge position = @challenge.position @challenge.update_attribute(:position, (position + 1)) @@ -160,48 +155,7 @@ class ChallengesController < ApplicationController end end - def game_build_result - # status = params[:status].to_i - # task_id = params[:taskId] - # message = Base64.decode64(params[:msg]) unless params[:msg].blank? - # begin - # @training_task = TrainingTask.find(task_id) - # # 如果已经执行成功过,则不重复执行 - # return if @training_task.status == 1 - # original_project_id = Project.find(@training_task.project_id).try(:forked_from_project_id) - # original_project = Project.find(original_project_id) - # rescue - # return - # end - # original_tasks_count = original_project.training_tasks.count - # position = @training_task.try(:position) - # # 测试,默认成功 - # if status == 0 - # ActiveRecord::Base.transaction do - # if position < original_tasks_count - # # 继续发布下一个任务 - # publish_training_tasks original_project_id, @training_task.project_id, position + 1, original_project.user_id - # end - # @training_task.update_attribute(:status, 1) - # @training_task.update_attribute(:result, 1) - # # 创建一条回复提醒 - # content = (position == original_tasks_count) ? "恭喜您,您已经完成了实训项目的所有任务" : "恭喜您,您已经完成了本任务,请继续下一任务" - # add_training_task_journal(content, original_project.user_id) - # end - # else - # content = "很抱歉,您的任务未通过,请继续加油,错误信息如下:#{message}" - # add_training_task_journal(content, original_project.user_id) - # # 失败的时候可以继续提交 - # @training_task.update_attribute(:status, 0) - # @training_task.update_attribute(:result, 2) - # end - end - private - def is_members - render_403 unless User.current.manager_of_shixun?(@shixun) - end - def count_input input, output if input.length == 0 && output.length == 0 result = 0 @@ -213,7 +167,7 @@ class ChallengesController < ApplicationController end def tpi_manager_allowed - unless (User.current.manager_of_shixun?(@shixun)) + unless (User.current.manager_of_shixun?(@shixun) || User.current.admin?) render_403 end end @@ -243,12 +197,6 @@ class ChallengesController < ApplicationController render_404 end - # TPM:parent_id is nil - # TPI is not allowed - def authorize_tpi - render_403 if @shixun.tpi? - end - # Find project of id params[:id] def find_shixun shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id]) @@ -257,4 +205,9 @@ class ChallengesController < ApplicationController render_404 end + def find_shixun_language + language = @shixun.language + @language = language_switch language + end + end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 10c9a66cc..eb7bd1c60 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,3 +1,4 @@ +#coding=utf-8 class GamesController < ApplicationController layout "base_myshixun" skip_before_filter :verify_authenticity_token, :only => [:file_update] @@ -5,10 +6,12 @@ class GamesController < ApplicationController before_filter :find_game, :only => [:show, :game_build, :entry,:next_step, :outputs_show, :file_edit, :file_update, :game_status, :change_status] before_filter :find_repository, :only => [:show, :entry, :file_edit, :file_update] before_filter :allowd_manager + before_filter :allowd_view, :only => [:show] + before_filter :find__shixun_language, :only => [:show, :entry] include ApplicationHelper def index - @games = @myshixun.games + @games = @myshixun.games.includes(:challenge) @games_count = @games.count respond_to do |format| format.html @@ -20,7 +23,8 @@ class GamesController < ApplicationController # 首次进入版本库自动打开文件 # path:"" && path: @game.path def show - game_path = (@game.path.blank? ? @game.path : @game.path.strip) + @game_challenge = @game.challenge + game_path = (@game_challenge.path.blank? ? @path : @game_challenge.path.strip) @rev = @rev.nil? ? "master" : @rev @git_url = git_repository_url(@myshixun, "Myshixun") @type = params[:type] @@ -32,9 +36,10 @@ class GamesController < ApplicationController else @entries = @repository.entries(@path, @rev) end + @latest_output = @game.latest_output.try(:out_put) outputs = @game.outputs - @had_done = 1 if (@myshixun.games.count == @game.stage && @game.status ==2) + @had_done = 1 if (@myshixun.games.count == @game_challenge.position && @game.status ==2) if outputs.count == 0 @results = [] else @@ -58,15 +63,16 @@ class GamesController < ApplicationController end def file_edit - entry_and_raw(false) - @content = @repository.cat(@path, @rev).rstrip - # respond_to do |format| - # format.js + # entry_and_raw(false) + # @content = @repository.cat(@path, @rev).strip + # # respond_to do |format| + # # format.js # end end def file_update @g = Gitlab.client + content = params[:content].strip @g.edit_file(@myshixun.gpid, :content => params[:content], :file_path => @path, :branch_name => @rev, :commit_message => "shixun exec") respond_to do |format| format.js{redirect_to entry_myshixun_game_path(@game, :myshixun_id => @myshixun, :path => @path)} @@ -86,36 +92,45 @@ class GamesController < ApplicationController # status 0: 未提交测评或者提交测评失败后报错;1:中间状态还没返回值;2:返回值并成功 def game_build + game_challenge = @game.challenge gitUrl = git_repository_url(@myshixun, "Myshixun") gitUrl = Base64.encode64(gitUrl) taskId = params[:id] jobName = "myshixun_#{@myshixun.id}" - @game.update_attribute(:status, 1) - testCode = {} - test_sets = @game.test_sets - unless test_sets.blank? - test_sets.each_with_index do |test_set, index| - testCode.store("testCode_#{index}",test_set.try(:output)) + ActiveRecord::Base.transaction do + @game.update_attributes!(:status => 1) + testCode = {} + test_sets = game_challenge.test_sets + unless test_sets.blank? + test_sets.each_with_index do |test_set, index| + testCode.store("testCode_#{index}",test_set.try(:output)) + end end + testCode = testCode.to_json + jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] + step = game_challenge.try(:position) + params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}", :testCode => "#{testCode}"} + uri = URI("#{jenkins_shixuns}/jenkins-exec/api/buildJob") + res = uri_exec uri, params + if (res && res['code'] != 0) + raise("Build job failed") + end + render :json => {data:"success"} end - testCode = testCode.to_json - jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] - step = @game.stage - params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}", :testCode => "#{testCode}"} - uri = URI("#{jenkins_shixuns}/jenkins-exec/api/buildJob") - res = uri_exec uri, params - render :json => {data:"success"} - + rescue + raise ActiveRecord::Rollback + redirect_to myshixun_game_path(@game, :myshixun_id => @myshixun) end def game_status outputs = @game.outputs + game_challenge = @game.challenge if outputs.count == 0 outputs = "" else outputs = outputs.map{|result| [result.code, result.id]} end - had_done = 1 if (@myshixun.games.count == @game.stage && @game.status ==2) + (@myshixun.games.count == game_challenge.position && @game.status ==2) ? had_done = 1 : had_done = 0 latest_output = @game.latest_output.try(:out_put) render :json => {status: @game.status, output: latest_output, results: outputs, had_done: had_done} end @@ -128,7 +143,7 @@ class GamesController < ApplicationController else outputs = outputs.map{|result| [result.code, result.id]} end - render :json => {status: @game.status, output: latest_output, results: outputs, had_done: had_done} + render :json => {status: @game.status, output: "服务器网络异常", results: outputs, had_done: 0} end # 自动推送下一个任务 @@ -193,7 +208,17 @@ class GamesController < ApplicationController end def allowd_manager - render_403 unless (User.current.manager_of_myshixun?(@myshixun) || User.current.admin?) + render_403 unless (User.current.manager_of_myshixun?(@myshixun) || User.current.admin? || User.current.id == @myshixun.shixun.try(:user_id)) + end + + # 判断成员是否允许查看 + def allowd_view + render_403 if @game.status == 3 + end + + def find__shixun_language + language = @myshixun.shixun.try(:language) + @language = language_switch(language) end # Find myshixun of id params[:id] diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index e18fbae05..0b2756afe 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -12,7 +12,7 @@ class ShixunsController < ApplicationController def shixun_monitor monitor_filter if @had_exec - @tpm = Myshixun.where(:user_id => User.current, :parent_id => @shixun).first + @tpm = Myshixun.where(:user_id => User.current, :shixun_id => @shixun).first end respond_to do |format| @@ -21,39 +21,39 @@ class ShixunsController < ApplicationController end # copy_shixun:复制一个新的实训模块包括版本库 - # publish_challenges:自动创建第一个Challenge + # copy_myshixun自动创建系列game,game中只包含状态等信息,公共信息从Challeges中读取 + # def shixun_exec - monitor_filter - if @had_exec == true || User.current.id == @shixun.user_id + if has_exec_cur_shixun(@shixun) || User.current.id == @shixun.user_id render_403 end repository = @shixun.repository ActiveRecord::Base.transaction do begin - g = Gitlab.client + @g = Gitlab.client if User.current.gid.nil? s = Trustie::Gitlab::Sync.new s.sync_user(User.current) end - gshixun = g.fork(@shixun.gpid, User.current.gid) - if gshixun.id + gshixun = @g.fork(@shixun.gpid, User.current.gid) + if !gshixun.id.nil? myshixun = copy_myshixun(@shixun, gshixun) challenges = @shixun.challenges + # 之所以增加user_id是为了方便统计查询性能 challenges.each_with_index do |challenge, index| - publish_games(challenge, myshixun.id, index) + status = (index == 0 ? 0 : 3) + Game.create!(:challenge_id => challenge.id, :myshixun_id => myshixun.id, :status => status, :user_id => myshixun.user_id) end + else + raise("网络异常,请稍后重试") end - respond_to do |format| format.html{redirect_to myshixun_path(myshixun)} end rescue Exception => e - flash[:notice] = l(:notice_shixun_failed_exec)+ ":" + e.message + flash[:error] = l(:notice_shixun_failed_exec)+ " : " + e.message + @g.delete_project(gshixun.id) if !gshixun.id.nil? raise ActiveRecord::Rollback - respond_to do |format| - format.html{ - redirect_to shixun_path(@shixun)} - end end end end @@ -70,6 +70,7 @@ class ShixunsController < ApplicationController def create @shixun = Shixun.new(params[:shixun]) @shixun.user_id = User.current.id + @shixun.language = params[:language] (params[:shixun][:is_public] == "1" ? @shixun.is_public = true : @shixun.is_public = false) respond_to do |format| if @shixun.save @@ -90,21 +91,17 @@ class ShixunsController < ApplicationController end end - def index - - end - def edit end def update @shixun.attributes = params[:shixun] + @shixun.language = params[:language] params[:shixun][:is_public] == "on" ? @shixun.is_public = 1 : @shixun.is_public = 0 - if @shixun.save - redirect_to settings_shixun_url(@shixun) - else - + @shixun.save + respond_to do |format| + format.html{redirect_to settings_shixun_url(@shixun)} end end @@ -140,15 +137,6 @@ class ShixunsController < ApplicationController return end @shixun.update_attribute(:status, 1) - # jobName = "#{@shixun.id}" - # pipeLine = "#{Base64.encode64(@shixun.script)}" - # uri = URI("http://123.59.135.74:9999/jenkins-exec/api/createJob") - # params = {jobName: jobName, pipeLine: pipeLine} - # res = uri_exec uri, params - # training_shixun_notice res - # if res['code'] == 0 - # @shixun.update_attribute(:status, 1) - # end end # 更新实训job @@ -189,39 +177,29 @@ class ShixunsController < ApplicationController # REDO: 新增类型copy的时候 # 复制项目 # gshixun --> gitlab project - def copy_myshixun tpm_shixun, gshixun + def copy_myshixun shixun, gshixun myshixun = Myshixun.new - myshixun.name = tpm_shixun.name - myshixun.description = tpm_shixun.description - myshixun.is_public = tpm_shixun.is_public - myshixun.parent_id = tpm_shixun.id + myshixun.attributes = shixun.attributes.dup.except("id","user_id","visits","gpid","status") + myshixun.shixun_id = shixun.id myshixun.user_id = User.current.id myshixun.gpid = gshixun.id - myshixun.forked_from = tpm_shixun.id jenkins_shixuns = Redmine::Configuration['jenkins_shixuns'] - if myshixun.save - m = MyshixunMember.new(:user_id => User.current.id, :role => 1) - myshixun.myshixun_members << m + if myshixun.save! + MyshixunMember.create!(:myshixun_id => myshixun.id, :user_id => User.current.id, :role => 1) uri = URI("#{jenkins_shixuns}/jenkins-exec/api/createJob") - pipeLine = "#{Base64.encode64(tpm_shixun.script)}" + pipeLine = "#{Base64.encode64(shixun.script)}" params = {jobName: "myshixun_#{myshixun.id}", pipeLine: pipeLine} res = uri_exec uri, params - copy_myshixun_repository(myshixun, gshixun) + rep = Repository.new(:myshixun_id => myshixun.id, :identifier => gshixun.name,:project_id => -1, :shixun_id => -2) + rep.type = "Repository::Gitlab" + rep.save! + if (res && res['code'] != 0) + raise("Job 创建失败") + end return myshixun end end - def copy_myshixun_repository(myshixun, gshixun) - repository = Repository.factory('Git') - repository.myshixun_id = myshixun.id - repository.type = 'Repository::Gitlab' - repository.url = gshixun.name - repository.identifier = gshixun.name - repository.project_id = -1 - repository.shixun_id = -2 - repository = repository.save - end - def training_shixun_notice res if res['code'] == 0 @notice = "实训开启成功" diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ddff55733..e5ddb51a1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -42,7 +42,8 @@ class UsersController < ApplicationController :activity_new_score_index, :influence_new_score_index, :score_new_index,:user_projects_index,:user_resource,:user_contestlist, :user_manage_issues, :user_receive_issues, :user_courses4show,:user_projects4show,:user_contests4show,:user_course_activities,:user_project_activities,:user_feedback4show,:user_visitorlist,:user_messages,:edit_brief_introduction, :user_import_homeworks,:user_search_homeworks,:user_import_resource, :user_system_messages,:choose_user_course,:user_courselist,:user_projectlist,:sort_syllabus_list, - :sort_project_list,:my_homeworks,:manage_or_receive_homeworks,:search_m_r_homeworks, :cancel_or_collect,:expand_courses,:homepage, :user_issues, :course_community, :project_community, :contest_community] + :sort_project_list,:my_homeworks,:manage_or_receive_homeworks,:search_m_r_homeworks, :cancel_or_collect,:expand_courses,:homepage, :user_issues, :course_community, :project_community, :contest_community, + :user_shixuns] before_filter :auth_user_extension, only: :show before_filter :show_system_message, :only => [:show] #before_filter :rest_user_score, only: :show @@ -651,6 +652,14 @@ class UsersController < ApplicationController end end + # 我发布的实训 + def user_shixuns + @shixuns = Shixun.where(:user_id => User.current) + respond_to do |format| + format.html{render :layout=>'base_users_new'} + end + end + #处理加入课程成为教辅教师的请求 #status 1 同意 2 拒绝 def dealwith_apply_request diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cbb4bf3a0..48f1ca88f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -33,26 +33,41 @@ module ApplicationHelper extend Forwardable def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter + # codeMirror语言转换 + def language_switch language + case language + when "Java" + "text/x-java" + when "C" + "text/x-csrc" + when "C++" + "text/x-c++src" + when "Python" + "text/x-python" + when "Ruby" + "text/x-ruby" + end + end # 定义实训相关方法 # myshixun 最高分 def top_score shixun, position - Game.find_by_sql("SELECT max(final_score) as score FROM `games` where stage=#{position} and myshixun_id in (SELECT id FROM `myshixuns` where parent_id=#{shixun.id})").first + Game.find_by_sql("SELECT max(final_score) as top_score FROM `games` g, `challenges` c where g.challenge_id = c.id and c.position=#{position} and g.myshixun_id in (SELECT id FROM `myshixuns` ms where ms.shixun_id=#{shixun.id})").first.try(:top_score) end # 实训平均分 def shixun_avg_score shixun, position - Game.find_by_sql("SELECT avg(final_score) as avg_score FROM `games` where stage=#{position} and myshixun_id in (SELECT id FROM `myshixuns` where parent_id=#{shixun.id})") + Game.find_by_sql("SELECT avg(g.final_score) as avg_score FROM `games` g, `challenges` c where g.challenge_id=c.id and c.position=#{position} and g.myshixun_id in (SELECT id FROM `myshixuns` ms where ms.shixun_id=#{shixun.id})").first.try(:avg_score) end # 正在进行中任务 def shixun_running shixun, position - Myshixun.find_by_sql("SELECT * FROM `myshixuns` ms, `games` g where g.myshixun_id = ms.id and parent_id =#{shixun.id} and g.stage=#{position} and (g.status=0 or g.status=1);").count + Shixun.find_by_sql("SELECT count(*) as count FROM `myshixuns` ms, `games` g, `challenges` c where g.myshixun_id = ms.id and ms.shixun_id =#{shixun.id} and g.challenge_id=c.id and c.position=#{position} and g.status in ('0','1');").first.try(:count) end # 已完成任务 def shixun_done shixun, position - Myshixun.find_by_sql("SELECT * FROM `myshixuns` ms, `games` g where g.myshixun_id = ms.id and parent_id =#{shixun.id} and g.stage=#{position} and g.status=2;").count + Shixun.find_by_sql("SELECT count(*) as count FROM `myshixuns` ms, `games` g, `challenges` c where g.myshixun_id = ms.id and ms.shixun_id =#{shixun.id} and g.challenge_id=c.id and c.position=#{position} and g.status=2;").first.try(:count) end # 测评次数 @@ -85,9 +100,36 @@ module ApplicationHelper min = time % (24*60*60) % (60*60) / 60 end + # 耗时:天、小时、分 + # 小于1分钟则不显示 + def game_spend_time start_time, end_time + time = (end_time - start_time).to_i + day = time / 86400 + hour = time % (24*60*60) / (60*60) + min = time % (24*60*60) % (60*60) / 60 + if day < 1 + if hour < 1 + if time < 1 + time = time == 0 ? "--" : "小于1分钟" + else + time = "#{min} 分钟" + end + else + time = "#{hour}小时 : #{min}分" + end + else + time = "#{day}天 : #{hour}小时 : #{min}分" + end + return time + end + + def avg_spend_time shixun_id, position + Game.find_by_sql("SELECT avg(g.updated_at - g.created_at) as avg_time FROM `games` g, `challenges` c where c.id=g.challenge_id and g.status =2 and c.position = #{position} and g.myshixun_id in (SELECT id FROM `myshixuns` where shixun_id= #{shixun_id});").first.try(:avg_time).to_i + end + # 已闯关 - def had_pass myshixun - Game.find_by_sql("SELECT * FROM `games` where status =2 and myshixun_id in (SELECT id FROM `myshixuns` where parent_id=#{myshixun.parent_id});").count + def had_pass shixun_id, position + Game.find_by_sql("SELECT count(*) as count FROM `games` g, `challenges` c where c.id=g.challenge_id and g.status =2 and c.position =#{position} and g.myshixun_id in (SELECT id FROM `myshixuns` where shixun_id=#{shixun_id});").first.try(:count) end # 单个game测评次数 @@ -96,7 +138,7 @@ module ApplicationHelper end def shixun_final_score myshixun - Game.find_by_sql("SELECT sum(final_score) as score FROM `games` where myshixun_id='#{myshixun.id}';") + Game.find_by_sql("SELECT sum(final_score) as final_score FROM `games` where myshixun_id='#{myshixun.id}';").first.try(:final_score) end # def user_blogs_path(resource,parameters={}) @@ -230,7 +272,7 @@ module ApplicationHelper # 判断当前用户是否已经实训过当前项目 # project: current_project def has_exec_cur_shixun shixun - Myshixun.where(:user_id => User.current.id, :parent_id => shixun.id).count > 0 ? true : false + Myshixun.where(:user_id => User.current.id, :shixun_id => shixun.id).count > 0 ? true : false end def allow_shixun_exec shixun diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 04d984945..35630a1f0 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -5,6 +5,7 @@ class Challenge < ActiveRecord::Base has_many :challenge_samples, :dependent => :destroy has_many :test_sets, :dependent => :destroy has_many :challenge_tags, :dependent => :destroy + has_many :games, :dependent => :destroy validates_presence_of :subject validates_presence_of :score diff --git a/app/models/game.rb b/app/models/game.rb index e7bd9b6dc..3810c91e9 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,15 +1,18 @@ class Game < ActiveRecord::Base # stauts 0: can exe 1:doing 2:successed 3:locked - default_scope :order => 'stage' - attr_accessible :description, :myshixun_id, :stage, :subject, :user_id, :status, :ready_knowledge, :task_pass, :answer, :score, :final_score, :path + default_scope :order => 'created_at desc' + attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id belongs_to :myshixun,:touch=> true belongs_to :user + belongs_to :challenge has_many :outputs, :dependent => :destroy has_many :test_sets, :dependent => :destroy has_many :challenge_samples, :dependent => :destroy def next_game - game = Game.where(:myshixun_id => self.myshixun_id, :stage => self.stage + 1).first + challenge = self.challenge + next_challenge_id = challenge.next_challenge + game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => next_challenge_id).first render_404 if game.nil? return game end diff --git a/app/models/myshixun.rb b/app/models/myshixun.rb index b57c1f041..c9894d4ee 100644 --- a/app/models/myshixun.rb +++ b/app/models/myshixun.rb @@ -4,6 +4,7 @@ class Myshixun < ActiveRecord::Base has_many :myshixun_members has_one :repository has_many :games, :dependent => :destroy, :order => "games.id ASC" + belongs_to :shixun # 当前任务:一个实训中只可能一个未完成任务(status 0或1只会存在一条记录) # status:0 可以测评的,正在测评的 @@ -12,7 +13,8 @@ class Myshixun < ActiveRecord::Base games = self.games current_game = games.select{|game| game.status==1 || game.status==0}.first if current_game.blank? - current_game = games.last + passed_games = games.select{|game| game.status==2} + current_game = passed_games.last end return current_game end diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 22da9a7e3..5ab4e4b99 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -1,28 +1,12 @@ # status 控制实训的状态,0:未开启;1: 已开启(TPM) class Shixun < ActiveRecord::Base - attr_accessible :description, :is_public, :name, :parent_id, :changeset_num, :forked_form, :status, :user_id, :gpid, :forked_count + attr_accessible :description, :is_public, :name, :changeset_num, :status, :user_id, :gpid, :language has_many :users, :through => :shixun_members has_many :shixun_members has_one :repository has_many :challenges, :dependent => :destroy, :order => "challenges.id ASC" - - def parent - shixun = Shixun.find_by_parent_id(self.parent_id) - # raise ActiveRecord::RecordNotFound, "Couldn't find Shixun with parent_id=#{self.parent_id}" if shixun.nil? - end - - def children - Myshixun.where(:parent_id => self.id) - end - - def tpi? - (!self.parent_id.nil? && !self.forked_form.nil? && self.status == 2) ? true : false - end - - def tpm? - (self.parent_id.nil? && self.status == 1) ? true : false - end + has_many :myshixuns def owner User.find(self.user_id) diff --git a/app/views/admin/users.html.erb b/app/views/admin/users.html.erb index 9215782bb..f15bbfe49 100644 --- a/app/views/admin/users.html.erb +++ b/app/views/admin/users.html.erb @@ -31,7 +31,7 @@
- + <%= sort_header_tag('login', :caption => l(:field_login)) %> <%= sort_header_tag('firstname', :caption => l(:field_firstname)) %> <%= sort_header_tag('lastname', :caption => l(:field_lastname)) %> @@ -59,7 +59,6 @@ - <% end -%> diff --git a/app/views/challenges/_content_list.html.erb b/app/views/challenges/_content_list.html.erb index 131f87968..954fc79dd 100644 --- a/app/views/challenges/_content_list.html.erb +++ b/app/views/challenges/_content_list.html.erb @@ -4,7 +4,9 @@ <% if @challenges.count != 0 %> <%= @challenges.count %> <% end %> - 新建阶段 + <% if User.current.manager_of_shixun?(@shixun) || User.current.admin? %> + 新建阶段 + <% end %>
<% @challenges.each_with_index do |challenge, index| -%> @@ -21,25 +23,16 @@ <% end %> - +

<% end %>

- <% if shixun_running(@shixun, challenge.position) != 0 %> - 正在挑战:<%= shixun_running(@shixun, challenge.position) %> - <% end %> - <% if shixun_done(@shixun, challenge.position) != 0 %> - 完成挑战:<%= shixun_done(@shixun, challenge.position) %> - <% end %> - <% if top_score(@shixun, challenge.position).score.to_i != 0 %> - 最佳表现:<%= top_score(@shixun, challenge.position).score.to_i %>分 - <% end %> - <% if shixun_exec_total_count(@shixun, challenge.position).count != 0 %> - 测评次数:<%= shixun_exec_total_count(@shixun, challenge.position).count %>次 - <% end %> - + 正在挑战:<%= shixun_running(@shixun, challenge.position).to_i %> + 完成挑战:<%= shixun_done(@shixun, challenge.position).to_i %> + 最佳表现:<%= top_score(@shixun, challenge.position).to_i %>分 + 平均表现:<%= shixun_avg_score(@shixun, challenge.position).to_i %>分

+ <%= f.text_area :answer, :class => "panel-form-width-690 panel-form-height-150 fl task-textarea-pd", :no_label => true, :style => "line-height:1.9;" %>
  • @@ -153,71 +150,16 @@
  • <% if params[:action] == "edit" %> 保存 + 取消 <% else %> 保存 + 取消 <% end %> - 取消 +
  • diff --git a/app/views/challenges/show.html.erb b/app/views/challenges/show.html.erb index 84808950a..be328c660 100644 --- a/app/views/challenges/show.html.erb +++ b/app/views/challenges/show.html.erb @@ -1,10 +1,14 @@ +<%= javascript_include_tag "/assets/codemirror/codemirror_python_ruby_c" %> +<%= stylesheet_link_tag "/assets/codemirror/codemirror" %> +<%= stylesheet_link_tag "/assets/kindeditor/plugins/code/previewcode" %> +

    阶段详情

    新建阶段
    -
    +

    第<%= @challenge.position %>关<%= @challenge.subject %>

    @@ -20,21 +24,21 @@ <%= @challenge.path.blank? ? "无" : (h @challenge.path.html_safe) %>

    -
  • +
  • -
    +
    <%= @challenge.ready_knowledge.blank? ? "无" : (h @challenge.ready_knowledge.html_safe) %>
  • -
    +
    <%= h @challenge.task_pass.html_safe %>
  • -
      +
        <% if @challenge_samples.count > 0 %> <% @challenge_samples.each_with_index do |sample, index| %>
      • @@ -52,7 +56,7 @@
      • -
          +
            <% if @test_sets.count > 0 %> <% @test_sets.each_with_index do |test, index| %>
          • @@ -82,9 +86,8 @@
          • -
            - <%= @challenge.answer.blank? ? "无" : (h @challenge.answer) %> -
            +
            +
            <%= @challenge.answer.blank? ? "无" : (@challenge.answer) %>
          • @@ -96,5 +99,20 @@
  • + diff --git a/app/views/games/_desc_full_show.html.erb b/app/views/games/_desc_full_show.html.erb index 7b7152298..cabef0388 100644 --- a/app/views/games/_desc_full_show.html.erb +++ b/app/views/games/_desc_full_show.html.erb @@ -1,6 +1,6 @@ -
    + \ No newline at end of file diff --git a/app/views/games/_exec_results.html.erb b/app/views/games/_exec_results.html.erb index 9b85a3507..a0ef6d414 100644 --- a/app/views/games/_exec_results.html.erb +++ b/app/views/games/_exec_results.html.erb @@ -1,35 +1,10 @@ <%= content_for(:header_tags) do %> <%= javascript_include_tag 'baiduTemplate', 'jquery.datetimepicker.js' %> <% end %> -
    + + \ No newline at end of file diff --git a/app/views/games/_file_edit_form.html.erb b/app/views/games/_file_edit_form.html.erb index dd4185b5f..c9b889c67 100644 --- a/app/views/games/_file_edit_form.html.erb +++ b/app/views/games/_file_edit_form.html.erb @@ -1,61 +1,36 @@ <%= javascript_include_tag 'baiduTemplate', 'jquery.datetimepicker.js' %> -<%= javascript_include_tag "/assets/codemirror/codemirror_python_ruby_c" %> +<%= javascript_include_tag "/assets/codemirror/codemirror_python_ruby_c", '/assets/codemirror/autorefresh' %> <%= stylesheet_link_tag "/assets/codemirror/codemirror" %>
  • - +
  • \ No newline at end of file diff --git a/app/views/games/_game_show.html.erb b/app/views/games/_game_show.html.erb index 6cf171827..ad9df7f9b 100644 --- a/app/views/games/_game_show.html.erb +++ b/app/views/games/_game_show.html.erb @@ -1,13 +1,13 @@ -
    -
    - - 完成任务后,请点击“提交评测”按钮,若通过系统评测,将为你解锁下一关,祝你早日通关,加油! -
    -
    - <%= render :partial => 'desc_full_show'%> -
    <%= truncate(get_occupation_from_user(user), :length => 12 ) %> <%= change_status_link(user) %> <%= delete_link user_path(user, :back_url => admin_users_path(params)) unless User.current == user %>