diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 314a58d20..7a255cb92 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -6,6 +6,7 @@ class ChallengesController < ApplicationController before_filter :build_challege_from_params, :only => [:new, :create] before_filter :tpi_manager_allowed, :only => [:challenge_build, :destroy, :show, :edit, :new, :create] before_filter :query_challeges, :only => [:show, :edit, :update] + before_filter :find_language, :only => [:show, :new, :edit] include ApplicationHelper @@ -204,4 +205,9 @@ class ChallengesController < ApplicationController render_404 end + def find_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 2862f2695..24a6283a7 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -6,6 +6,7 @@ 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, :game_build, :next_step, :change_status, :file_update] include ApplicationHelper def index @@ -26,6 +27,8 @@ class GamesController < ApplicationController @rev = @rev.nil? ? "master" : @rev @git_url = git_repository_url(@myshixun, "Myshixun") @type = params[:type] + language = @shixun.myshixuns.try(:language) + @language = language_switch(language) # 默认打开文件 if @path == "" && !game_path.nil? && !@repository.cat(game_path, @rev).blank? && @type != "root" @path = game_path @@ -209,6 +212,11 @@ class GamesController < ApplicationController 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 + # Find myshixun of id params[:id] def find_myshixun myshixun_id = params[:myshixun_id] || (params[:game] && params[:game][:myshixun_id]) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a5f2d6074..2a9f5e6e2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -33,6 +33,21 @@ module ApplicationHelper extend Forwardable def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter + # codeMirror语言转换 + def language_switch language + case language + when "Java" + "text/c-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 最高分 @@ -94,7 +109,11 @@ module ApplicationHelper min = time % (24*60*60) % (60*60) / 60 if day < 1 if hour < 1 - time = "#{min} 分钟" + if time < 1 + time = time == 0 ? "--" : "小于1分钟" + else + time = "#{min} 分钟" + end else time = "#{hour}小时 : #{min}分" end @@ -104,9 +123,13 @@ module ApplicationHelper 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测评次数 diff --git a/app/views/challenges/_form.html.erb b/app/views/challenges/_form.html.erb index 241e99228..ad9b4983a 100644 --- a/app/views/challenges/_form.html.erb +++ b/app/views/challenges/_form.html.erb @@ -272,7 +272,7 @@ } var editor = CodeMirror.fromTextArea(document.getElementById("challenge_answer"), { - mode: {name: 'text/x-java', + mode: {name: 'text/x-<%= @language %>', version: 2, singleLineStringErrors: false}, lineNumbers: true, diff --git a/app/views/games/_file_edit_form.html.erb b/app/views/games/_file_edit_form.html.erb index 37f76c646..25f74ebf1 100644 --- a/app/views/games/_file_edit_form.html.erb +++ b/app/views/games/_file_edit_form.html.erb @@ -13,7 +13,7 @@