diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index cd1de16fc..0c61c449e 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -374,6 +374,7 @@ class CoursesController < ApplicationController def settings if User.current.allowed_to?(:as_teacher,@course) + @select_tab = params[:tab] @issue_custom_fields = IssueCustomField.sorted.all @issue_category ||= IssueCategory.new @member ||= @course.members.new diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 2bd54954a..c1e124063 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -64,32 +64,53 @@ class RepositoriesController < ApplicationController end def forked - # 被forked的标识如果不满足单个用户唯一性,则不执行fork - if is_sigle_identifier?(User.current, @repository.identifier) - # REDO: 那些人有权限forked项目 - g = Gitlab.client - gproject = g.post ("/projects/fork/#{@project.gpid}?user_id=#{User.current.gid}") - if gproject - copy_project(@project, gproject) - end + # 如果当前用户已经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 - flash[:notice] = l(:project_gitlab_fork_double_message) - redirect_to settings_project_url(@project, :tab => 'repositories') + # 单个用户只能拥有一个名字一样的版本库,否则不能fork + # if is_sigle_identifier?(User.current, @repository.identifier) + # REDO: 那些人有权限forked项目 + g = Gitlab.client + gproject = g.fork(@project.gpid, User.current.gid) + if gproject + copy_project(@project, gproject) + forked_count = @project.forked_count.to_i + 1 + @project.update_attributes(:forked_count => forked_count) + end + # else + # flash[:notice] = l(:project_gitlab_fork_double_message) + # redirect_to settings_project_url(@project, :tab => 'repositories') + # 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 + + # 获取当前用户fork过的项目 + def project_from_current_project(project, user) + project = Project.where("user_id =? and forked_from_project_id =?",user, project).first end # copy a project for fork - def copy_project(project, gproject) + def copy_project(tproject, gproject) project = Project.new - project.name = @project.name - project.is_public = @project.is_public - project.status = @project.status - project.description = @project.description - project.hidden_repo = @project.hidden_repo + 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 = @project.project_new_type + project.project_new_type = tproject.project_new_type project.gpid = gproject.id + project.forked_from_project_id = tproject.id if project.save 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]) @@ -124,16 +145,16 @@ class RepositoriesController < ApplicationController def copy_repository(project, gproject) # 避免 - if is_sigle_identifier?(project.user_id, gproject.name) + # if is_sigle_identifier?(project.user_id, gproject.name) repository = Repository.factory('Git') repository.project_id = project.id repository.type = 'Repository::Gitlab' repository.url = gproject.name repository.identifier = gproject.name repository = repository.save - else - flash[:notice] = l(:project_gitlab_create_double_message) - end + # else + # flash[:notice] = l(:project_gitlab_create_double_message) + # end end def newrepo diff --git a/app/views/courses/member.html.erb b/app/views/courses/member.html.erb index ce336373b..3aa7d4b57 100644 --- a/app/views/courses/member.html.erb +++ b/app/views/courses/member.html.erb @@ -1,9 +1,14 @@ -
-

<%= @subPage_title%>

-
-<% if @subPage_title == l(:label_student_list)%> - <%= render :partial => 'course_student', :locals => {:members => @members} %> -<% else%> - <%= render :partial => 'course_teacher', :locals => {:members => @members} %> -<% end%> - +
+

<%= @subPage_title%>

+ <% if User.current.allowed_to?(:as_teacher,@course) %> + + <%=link_to "修改角色", :controller => 'courses', :action => 'settings', :id => @course.id, :tab=>'member' %> + + <% end %> +
+<% if @subPage_title == l(:label_student_list)%> + <%= render :partial => 'course_student', :locals => {:members => @members} %> +<% else%> + <%= render :partial => 'course_teacher', :locals => {:members => @members} %> +<% end%> + diff --git a/app/views/courses/settings.html.erb b/app/views/courses/settings.html.erb index 5c3918f00..3456634d5 100644 --- a/app/views/courses/settings.html.erb +++ b/app/views/courses/settings.html.erb @@ -1,6 +1,13 @@

