Chris@909: # encoding: utf-8 Chris@909: # Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module ProjectsHelper Chris@0: def link_to_version(version, options = {}) Chris@0: return '' unless version && version.is_a?(Version) Chris@0: link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, options Chris@0: end Chris@441: Chris@0: def project_settings_tabs Chris@0: tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural}, chris@354: {:name => 'overview', :action => :edit_project, :partial => 'projects/settings/overview', :label => :label_welcome_page}, Chris@0: {:name => 'modules', :action => :select_project_modules, :partial => 'projects/settings/modules', :label => :label_module_plural}, Chris@0: {:name => 'versions', :action => :manage_versions, :partial => 'projects/settings/versions', :label => :label_version_plural}, Chris@0: {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural}, Chris@0: {:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki}, Chris@1115: {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural}, Chris@0: {:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural}, Chris@0: {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities} Chris@0: ] Chris@441: tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)} Chris@0: end Chris@441: Chris@0: def parent_project_select_tag(project) Chris@0: selected = project.parent Chris@0: # retrieve the requested parent project Chris@0: parent_id = (params[:project] && params[:project][:parent_id]) || params[:parent_id] Chris@0: if parent_id Chris@0: selected = (parent_id.blank? ? nil : Project.find(parent_id)) Chris@0: end Chris@441: Chris@0: options = '' Chris@1464: options << "" if project.allowed_parents.include?(nil) Chris@0: options << project_tree_options_for_select(project.allowed_parents.compact, :selected => selected) Chris@909: content_tag('select', options.html_safe, :name => 'project[parent_id]', :id => 'project_parent_id') Chris@0: end Chris@441: Chris@1517: def render_project_action_links Chris@1517: links = [] Chris@1517: links << link_to(l(:label_project_new), {:controller => 'projects', :action => 'new'}, :class => 'icon icon-add') if User.current.allowed_to?(:add_project, nil, :global => true) Chris@1517: links << link_to(l(:label_issue_view_all), issues_path) if User.current.allowed_to?(:view_issues, nil, :global => true) Chris@1517: links << link_to(l(:label_overall_spent_time), time_entries_path) if User.current.allowed_to?(:view_time_entries, nil, :global => true) Chris@1517: links << link_to(l(:label_overall_activity), { :controller => 'activities', :action => 'index', :id => nil }) Chris@1517: links.join(" | ").html_safe Chris@1517: end Chris@1517: chris@335: def render_project_short_description(project) chris@335: s = '' chris@335: if (project.short_description) chris@335: s << "
" chris@335: s << textilizable(project.short_description, :project => project).gsub(/<[^>]+>/, '') chris@335: s << "
" chris@335: end chris@1139: s.html_safe chris@335: end Chris@14: Chris@0: # Renders a tree of projects as a nested set of unordered lists Chris@0: # The given collection may be a subset of the whole project tree Chris@0: # (eg. some intermediate nodes are private and can not be seen) Chris@0: def render_project_hierarchy(projects) Chris@0: s = '' Chris@0: if projects.any? Chris@0: ancestors = [] Chris@0: original_project = @project Chris@0: projects.each do |project| Chris@0: # set the project environment to please macros. Chris@0: @project = project Chris@0: if (ancestors.empty? || project.is_descendant_of?(ancestors.last)) Chris@0: s << "\n" Chris@0: end Chris@0: end Chris@0: classes = (ancestors.empty? ? 'root' : 'child') Chris@0: s << "
  • " + Chris@14: link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}") chris@335: s << render_project_short_description(project) Chris@0: s << "
    \n" Chris@0: ancestors << project Chris@0: end Chris@0: end Chris@909: s.html_safe Chris@0: end Chris@0: luisf@68: chris@420: def render_my_project_in_hierarchy(project) chris@420: chris@420: s = '' chris@420: chris@420: if User.current.member_of?(project) chris@420: chris@420: # set the project environment to please macros. chris@420: @project = project chris@420: chris@420: classes = (project.root? ? 'root' : 'child') chris@420: chris@420: s << "
  • " + chris@420: link_to_project(project, {}, :class => "project my-project") chris@420: if project.is_public? chris@939: s << " " << l(:field_is_public) << "" chris@420: else chris@939: s << " " << l(:field_is_private) << "" chris@420: end chris@420: s << render_project_short_description(project) chris@420: s << "
    \n" chris@420: chris@420: cs = '' chris@420: project.children.each do |child| chris@420: cs << render_my_project_in_hierarchy(child) chris@420: end chris@420: chris@420: if cs != '' chris@420: s << "\n"; chris@420: end chris@420: chris@420: end chris@420: chris@420: s chris@420: chris@420: end chris@420: luisf@68: # Renders a tree of projects where the current user belongs luisf@68: # as a nested set of unordered lists luisf@68: # The given collection may be a subset of the whole project tree luisf@68: # (eg. some intermediate nodes are private and can not be seen) luisf@68: def render_my_project_hierarchy(projects) chris@420: luisf@68: s = '' luisf@69: chris@420: original_project = @project luisf@69: chris@420: projects.each do |project| chris@420: if project.root? || !projects.include?(project.parent) chris@420: s << render_my_project_in_hierarchy(project) chris@420: end luisf@68: end luisf@69: chris@420: @project = original_project chris@420: chris@420: if s != '' chris@420: a = '' chris@420: a << "\n" chris@420: s = a luisf@69: end chris@420: chris@1139: s.html_safe luisf@69: luisf@68: end luisf@68: chris@995: # Renders a tree of projects. The given collection may be a subset chris@995: # of the whole project tree (eg. some intermediate nodes are private chris@995: # and can not be seen). We are potentially interested in various chris@995: # things: the project name, description, manager(s), creation date, chris@995: # last activity date, general activity level, whether there is chris@995: # anything actually hosted here for the project, etc. chris@124: def render_project_table(projects) luisf@68: chris@124: s = "" chris@124: s << "
    " chris@124: s << "" chris@124: s << "" chris@124: chris@939: s << sort_header_tag('name', :caption => l(:field_name)) chris@939: s << "" chris@124: s << sort_header_tag('created_on', :default_order => 'desc') chris@124: s << sort_header_tag('updated_on', :default_order => 'desc') Chris@100: chris@124: s << "" luisf@68: chris@124: original_project = @project luisf@68: chris@124: projects.each do |project| chris@205: s << render_project_in_table(project, cycle('odd', 'even'), 0) luisf@68: end luisf@69: chris@124: s << "
    " << l(:label_managers) << "
    " luisf@69: chris@124: @project = original_project chris@124: chris@1139: s.html_safe luisf@68: end luisf@68: luisf@68: chris@205: def render_project_in_table(project, oddeven, level) chris@205: chris@205: # set the project environment to please macros. chris@205: @project = project chris@205: chris@205: classes = (level == 0 ? 'root' : 'child') chris@205: chris@205: s = "" chris@205: chris@205: s << "" chris@205: s << "
    " << link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}"); chris@205: s << "
    " chris@335: s << render_project_short_description(project) chris@205: chris@205: s << "" chris@205: chris@205: u = project.users_by_role chris@205: if u chris@205: u.keys.each do |r| chris@205: if r.allowed_to?(:edit_project) chris@205: mgrs = [] chris@205: u[r].sort.each do |m| chris@205: mgrs << link_to_user(m) chris@205: end chris@205: if mgrs.size < 3 chris@205: s << '' << mgrs.join(', ') << '' chris@205: else chris@205: s << mgrs.join(', ') chris@205: end chris@205: end chris@205: end chris@205: end chris@205: chris@205: s << "" chris@205: s << "" << format_date(project.created_on) << "" chris@205: s << "" << format_date(project.updated_on) << "" chris@205: chris@205: s << "" chris@205: chris@205: project.children.each do |child| chris@414: if child.is_public? or User.current.member_of?(child) chris@414: s << render_project_in_table(child, oddeven, level + 1) chris@414: end chris@205: end chris@205: chris@205: s chris@205: end chris@205: luisf@68: Chris@0: # Returns a set of options for a select field, grouped by project. Chris@0: def version_options_for_select(versions, selected=nil) Chris@0: grouped = Hash.new {|h,k| h[k] = []} Chris@0: versions.each do |version| Chris@0: grouped[version.project.name] << [version.name, version.id] Chris@0: end Chris@441: Chris@1464: selected = selected.is_a?(Version) ? selected.id : selected Chris@0: if grouped.keys.size > 1 Chris@1464: grouped_options_for_select(grouped, selected) Chris@0: else Chris@1464: options_for_select((grouped.values.first || []), selected) Chris@0: end Chris@0: end Chris@0: Chris@0: def format_version_sharing(sharing) Chris@0: sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing) Chris@0: l("label_version_sharing_#{sharing}") Chris@0: end chris@1192: chris@1192: def score_maturity(project) chris@1192: nr_changes = (project.repository.nil? ? 0 : project.repository.changesets.count) chris@1192: downloadables = [project.attachments, chris@1192: project.versions.collect { |v| v.attachments }, chris@1192: project.documents.collect { |d| d.attachments }].flatten chris@1192: nr_downloadables = downloadables.count chris@1192: nr_downloads = downloadables.map do |d| d.downloads end.sum chris@1192: nr_members = project.members.count chris@1192: nr_publications = if project.respond_to? :publications chris@1192: then project.publications.count else 0 end chris@1192: Math.log(1 + nr_changes) + chris@1192: Math.log(1 + nr_downloadables) + chris@1192: Math.log(1 + nr_downloads) + chris@1192: Math.sqrt(nr_members > 1 ? (nr_members - 1) : 0) + Chris@1501: Math.sqrt(nr_publications * 2) chris@1192: end chris@1192: chris@1192: def all_maturity_scores() chris@1192: phash = Hash.new chris@1192: pp = Project.visible(User.anonymous) chris@1192: pp.each do |p| chris@1192: phash[p] = score_maturity p chris@1192: end chris@1192: phash chris@1192: end chris@1194: Chris@1501: def top_level_maturity_scores() Chris@1501: phash = Hash.new chris@1503: pp = Project.visible_roots(User.anonymous) Chris@1501: pp.each do |p| Chris@1501: phash[p] = score_maturity p Chris@1501: end Chris@1501: phash Chris@1501: end Chris@1501: chris@1194: def mature_projects(count) chris@1194: phash = all_maturity_scores chris@1194: scores = phash.values.sort chris@1194: threshold = scores[scores.length / 2] chris@1194: if threshold == 0 then threshold = 1 end chris@1194: phash.keys.select { |k| phash[k] > threshold }.sample(count) chris@1194: end Chris@1501: Chris@1501: def varied_mature_projects(count) Chris@1501: phash = top_level_maturity_scores Chris@1501: scores = phash.values.sort Chris@1501: threshold = scores[scores.length / 2] Chris@1501: if threshold == 0 then threshold = 1 end Chris@1506: mhash = Hash.new chris@1507: phash.keys.shuffle.select do |k| Chris@1501: if phash[k] < threshold or k.description == "" then Chris@1501: false Chris@1501: else Chris@1506: u = k.users_by_role Chris@1506: mgrs = [] Chris@1506: u.keys.each do |r| Chris@1506: if r.allowed_to?(:edit_project) chris@1507: u[r].each { |m| mgrs << m } Chris@1506: end Chris@1506: end Chris@1506: novel = (mhash.keys & mgrs).empty? Chris@1506: mgrs.each { |m| mhash[m] = 1 } Chris@1506: novel and not mgrs.empty? Chris@1501: end Chris@1501: end.sample(count) Chris@1501: end Chris@1501: Chris@0: end