annotate app/helpers/projects_helper.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 16826c3afbba 622f24f53b42
children
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@441 3 # Redmine - project management software
Chris@1295 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 module ProjectsHelper
Chris@0 21 def link_to_version(version, options = {})
Chris@0 22 return '' unless version && version.is_a?(Version)
Chris@0 23 link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, options
Chris@0 24 end
Chris@441 25
Chris@0 26 def project_settings_tabs
Chris@0 27 tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural},
chris@354 28 {:name => 'overview', :action => :edit_project, :partial => 'projects/settings/overview', :label => :label_welcome_page},
Chris@0 29 {:name => 'modules', :action => :select_project_modules, :partial => 'projects/settings/modules', :label => :label_module_plural},
Chris@0 30 {:name => 'versions', :action => :manage_versions, :partial => 'projects/settings/versions', :label => :label_version_plural},
Chris@0 31 {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural},
Chris@0 32 {:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki},
Chris@1115 33 {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
Chris@0 34 {:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural},
Chris@0 35 {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities}
Chris@0 36 ]
Chris@441 37 tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
Chris@0 38 end
Chris@441 39
Chris@0 40 def parent_project_select_tag(project)
Chris@0 41 selected = project.parent
Chris@0 42 # retrieve the requested parent project
Chris@0 43 parent_id = (params[:project] && params[:project][:parent_id]) || params[:parent_id]
Chris@0 44 if parent_id
Chris@0 45 selected = (parent_id.blank? ? nil : Project.find(parent_id))
Chris@0 46 end
Chris@441 47
Chris@0 48 options = ''
Chris@0 49 options << "<option value=''></option>" if project.allowed_parents.include?(nil)
Chris@0 50 options << project_tree_options_for_select(project.allowed_parents.compact, :selected => selected)
Chris@909 51 content_tag('select', options.html_safe, :name => 'project[parent_id]', :id => 'project_parent_id')
Chris@0 52 end
Chris@441 53
chris@335 54 def render_project_short_description(project)
chris@335 55 s = ''
chris@335 56 if (project.short_description)
chris@335 57 s << "<div class='description'>"
chris@335 58 s << textilizable(project.short_description, :project => project).gsub(/<[^>]+>/, '')
chris@335 59 s << "</div>"
chris@335 60 end
chris@1139 61 s.html_safe
chris@335 62 end
Chris@14 63
Chris@909 64 # Renders a tree of projects as a nested set of unordered lists
Chris@909 65 # The given collection may be a subset of the whole project tree
Chris@909 66 # (eg. some intermediate nodes are private and can not be seen)
Chris@0 67 def render_project_hierarchy(projects)
Chris@909 68 s = ''
Chris@909 69 if projects.any?
Chris@909 70 ancestors = []
Chris@909 71 original_project = @project
Chris@909 72 projects.each do |project|
Chris@909 73 # set the project environment to please macros.
Chris@909 74 @project = project
Chris@909 75 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
Chris@909 76 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
Chris@909 77 else
Chris@909 78 ancestors.pop
Chris@909 79 s << "</li>"
Chris@909 80 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@909 81 ancestors.pop
Chris@909 82 s << "</ul></li>\n"
Chris@909 83 end
Chris@909 84 end
Chris@909 85 classes = (ancestors.empty? ? 'root' : 'child')
Chris@909 86 s << "<li class='#{classes}'><div class='#{classes}'>" +
Chris@909 87 link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}")
chris@335 88 s << render_project_short_description(project)
Chris@909 89 s << "</div>\n"
Chris@909 90 ancestors << project
Chris@0 91 end
Chris@0 92 end
Chris@909 93 s.html_safe
Chris@0 94 end
Chris@0 95
luisf@68 96
chris@420 97 def render_my_project_in_hierarchy(project)
chris@420 98
chris@420 99 s = ''
chris@420 100
chris@420 101 if User.current.member_of?(project)
chris@420 102
chris@420 103 # set the project environment to please macros.
chris@420 104 @project = project
chris@420 105
chris@420 106 classes = (project.root? ? 'root' : 'child')
chris@420 107
chris@420 108 s << "<li class='#{classes}'><div class='#{classes}'>" +
chris@420 109 link_to_project(project, {}, :class => "project my-project")
chris@420 110 if project.is_public?
chris@939 111 s << " <span class='public'>" << l(:field_is_public) << "</span>"
chris@420 112 else
chris@939 113 s << " <span class='private'>" << l(:field_is_private) << "</span>"
chris@420 114 end
chris@420 115 s << render_project_short_description(project)
chris@420 116 s << "</div>\n"
chris@420 117
chris@420 118 cs = ''
chris@420 119 project.children.each do |child|
chris@420 120 cs << render_my_project_in_hierarchy(child)
chris@420 121 end
chris@420 122
chris@420 123 if cs != ''
chris@420 124 s << "<ul class='projects'>\n" << cs << "</ul>\n";
chris@420 125 end
chris@420 126
chris@420 127 end
chris@420 128
chris@420 129 s
chris@420 130
chris@420 131 end
chris@420 132
luisf@68 133 # Renders a tree of projects where the current user belongs
luisf@68 134 # as a nested set of unordered lists
luisf@68 135 # The given collection may be a subset of the whole project tree
luisf@68 136 # (eg. some intermediate nodes are private and can not be seen)
luisf@68 137 def render_my_project_hierarchy(projects)
chris@420 138
luisf@68 139 s = ''
luisf@69 140
chris@420 141 original_project = @project
luisf@69 142
chris@420 143 projects.each do |project|
chris@420 144 if project.root? || !projects.include?(project.parent)
chris@420 145 s << render_my_project_in_hierarchy(project)
chris@420 146 end
luisf@68 147 end
luisf@69 148
chris@420 149 @project = original_project
chris@420 150
chris@420 151 if s != ''
chris@420 152 a = ''
chris@420 153 a << "<ul class='projects root'>\n"
luisf@69 154 a << s
chris@420 155 a << "</ul>\n"
chris@420 156 s = a
luisf@69 157 end
chris@420 158
chris@1139 159 s.html_safe
luisf@69 160
luisf@68 161 end
luisf@68 162
chris@995 163 # Renders a tree of projects. The given collection may be a subset
chris@995 164 # of the whole project tree (eg. some intermediate nodes are private
chris@995 165 # and can not be seen). We are potentially interested in various
chris@995 166 # things: the project name, description, manager(s), creation date,
chris@995 167 # last activity date, general activity level, whether there is
chris@995 168 # anything actually hosted here for the project, etc.
chris@124 169 def render_project_table(projects)
luisf@68 170
chris@124 171 s = ""
chris@124 172 s << "<div class='autoscroll'>"
chris@124 173 s << "<table class='list projects'>"
chris@124 174 s << "<thead><tr>"
chris@124 175
chris@939 176 s << sort_header_tag('name', :caption => l(:field_name))
chris@939 177 s << "<th class='managers'>" << l(:label_managers) << "</th>"
chris@124 178 s << sort_header_tag('created_on', :default_order => 'desc')
chris@124 179 s << sort_header_tag('updated_on', :default_order => 'desc')
Chris@100 180
chris@124 181 s << "</tr></thead><tbody>"
luisf@68 182
chris@124 183 original_project = @project
luisf@68 184
chris@124 185 projects.each do |project|
chris@205 186 s << render_project_in_table(project, cycle('odd', 'even'), 0)
luisf@68 187 end
luisf@69 188
chris@124 189 s << "</table>"
luisf@69 190
chris@124 191 @project = original_project
chris@124 192
chris@1139 193 s.html_safe
luisf@68 194 end
luisf@68 195
luisf@68 196
chris@205 197 def render_project_in_table(project, oddeven, level)
chris@205 198
chris@205 199 # set the project environment to please macros.
chris@205 200 @project = project
chris@205 201
chris@205 202 classes = (level == 0 ? 'root' : 'child')
chris@205 203
chris@205 204 s = ""
chris@205 205
chris@205 206 s << "<tr class='#{oddeven} #{classes} level#{level}'>"
chris@205 207 s << "<td class='firstcol' align=top><div class='name hosted_here"
chris@205 208 s << " no_description" if project.description.blank?
chris@205 209 s << "'>" << link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}");
chris@205 210 s << "</div>"
chris@335 211 s << render_project_short_description(project)
chris@205 212
chris@205 213 s << "<td class='managers' align=top>"
chris@205 214
chris@205 215 u = project.users_by_role
chris@205 216 if u
chris@205 217 u.keys.each do |r|
chris@205 218 if r.allowed_to?(:edit_project)
chris@205 219 mgrs = []
chris@205 220 u[r].sort.each do |m|
chris@205 221 mgrs << link_to_user(m)
chris@205 222 end
chris@205 223 if mgrs.size < 3
chris@205 224 s << '<nobr>' << mgrs.join(', ') << '</nobr>'
chris@205 225 else
chris@205 226 s << mgrs.join(', ')
chris@205 227 end
chris@205 228 end
chris@205 229 end
chris@205 230 end
chris@205 231
chris@205 232 s << "</td>"
chris@205 233 s << "<td class='created_on' align=top>" << format_date(project.created_on) << "</td>"
chris@205 234 s << "<td class='updated_on' align=top>" << format_date(project.updated_on) << "</td>"
chris@205 235
chris@205 236 s << "</tr>"
chris@205 237
chris@205 238 project.children.each do |child|
chris@414 239 if child.is_public? or User.current.member_of?(child)
chris@414 240 s << render_project_in_table(child, oddeven, level + 1)
chris@414 241 end
chris@205 242 end
chris@205 243
chris@205 244 s
chris@205 245 end
chris@205 246
luisf@68 247
Chris@0 248 # Returns a set of options for a select field, grouped by project.
Chris@0 249 def version_options_for_select(versions, selected=nil)
Chris@0 250 grouped = Hash.new {|h,k| h[k] = []}
Chris@0 251 versions.each do |version|
Chris@0 252 grouped[version.project.name] << [version.name, version.id]
Chris@0 253 end
Chris@441 254
Chris@0 255 if grouped.keys.size > 1
Chris@0 256 grouped_options_for_select(grouped, selected && selected.id)
Chris@0 257 else
Chris@0 258 options_for_select((grouped.values.first || []), selected && selected.id)
Chris@0 259 end
Chris@0 260 end
Chris@0 261
Chris@0 262 def format_version_sharing(sharing)
Chris@0 263 sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
Chris@0 264 l("label_version_sharing_#{sharing}")
Chris@0 265 end
chris@1192 266
chris@1192 267 def score_maturity(project)
chris@1192 268 nr_changes = (project.repository.nil? ? 0 : project.repository.changesets.count)
chris@1192 269 downloadables = [project.attachments,
chris@1192 270 project.versions.collect { |v| v.attachments },
chris@1192 271 project.documents.collect { |d| d.attachments }].flatten
chris@1192 272 nr_downloadables = downloadables.count
chris@1192 273 nr_downloads = downloadables.map do |d| d.downloads end.sum
chris@1192 274 nr_members = project.members.count
chris@1192 275 nr_publications = if project.respond_to? :publications
chris@1192 276 then project.publications.count else 0 end
chris@1192 277 Math.log(1 + nr_changes) +
chris@1192 278 Math.log(1 + nr_downloadables) +
chris@1192 279 Math.log(1 + nr_downloads) +
chris@1192 280 Math.sqrt(nr_members > 1 ? (nr_members - 1) : 0) +
chris@1192 281 Math.sqrt(nr_publications)
chris@1192 282 end
chris@1192 283
chris@1192 284 def all_maturity_scores()
chris@1192 285 phash = Hash.new
chris@1192 286 pp = Project.visible(User.anonymous)
chris@1192 287 pp.each do |p|
chris@1192 288 phash[p] = score_maturity p
chris@1192 289 end
chris@1192 290 phash
chris@1192 291 end
chris@1194 292
chris@1194 293 def mature_projects(count)
chris@1194 294 phash = all_maturity_scores
chris@1194 295 scores = phash.values.sort
chris@1194 296 threshold = scores[scores.length / 2]
chris@1194 297 if threshold == 0 then threshold = 1 end
chris@1194 298 phash.keys.select { |k| phash[k] > threshold }.sample(count)
chris@1194 299 end
Chris@0 300 end