<%= l(:label_course_modify_settings)%>

+
@@ -25,8 +25,10 @@
ZIP
-
Fork 0
- + + +
<%= link_to "Fork", :controller => 'repositories', :action => 'forked'%> + <%= @project.forked_count.to_i %>
<% if @changesets && !@changesets.empty? %> diff --git a/db/migrate/20151203072815_add_forked_from_project_id_to_projects.rb b/db/migrate/20151203072815_add_forked_from_project_id_to_projects.rb new file mode 100644 index 000000000..f679eaecd --- /dev/null +++ b/db/migrate/20151203072815_add_forked_from_project_id_to_projects.rb @@ -0,0 +1,5 @@ +class AddForkedFromProjectIdToProjects < ActiveRecord::Migration + def change + add_column :projects, :forked_from_project_id, :integer + end +end diff --git a/db/migrate/20151204062220_add_forked_count_to_projects.rb b/db/migrate/20151204062220_add_forked_count_to_projects.rb new file mode 100644 index 000000000..79fdc2573 --- /dev/null +++ b/db/migrate/20151204062220_add_forked_count_to_projects.rb @@ -0,0 +1,5 @@ +class AddForkedCountToProjects < ActiveRecord::Migration + def change + add_column :projects, :forked_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index d84bc636c..6cc6b90c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20151203030635) do +ActiveRecord::Schema.define(:version => 20151204062220) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -970,6 +970,7 @@ ActiveRecord::Schema.define(:version => 20151203030635) do t.integer "course_group_id", :default => 0 end + add_index "members", ["course_id"], :name => "index_members_on_course_id" add_index "members", ["project_id"], :name => "index_members_on_project_id" add_index "members", ["user_id", "project_id", "course_id"], :name => "index_members_on_user_id_and_project_id", :unique => true add_index "members", ["user_id"], :name => "index_members_on_user_id" @@ -1303,27 +1304,29 @@ ActiveRecord::Schema.define(:version => 20151203030635) do end create_table "projects", :force => true do |t| - t.string "name", :default => "", :null => false + t.string "name", :default => "", :null => false t.text "description" - t.string "homepage", :default => "" - t.boolean "is_public", :default => true, :null => false + t.string "homepage", :default => "" + t.boolean "is_public", :default => true, :null => false t.integer "parent_id" t.datetime "created_on" t.datetime "updated_on" t.string "identifier" - t.integer "status", :default => 1, :null => false + t.integer "status", :default => 1, :null => false t.integer "lft" t.integer "rgt" - t.boolean "inherit_members", :default => false, :null => false + t.boolean "inherit_members", :default => false, :null => false t.integer "project_type" - t.boolean "hidden_repo", :default => false, :null => false - t.integer "attachmenttype", :default => 1 + t.boolean "hidden_repo", :default => false, :null => false + t.integer "attachmenttype", :default => 1 t.integer "user_id" - t.integer "dts_test", :default => 0 + t.integer "dts_test", :default => 0 t.string "enterprise_name" t.integer "organization_id" t.integer "project_new_type" t.integer "gpid" + t.integer "forked_from_project_id" + t.integer "forked_count" end add_index "projects", ["lft"], :name => "index_projects_on_lft" diff --git a/lib/gitlab-cli/lib/gitlab/client/projects.rb b/lib/gitlab-cli/lib/gitlab/client/projects.rb index 3fab4347f..8d91edd06 100644 --- a/lib/gitlab-cli/lib/gitlab/client/projects.rb +++ b/lib/gitlab-cli/lib/gitlab/client/projects.rb @@ -241,8 +241,9 @@ class Gitlab::Client # Forks a project into the user namespace of the authenticated user. # @param [Integer] - The ID of the project to be forked - def fork(id) - post("/projects/fork/#{id}") + def fork(gpid, gid) + post ("/projects/fork/#{gpid}?user_id=#{gid}") + # post("/projects/fork/#{id}") end # Mark this project as forked from the other