# encoding: utf-8
#
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
include AvatarHelper
include StudentWorkHelper
include ApiHelper
module ProjectsHelper
# 时间转换
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
options = {
:scope => :'datetime.distance_in_words',
}.merge!(options)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance = (to_time.to_f - from_time.to_f).abs
distance_in_minutes = (distance / 60.0).round
distance_in_seconds = distance.round
I18n.with_options :locale => options[:locale], :scope => options[:scope] do |locale|
case distance_in_minutes
when 0..1
return distance_in_minutes == 0 ?
locale.t(:less_than_x_minutes, :count => 1) :
locale.t(:x_minutes, :count => distance_in_minutes) unless include_seconds
case distance_in_seconds
when 0..4 then locale.t :less_than_x_seconds, :count => 5
when 5..9 then locale.t :less_than_x_seconds, :count => 10
when 10..19 then locale.t :less_than_x_seconds, :count => 20
when 20..39 then locale.t :half_a_minute
when 40..59 then locale.t :less_than_x_minutes, :count => 1
else locale.t :x_minutes, :count => 1
end
when 2..44 then locale.t :x_minutes, :count => distance_in_minutes
when 45..89 then locale.t :about_x_hours, :count => 1
when 90..1439 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round
when 1440..2519 then locale.t :x_days, :count => 1
when 2520..43199 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).round
when 43200..86399 then locale.t :about_x_months, :count => 1
when 86400..525599 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round
else
fyear = from_time.year
fyear += 1 if from_time.month >= 3
tyear = to_time.year
tyear -= 1 if to_time.month < 3
leap_years = (fyear > tyear) ? 0 : (fyear..tyear).count{|x| Date.leap?(x)}
minute_offset_for_leap_year = leap_years * 1440
# Discount the leap year days when calculating year distance.
# e.g. if there are 20 leap year days between 2 dates having the same day
# and month then the based on 365 days calculation
# the distance in years will come out to over 80 years when in written
# english it would read better as about 80 years.
minutes_with_offset = distance_in_minutes - minute_offset_for_leap_year
remainder = (minutes_with_offset % 525600)
distance_in_years = (minutes_with_offset.div 525600)
if remainder < 131400
locale.t(:about_x_years, :count => distance_in_years)
elsif remainder < 394200
locale.t(:over_x_years, :count => distance_in_years)
else
locale.t(:almost_x_years, :count => distance_in_years + 1)
end
end
end
end
def link_to_version(version, options = {})
return '' unless version && version.is_a?(Version)
link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, :class => "c_blue02"
end
def link_to_version_show(version, options = {})
return '' unless version && version.is_a?(Version)
link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, :class => " f16 fb c_dblue "
end
def project_settings_tabs
tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural},
{:name => 'modules', :action => :select_project_modules, :partial => 'projects/settings/modules', :label => :label_module_plural},
{:name => 'members', :action => :manage_members, :partial => 'projects/settings/members', :label => :label_member_plural},
{:name => 'versions', :action => :manage_versions, :partial => 'projects/settings/versions', :label => :label_version_plural},
# {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural},
# {:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki},
{:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
#{:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural},
{:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities}
]
tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
end
def sort_project(state, project_type)
content = ''.html_safe
case state
when 0
content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type)))
content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type)))
content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type), :class=>"selected"), :class=>"selected")
when 1
content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type), :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type)))
content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type)))
when 2
content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type)))
content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type), :class=>"selected"), :class=>"selected")
content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type)))
end
content = content_tag('ul', content)
content_tag('div', content, :class => "tabs")
end
# 判断我的项目中是否有重名项目
def judge_same_projectname(user, project_name)
result = false
my_projects = user.projects
my_projects.each do |mp|
result = true if mp.name == project_name
end
return result
end
# 项目类型
def project_type_select
type = []
option1 = []
option1 << l(:label_development_team)
option1 << l(:label_development_team)
option2 = []
option2 << l(:label_research_group)
option2 << l(:label_research_group)
option3 = []
option3 << l(:label_friend_organization)
option3 << l(:label_friend_organization)
type << option1
type << option2
type << option3
type
end
# 项目类型描述
def project_newtype_descrption
case params
when 1
value = l(:label_type_des_development)
when 2
value = l(:label_type_des_research)
when 3
value = l(:label_type_des_friend)
end
end
# 被邀请成员的状态
def status_for_ivitied(ivite_list, project)
if ivite_list.user.member_of?(project)
value = "已经加入了项目!"
elsif ivite_list.user.active?
value = "邀请已发送,等待用户加入!"
else
value = "邀请已发送,等待用户激活账号!"
end
end
# 获取新增gitlab版本库
def rep_gitlab(project)
rep = Repository.where("project_id =? and type =?", project, "Repository::Gitlab")
end
# 获取新项目的版本库地址
def rep_gitlab_url(project)
gitlab_address = Redmine::Configuration['gitlab_address']
url = gitlab_address.to_s+"/"+project.owner.to_s+"/"+ rep_gitlab(project).first.identifier+"."+"git"
end
# # 获取Forge历史版本库
def rep_forge(project)
rep = Repository.where("project_id =? and type =?", project, "Repository::Git")
end
# Added by young
def course_settings_tabs
tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural, :course=>'1'},
#{:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural, :project_type => 1},
# {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
{:name => 'members', :action => :manage_members, :partial => 'projects/settings/members', :label => :label_member_plural}
]
tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
end
# Ended by young
def parent_project_select_tag(project)
selected = project.parent
# retrieve the requested parent project
parent_id = (params[:project] && params[:project][:parent_id]) || params[:parent_id]
if parent_id
selected = (parent_id.blank? ? nil : Project.find(parent_id))
end
options = ''
options << "" if project.allowed_parents.include?(nil)
options << project_tree_options_for_select(project.allowed_parents.compact, :selected => selected)
content_tag('select', options.html_safe, :name => 'project[parent_id]', :id => 'project_parent_id')
end
# Renders the projects index
def render_project_hierarchy(projects)
render_project_nested_lists(projects) do |project|
#Modified by young
if project.try(:project_type) == Project::ProjectType_course
# modified by longjun
# never use unless and else
# unless project.is_public == 1
if project.is_public != 1
s = "#{l(:label_private)}".html_safe
else
s = "".html_safe
end
# end longjun
# modified by Longjun
s += link_to_project(project, {},
:class => "#{project.css_classes} #{User.current.member_of?(project) ? 'my-project' : nil}").html_safe
# end longjun
else
# modified by longjun
# unless project.is_public
if !project.is_public
# end longjun
s = "#{l(:label_private)}".html_safe
else
s = "".html_safe
end
# modified by longjun
s += link_to_project(project, {},
:class => "#{project.css_classes} #{User.current.member_of?(project) ? 'my-project' : nil}")
# end longjun
end
#Ended by young
if project.description.present?
#Delete by nie.
# s << content_tag('td', textilizable(project.short_description, :project => project), :class => 'wiki description')
end
s
end
end
# Returns a set of options for a select field, grouped by project.
def version_options_for_select(versions, selected=nil)
project_name = versions.blank? ? "" : versions.first.project.name
grouped = Hash.new {|h,k| h[k] = []}
grouped[project_name] << ["请选择里程碑", 0]
=begin
unless selected.nil?
if (params[:action] == "show" ) && Version.find(selected.id).status == "closed"
version_name = Version.find(selected.id).name
grouped[project_name] << [version_name, selected.id]
end
end
=end
versions.each do |version|
grouped[version.project.name] << [version.name, version.id]
end
if grouped.keys.size > 1
grouped_options_for_select(grouped, selected && selected.id)
else
options_for_select((grouped.values.first || []), selected && selected.id)
end
end
def format_version_sharing(sharing)
sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
l("label_version_sharing_#{sharing}")
end
# this method is used to get all projects that tagged one tag
# added by william
def get_projects_by_tag(tag_name)
Project.tagged_with(tag_name).order('updated_on desc')
end
#是否启动互评下拉框
def is_evaluation_option
type = []
option1 = []
option2 = []
option1 << l(:lable_start_mutual_evaluation)
option1 << 1
option2 << l(:lable_close_mutual_evaluation)
option2 << 2
type << option1
type << option2
end
# 判断用户是否为项目管理员
def is_project_manager?(user_id, project_id)
@result = false
mem = Member.where("user_id = ? and project_id = ?",user_id, project_id)
unless mem.blank?
@result = mem.first.roles.to_s.include?("Manager") ? true : false
end
return @result
end
# 用来判断用户是否是项目的管理员
# added by william
def is_manager?(user_id,project_id)
@result = false
@user_id = ProjectInfo.find_by_project_id(project_id)
# modified by longjun
# if @user_id == user.id
# @result = true
# end
@result = true if @user_id = user.id
# end longjun
return @result
end
# 将动态中类型转换为可读的字符串
def eventToLanguage event_type
case event_type
when "issue-note"
l :label_issue
when "issue"
l :label_issue
when "attachment"
l :label_attachment
when "news"
l :label_news
else
""
end
end
def eventToLanguageCourse event_type, project
case event_type
when "issue-note"
l :label_issue
when "issue"
l :label_issue
when "attachment"
l :label_attachment
when "news"
project.project_type == 1 ? (l :label_notification) : (l :label_news)
else
""
end
end
def rolesToLanguage rolesArray
rolesArray = ([] << rolesArray) unless rolesArray.is_a?(Array)
rolesArray.map{ |roleName|
case roleName.to_sym
when :Manager
l :default_role_manager
when :Developer
l :default_role_developer
when :Reporter
l :default_role_reporter
else
'Unkown'
end
}
end
def sort_project_by_hot
return sort_project_by_hot_rails
@projects_status = ProjectStatus.visible.where("project_statuses.project_type <> ? or project_statuses.project_type is null", 1)
@projects_status = @projects_status.reorder('grade').all.reverse
@projects = []
@projects_status.each do |obj|
break if(@projects_status[10] == obj)
@projects << Project.visible.find_by_id("#{obj.project_id}")#where('id=:id', id: obj.project_id)
end
@projects
rescue NoMethodError => e
logger.error "Logger.Error [ProjectsHelper] ===> #sort_project_by_hot, NoMethodError: #{e}"
[]
end
def sort_project_by_hot_rails
# @projects_status = ProjectStatus.visible.where("project_statuses.project_type <> ? or project_statuses.project_type is null", 1)
# @projects_status = @projects_status.reorder('grade').all.reverse
# Project.joins(@projects_status).limit(10)
limit = 10
#Project.find_by_sql("SELECT * FROM projects RIGHT OUTER JOIN (SELECT * FROM project_statuses ORDER BY grade DESC LIMIT #{limit} ) AS t ON projects.id = t.project_id ")
Project.find_by_sql("
SELECT p.id, p.name, p.description, p.identifier, t.project_id
FROM projects AS p RIGHT OUTER JOIN (
SELECT project_id,grade FROM project_statuses
WHERE project_type = 0 ORDER BY grade DESC LIMIT #{limit} ) AS t ON p.id = t.project_id ")
end
# 判断课程是否结束,快别用,这个定日子的方法有问题
def course_timeout? project
return true if (project.nil? && project.course_extra.nil?)
courses_year = project.course_extra.time
current_year = Time.now.year
if courses_year >= current_year
return false
elsif (courses_year < current_year) && (Time.now.month < 3)
return false
else
return true
end
end
def find_project_repository project
unless project.repositories.nil?
project.repositories.each do |repository|
repository.fetch_changesets if Setting.autofetch_changesets?
end
end
end
# Time 2015-01-29 11:39:02
# Author lizanle
# Description 计算projects
def get_project_activity projects,activities
@project_ids=activities.keys()
days = Setting.activity_days_default.to_i
date_to ||= Date.today + 1
date_from = date_to - days-1.years
#issue_count
Issue.where(project_id: @project_ids).where("updated_on>?",date_from).each do |issue|
# activities[issue.project_id.to_s]+=1
activities[issue.project_id]+=issue.journals.where("created_on>?",date_from).count
end
#repository_count
Repository.where(project_id: @project_ids).each do |repository|
# activities[repository.project_id.to_s]+=1
activities[repository.project_id]+=repository.changesets.where("committed_on>?",date_from).count
end
#news_count
News.where(project_id: @project_ids).where("created_on>?",date_from).each do |news|
activities[news.project_id]+=1
end
#document_count
Document.where(project_id: @project_ids).where("created_on>?",date_from).each do |document|
activities[document.project_id]+=1
end
#file_count
Attachment.where(container_id: @project_ids, container_type: Project).where("created_on>?",date_from).each do |attachment|
activities[attachment.container_id]+=1
end
#message_count
Board.where(project_id: @project_ids).each do |board|
# activities[board.project_id]+=1
activities[board.project_id]+=board.messages.where("updated_on>?",date_from).count
end
#time_entry_count
TimeEntry.where(project_id: @project_ids).where("updated_on>?",date_from).each do |timeentry|
activities[timeentry.project_id]+=1
end
#feedbackc_count
JournalsForMessage.where(jour_id: @project_ids, jour_type: Project).each do |jourformess|
activities[jourformess.jour_id]+=1
end
#activities!=0
i=0;
projects.each do |project|
id=project.id
if activities[id]==0
activities[id]=1
end
end
return activities
end
def handle_project projects,activities
project_activity_count_array=activities.values()
project_array=[]
i=0;
projects.each do |project|
project_array[i]=project
i=i+1
end
projects=desc_sort_course_by_avtivity(project_activity_count_array,project_array)
return projects
end
def project_organizations_id_option
type = []
option1 = []
option1 << l(:label_organization_choose)
option1 << 0
type << option1
Organization.all.each do |org|
option = []
option << org.name
option << org.id
type << option
end
type
end
#显示项目配置菜单
def show_project_memu user
if user.allowed_to?(:edit_project, @project)
result = "edit_project"
elsif user.allowed_to?(:select_project_modules, @project)
result = "select_project_modules"
elsif user.allowed_to?(:manage_members, @project)
result = "manage_members"
elsif user.allowed_to?(:manage_versions, @project)
result = "manage_versions"
elsif user.allowed_to?(:manage_repository, @project)
result = "manage_repository"
end
result
end
end