diff --git a/Gemfile b/Gemfile index e40c468aa..758ef59dd 100644 --- a/Gemfile +++ b/Gemfile @@ -4,9 +4,9 @@ unless RUBY_PLATFORM =~ /w32/ # unix-like only gem 'iconv' if RUBY_PLATFORM =~ /darwin/ - gem "rmagick", "= 2.15.4" ## osx must be this version + # gem "rmagick", "= 2.15.4" ## osx must be this version elsif RUBY_PLATFORM =~ /linux/ - gem "rmagick", "~> 2.13.1" ## centos yum install ImageMagick-devel + # gem "rmagick", "~> 2.13.1" ## centos yum install ImageMagick-devel end gem 'certified' gem 'net-ssh', '2.9.1' diff --git a/app/assets/javascripts/training_tasks.js.coffee b/app/assets/javascripts/training_tasks.js.coffee new file mode 100644 index 000000000..b3f5f376b --- /dev/null +++ b/app/assets/javascripts/training_tasks.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/training_tasks.css.scss b/app/assets/stylesheets/training_tasks.css.scss new file mode 100644 index 000000000..5a071ec72 --- /dev/null +++ b/app/assets/stylesheets/training_tasks.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the training_tasks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 9d8a5ecc5..4c7d578ba 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -279,6 +279,14 @@ class AccountController < ApplicationController render :json => result end + def change_email + user = User.find params[:user_id].to_i + user.update_attributes(:mail => params[:value]) + result = {:email => user.mail} + + render :json => result + end + def email_activation diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index c2499e824..9d51e5839 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -978,7 +978,11 @@ class AdminController < ApplicationController #作业 def homework #@homework = HomeworkCommon.order('end_time desc') - @homework = HomeworkCommon.order( 'created_at desc ') + if params[:type] == "2" + @homework = HomeworkCommon.where(:homework_type => 2).order( 'created_at desc ') + else + @homework = HomeworkCommon.order( 'created_at desc ') + end @homework = paginateHelper @homework,30 @page = (params['page'] || 1).to_i - 1 respond_to do |format| diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cd989ef2d..8a3445494 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -312,7 +312,11 @@ class ApplicationController < ActionController::Base def authorize_allowed(ctrl = params[:controller], action = params[:action], global = false) #modify by NWB if @project - allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) + if ctrl == "repositories" && action == "show" && @project.hidden_repo + allowed = User.current.admin? || (User.current.member_of?(@project) && !(role_of_members_in_project(@project.id, User.current.id) == "Reporter")) + else + allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global) + end elsif @course allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @courses, :global => global) else diff --git a/app/controllers/at_controller.rb b/app/controllers/at_controller.rb index 66cbf27b8..81d678261 100644 --- a/app/controllers/at_controller.rb +++ b/app/controllers/at_controller.rb @@ -29,6 +29,8 @@ class AtController < ApplicationController case type when "Issue" find_issue(id) + whne 'TrainingTask' + find_training_task(id) when 'Project' find_project(id) when 'Course' @@ -69,6 +71,13 @@ class AtController < ApplicationController at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id } end + def find_training_task(id) + training_task = TrainingTask.find(id) + journals = training_task.journals + at_persons = journals.map(&:user) + training_task.project.users + at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id } + end + def find_project(id) return [] if id.to_i<0 at_persons = Project.find(id).users diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 8eff74041..0f5a55ab9 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -143,7 +143,7 @@ class ForumsController < ApplicationController reorder(order). preload(:author, {:last_reply => :author}). all - @forums = Forum.reorder("topic_count desc,updated_at desc") + @forums = Forum.find_by_sql("SELECT *,topic_count + memo_count tm FROM `forums` order by tm desc;") @my_forums_count = Memo.where("author_id =? and parent_id is null", User.current.id).count @my_memos_count = Memo.where("author_id =? and parent_id is not null", User.current.id).count @errors = params[:errors] diff --git a/app/controllers/praise_tread_controller.rb b/app/controllers/praise_tread_controller.rb index 3e3c338b4..a60b85b90 100644 --- a/app/controllers/praise_tread_controller.rb +++ b/app/controllers/praise_tread_controller.rb @@ -1,166 +1,168 @@ -class PraiseTreadController < ApplicationController - - accept_api_auth :tread_plus,:praise_plus - # before_filter :require_login,:only => [:praise_plus,:tread_plus] - before_filter :user_unlogged_check,:only => [:praise_plus,:tread_plus,:praise_minus] - - def praise_plus - @obj = nil - @activity = false - if request.get? - @obj_id = params[:obj_id] - @obj_type = params[:obj_type] - if !params[:user_activity_id].nil? && !params[:type].nil? - @user_activity_id = params[:user_activity_id] - @type = params[:type] - @activity = true - end - # @is_in_list = nil - @obj = find_object_by_type_and_id(@obj_type,@obj_id) - pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id) - unless pts.empty? - respond_to do |format| - format.js - end - return - end - @horizontal = params[:horizontal].downcase == "false" ? false:true if params[:horizontal] - # if @obj.respond_to?("author_id") - # author_id = @obj.author_id - # elsif @obj.respond_to?("user_id") - # author_id = @obj.user_id - # end - # unless author_id == User.current.id - praise_tread_plus(@obj_type,@obj_id,1) - # end - respond_to do |format| - format.js - end - end - end - - def praise_minus - @obj = nil - @activity = false - if request.get? - #@obj = params[:obj] # 传的是对象,最后变成id了 - #首先更新praise_tread 表 删除关注记录 - #@pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user") - @obj_id = params[:obj_id] - @obj_type = params[:obj_type] - if !params[:user_activity_id].nil? && !params[:type].nil? - @user_activity_id = params[:user_activity_id] - @type = params[:type] - @activity = true - end - @obj = find_object_by_type_and_id(@obj_type,@obj_id) - @pt = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id).first - if @pt.nil? - respond_to do |format| - format.js - end - return - end - @pt.delete if !@pt.nil? - #再更新praise_tread_cache表 使相应的记录减1 当为0时删除 - @ptc = PraiseTreadCache.where("object_id=? and object_type=?",@obj_id,@obj_type.to_s).first - @ptc.minus(1) if !@ptc.nil? - if @ptc.praise_num == 0 - @ptc.delete - end - - end - #@obj = User.find_by_id(@obj) - respond_to do |format| - format.js - end - end - - def tread_plus - @obj = nil - # @is_in_list = nil - if request.get? - @obj_id = params[:obj_id] - @obj_type = params[:obj_type] - #@horizontal = params[:horizontal].downcase == "false" ? false:true - @obj = find_object_by_type_and_id(@obj_type,@obj_id) - unless @obj.author_id == User.current.id - praise_tread_plus(@obj_type,@obj_id,0) - end - - end - end - - def tread_minus - respond_to do |format| - format.html - format.js - end - end - - private - - def find_object_by_type_and_id(type,id) - @obj = nil - case type - when 'Memo' - @obj = Memo.find_by_id(id) - when 'Message' - @obj = Message.find_by_id(id) - when 'HomeworkCommon' - @obj = HomeworkCommon.find_by_id(id) - when 'JournalsForMessage' - @obj = JournalsForMessage.find_by_id(id) - when 'News' - @obj = News.find_by_id(id) - when 'Comment' - @obj = Comment.find_by_id(id) - when 'Journal' - @obj = Journal.find_by_id(id) - when 'BlogComment' - @obj = BlogComment.find_by_id(id) - when 'OrgDocumentComment' - @obj = OrgDocumentComment.find_by_id(id) - when 'User' - @obj = User.find_by_id(id) - when 'Issue' - @obj = Issue.find_by_id(id) - when 'Project' - @obj = Project.find_by_id(id) - when 'Bid' - @obj = Bid.find_by_id(id) - when 'Contest' - @obj = Contest.find_by_id(id) - when 'Syllabus' - @obj = Syllabus.find_by_id(id) - when 'Work' - @obj = Work.find_by_id(id) - else - @obj = nil - end - return @obj - end - - def praise_tread_plus(type,id,flag) - unless id.nil? and type.nil? - #首先创建或更新praise_tread 表 - @pt = PraiseTread.new - @pt.user_id = User.current.id - @pt.praise_tread_object_id = id.to_i - @pt.praise_tread_object_type = type - @pt.praise_or_tread = flag - @pt.save - # end - - #再创建或更新praise_tread_cache表 - #@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type) - @ptc = PraiseTreadCache.where("object_id = ? and object_type = ?",id.to_i,type).first - @ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc - @ptc.object_id = id.to_i - @ptc.object_type = type - @ptc.save - @ptc.plus(flag,1) - end - end - -end +class PraiseTreadController < ApplicationController + + accept_api_auth :tread_plus,:praise_plus + # before_filter :require_login,:only => [:praise_plus,:tread_plus] + before_filter :user_unlogged_check,:only => [:praise_plus,:tread_plus,:praise_minus] + + def praise_plus + @obj = nil + @activity = false + if request.get? + @obj_id = params[:obj_id] + @obj_type = params[:obj_type] + if !params[:user_activity_id].nil? && !params[:type].nil? + @user_activity_id = params[:user_activity_id] + @type = params[:type] + @activity = true + end + # @is_in_list = nil + @obj = find_object_by_type_and_id(@obj_type,@obj_id) + pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id) + unless pts.empty? + respond_to do |format| + format.js + end + return + end + @horizontal = params[:horizontal].downcase == "false" ? false:true if params[:horizontal] + # if @obj.respond_to?("author_id") + # author_id = @obj.author_id + # elsif @obj.respond_to?("user_id") + # author_id = @obj.user_id + # end + # unless author_id == User.current.id + praise_tread_plus(@obj_type,@obj_id,1) + # end + respond_to do |format| + format.js + end + end + end + + def praise_minus + @obj = nil + @activity = false + if request.get? + #@obj = params[:obj] # 传的是对象,最后变成id了 + #首先更新praise_tread 表 删除关注记录 + #@pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user") + @obj_id = params[:obj_id] + @obj_type = params[:obj_type] + if !params[:user_activity_id].nil? && !params[:type].nil? + @user_activity_id = params[:user_activity_id] + @type = params[:type] + @activity = true + end + @obj = find_object_by_type_and_id(@obj_type,@obj_id) + @pt = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id).first + if @pt.nil? + respond_to do |format| + format.js + end + return + end + @pt.delete if !@pt.nil? + #再更新praise_tread_cache表 使相应的记录减1 当为0时删除 + @ptc = PraiseTreadCache.where("object_id=? and object_type=?",@obj_id,@obj_type.to_s).first + @ptc.minus(1) if !@ptc.nil? + if @ptc.praise_num == 0 + @ptc.delete + end + + end + #@obj = User.find_by_id(@obj) + respond_to do |format| + format.js + end + end + + def tread_plus + @obj = nil + # @is_in_list = nil + if request.get? + @obj_id = params[:obj_id] + @obj_type = params[:obj_type] + #@horizontal = params[:horizontal].downcase == "false" ? false:true + @obj = find_object_by_type_and_id(@obj_type,@obj_id) + unless @obj.author_id == User.current.id + praise_tread_plus(@obj_type,@obj_id,0) + end + + end + end + + def tread_minus + respond_to do |format| + format.html + format.js + end + end + + private + + def find_object_by_type_and_id(type,id) + @obj = nil + case type + when 'Memo' + @obj = Memo.find_by_id(id) + when 'Message' + @obj = Message.find_by_id(id) + when 'HomeworkCommon' + @obj = HomeworkCommon.find_by_id(id) + when 'JournalsForMessage' + @obj = JournalsForMessage.find_by_id(id) + when 'News' + @obj = News.find_by_id(id) + when 'Comment' + @obj = Comment.find_by_id(id) + when 'Journal' + @obj = Journal.find_by_id(id) + when 'BlogComment' + @obj = BlogComment.find_by_id(id) + when 'OrgDocumentComment' + @obj = OrgDocumentComment.find_by_id(id) + when 'User' + @obj = User.find_by_id(id) + when 'Issue' + @obj = Issue.find_by_id(id) + when 'TrainingTask' + @obj = TrainingTask.find_by_id(id) + when 'Project' + @obj = Project.find_by_id(id) + when 'Bid' + @obj = Bid.find_by_id(id) + when 'Contest' + @obj = Contest.find_by_id(id) + when 'Syllabus' + @obj = Syllabus.find_by_id(id) + when 'Work' + @obj = Work.find_by_id(id) + else + @obj = nil + end + return @obj + end + + def praise_tread_plus(type,id,flag) + unless id.nil? and type.nil? + #首先创建或更新praise_tread 表 + @pt = PraiseTread.new + @pt.user_id = User.current.id + @pt.praise_tread_object_id = id.to_i + @pt.praise_tread_object_type = type + @pt.praise_or_tread = flag + @pt.save + # end + + #再创建或更新praise_tread_cache表 + #@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type) + @ptc = PraiseTreadCache.where("object_id = ? and object_type = ?",id.to_i,type).first + @ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc + @ptc.object_id = id.to_i + @ptc.object_type = type + @ptc.save + @ptc.plus(flag,1) + end + end + +end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 43923f6bb..cf1a71424 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -29,7 +29,7 @@ class ProjectsController < ApplicationController menu_item :share, :only => :share before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise, - :view_homework_attaches,:join_project, :project_home] + :view_homework_attaches,:join_project, :project_home, :training_execute, :training_task_status] before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen,:view_homework_attaches,:course] before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches] before_filter :require_admin, :only => [ :copy, :unarchive, :destroy, :calendar] @@ -332,9 +332,11 @@ class ProjectsController < ApplicationController if User.current.member_of?(@project) || User.current.admin? case params[:type] when nil - @events_pages = ForgeActivity.where("project_id = ? and forge_act_type in ('Issue', 'Message','News', 'Project', 'Attachment','Commit')", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type in ('Issue', 'TrainingTask','Message','News', 'Project', 'Attachment','Commit')", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) when 'issue' @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Issue'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) + when 'training_task' + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'TrainingTask'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) when 'news' @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'News'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) when 'message' @@ -379,7 +381,9 @@ class ProjectsController < ApplicationController def settings # 顶部导航 @project_menu_type = 10 - + if @project.is_child_training_project? + return render_404 + end # 修改查看消息状态 applied_messages = ForgeMessage.where("user_id =? and project_id =? and forge_message_type =? and viewed =?", User.current.id, @project, "AppliedProject", 0) applied_messages.update_all(:viewed => true) @@ -765,6 +769,18 @@ class ProjectsController < ApplicationController end end + def add_script + if User.current.admin? || User.current.member_of?(@project) + if @project.update_attribute(:script, params[:project_script]) + @notice = "脚本添加成功" + else + @notice = "脚本添加失败" + end + else + return render_403 + end + end + def modules @project.enabled_module_names = params[:enabled_module_names] flash[:notice] = l(:notice_successful_update) @@ -805,6 +821,231 @@ class ProjectsController < ApplicationController redirect_to admin_projects_url(:status => params[:status]) end + # 弹框提醒: + # 自己不能参加自己的实训项目 + # 没有建立版本库的项目不能开启实训 + # 已经实训过直接跳入 + # + def training_chiled_project_exec + respond_to do |format| + format.js + end + end + + # training_status: 默认为0; 1代表实训项目; 2:代表实训子项目 + def training_project_execute + if @project.training_tasks.count == 0 + @notice = "实训开启失败:请先发布实训任务" + return + elsif Repository.where(:project_id => @project.id, :type => "Repository::Gitlab").count == 0 + @notice = "实训开启失败:请先创建版本库" + return + end + jobName = "#{@project.id}" + pipeLine = "#{Base64.encode64(@project.script)}" + uri = URI("http://123.59.135.74:9999/jenkins-exec/api/createJob") + params = {jobName: jobName, pipeLine: pipeLine} + res = uri_exec uri, params + training_project_notice res + @project.update_attribute(:training_status, 1) + end + + def training_project_update + jobName = "#{@project.id}" + pipeLine = "#{Base64.encode64(@project.script)}" + uri = URI("http://123.59.135.74:9999/jenkins-exec/api/updateJob") + params = {jobName: jobName, pipeLine: pipeLine} + res = uri_exec uri, params + training_project_notice res + @project.update_attribute(:training_status, 1) + end + + def training_project_notice res + if res['code'] == 0 + @notice = "实训开启成功" + elsif res['code'] == -2 + @notice = "Job已存在" + else + @notice = res['msg'].nil? ? "实训开启失败" : res['msg'] + end + end + + # TrainintTask.status 0:评测中 1:评测成功 2:评测惊醒中 + # 非项目成员、非实训子项目不允许执行该方法 + def task_execute + if !@project.is_child_training_project? || !User.current.member_of?(@project) + return render_403 + end + taskId = params[:training_task_id] + jobName = @project.forked_from_project_id + @training_task = TrainingTask.find(taskId) + if @training_task.status == 0 + params = {:jobName => "#{jobName}", :taskId => "#{taskId}"} + uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob") + begin + res = uri_exec uri, params + # 任务开启成功后创建一条留言提示,并发布 + @training_task.update_attribute(:status, 2) + return + rescue Exception => e + puts e + end + end + end + + def uri_exec uri, params + res = Net::HTTP.post_form(uri, params).body + res = JSON.parse(res) + end + + # 开启实训项目,学生会fork一个项目并自动发送任务 + def training_project_extend + @project = Project.find(params[:id]) + @repository = Repository.where("project_id =? and type =?", @project.id, "Repository::Gitlab") + # 如果当前用户已经fork过该项目,不会新fork项目,则跳至已fork的项 + unless has_forked?(@project, User.current) + project = project_from_current_project(@project.id, User.current.id) + redirect_to project_path(project) + else + ActiveRecord::Base.transaction do + g = Gitlab.client + if User.current.gid.nil? + s = Trustie::Gitlab::Sync.new + s.sync_user(User.current) + end + gproject = g.fork(@project.gpid, User.current.gid) + if gproject + new_training_project = copy_project_and_module(@project, gproject) + forked_count = @project.forked_count.to_i + 1 + @project.update_attributes(:forked_count => forked_count) + # 发布实训任务,只发布实训任务的第一个 + publish_training_tasks(@project.id, new_training_project.id, 1) + end + end + end + end + + # 需要传Task ID + # 判断任务是否完成 + # 如果完成则发送下一个任务直到任务结束 + # TrainingTask.status 1 成功,其它失败 + def training_task_status + status = params[:status].to_i + task_id = params[:taskId] + message = params[:msg].to_i + 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) + 1 + # 测试,默认成功 + if status == 0 + ActiveRecord::Base.transaction do + if position <= original_tasks_count + # 继续发布下一个任务 + publish_training_tasks original_project_id, @training_task.project_id, position + @training_task.update_attribute(:status, 1) + end + # 创建一条回复提醒 + content = "恭喜您通过测评" + add_training_task_journal(content, original_project.user_id) + end + else + content = "很遗憾,您没有通过该步" + add_training_task_journal(content, original_project.user_id) + end + end + + # 创建一条回复 + def add_training_task_journal content, user_id + jour = Journal.new + jour.user_id = user_id + jour.notes = content + jour.journalized = @training_task + jour.save + update_user_activity(@training_task.class, @training_task.id) + update_forge_activity(@training_task.class, @training_task.id) + end + + # 实训开启成功后,发布第一个任务 + # REDO:失败后提醒用户,及相关处理 + def publish_training_tasks original_project_id, new_training_project_id, position + original_task = TrainingTask.where(:project_id => original_project_id, :position => position).first + training_task = TrainingTask.new + training_task.save_attachments(params[:attachments] || (params[:training_task] && params[:training_task][:uploads])) + training_task.subject = original_task.subject + training_task.description = original_task.description + training_task.position = original_task.position + training_task.project_id = new_training_project_id + training_task.author_id = User.current.id + if training_task.save + respond_to do |format| + format.html{redirect_to project_url(new_training_project_id)} + end + else + raise "create task failed" + end + end + + # 复制项目 + def copy_project_and_module tproject, gproject + project = Project.new + project.name = tproject.name + project.is_public = tproject.is_public + project.status = tproject.status + project.description = tproject.description + project.hidden_repo = tproject.hidden_repo + project.user_id = User.current.id + project.project_type = 0 + project.project_new_type = tproject.project_new_type + project.gpid = gproject.id + project.forked_from_project_id = tproject.id + project.enabled_module_names = tproject.enabled_module_names + if project.save + project.update_attribute(:training_status,2) + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => User.current, :roles => [r]) + if ProjectScore.where("project_id=?", project.id).first.nil? + ProjectScore.create(:project_id => project.id, :score => false) + end + project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => project.id) + user_grades = UserGrade.create(:user_id => User.current.id, :project_id => project.id) + Rails.logger.debug "UserGrade created: #{user_grades.to_json}" + project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type,:grade => 0) + Rails.logger.debug "ProjectStatus created: #{project_status.to_json}" + project.members << m + project.project_infos << project_info + copy_repository(project, gproject) + return project + else + respond_to do |format| + format.html { render :action => 'forked', :layout => 'base_projects'} + format.api { render_validation_errors(@project) } + end + end + end + + # 判断用户是否已经fork过该项目 + def has_forked?(project, user) + projects = Project.where("user_id =?", user) + projects.map(&:forked_from_project_id).detect{|s| s == @project.id}.nil? ? true : false + end + + def copy_repository(project, gproject) + repository = Repository.factory('Git') + repository.project_id = project.id + repository.type = 'Repository::Gitlab' + repository.url = gproject.name + repository.identifier = gproject.name + repository = repository.save + end + # 资源库fork弹框 def forked_pop respond_to do |format| diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index a955fa88b..35fde54ac 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -35,12 +35,13 @@ class RepositoriesController < ApplicationController before_filter :find_project_by_project_id, :only => [:new, :create, :newrepo, :stats, :quality_analysis] before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] - before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo, :to_gitlab, :forked, :export_rep_static] + before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo, :to_gitlab, :forked, :export_rep_static, :training_project_extend] # 连接gitlab # before_filter :connect_gitlab, :only => [:quality_analysis, :commit_diff] before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] - before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked, :project_archive, :quality_analysis, :commit_diff] + # before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked, :project_archive, :quality_analysis, :commit_diff] + before_filter :authorize_visible , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked, :project_archive, :quality_analysis, :commit_diff] # 版本库新增权限 # before_filter :show_rep, :only => [:show, :stats, :revisions, :revision, :diff, :commit_diff ] accept_rss_auth :revisions @@ -132,6 +133,94 @@ class RepositoriesController < ApplicationController # send_file "/path/to/file.zip" end + # 开启实训项目,学生会fork一个项目并自动发送任务 + def training_project_extend + @project = Project.find(params[:id]) + @repository = Repository.where("project_id =? and type =?", @project.id, "Repository::Gitlab") + # 如果当前用户已经fork过该项目,不会新fork项目,则跳至已fork的项 + unless has_forked?(@project, User.current) + project = project_from_current_project(@project.id, User.current.id) + redirect_to project_path(project) + else + ActiveRecord::Base.transaction do + g = Gitlab.client + if User.current.gid.nil? + begin + s = Trustie::Gitlab::Sync.new + s.sync_user(User.current) + ensure + logger.error "Syn user failed ==>#{User.current.id}" + end + end + gproject = g.fork(@project.gpid, User.current.gid) + if gproject + new_training_project = copu_project_and_module(@project, gproject) + forked_count = @project.forked_count.to_i + 1 + @project.update_attributes(:forked_count => forked_count) + # 发布实训任务,只发布实训任务的第一个 + publish_training_tasks(@project, new_training_project) + end + end + end + end + + def copu_project_and_module tproject, gproject + project = Project.new + project.name = tproject.name + project.is_public = tproject.is_public + project.status = tproject.status + project.description = tproject.description + project.hidden_repo = tproject.hidden_repo + project.user_id = User.current.id + project.project_type = 0 + project.project_new_type = tproject.project_new_type + project.gpid = gproject.id + project.forked_from_project_id = tproject.id + project.enabled_module_names = tproject.enabled_module_names + if project.save + project.update_attribute(:training_status,1) + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => User.current, :roles => [r]) + if ProjectScore.where("project_id=?", project.id).first.nil? + ProjectScore.create(:project_id => project.id, :score => false) + end + project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => project.id) + user_grades = UserGrade.create(:user_id => User.current.id, :project_id => project.id) + Rails.logger.debug "UserGrade created: #{user_grades.to_json}" + project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type,:grade => 0) + Rails.logger.debug "ProjectStatus created: #{project_status.to_json}" + project.members << m + project.project_infos << project_info + copy_repository(project, gproject) + return project + else + respond_to do |format| + format.html { render :action => 'forked', :layout => 'base_projects'} + format.api { render_validation_errors(@project) } + end + end + end + + # REDO: 如果实训项目还没有创建任务的时候应该跳出 + def publish_training_tasks original_project, new_training_project + original_task = TrainingTask.where(:project_id => original_project.id, :position => 1).first + training_task = TrainingTask.new + training_task.save_attachments(params[:attachments] || (params[:training_task] && params[:training_task][:uploads])) + training_task.subject = original_task.subject + training_task.description = original_task.description + training_task.position = original_task.position + training_task.project_id = new_training_project.id + training_task.author_id = User.current.id + if training_task.save + respond_to do |format| + format.html{redirect_to project_training_tasks_url(:project_id => new_training_project.id)} + end + else + raise "create task failed" + end + end + + # 判断用户是否已经fork过该项目 def has_forked?(project, user) projects = Project.where("user_id =?", user) @@ -183,6 +272,7 @@ class RepositoriesController < ApplicationController format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => project.id) } format.js end + return project else respond_to do |format| format.html { render :action => 'forked', :layout => 'base_projects'} @@ -396,11 +486,12 @@ update redirect_to :controller => 'repositories', :action => 'show', :id => @project.id, to: 'gitlab' end + # 权限: + # 如果项目隐藏了版本库,则非项目成员及项目报告人员不能够访问版本库 + # 如果没有隐藏版本库,只要项目公开,其它成员都可以看到版本库 + # 项目关联了课程,课程的老师是可以看到版本库的 + # 超级管理员可以看到项目版本库 def show - if !User.current.admin? && (@project.hidden_repo && (role_of_members_in_project(@project.id, User.current.id) == "Reporter" || !is_project_member?(User.current.id, @project.id))) - render_403 - return - else # 顶部导航 @project_menu_type = 5 @@ -410,7 +501,6 @@ update if request.xhr? @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) else - unless @entries.blank? @changesets_latest_coimmit = @g.rep_last_changes(@project.gpid, :rev => @rev, :path => @path) # @g.rep_last_changes(@project.gpid, :rev => @rev, :path => @path) # 总的提交数 @@ -426,7 +516,6 @@ update # token值解密 # gitlab_token = aes_dicrypt("priEn3UwXfJs3Pmy", token) @zip_path = Gitlab.endpoint.to_s + "/projects/" + @project.gpid.to_s + "/repository/archive?&private_token=" + token - end @creator = @project.owner.to_s gitlab_address = Redmine::Configuration['gitlab_address'] @@ -448,7 +537,6 @@ update render :layout => 'base_projects' end end - end alias_method :browse, :show @@ -698,6 +786,19 @@ update end end + def authorize_visible + allowed = authorize_allowed(params[:controller], params[:action], global = false) + if allowed || User.current.admin? || (@project.hidden_repo && User.current.member_of?(@project) && !role_of_members_in_project(@project.id, User.current.id) == "Reporter") + true + else + if @project && @project.archived? + render_403 :message => :notice_not_authorized_archived_project + else + deny_access + end + end + end + private # 更新项目统计数 def update_commits_count project, count diff --git a/app/controllers/training_tasks_controller.rb b/app/controllers/training_tasks_controller.rb new file mode 100644 index 000000000..115754ff2 --- /dev/null +++ b/app/controllers/training_tasks_controller.rb @@ -0,0 +1,388 @@ +# encoding: utf-8 +class TrainingTasksController < ApplicationController + + layout 'base_projects' + before_filter :find_project, :only => [:index, :new, :create, :update_form, :issue_commits, :commit_for_issue, :issue_commit_delete, :destroy] + before_filter :allow_manager, :only => [:index, :show] + before_filter :allow_members, :only => [:new, :create] + before_filter :build_new_task_from_params, :only => [:new, :create, :update_form] + before_filter :find_training_task, :only => [:show, :edit, :update, :add_journal, :complete_training_task] + + # before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] + + # before_filter :authorize, :except => [:index, :show] + # before_filter :authorize, :except => [:new, :index,:add_journal, :add_journal_in_org,:delete_journal,:reply,:add_reply, :issue_commits, :commit_for_issue, :issue_commit_delete] + + before_filter :check_for_default_issue_status, :only => [:new, :create] + before_filter :build_new_task_from_params, :only => [:new, :create, :update_form] + accept_rss_auth :index, :show + accept_api_auth :index, :show, :create, :update, :destroy + + rescue_from Query::StatementInvalid, :with => :query_statement_invalid + + helper :journals + helper :projects + include ProjectsHelper + helper :custom_fields + include CustomFieldsHelper + helper :issue_relations + include IssueRelationsHelper + helper :watchers + include WatchersHelper + helper :attachments + include AttachmentsHelper + helper :queries + include QueriesHelper + helper :repositories + include RepositoriesHelper + helper :sort + include SortHelper + include IssuesHelper + helper :timelog + include Redmine::Export::PDF + helper :project_score + include ApplicationHelper + + def issue_commit_delete + commit_id = params[:commit_id].split(",") + issue_commit_ids = params[:issue_commit_ids] + # issue_id存在则为issue详情或者编辑的时候,否则为新建Issue + # 编辑和详情的时候需要在数据库中删除记录,新建的时候在内存中删除 + if params[:issue_id] + commit_issue = CommitIssues.where(:commit_id => commit_id, :issue_id => params[:issue_id], :project_id => @project.id).first + commit_issue.delete if commit_issue + end + @issue_commit_ids = issue_commit_ids - commit_id + end + # over + + def index + # 顶部导航 + @project_menu_type = 11 + @training_tasks = TrainingTask.where(:project_id => @project.id).order('position desc') + @training_tasks_count = @training_tasks.count + @limit = 10 + @is_remote = true + @training_tasks_pages = Paginator.new @training_tasks_count, @limit, params['page'] || 1 + @offset ||= @training_tasks_pages.offset + @training_tasks = paginateHelper @training_tasks, @limit + respond_to do |format| + format.js + format.html + end + rescue ActiveRecord::RecordNotFound + render_404 + end + + def show + # 顶部导航 + @project_menu_type = 11 + # 打开编辑内容 + @is_edit = true unless params[:edit].nil? + @jour_reply = Journal.new + @journals = @training_task.journals.includes(:user, :details).reorder("#{Journal.table_name}.id desc").all + @journals = get_no_children_comments_all @journals + @journals.each_with_index {|j,i| j.indice = i+1} + @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @training_task.project) + @journals.reverse! if User.current.wants_comments_in_reverse_order? + @journal = Journal.new(:journalized => @training_task) + respond_to do |format| + format.html + format.js + end + end + + # Add a new issue + # The new issue will be created from an existing one if copy_from parameter is given + def new + # 顶部导航 + @project_menu_type = 11 + respond_to do |format| + format.html { render :action => 'new', :layout => 'base_projects' } + end + end + + # 用户发布新任务 + def create + @training_task.save_attachments(params[:attachments] || (params[:training_task] && params[:training_task][:uploads])) + @training_task.subject = params[:training_task][:subject] + @training_task.description = params[:training_task][:description] + @training_task.tracker_id = params[:training_task][:tracker_id] + @training_task.position = params[:training_task][:position].to_i + if @training_task.save + respond_to do |format| + format.html { + render_attachment_warning_if_needed(@training_task) + redirect_to training_task_url(@training_task) + } + end + return + else + respond_to do |format| + format.html { render :action => 'new' } + end + end + end + + def complete_training_task + taskId = @training_task.position + end + + def edit + @project_menu_type = 11 + # 修改实例变量的值 + return unless build_new_task_from_params + respond_to do |format| + format.html {render :layout => 'base_projects' } + format.xml { } + end + end + + # 用户编辑更改issue + def update + return unless build_new_task_from_params + @training_task.save_attachments(params[:attachments] || (params[:training_task] && params[:training_task][:uploads])) + if @training_task.update_attributes(params[:training_task]) + respond_to do |format| + format.js + format.html{redirect_to training_task_url(@training_task)} + end + end + end + + def update_user_issue_detail(issue, params) + case params[:type] + when "status" + issue.update_attribute(:status_id, params[:status_id]) + when "assigned" + issue.update_attribute(:assigned_to_id, params[:assigned_to_id]) + when "ratio" + issue.update_attribute(:done_ratio, params[:done_ratio]) + when "prior" + issue.update_attribute(:priority_id, params[:priority_id]) + end + end + + # Updates the issue form when changing the project, status or tracker + # on issue creation/update + def update_form + end + + def destroy + return unless build_new_task_from_params + @training_task.destroy + respond_to do |format| + if params[:type] == "project_show" + format.html {redirect_to project_path(@project)} + else + format.html {redirect_to project_training_tasks_path(@project)} + end + end + end + + def add_journal + if User.current.logged? + jour = Journal.new + jour.user_id = User.current.id + jour.notes = params[:notes] + jour.journalized = @training_task + jour.save_attachments(params[:attachments]) + jour.save + update_user_activity(@training_task.class, @training_task.id) + update_forge_activity(@training_task.class, @training_task.id) + # @allowed_statuses = @training_task.new_statuses_allowed_to(User.current) + @user_activity_id = params[:user_activity_id] + @priorities = IssuePriority.active + + respond_to do |format| + # Issue详情单独处理 + if params[:is_issue_show] + format.js{ redirect_to training_task_path(@training_task) } + else + format.js + end + end + end + end + + def add_journal_in_org + if User.current.logged? + jour = Journal.new + jour.user_id = User.current.id + jour.notes = params[:notes] + jour.journalized = @issue + jour.save + org_activity = OrgActivity.where("org_act_type='Issue' and org_act_id =#{@issue.id}").first + org_activity.updated_at = jour.created_on + org_activity.save + @user_activity_id = params[:user_activity_id] + respond_to do |format| + format.js + end + end + end + + #对某个journ回复,显示回复框 + def reply + @training_task = TrainingTask.find(params[:id]) + @jour = Journal.find(params[:journal_id]) + respond_to do |format| + format.js + end + end + + #给issue添加journ。回复内容包含 对某个被回复的journ的内容 + def add_reply + if User.current.logged? + jour = Journal.find(params[:journal_id]) + @training_task = TrainingTask.find params[:id] + @project = @training_task.project + # @allowed_statuses = @training_task.new_statuses_allowed_to(User.current) + @priorities = IssuePriority.active + new_jour = Journal.new + new_jour.user_id = User.current.id + new_jour.reply_id = params[:journal_id] + new_jour.parent_id = jour.id + new_jour.notes = params[:content] + new_jour.journalized = @training_task + new_jour.save_attachments(params[:attachments]) + # new_jour = @issue.journals.build(:user_id => User.current.id, :reply_id => params[:journal_id], :notes => params[:content], :parent_id => jour.id) + @user_activity_id = params[:user_activity_id] + if new_jour.save + update_user_activity(@training_task.class, @training_task.id) + update_forge_activity(@training_task.class, @training_task.id) + respond_to do |format| + if params[:is_issue_show] + format.js{ redirect_to training_task_path(@training_task) } + else + format.js + end + end + end +=begin + @journals = @training_task.journals.includes(:user, :details).reorder("#{Journal.table_name}.id desc").all + @journals = get_no_children_comments_all @journals + @journals.each_with_index {|j,i| j.indice = i+1} + @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @training_task.project) + @journals.reverse! if User.current.wants_comments_in_reverse_order? + @journal = Journal.new(:journalized => @training_task) + respond_to do |format| + #if params[:is_issue_show] + # format.js{ redirect_to training_task_path(@training_task) } + #else + format.js + #end + end +=end + end + end + + # 需要刷新详情页面,代码同步一致 + # 获取project和issue状态是为了刷新页面 + # 值所以用delete是因为issue和journal在act_as_attachment中是同一个类型 + # 非动态页面直接刷新,动态页面手动刷新 + def delete_journal + @is_project = params[:is_project] + @training_task = TrainingTask.find(params[:id]) + begin + forge_acts = ForgeMessage.where(:forge_message_type => "Journal", :forge_message_id => params[:journal_id]) + forge_acts.destroy_all unless forge_acts.empty? + at_message = AtMessage.where(:at_message_type => "Journal", :at_message_id => params[:journal_id]) + at_message.destroy_all unless at_message.empty? + Journal.delete(params[:journal_id]) + rescue Exception => e + puts e + end + @user_activity_id = params[:user_activity_id] + respond_to do |format| + if @user_activity_id + format.js + else + format.js{ redirect_to training_task_path(@training_task)} + end + end + end + + private + + def find_project + project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) + @project = Project.find(project_id) + rescue ActiveRecord::RecordNotFound + render_404 + end + + def allow_members + if !(User.current.member_of?(@project) || User.current.admin?) + render_403 + end + end + + def allow_manager + if !(User.current.admin? || is_project_member?(User.current.id, @project.id)) + return render_403 + end + end + + def retrieve_previous_and_next_issue_ids + retrieve_query_from_session + if @query + sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) + sort_update(@query.sortable_columns, 'issues_index_sort') + limit = 500 + issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) + if (idx = issue_ids.index(@issue.id)) && idx < limit + if issue_ids.size < 500 + @issue_position = idx + 1 + @issue_count = issue_ids.size + end + @prev_issue_id = issue_ids[idx - 1] if idx > 0 + @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) + end + end + end + + def build_new_task_from_params + if params[:id].blank? + @training_task = TrainingTask.new + @training_task.project = @project + else + @training_task = @project.training_tasks.find(params[:id]) + end + + @training_task.project = @project + @training_task.author ||= User.current + end + + def check_for_default_issue_status + if IssueStatus.default.nil? + render_error l(:error_no_default_issue_status) + return false + end + end + + def parse_params_for_bulk_issue_attributes(params) + attributes = (params[:issue] || {}).reject {|k,v| v.blank?} + attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} + if custom = attributes[:custom_field_values] + custom.reject! {|k,v| v.blank?} + custom.keys.each do |k| + if custom[k].is_a?(Array) + custom[k] << '' if custom[k].delete('__none__') + else + custom[k] = '' if custom[k] == '__none__' + end + end + end + attributes + end + + # Find the issue whose id is the :id parameter + # Raises a Unauthorized exception if the issue is not visible + def find_training_task + @training_task = TrainingTask.find(params[:id]) + @project = @training_task.project + rescue ActiveRecord::RecordNotFound + render_404 + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f54b5ba3b..7113d0c98 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -310,6 +310,11 @@ class UsersController < ApplicationController @user_activity_id = params[:user_activity_id] @activity_id = params[:activity_id] @type = 'Issue' + when 'Training_task' + @reply = Journal.find params[:reply_id] + @user_activity_id = params[:user_activity_id] + @activity_id = params[:activity_id] + @type = 'Training_task' when 'Syllabus' @reply = JournalsForMessage.find params[:reply_id] @type = 'Syllabus' @@ -417,6 +422,11 @@ class UsersController < ApplicationController comment = @root.journals.build(:user_id => User.current.id, :reply_id => params[:reply_id], :notes => params[:reply_message], :parent_id => reply.id) comment.save @is_project = params[:is_project] if params[:is_project] + when 'TrainingTask' + @root = reply.training_task + comment = @root.journals.build(:user_id => User.current.id, :reply_id => params[:reply_id], :notes => params[:reply_message], :parent_id => reply.id) + comment.save + @is_project = params[:is_project] if params[:is_project] when 'Syllabus' @root = Syllabus.find reply.jour_id options = {:notes => params[:reply_message], :reply_id => reply.user_id,:user_id => User.current.id,:m_parent_id => params[:reply_id].to_i,:m_reply_id => params[:reply_id].to_i, :root_id => reply.root_id} @@ -4402,6 +4412,11 @@ class UsersController < ApplicationController @journals = @activity.journals.reorder("created_on desc") @user_activity_id = params[:div_id].to_i if params[:div_id] @user_activity = ForgeActivity.where(params[:div_id].to_i).first if params[:div_id] + when 'is_project_training_task' + @activity = TrainingTask.where('id = ?', params[:id].to_i).first + @journals = @activity.journals.reorder("created_on desc") + @user_activity_id = params[:div_id].to_i if params[:div_id] + @user_activity = ForgeActivity.where(params[:div_id].to_i).first if params[:div_id] when 'BlogComment' obj = BlogComment.where('id = ?', params[:id].to_i).first @user_activity_id = params[:div_id].to_i if params[:div_id] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 545062bca..a8c3d2de4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3697,7 +3697,7 @@ def get_reply_by_type type, reply_id reply = OrgDocumentComment.find reply_id when 'News','Comment' reply = Comment.find reply_id - when 'Issue' + when 'Issue', 'TrainingTask' reply = Journal.find reply_id when 'Journal' reply = Journal.find reply_id diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 653afef62..f9b185ba4 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -544,6 +544,8 @@ module ProjectsHelper result = "manage_versions" elsif user.allowed_to?(:manage_repository, @project) result = "manage_repository" + elsif @project.is_training_project? + result = "training_task" end result end diff --git a/app/helpers/training_tasks_helper.rb b/app/helpers/training_tasks_helper.rb new file mode 100644 index 000000000..bb52e8a79 --- /dev/null +++ b/app/helpers/training_tasks_helper.rb @@ -0,0 +1,44 @@ +module TrainingTasksHelper + include ApplicationHelper + include TagsHelper + require 'iconv' + + # Returns the textual representation of a journal details + # as an array of strings + def details_to_strings(details, no_html=false, options={}) + options[:only_path] = (options[:only_path] == false ? false : true) + options[:token] = options[:token] if options[:token] + strings = [] + values_by_field = {} + details.each do |detail| + + if detail.property == 'cf' + field_id = detail.prop_key + field = CustomField.find_by_id(field_id) + if field && field.multiple? + values_by_field[field_id] ||= {:added => [], :deleted => []} + if detail.old_value + values_by_field[field_id][:deleted] << detail.old_value + end + if detail.value + values_by_field[field_id][:added] << detail.value + end + next + end + end + strings << show_detail(detail, no_html, options) + + end + values_by_field.each do |field_id, changes| + detail = JournalDetail.new(:property => 'cf', :prop_key => field_id) + if changes[:added].any? + detail.value = changes[:added] + strings << show_detail(detail, no_html, options) + elsif changes[:deleted].any? + detail.old_value = changes[:deleted] + strings << show_detail(detail, no_html, options) + end + end + strings + end +end diff --git a/app/models/journal.rb b/app/models/journal.rb index f14484a31..822d7beca 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -23,7 +23,8 @@ class Journal < ActiveRecord::Base belongs_to :journalized, :polymorphic => true,:touch => true # added as a quick fix to allow eager loading of the polymorphic association # since always associated to an issue, for now - belongs_to :issue, :foreign_key => :journalized_id,:touch => true + belongs_to :issue, :foreign_key => :journalized_id, :touch => true + belongs_to :training_task, :foreign_key => :journalized_id, :touch => true belongs_to :user has_many :details, :class_name => "JournalDetail", :dependent => :delete_all @@ -192,27 +193,44 @@ class Journal < ActiveRecord::Base def act_as_forge_message receivers = [] # 直接回复 - if self.user_id != self.issue.author_id - receivers << self.issue.author_id - end - if self.user_id != self.issue.assigned_to_id && self.issue.assigned_to_id != self.issue.author_id # 指派人不是自己的话,则给指派人发送 - receivers << self.issue.assigned_to_id - end - receivers.each do |r| - self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.issue.project_id, :viewed => false) + if self.journalized_type == 'Issue' + if self.user_id != self.issue.author_id + receivers << self.issue.author_id + end + if self.user_id != self.issue.assigned_to_id && self.issue.assigned_to_id != self.issue.author_id # 指派人不是自己的话,则给指派人发送 + receivers << self.issue.assigned_to_id + end + receivers.each do |r| + self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.issue.project_id, :viewed => false) + end + elsif self.journalized_type == 'TrainingTask' + if self.user_id != self.training_task.author_id + receivers << self.training_task.author_id + end + receivers.each do |r| + self.forge_messages << ForgeMessage.new(:user_id => r, :project_id => self.training_task.project, :viewed => false) + end end end # 更新用户分数 -by zjc def be_user_score + if (self.journalized_type == 'Issue') #新建了缺陷留言且留言不为空,不为空白 - if !self.notes.nil? && self.notes.gsub(' ','') != '' - #协同得分加分 - UserScore.joint(:post_issue_message, self.user,self.issue.author,self, { message_id: self.id }) - update_messges_for_issue(self.user,1) - update_messges_for_issue(self.user,2,self.issue.project) + if !self.notes.nil? && self.notes.gsub(' ','') != '' + #协同得分加分 + UserScore.joint(:post_issue_message, self.user,self.issue.author,self, { message_id: self.id }) + update_messges_for_issue(self.user,1) + update_messges_for_issue(self.user,2,self.issue.project) + end + elsif( self.journalized_type == 'TrainingTask') + if !self.notes.nil? && self.notes.gsub(' ','') != '' + #协同得分加分 + UserScore.joint(:post_issue_message, self.user, self.training_task.author, self, { message_id: self.id }) + update_messges_for_issue(self.user, 1) + update_messges_for_issue(self.user, 2, self.training_task.project) + end end - end # 减少用户分数 -by zjc def down_user_score @@ -237,12 +255,18 @@ class Journal < ActiveRecord::Base # issue留言总数更新 def add_journals_count - if !self.issue.project.nil? && self.journalized_type == "Issue" && !self.issue.project.project_score.nil? - project = self.issue.project - project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + if self.journalized_type == 'Issue' + if !self.issue.project.nil? && self.journalized_type == "Issue" && !self.issue.project.project_score.nil? + project = self.issue.project + project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + end + elsif self.journalized_type == 'TrainingTask' + if !self.training_task.project.nil? && self.journalized_type == "TrainingTask" && !self.training_task.project.project_score.nil? + project = self.training_task.project + project.project_score.update_attribute(:issue_journal_num, project.project_score.issue_journal_num + 1) + end end end - # 回复issue的时候,更新issue的时候 def update_issue_time if self.journalized_type == "Issue" diff --git a/app/models/praise_tread.rb b/app/models/praise_tread.rb index 63ba89b18..d0c067437 100644 --- a/app/models/praise_tread.rb +++ b/app/models/praise_tread.rb @@ -1,98 +1,100 @@ -class PraiseTread < ActiveRecord::Base - attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread - belongs_to :user - belongs_to :praise_tread_object, polymorphic: true - #after_create :be_user_score - #after_destroy :down_user_score - include UserScoreHelper - def self.find_object_by_type_and_id(type,id) - @obj = nil - case type - when 'User' - @obj = User.find_by_id(id) - when 'Issue' - @obj = Issue.find_by_id(id) - when 'Project' - @obj = Project.find_by_id(id) - when 'Bid' - @obj = Bid.find_by_id(id) - when 'Contest' - @obj = Contest.find_by_id(id) - when 'Memo' - @obj = Memo.find_by_id(id) - when 'Message' - @obj = Message.find_by_id(id) - end - return @obj - end - - # 获取裁定对象为Message时Message所属的项目或课程 - def project - project = nil - if self.praise_tread_object_type == 'Message' || self.praise_tread_object_type == 'Issues' - obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) - project = obj.project - end - project - end - - #更新用户分数 - by zjc - def be_user_score - #踩贴吧或讨论区帖子 - if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') - obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) - target_user = obj.author - UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id }) - update_tread(self.user,1) - update_tread(target_user,1) - unless self.project.nil? - update_tread(self.user,2,self.project) - update_tread(target_user,2,self.project) - end - #顶贴吧或讨论区帖子 - elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') - obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) - target_user = obj.author - UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id }) - update_praise(target_user,1) - unless self.project.nil? - update_praise(target_user,2,self.project) - end - #更新用户等级 - UserLevels.update_user_level(target_user) - end - end - - def down_user_score - #踩贴吧或讨论区帖子 - if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') - obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) - target_user = obj.author - #UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id }) - update_tread(self.user,1) - update_tread(target_user,1) - unless self.project.nil? - update_tread(self.user,2,self.project) - update_tread(target_user,2,self.project) - end - #顶贴吧或讨论区帖子 - elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') - obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) - target_user = obj.author - #UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id }) - update_praise(target_user,1) - unless self.project.nil? - update_praise(target_user,2,self.project) - end - #更新用户等级 - UserLevels.update_user_level(target_user) - end - end - - def self.praised(activity) - self.where(praise_tread_object_id: activity.id, - praise_tread_object_type: activity.class.to_s, - user_id: User.current.id).empty? - end - -end +class PraiseTread < ActiveRecord::Base + attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread + belongs_to :user + belongs_to :praise_tread_object, polymorphic: true + #after_create :be_user_score + #after_destroy :down_user_score + include UserScoreHelper + def self.find_object_by_type_and_id(type,id) + @obj = nil + case type + when 'User' + @obj = User.find_by_id(id) + when 'Issue' + @obj = Issue.find_by_id(id) + when 'TrainingTask' + @obj = TrainingTask.find_by_id(id) + when 'Project' + @obj = Project.find_by_id(id) + when 'Bid' + @obj = Bid.find_by_id(id) + when 'Contest' + @obj = Contest.find_by_id(id) + when 'Memo' + @obj = Memo.find_by_id(id) + when 'Message' + @obj = Message.find_by_id(id) + end + return @obj + end + + # 获取裁定对象为Message时Message所属的项目或课程 + def project + project = nil + if self.praise_tread_object_type == 'Message' || self.praise_tread_object_type == 'Issues' + obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) + project = obj.project + end + project + end + + #更新用户分数 - by zjc + def be_user_score + #踩贴吧或讨论区帖子 + if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') + obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) + target_user = obj.author + UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id }) + update_tread(self.user,1) + update_tread(target_user,1) + unless self.project.nil? + update_tread(self.user,2,self.project) + update_tread(target_user,2,self.project) + end + #顶贴吧或讨论区帖子 + elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') + obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) + target_user = obj.author + UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id }) + update_praise(target_user,1) + unless self.project.nil? + update_praise(target_user,2,self.project) + end + #更新用户等级 + UserLevels.update_user_level(target_user) + end + end + + def down_user_score + #踩贴吧或讨论区帖子 + if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') + obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) + target_user = obj.author + #UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id }) + update_tread(self.user,1) + update_tread(target_user,1) + unless self.project.nil? + update_tread(self.user,2,self.project) + update_tread(target_user,2,self.project) + end + #顶贴吧或讨论区帖子 + elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message') + obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) + target_user = obj.author + #UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id }) + update_praise(target_user,1) + unless self.project.nil? + update_praise(target_user,2,self.project) + end + #更新用户等级 + UserLevels.update_user_level(target_user) + end + end + + def self.praised(activity) + self.where(praise_tread_object_id: activity.id, + praise_tread_object_type: activity.class.to_s, + user_id: User.current.id).empty? + end + +end diff --git a/app/models/project.rb b/app/models/project.rb index e48a5296c..d5d2da038 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -57,6 +57,7 @@ class Project < ActiveRecord::Base has_many :enabled_modules, :dependent => :delete_all has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" has_many :issues, :dependent => :destroy, :include => [:status, :tracker],:order => "issues.id ASC" + has_many :training_tasks, :dependent => :destroy, :order => "training_tasks.id ASC" has_many :issue_changes, :through => :issues, :source => :journals has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" has_many :time_entries, :dependent => :delete_all @@ -159,6 +160,7 @@ class Project < ActiveRecord::Base after_create :create_board_sync,:acts_as_forge_activities, :create_project_ealasticsearch_index before_destroy :delete_all_members,:delete_project_ealasticsearch_index after_update :update_project_ealasticsearch_index + def remove_references_before_destroy return if self.id.nil? Watcher.delete_all ['watchable_id = ?', id] @@ -193,6 +195,7 @@ class Project < ActiveRecord::Base scope :project_entities, -> { where(project_type: ProjectType_project) } scope :course_entities, -> { where(project_type: ProjectType_course) } scope :indexable,lambda { where('is_public = 1')} #用于elastic建索引的scope + def self.search(query) __elasticsearch__.search( { @@ -219,6 +222,17 @@ class Project < ActiveRecord::Base } ) end + + # 判断项目是否为实训项目 + def is_training_project? + (!self.enabled_modules.where("name = 'training_tasks'").empty? && self.training_status == 1) ? true : false + end + + # 判断项目是否为开启实训的项目(eg:学生开启实训) + def is_child_training_project? + self.training_status == 2 ? true :false + end + def new_course self.where('project_type = ?', 1) end diff --git a/app/models/training_task.rb b/app/models/training_task.rb new file mode 100644 index 000000000..3ddf99286 --- /dev/null +++ b/app/models/training_task.rb @@ -0,0 +1,28 @@ +class TrainingTask < ActiveRecord::Base + attr_accessible :description, :project_id, :subject, :tracker_id + belongs_to :project,:touch=> true + belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' + belongs_to :tracker + has_many :journals, :as => :journalized, :dependent => :destroy + has_many :visible_journals, + :class_name => 'Journal', + :as => :journalized, + :conditions => Proc.new { + ["(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(User.current, :view_private_notes)}))", false] + }, + :readonly => true + has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + has_one :praise_tread_cache, as: :object, dependent: :destroy + has_many :forge_acts, :class_name => 'ForgeActivity', :as => :forge_act, :dependent => :destroy + + acts_as_attachable + + validates_presence_of :subject, :author + validates_length_of :subject, :maximum => 255 + after_create :act_as_forge_activity + + def act_as_forge_activity + self.forge_acts << ForgeActivity.new(:user_id => self.author_id, :project_id => self.project_id) + end + +end diff --git a/app/models/user.rb b/app/models/user.rb index 72e89020e..0d2295ee8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1239,6 +1239,16 @@ class User < Principal Thread.current[:current_user] ||= User.anonymous end + def user_real_extension + ue = self.user_extensions + if ue.nil? + ue = UserExtensions.new + ue.user_id = self.id + ue.save + end + ue + end + # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only # one anonymous user per database. def self.anonymous diff --git a/app/views/account/_change_user_email.html.erb b/app/views/account/_change_user_email.html.erb new file mode 100644 index 000000000..20eae1374 --- /dev/null +++ b/app/views/account/_change_user_email.html.erb @@ -0,0 +1,72 @@ +
您的账号尚未激活,请先进入您的注册邮箱,激活您的账号。
+您的账号尚未激活,请先进入您的注册邮箱(<%= @user.mail %>),激活您的账号。
<%#= link_to l(:label_mail_resend), { :controller => 'account', :action => 'resendmail',:user => @user}, :class=>"email_verify_btn mt30 ml30", :remote => true, :method => 'get' %> -如果您尚未收到激活邮件,请按照以下步骤操作:
注册成功! 请在24小时内点击邮件中的链接来激活您的账号。
-请登录邮箱(<%= @user.mail %>)收取账号激活邮件。
点击邮件中的激活链接,方可使用该账号
+
请登录邮箱(<%= @user.mail %>)收取账号激活邮件。
点击邮件中的激活链接,方可使用该账号
<%= l(:label_check_email)%> @@ -14,14 +14,15 @@
如果您一直收不到激活邮件,请按照以下步骤操作:
-<%= l(:label_rights_reserved)%><%= l(:label_license)%>
+<%= l(:label_rights_reserved)%><%= l(:label_license)%>
+<%= l(:label_rights_reserved)%><%= l(:label_license)%>
<%= l_or_humanize(m, :prefix => "project_module_").html_safe %>
-<%= check_all_links('modules-form').html_safe %>
-问题跟踪模块与实训模块不能同时选择
一旦选定并保存后,将不能修改
<%= l_or_humanize(m, :prefix => "project_module_").html_safe %>
+<%= check_all_links('modules-form').html_safe %>
+<%= @notice %>
<%= @project.name %>
@@ -24,15 +24,15 @@名称 | +名称 | 分支 | 语言 | 路径 | @@ -20,7 +20,7 @@ <% if @quality_analyses && @quality_analyses.count > 0 %> <% @quality_analyses.each do |qa| %>
<%= qa.branch %> | <%= qa.language %> | diff --git a/app/views/repositories/_forked_popbox.html.erb b/app/views/repositories/_forked_popbox.html.erb index c28bef2d6..eca24ea24 100644 --- a/app/views/repositories/_forked_popbox.html.erb +++ b/app/views/repositories/_forked_popbox.html.erb @@ -6,16 +6,17 @@|||
+ <% if activity.status == 1 %>
+
+ <% else %>
+
+ <% end %>
+
+
+
+
+ step<%= activity.position %>
+ <%= activity.subject.to_s %>
+
+ <% if activity.status == 1 %>
+ 已解决
+ <% else %>
+ 正在解决中
+ <% end %>
+
+
+
+
+ <%= format_time(activity.created_at) %>发布 +<%= format_time(activity.updated_at) %>更新 + |
+ + <% if activity.journals.count != 0 %> + + <%= activity.journals.count %> + <% end %> + | + <% if @project.is_child_training_project? %> +
+
+ <%= render :partial => 'action_status', :locals => {:activity => activity} %>
+
+ |
+ <% end %>
+
所有<%= @training_tasks_count %> |
+ + <% if @project.is_child_training_project? %> + | + <% end %> + |
---|