You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

projects_controller.rb 50 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
10 years ago
10 years ago
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
9 years ago
9 years ago
​
9 years ago
9 years ago
​
9 years ago
9 years ago
​
​
9 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
9 years ago
9 years ago
​
​
​
​
​
​
9 years ago
​
​
9 years ago
​
9 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
13 years ago
13 years ago
​
​
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. # encoding: utf-8
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2013 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # Time 2015-01-28 16:34:21
  19. # Author lizanle
  20. # Description 封装代码,简化代码,格式化代码,
  21. class ProjectsController < ApplicationController
  22. layout 'base_projects'
  23. before_filter :authorize1, :only => [:show]
  24. # menu_item :overview, :only => :show
  25. # menu_item :roadmap, :only => :roadmap
  26. # menu_item :settings, :only => :settings
  27. # menu_item :homework, :only => [:homework, :new_homework]
  28. # menu_item :feedback, :only => :feedback
  29. # menu_item :share, :only => :share
  30. skip_before_filter :verify_authenticity_token, :only => [:training_task_status]
  31. skip_before_filter :check_if_login_required, :only => [:training_task_status]
  32. before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,
  33. :view_homework_attaches,:join_project, :project_home, :training_execute, :training_task_status]
  34. before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen,:view_homework_attaches,:course]
  35. before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches]
  36. before_filter :require_admin, :only => [ :copy, :unarchive, :calendar]
  37. before_filter :require_admin_or_manager, :only => [ :destroy]
  38. before_filter :file
  39. # 除非项目内人员,不可查看成员, TODO: 完了写报表里去
  40. # before_filter :memberAccess, only: :member
  41. # accept_rss_auth :index
  42. accept_api_auth :index, :show, :create, :update, :destroy
  43. after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
  44. if controller.request.post?
  45. controller.send :expire_action, :controller => 'welcome', :action => 'robots'
  46. end
  47. end
  48. helper :bids
  49. include BidsHelper
  50. helper :contests
  51. include ContestsHelper
  52. helper :sort
  53. include SortHelper
  54. helper :custom_fields
  55. include CustomFieldsHelper
  56. helper :issues
  57. helper :queries
  58. include QueriesHelper
  59. helper :repositories
  60. include RepositoriesHelper
  61. include ProjectsHelper
  62. helper :members
  63. helper :activities
  64. helper :documents
  65. helper :watchers
  66. # helper :watcherlist
  67. helper :words
  68. helper :project_score
  69. helper :user_score
  70. include UsersHelper
  71. ### added by william
  72. include ActsAsTaggableOn::TagsHelper
  73. include ApplicationHelper
  74. # 仅仅为了转换Gitlab地址
  75. def project_home
  76. rep = params[:rep]
  77. login = params[:username]
  78. begin
  79. user = User.find_by_login(login)
  80. project = Project.find_by_sql("SELECT projects.* FROM `repositories`,`projects` where repositories.project_id = projects.id and projects.user_id =#{user.try(:id)} and repositories.identifier='#{rep}'").first
  81. respond_to do |format|
  82. format.html{redirect_to(:controller => 'repositories', :action => 'show', :id => project.id, :repository_id => rep)}
  83. end
  84. rescue
  85. render_404
  86. return
  87. end
  88. end
  89. #查找组织
  90. def search_public_orgs_not_in_project
  91. condition = '%%'
  92. if !params[:name].nil?
  93. condition = "%#{params[:name].strip}%".gsub(" ","")
  94. end
  95. limit = 15
  96. project_org_ids = OrgProject.find_by_sql("select distinct organization_id from org_projects where project_id = #{params[:id]}").map(&:organization_id) << 0
  97. @orgs_not_in_project = User.current.organizations.where("organizations.id not in (#{project_org_ids.join(',')}) and organizations.name like ?", condition).page(params[:page].to_i || 1).per(limit)
  98. @org_count = User.current.organizations.where("organizations.id not in (#{project_org_ids.join(',')}) and organizations.name like '#{condition}'").count
  99. # if project_org_ids.empty?
  100. # @orgs_not_in_project = Organization.where("(is_public or creator_id =?) = 1 and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(limit)
  101. # @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count
  102. # else
  103. # project_org_ids = "(" + project_org_ids.join(',') + ")"
  104. # @orgs_not_in_project = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(limit)
  105. # @org_count = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count
  106. # end
  107. @orgs_page = Paginator.new @org_count, limit,params[:page]
  108. @no_roll_hint = params[:hint_flag]
  109. #render :json => {:orgs => @orgs_not_in_project, :count => @org_count}.to_json
  110. respond_to do |format|
  111. format.js
  112. end
  113. end
  114. def index
  115. render_404
  116. end
  117. def courserender_404
  118. end
  119. # Time 2015-01-29 11:19:11
  120. # Author lizanle
  121. # Description 项目搜索方法
  122. def search
  123. # 如果有名字,就按名字搜索,如果没有,就展示所有,用Karminari分页
  124. if params[:name].present?
  125. @project_pages = Project.project_entities.visible.like(params[:name]).page(params[:page]).per(10)
  126. else
  127. @project_pages = Project.project_entities.visible.page(params[:page] ).per(10)
  128. end
  129. @projects = @project_pages.order("created_on desc")
  130. @limit = 10#per_page_option
  131. @project_count = Project.project_entities.visible.like(params[:name]).page(params[:page]).count
  132. @project_pages = Paginator.new @project_count, @limit, params['page']
  133. @name = params[:name]
  134. @type = 'projects'
  135. respond_to do |format|
  136. format.html {
  137. render :layout => 'base'
  138. scope = Project
  139. unless params[:closed]
  140. scope = scope.active
  141. end
  142. }
  143. # 需要到处atom使用的格式 (redmine自带)
  144. format.atom {
  145. projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
  146. render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
  147. }
  148. end
  149. end
  150. # Time 2015-01-29 16:13:20
  151. # Author lizanle
  152. # Description 项目首页中用户反馈 方法
  153. def feedback
  154. @page = params[:page].to_i
  155. # Find the page of the requested reply
  156. @jours = @project.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC')
  157. limit = 10
  158. offset = @jours.count(:conditions => ["#{JournalsForMessage.table_name}.id > ?", params[:r].to_i])
  159. page = 1 + offset / limit
  160. if params[:r] && @page.nil?
  161. @page = @page < 0 ? 1 : @page
  162. end
  163. @page = @page > page ? page : @page
  164. @jour = paginateHelper @jours,10
  165. @state = false
  166. respond_to do |format|
  167. format.html
  168. format.api
  169. end
  170. end
  171. def project_respond
  172. project_id = request.headers["Referer"].match((%r|/([0-9]{1,})/|))[1]
  173. parent_id = params[:reference_id]
  174. author_id = User.current.id
  175. reply_user_id = params[:reference_user_id]
  176. reply_id = params[:reference_message_id]
  177. content = params[:project_respond]
  178. options = {:user_id => author_id,
  179. :m_parent_id => parent_id,
  180. :m_reply_id => reply_id,
  181. :reply_id => reply_user_id,
  182. :notes => content,
  183. :is_readed => false}
  184. @jfm = Project.add_new_jour(nil, nil, project_id, options)
  185. @save_succ = @jfm.errors.empty?
  186. respond_to do |format|
  187. format.js
  188. end
  189. end
  190. def new
  191. if User.current.login?
  192. @issue_custom_fields = IssueCustomField.sorted.all
  193. @trackers = Tracker.sorted.all
  194. @project = Project.new
  195. @project.safe_attributes = params[:project]
  196. if params[:course_id]
  197. @course = Course.find params[:course_id]
  198. elsif params[:contest_id]
  199. @contest = Contest.find params[:contest_id]
  200. end
  201. render :layout => 'new_base'
  202. else
  203. redirect_to signin_url
  204. end
  205. end
  206. def share
  207. @shares = @project.shares.reverse
  208. @base_courses_tag = @project.project_type
  209. respond_to do |format|
  210. format.html{render :layout => 'base_courses' if @base_courses_tag==1}
  211. format.api
  212. end
  213. end
  214. # 注意:修改该方法的时候注意同步修改forked方法
  215. # forked方法也会创建项目
  216. def create
  217. unless User.current.login?
  218. redirect_to signin_url
  219. return
  220. end
  221. @issue_custom_fields = IssueCustomField.sorted.all
  222. @trackers = Tracker.sorted.all
  223. @project = Project.new
  224. @project.safe_attributes = params[:project]
  225. @project.organization_id = params[:organization_id]
  226. @project.user_id = User.current.id
  227. @project.project_new_type = params[:project_new_type]
  228. params[:project][:is_public] ? @project.is_public = 1 : @project.is_public = 0
  229. if validate_parent_id && @project.save
  230. @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
  231. # Add current user as a project member if he is not admin
  232. #unless User.current.admin?
  233. r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
  234. m = Member.new(:user => User.current, :roles => [r])
  235. # project's score
  236. if ProjectScore.where("project_id=?", @project.id).first.nil?
  237. ProjectScore.create(:project_id => @project.id, :score => false)
  238. end
  239. # end
  240. project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => @project.id)
  241. user_grades = UserGrade.create(:user_id => User.current.id, :project_id => @project.id)
  242. Rails.logger.debug "UserGrade created: #{user_grades.to_json}"
  243. #if params[:project][:is_public] == '1'
  244. project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type,:grade => 0)
  245. Rails.logger.debug "ProjectStatus created: #{project_status.to_json}"
  246. #end
  247. @project.members << m
  248. @project.project_infos << project_info
  249. p = Project.find("#{@project.id}")
  250. ps = ProjectsService.new
  251. ps.send_wechat_create_project_notice User.current,p
  252. #end
  253. respond_to do |format|
  254. format.html {
  255. flash[:notice] = l(:notice_successful_create)
  256. if params[:continue]
  257. attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
  258. redirect_to new_project_url(attrs, :course => '0')
  259. else
  260. redirect_to settings_project_url(@project)
  261. end
  262. }
  263. format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
  264. format.js
  265. end
  266. else
  267. respond_to do |format|
  268. format.html { render :action => 'new', :layout => 'new_base'}#Added by young
  269. format.api { render_validation_errors(@project) }
  270. end
  271. end
  272. end
  273. def copy
  274. @issue_custom_fields = IssueCustomField.sorted.all
  275. @trackers = Tracker.sorted.all
  276. @source_project = Project.find(params[:id])
  277. if request.get?
  278. @project = Project.copy_from(@source_project)
  279. @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
  280. else
  281. Mailer.with_deliveries(params[:notifications] == '1') do
  282. @project = Project.new
  283. @project.safe_attributes = params[:project]
  284. if validate_parent_id && @project.copy(@source_project, :only => params[:only])
  285. @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
  286. flash[:notice] = l(:notice_successful_create)
  287. redirect_to settings_project_url(@project)
  288. elsif !@project.new_record?
  289. # Project was created
  290. # But some objects were not copied due to validation failures
  291. # (eg. issues from disabled trackers)
  292. # TODO: inform about that
  293. redirect_to settings_project_url(@project)
  294. end
  295. end
  296. end
  297. rescue ActiveRecord::RecordNotFound
  298. # source_project not found
  299. render_404
  300. end
  301. # Time 2015-01-29 10:42:00
  302. # Author lizanle
  303. # Description 项目动态展示方法,删除了不必要的代码
  304. def show
  305. # 顶部导航
  306. @project_menu_type = 1
  307. # 更新消息为已读
  308. update_message_status(User.current, @project)
  309. # over
  310. if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
  311. return
  312. end
  313. @author = params[:user_id].blank? ? nil : User.active.find(params[:user_id])
  314. @page = params[:page] ? params[:page].to_i + 1 : 0
  315. # 根据私密性,取出符合条件的所有数据
  316. if User.current.member_of?(@project) || User.current.admin?
  317. case params[:type]
  318. when nil
  319. @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)
  320. when 'issue'
  321. @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Issue'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10)
  322. when 'training_task'
  323. @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'TrainingTask'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10)
  324. when 'news'
  325. @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'News'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10)
  326. when 'message'
  327. @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Message'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10)
  328. when 'attachment'
  329. @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Attachment'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10)
  330. end
  331. else
  332. @events_pages = ForgeActivity.includes(:project).where("forge_activities.project_id = ? and projects.is_public = ? and forge_act_type != ? ",@project,1, "Document").order("created_at desc").page(params['page'|| 1]).per(10);
  333. end
  334. @type = params[:type]
  335. # 版本库统计图
  336. # unless @project.gpid.nil? || @project.project_score.changeset_num == 0
  337. # # rep_statics_commit = @project.rep_statics.order("commits_num desc")
  338. # rep_statics_commit = RepStatics.find_by_sql("SELECT * FROM `rep_statics` where project_id = #{@project.id} order by commits_num desc limit 10")
  339. # rep_statics_code = RepStatics.find_by_sql("SELECT * FROM `rep_statics` where project_id = #{@project.id} order by changeset desc limit 10")
  340. # # rep_statics_code = @project.rep_statics.sort_by {|u| u.changeset}.reverse
  341. # @a_uname = rep_statics_commit.map {|s| s.uname }
  342. # @a_uname_code = rep_statics_code.map {|s| s.uname }
  343. # @a_commits_num = rep_statics_commit.map {|s| s.commits_num.to_i }
  344. # @a_commits_add = rep_statics_code.map {|s| s.add.to_i }
  345. # @a_commits_del = rep_statics_code.map {|s| s.del.to_i }
  346. # @a_commits_changeset = rep_statics_code.map {|s| s.changeset.to_i }
  347. # g = Gitlab.client
  348. # begin
  349. # gid = @project.gpid
  350. # g_project = g.project(gid)
  351. # g_branch = g_project.default_branch.to_s
  352. # rescue =>e
  353. # logger.error("get default branch failed: " + e)
  354. # end
  355. # @rev = g_branch.nil? ? "master" : g_branch
  356. # end
  357. # 根据对应的请求,返回对应的数据
  358. respond_to do |format|
  359. format.html
  360. format.api
  361. format.js
  362. end
  363. end
  364. def che_request
  365. response = send_to_che
  366. @link_ide = response["links"]["ide"] if response["links"]
  367. @link_ide = "http://106.75.119.131:8080/che/test_for_trustie" unless @link_ide
  368. Rails.logger.info("#############link_ide: #{@link_ide}")
  369. end
  370. def settings
  371. # 顶部导航
  372. @project_menu_type = 10
  373. if @project.is_child_training_project?
  374. return render_404
  375. end
  376. # 修改查看消息状态
  377. applied_messages = ForgeMessage.where("user_id =? and project_id =? and forge_message_type =? and viewed =?", User.current.id, @project, "AppliedProject", 0)
  378. applied_messages.update_all(:viewed => true)
  379. @issue_custom_fields = IssueCustomField.sorted.all
  380. @issue_category ||= IssueCategory.new
  381. @member ||= @project.members.new
  382. @trackers = Tracker.sorted.all
  383. @wiki ||= @project.wiki
  384. @select_tab = params[:tab]
  385. #找出所有不属于项目的公共组织
  386. project_org_ids = OrgProject.find_by_sql("select distinct organization_id from org_projects where project_id = #{@project.id}")
  387. if project_org_ids.empty?
  388. @orgs_not_in_project = Organization.where("is_public = 1")
  389. else
  390. project_org_ids = "(" + project_org_ids.join(',') + ")"
  391. @orgs_not_in_project = Organization.where("id not in #{project_org_ids} and is_public = 1")
  392. end
  393. # 里程碑
  394. @versions = @project.shared_versions.sort
  395. # 处理从新建版本库返回来的错误信息
  396. if !params[:repository_error_message].to_s.blank?
  397. html = ""
  398. errors = params[:repository_error_message].flatten
  399. errors.each do |error|
  400. # 版本库路径为空的错误信息不予提示
  401. if(error!=l(:label_repository_path_not_null))
  402. html << error << ";"
  403. end
  404. end
  405. if params[:repository] == "pswd_is_null"
  406. html << l(:label_password_not_null)
  407. end
  408. flash.now[:error] = html if !html.to_s.blank?
  409. end
  410. # for:设置默认分支
  411. @gitlab_repository = Repository.where(:project_id => @project, :type => "Repository::Gitlab").first
  412. if @gitlab_repository.present? && @project.gpid.present?
  413. @repos_url = Gitlab.client.project(@project.gpid).try(:http_url_to_repo)
  414. end
  415. scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
  416. @repository = Repository.factory(scm)
  417. @repository.is_default = @project.repository.nil?
  418. @repository.project = @project
  419. @gitlab_rep = Repository.where(:type => "Repository::Gitlab", :project_id => @project).first
  420. unless @project.gpid.nil?
  421. g = Gitlab.client
  422. @gitlab_branches = g.branches(@project.gpid)
  423. @branch_names = @gitlab_branches.map{|b| b.name}
  424. @gitlab_default_branch = g.project(@project.gpid).default_branch
  425. end
  426. end
  427. # 项目邀请用户加入实现过程
  428. # 两种情况:1、系统外用户;2、系统内用户 (通过邮件判定)
  429. def send_mail_to_member
  430. # 该邮箱未注册过
  431. if !params[:mail].blank? && User.find_by_mail(params[:mail].to_s).nil?
  432. if !User.where("login =?", params[:mail]).first.nil?
  433. # 用户名唯一,用户修改邮箱,未修改用户名,用户名等同邮箱的情况,默认改用户已经注册
  434. user = User.find_by_login(params[:mail].to_s)
  435. if !user.member_of?(@project)
  436. # 如果已经邀请过该用户,则不重复发送
  437. if InviteList.where("project_id =? and mail =?", @project.id, params[:mail].to_s).first.nil?
  438. email = params[:mail]
  439. # Mailer.request_member_to_project(email, @project, User.current).deliver
  440. flash[:notice] = l(:notice_email_sent, :value => email)
  441. else
  442. flash[:error] = l(:notice_email_invited)
  443. end
  444. else
  445. flash[:error] = l(:label_member_of_project, :value => email)
  446. end
  447. else
  448. email = params[:mail]
  449. first_name = params[:first_name]
  450. last_name = params[:last_name]
  451. gender = params[:gender]
  452. # Mailer.send_invite_in_project(email, @project, User.current, first_name, last_name, gender).deliver
  453. @is_zhuce = false
  454. flash[:notice] = l(:notice_email_sent, :value => email)
  455. end
  456. # 邮箱地址已被注册
  457. elsif !User.find_by_mail(params[:mail].to_s).nil?
  458. user = User.find_by_mail(params[:mail].to_s)
  459. if !user.member_of?(@project)
  460. # 如果已经邀请过该用户,则不重复发送
  461. invite_list = InviteList.where("project_id =? and mail =?", @project.id, params[:mail].to_s).first
  462. if invite_list.nil?
  463. email = params[:mail]
  464. # Mailer.request_member_to_project(email, @project, User.current).deliver
  465. flash[:notice] = l(:notice_email_sent, :value => email)
  466. else
  467. # 已经发送过了,则隔3小时才能再次发送
  468. if Time.now - invite_list.created_at > 10800
  469. email = params[:mail]
  470. # Mailer.request_member_to_project(email, @project, User.current).deliver
  471. flash[:notice] = l(:notice_email_sent, :value => email)
  472. else
  473. flash[:error] = l(:notice_email_invited)
  474. end
  475. end
  476. else
  477. flash[:error] = l(:label_member_of_project, :value => email)
  478. end
  479. else
  480. @is_zhuce = true
  481. end
  482. respond_to do |format|
  483. format.html{redirect_to invite_members_by_mail_project_url(@project)}
  484. end
  485. end
  486. # 发送邮件邀请新用户页面对应方法
  487. def invite_members_by_mail
  488. if User.current.member_of?(@project) || User.current.admin?
  489. @inviter_lists = InviteList.where(project_id:@project.id).order("created_at desc")
  490. # @inviters = []
  491. # @waiters = []
  492. # unless @inviter_lists.blank?
  493. # @inviter_lists.each do|inviter_list|
  494. # unless inviter_list.user.nil?
  495. # if inviter_list.user.member_of?(@project)
  496. # @inviters << inviter_list.user
  497. # @inviters_count = @inviters.size
  498. # else
  499. # @waiters << inviter_list.user
  500. # @waiters_count = @waiters.size
  501. # end
  502. # end
  503. # end
  504. # end
  505. @is_zhuce = false
  506. respond_to do |format|
  507. format.html
  508. format.js
  509. end
  510. else
  511. render_403
  512. end
  513. end
  514. # 邀请Trustie注册用户
  515. # def invite_members
  516. # if User.current.member_of?(@project) || User.current.admin?
  517. # @member ||= @project.members.new
  518. # respond_to do |format|
  519. # format.html
  520. # end
  521. # else
  522. # render_403
  523. # end
  524. # end
  525. def edit
  526. end
  527. def set_public_or_private
  528. @project = Project.find(params[:id])
  529. if @project.is_public?
  530. @project.update_attribute(:is_public, 0)
  531. else
  532. @project.update_attribute(:is_public, 1)
  533. end
  534. end
  535. def project_watcherlist
  536. unless @project.nil?
  537. if !@project.is_public? && !User.current.member_of?(@project) && !User.current.admin?
  538. render_403
  539. else
  540. @users -= watched.watcher_users if @watched
  541. end
  542. @watchers = @project.watcher_users
  543. @limit = 32
  544. @is_remote = true
  545. @watchers_count = @watchers.count
  546. @watcher_pages = Paginator.new @watchers_count, @limit, params['page'] || 1
  547. @offset ||= @watcher_pages.offset
  548. @watchers = paginateHelper @watchers, @limit
  549. end
  550. end
  551. # include CoursesHelper
  552. def member
  553. # 消息"同意加入项目"
  554. if params[:message_id]
  555. message_invite(params[:message_id], params[:key])
  556. end
  557. update_messsages_to_viewed("ForgeMessage", @project)
  558. # params[:login]为邮箱邀请用户加入,主要功能:
  559. # 1、自动注册
  560. # 2、加入项目、创建角色
  561. # 3、用户得分
  562. if params[:mail]
  563. userid = Token.find_by_value(params[:token]).user_id
  564. user = User.find(userid)
  565. user.activate!
  566. Member.create(:role_ids => [4], :user_id => userid, :project_id => params[:id])
  567. UserGrade.create(:user_id => userid, :project_id => params[:id])
  568. token = Token.get_token_from_user(user, 'autologin')
  569. #user = User.try_to_autologin(token.value)
  570. if user
  571. start_user_session(user)
  572. user.save
  573. redirect_to project_member_path(params[:id])
  574. return
  575. # account_ project_member_path(params[:id])
  576. flash[:notice] = l(:label_mail_invite_success)
  577. end
  578. end
  579. # 私有项目非项目成员无法访问成员列表
  580. unless @project.is_public?
  581. return render_403 unless User.current.member_of?(@project)
  582. end
  583. ## 有角色参数的才是课程,没有的就是项目
  584. @render_file = 'project_member_list'
  585. # 判断是否课程
  586. if @project.project_type == Project::ProjectType_course
  587. @teachers= searchTeacherAndAssistant(@project)
  588. @canShowCode = isCourseTeacher(User.current.id)
  589. case params[:role]
  590. when '1'
  591. @subPage_title = l :label_teacher_list
  592. @members = searchTeacherAndAssistant(@project)
  593. when '2'
  594. @subPage_title = l :label_student_list
  595. @members = searchStudent(@project)
  596. else
  597. @subPage_title = ''
  598. @members = @project.member_principals.includes(:roles, :principal).all.sort
  599. end
  600. else
  601. if !@project.is_public? && !User.current.member_of?(@project) && !User.current.admin?
  602. render_403
  603. else
  604. roles = Role.find_all_givable
  605. @subPage_title = l :label_member_list
  606. @members = @project.member_principals.includes(:roles, :principal).joins("LEFT JOIN #{OptionNumber.table_name} ON #{OptionNumber.table_name}.user_id = #{Member.table_name}.user_id and #{OptionNumber.table_name}.score_type = 2 AND #{Member.table_name}.project_id = #{OptionNumber.table_name}.project_id").order("#{OptionNumber.table_name}.total_score DESC").all
  607. @applied_members = appied_project_members(@project, @members)
  608. end
  609. end
  610. @members = paginateHelper @members, 32
  611. end
  612. def member_forked
  613. @forked_projects = Project.where(:forked_from_project_id => @project.id)
  614. @limit = 32
  615. @is_remote = true
  616. @forked_count = @forked_projects.count
  617. @forked_pages = Paginator.new @forked_count, @limit, params['page'] || 1
  618. @offset ||= @forked_pages.offset
  619. @forked_projects = paginateHelper @forked_projects, @limit
  620. # @forked_members = User.find_by_sql("SELECT u.* FROM `projects` p,`users` u where p.user_id = u.id and p.forked_from_project_id = #{@project.id} ;")
  621. end
  622. def update_message_status(user, project)
  623. # 更新加入项目消息
  624. project__messages = ForgeMessage.where("forge_message_type in ('ProjectInvite', 'JoinProject', 'RemoveFromProject') and user_id =? and project_id =? ", user, project)
  625. project__messages.update_all(:viewed => true) unless project__messages.blank?
  626. end
  627. def message_invite(message_id, key)
  628. forge_message = ForgeMessage.find(message_id)
  629. if key == forge_message.secret_key
  630. # 情况:用户收到邀请邮件还没看,但是管理员已经把该用户添加进项目
  631. if Member.where("user_id =? and project_id =?",forge_message.user_id, forge_message.project_id).first.nil?
  632. Member.create(:role_ids => [4], :user_id => forge_message.user_id, :project_id => forge_message.project_id)
  633. UserGrade.create(:user_id => forge_message.user_id, :project_id => forge_message.project_id)
  634. end
  635. end
  636. end
  637. #判断指定用户是否为课程教师
  638. def isCourseTeacher(id)
  639. result = false
  640. if @teachers.find_by_user_id(id) != nil
  641. result = true
  642. end
  643. result
  644. end
  645. def sort_project_members project, members
  646. #userGrade = UserGrade.where(:project_id => project.id)
  647. users = UserGrade.where(:project_id => project.id).
  648. order('grade DESC').
  649. joins("LEFT JOIN users ON users.id = user_grades.id").
  650. select("DISTINCT user_grades.user_id")
  651. memberlist = []
  652. users.each do |user|
  653. members.each do |member|
  654. if member[:user_id] == user[:user_id]
  655. memberlist << member
  656. end
  657. end
  658. end
  659. memberlist
  660. end
  661. def appied_project_members (project, members)
  662. users = AppliedProject.where(:project_id => project.id)
  663. memberlist = []
  664. users.each do |user|
  665. members.each do |member|
  666. if member[:user_id] == user[:user_id]
  667. memberlist << member
  668. end
  669. end
  670. end
  671. memberlist
  672. end
  673. def file
  674. end
  675. def statistics
  676. end
  677. #end
  678. # 获取项目tree目录的最新提交记录
  679. # 异步请求
  680. # gpid, rev, ent_name, g
  681. def repository_tree_changes
  682. rev = params[:rev]
  683. ent_path = params[:ent_path]
  684. gpid = params[:gpid]
  685. g = Gitlab.client
  686. begin
  687. result = g.rep_last_changes(gpid, :rev => rev, :path => ent_path)
  688. result = {:message => result.message, :author_name => User.find_by_mail(result.author_email).nil? ? result.author_email : User.find_by_mail(result.author_email).show_name, :time => distance_of_time_in_words(result.time, Time.now)}
  689. rescue Exception => e
  690. puts e
  691. end
  692. render :json => result
  693. end
  694. def update
  695. @project.safe_attributes = params[:project]
  696. @project.language = params[:language].try(:strip)
  697. @project.organization_id = params[:organization_id]
  698. params[:project][:is_public] == "on" ? @project.is_public = 1 : @project.is_public = 0
  699. params[:project][:hidden_repo] == "on" ? @project.hidden_repo = 1 : @project.hidden_repo = 0
  700. # 更新公开私有时同步gitlab公开私有
  701. if !@project.gpid.nil? && @project.is_public != (params[:project][:is_public] == "on" ? 1 : 0)
  702. g = Gitlab.client
  703. params[:project][:is_public] == "on" ? g.edit_project(@project.gpid, 20, params[:branch]) : g.edit_project(@project.gpid, 0, params[:branch])
  704. end
  705. # end
  706. if validate_parent_id && @project.save
  707. @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
  708. if params[:project][:is_public] == '0'
  709. project_status = ProjectStatus.find_by_project_id(@project.id)
  710. project_status.destroy if project_status
  711. elsif params[:project][:is_public] == '1'
  712. project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => @project.watchers.count, :changesets_count => @project.changesets.count,:grade => 0, :project_type => @project.project_type)
  713. end
  714. respond_to do |format|
  715. format.html {
  716. flash[:notice] = l(:notice_successful_update)
  717. redirect_to settings_project_url(@project,:course => @project.project_type)
  718. }
  719. format.api { render_api_ok }
  720. end
  721. else
  722. respond_to do |format|
  723. format.html {
  724. settings
  725. render :action => 'settings'
  726. }
  727. format.api { render_validation_errors(@project) }
  728. end
  729. end
  730. end
  731. def add_script
  732. if User.current.admin? || User.current.member_of?(@project)
  733. if @project.update_attribute(:script, params[:project_script])
  734. @notice = "脚本添加成功"
  735. else
  736. @notice = "脚本添加失败"
  737. end
  738. else
  739. return render_403
  740. end
  741. end
  742. def modules
  743. @project.enabled_module_names = params[:enabled_module_names]
  744. flash[:notice] = l(:notice_successful_update)
  745. redirect_to settings_project_url(@project, :tab => 'modules')
  746. end
  747. GITLABTYPE = "Repository::Gitlab"
  748. def archive
  749. if request.post?
  750. if @project.destroy && @project.gpid
  751. # 删除版本库信息
  752. begin
  753. g = Gitlab.client
  754. g.delete_project(@project.gpid)
  755. rescue Exception => e
  756. puts e
  757. end
  758. # 删除Trustie版本库记录
  759. repoisitory = Repository.where(:project_id => @project.id, :type => GITLABTYPE).first
  760. repoisitory.delete
  761. @project.update_column(:gpid, nil)
  762. @project.update_column(:forked_from_project_id, nil)
  763. else
  764. flash[:error] = l(:error_can_not_archive_project)
  765. end
  766. unless @project.archive
  767. end
  768. end
  769. if params[:type] == "project"
  770. redirect_to user_path(User.current)
  771. else
  772. redirect_to admin_projects_url(:status => params[:status])
  773. end
  774. end
  775. def unarchive
  776. @project.unarchive if request.post? && !@project.active?
  777. redirect_to admin_projects_url(:status => params[:status])
  778. end
  779. # 弹框提醒:
  780. # 自己不能参加自己的实训项目
  781. # 没有建立版本库的项目不能开启实训
  782. # 已经实训过直接跳入
  783. #
  784. def training_chiled_project_exec
  785. if !User.current.logged?
  786. return render_403
  787. end
  788. respond_to do |format|
  789. format.js
  790. end
  791. end
  792. # training_status: 默认为0; 1代表实训项目; 2:代表实训子项目
  793. def training_project_execute
  794. if @project.training_tasks.count == 0
  795. @notice = "实训开启失败:请先发布实训任务"
  796. return
  797. elsif Repository.where(:project_id => @project.id, :type => "Repository::Gitlab").count == 0
  798. @notice = "实训开启失败:请先创建版本库"
  799. return
  800. end
  801. jobName = "#{@project.id}"
  802. pipeLine = "#{Base64.encode64(@project.script)}"
  803. uri = URI("http://123.59.135.74:9999/jenkins-exec/api/createJob")
  804. params = {jobName: jobName, pipeLine: pipeLine}
  805. res = uri_exec uri, params
  806. training_project_notice res
  807. if res['code'] == 0
  808. @project.update_attribute(:training_status, 1)
  809. end
  810. end
  811. def training_project_update
  812. jobName = "#{@project.id}"
  813. pipeLine = "#{Base64.encode64(@project.script)}"
  814. uri = URI("http://123.59.135.74:9999/jenkins-exec/api/updateJob")
  815. params = {jobName: jobName, pipeLine: pipeLine}
  816. res = uri_exec uri, params
  817. training_project_notice res
  818. if res['code'] == 0
  819. @project.update_attribute(:training_status, 1)
  820. end
  821. end
  822. def training_project_notice res
  823. if res['code'] == 0
  824. @notice = "实训开启成功"
  825. elsif res['code'] == -2
  826. @notice = "Job已存在"
  827. else
  828. @notice = res['msg'].nil? ? "实训开启失败" : res['msg']
  829. end
  830. end
  831. # TrainintTask.status 0:评测中 1:评测成功 2:评测进行中
  832. # 非项目成员、非实训子项目不允许执行该方法
  833. def task_execute
  834. if !@project.is_child_training_project? || !User.current.member_of?(@project)
  835. return render_403
  836. end
  837. taskId = params[:training_task_id]
  838. jobName = @project.forked_from_project_id
  839. @training_task = TrainingTask.find(taskId)
  840. step = @training_task.position
  841. rep_identify = Repository.where(:project_id => @project.id, :type => "Repository::Gitlab").first.try(:identifier)
  842. gitlab_address = Redmine::Configuration['gitlab_address']
  843. gitUrl = gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ rep_identify + "."+"git"
  844. gitUrl = Base64.encode64(gitUrl)
  845. if @training_task.status == 0
  846. params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"}
  847. uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob")
  848. begin
  849. res = uri_exec uri, params
  850. # 轮询获取
  851. for i in 0..60 do
  852. sleep(1)
  853. result = TrainingTask.where(:id => @training_task.id).first.try(:result)
  854. if (result != 0)
  855. break
  856. end
  857. end
  858. # @training_task = TrainingTask.find(@training_task.id)
  859. @page = params[:page] ? params[:page].to_i + 1 : 0
  860. @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)
  861. rescue Exception => e
  862. puts e
  863. end
  864. end
  865. respond_to do |format|
  866. format.js
  867. end
  868. end
  869. def uri_exec uri, params
  870. res = Net::HTTP.post_form(uri, params).body
  871. res = JSON.parse(res)
  872. end
  873. # 开启实训项目,学生会fork一个项目并自动发送任务
  874. def training_project_extend
  875. @project = Project.find(params[:id])
  876. @repository = Repository.where("project_id =? and type =?", @project.id, "Repository::Gitlab")
  877. # 如果当前用户已经fork过该项目,不会新fork项目,则跳至已fork的项
  878. unless has_forked?(@project, User.current)
  879. project = project_from_current_project(@project.id, User.current.id)
  880. redirect_to project_path(project)
  881. else
  882. ActiveRecord::Base.transaction do
  883. g = Gitlab.client
  884. if User.current.gid.nil?
  885. s = Trustie::Gitlab::Sync.new
  886. s.sync_user(User.current)
  887. end
  888. gproject = g.fork(@project.gpid, User.current.gid)
  889. if gproject
  890. new_training_project = copy_project_and_module(@project, gproject)
  891. forked_count = @project.forked_count.to_i + 1
  892. @project.update_attributes(:forked_count => forked_count)
  893. # 发布实训任务,只发布实训任务的第一个
  894. publish_training_tasks(@project.id, new_training_project.id, 1, @project.user_id)
  895. end
  896. end
  897. end
  898. end
  899. # 需要传Task ID
  900. # 判断任务是否完成
  901. # 如果完成则发送下一个任务直到任务结束
  902. # TrainingTask.status 1 成功,其它失败
  903. def training_task_status
  904. status = params[:status].to_i
  905. task_id = params[:taskId]
  906. message = Base64.decode64(params[:msg]) unless params[:msg].blank?
  907. begin
  908. @training_task = TrainingTask.find(task_id)
  909. # 如果已经执行成功过,则不重复执行
  910. return if @training_task.status == 1
  911. original_project_id = Project.find(@training_task.project_id).try(:forked_from_project_id)
  912. original_project = Project.find(original_project_id)
  913. rescue
  914. return
  915. end
  916. original_tasks_count = original_project.training_tasks.count
  917. position = @training_task.try(:position)
  918. # 测试,默认成功
  919. if status == 0
  920. ActiveRecord::Base.transaction do
  921. if position < original_tasks_count
  922. # 继续发布下一个任务
  923. publish_training_tasks original_project_id, @training_task.project_id, position + 1, original_project.user_id
  924. end
  925. @training_task.update_attribute(:status, 1)
  926. @training_task.update_attribute(:result, 1)
  927. # 创建一条回复提醒
  928. content = (position == original_tasks_count) ? "恭喜您,您已经完成了实训项目的所有任务" : "恭喜您,您已经完成了本任务,请继续下一任务"
  929. add_training_task_journal(content, original_project.user_id)
  930. end
  931. else
  932. content = "很抱歉,您的任务未通过,请继续加油,错误信息如下:#{message}"
  933. add_training_task_journal(content, original_project.user_id)
  934. # 失败的时候可以继续提交
  935. @training_task.update_attribute(:status, 0)
  936. @training_task.update_attribute(:result, 2)
  937. end
  938. end
  939. # 创建一条回复
  940. def add_training_task_journal content, user_id
  941. jour = Journal.new
  942. jour.user_id = user_id
  943. jour.notes = content
  944. jour.journalized = @training_task
  945. jour.save
  946. update_user_activity(@training_task.class, @training_task.id)
  947. update_forge_activity(@training_task.class, @training_task.id)
  948. end
  949. # 实训开启成功后,发布第一个任务
  950. # REDO:失败后提醒用户,及相关处理
  951. def publish_training_tasks original_project_id, new_training_project_id, position, user_id
  952. original_task = TrainingTask.where(:project_id => original_project_id, :position => position).first
  953. training_task = TrainingTask.new
  954. training_task.save_attachments(params[:attachments] || (params[:training_task] && params[:training_task][:uploads]))
  955. training_task.subject = original_task.subject
  956. training_task.description = original_task.description
  957. training_task.position = original_task.position
  958. training_task.project_id = new_training_project_id
  959. training_task.author_id = User.current.id
  960. begin
  961. training_task.save
  962. rescue Exception => e
  963. puts e
  964. end
  965. end
  966. # 复制项目
  967. def copy_project_and_module tproject, gproject
  968. project = Project.new
  969. project.name = tproject.name
  970. project.is_public = tproject.is_public
  971. project.status = tproject.status
  972. project.description = tproject.description
  973. project.hidden_repo = tproject.hidden_repo
  974. project.user_id = User.current.id
  975. project.project_type = 0
  976. project.project_new_type = tproject.project_new_type
  977. project.gpid = gproject.id
  978. project.forked_from_project_id = tproject.id
  979. project.enabled_module_names = tproject.enabled_module_names
  980. if project.save
  981. project.update_attribute(:training_status,2)
  982. r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
  983. m = Member.new(:user => User.current, :roles => [r])
  984. if ProjectScore.where("project_id=?", project.id).first.nil?
  985. ProjectScore.create(:project_id => project.id, :score => false)
  986. end
  987. project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => project.id)
  988. user_grades = UserGrade.create(:user_id => User.current.id, :project_id => project.id)
  989. Rails.logger.debug "UserGrade created: #{user_grades.to_json}"
  990. project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type,:grade => 0)
  991. Rails.logger.debug "ProjectStatus created: #{project_status.to_json}"
  992. project.members << m
  993. project.project_infos << project_info
  994. copy_repository(project, gproject)
  995. return project
  996. else
  997. respond_to do |format|
  998. format.html { render :action => 'forked', :layout => 'base_projects'}
  999. format.api { render_validation_errors(@project) }
  1000. end
  1001. end
  1002. end
  1003. # 判断用户是否已经fork过该项目
  1004. def has_forked?(project, user)
  1005. projects = Project.where("user_id =?", user)
  1006. projects.map(&:forked_from_project_id).detect{|s| s == @project.id}.nil? ? true : false
  1007. end
  1008. def copy_repository(project, gproject)
  1009. repository = Repository.factory('Git')
  1010. repository.project_id = project.id
  1011. repository.type = 'Repository::Gitlab'
  1012. repository.url = gproject.name
  1013. repository.identifier = gproject.name
  1014. repository = repository.save
  1015. end
  1016. # 资源库fork弹框
  1017. def forked_pop
  1018. respond_to do |format|
  1019. format.js
  1020. end
  1021. end
  1022. # 配置成员弹框
  1023. def delete_member_pop
  1024. @member = Member.find(params[:member].to_i)
  1025. respond_to do |format|
  1026. format.js
  1027. end
  1028. end
  1029. def close
  1030. @project.close
  1031. redirect_to project_url(@project)
  1032. end
  1033. def reopen
  1034. @project.reopen
  1035. redirect_to project_url(@project)
  1036. end
  1037. REP_TYPE = "Repository::Gitlab"
  1038. # Delete @project
  1039. def destroy
  1040. ActiveRecord::Base.transaction do
  1041. g = Gitlab.client
  1042. g.delete_project(@project.gpid) unless @project.gpid.blank?
  1043. # 删除Trustie版本库记录
  1044. repoisitory = Repository.where(:project_id => @project.id, :type => GITLABTYPE).first
  1045. repoisitory.delete unless repoisitory.blank?
  1046. @project.update_column(:gpid, nil)
  1047. @project.update_column(:forked_from_project_id, nil)
  1048. @project_to_destroy = @project
  1049. @project_to_destroy.destroy
  1050. end
  1051. respond_to do |format|
  1052. if params[:type] == "project"
  1053. format.html{redirect_to user_path(User.current)}
  1054. else
  1055. format.html{redirect_to admin_projects_url(:status => params[:status])}
  1056. end
  1057. end
  1058. # hide project in layout
  1059. @project = nil
  1060. end
  1061. # Delete @project's repository
  1062. def destroy_repository
  1063. if is_project_manager?(User.current.id, @project.id)
  1064. @gitlab_repository = Repository.where(:project_id => @project, :type => REP_TYPE).first
  1065. @is_true = params[:is_true]
  1066. unless @is_true.nil?
  1067. begin
  1068. g = Gitlab.client
  1069. d_project = g.delete_project(@project.gpid)
  1070. if d_project
  1071. @gitlab_repository.destroy
  1072. @project.update_attribute(:gpid, nil)
  1073. @gitlab_repository = nil
  1074. end
  1075. rescue Exception => e
  1076. if @gitlab_repository
  1077. @gitlab_repository.destroy
  1078. @project.update_attribute(:gpid, nil)
  1079. @gitlab_repository = nil
  1080. end
  1081. end
  1082. end
  1083. else
  1084. return render_403
  1085. end
  1086. end
  1087. def show_projects_score
  1088. respond_to do |format|
  1089. format.html { render :layout => "base_projects"}
  1090. format.js
  1091. end
  1092. end
  1093. def issue_score_index
  1094. respond_to do |format|
  1095. format.js
  1096. end
  1097. end
  1098. def news_score_index
  1099. end
  1100. def file_score_index
  1101. end
  1102. def code_submit_score_index
  1103. end
  1104. def projects_topic_score_index
  1105. end
  1106. # end
  1107. before_filter :toggleCourse, only: [:finishcourse, :restartcourse]
  1108. # 最好通过用户与项目的权限解决这种事情。还没写
  1109. def finishcourse
  1110. yesterday = Date.today.prev_day.to_time
  1111. @course_prefs.endup_time = yesterday
  1112. @save_flag = @course_prefs.save
  1113. respond_to do |format|
  1114. format.js
  1115. end
  1116. end
  1117. def restartcourse
  1118. day = Time.parse("3000-01-01")
  1119. @course_prefs.endup_time = day
  1120. @save_flag = @course_prefs.save
  1121. respond_to do |format|
  1122. format.js {
  1123. render action:'finishcourse'
  1124. }
  1125. end
  1126. end
  1127. def exit_project
  1128. @project = Project.find params[:id]
  1129. if User.current.login?
  1130. members = Member.where(:user_id => User.current.id, :project_id=>params[:id]).first
  1131. if User.current != @project.owner
  1132. members.destroy
  1133. # 移出的时候删除申请消息,不需要删除消息,所以不必要关联删除
  1134. applied_projects = AppliedProject.where(:project_id => @project.id, :user_id => members.user_id).first
  1135. unless applied_projects.nil?
  1136. applied_projects.delete
  1137. end
  1138. end
  1139. respond_to do |format|
  1140. format.js
  1141. end
  1142. end
  1143. end
  1144. def store_mine
  1145. member = Member.where(:project_id => params[:id], :user_id => User.current.id).first
  1146. member.try(:is_collect) == 1 ? member.update_column(:is_collect, 0) : member.update_column(:is_collect, 1)
  1147. end
  1148. # 项目收藏
  1149. def enshrine
  1150. @stores = Member.where(:project_id => params[:id], :is_collect => 1).includes(:user)
  1151. end
  1152. #加入私有项目
  1153. def join_project
  1154. respond_to do |format|
  1155. format.js
  1156. end
  1157. end
  1158. #朋友圈、科研组、开发组之间的切换
  1159. def change_project_type
  1160. @project.project_new_type = params[:project_type]
  1161. if @project.save
  1162. message = @project.project_new_type
  1163. else
  1164. message = "0"
  1165. end
  1166. render :json => message
  1167. end
  1168. private
  1169. def memberAccess
  1170. # 如果是私有项目,项目成员不对外公开,公开项目成员列表对外公开。
  1171. unless @project.is_public?
  1172. render_403 unless User.current.member_of?(@project)
  1173. end
  1174. end
  1175. def toggleCourse
  1176. @course_prefs = Course.find_by_extra(@project.identifier)
  1177. unless (@course_prefs.teacher == User.current || User.current.admin?)
  1178. render_403
  1179. end
  1180. end
  1181. def select_project_layout
  1182. project = Project.find_by_id(params[:id])
  1183. project ||= @project ||= @course ||= params[:course] ||= params[:project_type]
  1184. (project.try(:project_type) == Project::ProjectType_project) ? 'base_projects' : 'base_courses'
  1185. end
  1186. # Validates parent_id param according to user's permissions
  1187. # TODO: move it to Project model in a validation that depends on User.current
  1188. def validate_parent_id
  1189. return true if User.current.admin?
  1190. parent_id = params[:project] && params[:project][:parent_id]
  1191. if parent_id || @project.new_record?
  1192. parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
  1193. unless @project.allowed_parents.include?(parent)
  1194. @project.errors.add :parent_id, :invalid
  1195. return false
  1196. end
  1197. end
  1198. true
  1199. end
  1200. #gcm
  1201. def desc_sort_course_by_avtivity(activity_count,projects)
  1202. return projects if activity_count.size<2
  1203. (activity_count.size-2).downto(0) do |i|
  1204. (0..i).each do |j|
  1205. if activity_count[j]<activity_count[j+1]
  1206. projects[j],projects[j+1]=projects[j+1],projects[j]
  1207. activity_count[j],activity_count[j+1]=activity_count[j+1],activity_count[j]
  1208. end
  1209. end
  1210. end
  1211. return projects
  1212. end
  1213. def require_admin_or_manager
  1214. return unless require_login
  1215. if !(User.current.admin? || User.current.manager_of_project?(@project.id))
  1216. render_403
  1217. return false
  1218. end
  1219. true
  1220. end
  1221. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...