Chris@909
|
1 # encoding: utf-8
|
Chris@909
|
2 #
|
Chris@441
|
3 # Redmine - project management software
|
Chris@1494
|
4 # Copyright (C) 2006-2014 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@1464
|
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@1517
|
54 def render_project_action_links
|
Chris@1517
|
55 links = []
|
Chris@1517
|
56 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
|
57 links << link_to(l(:label_issue_view_all), issues_path) if User.current.allowed_to?(:view_issues, nil, :global => true)
|
Chris@1517
|
58 links << link_to(l(:label_overall_spent_time), time_entries_path) if User.current.allowed_to?(:view_time_entries, nil, :global => true)
|
Chris@1517
|
59 links << link_to(l(:label_overall_activity), { :controller => 'activities', :action => 'index', :id => nil })
|
Chris@1517
|
60 links.join(" | ").html_safe
|
Chris@1517
|
61 end
|
Chris@1517
|
62
|
chris@335
|
63 def render_project_short_description(project)
|
chris@335
|
64 s = ''
|
chris@335
|
65 if (project.short_description)
|
chris@335
|
66 s << "<div class='description'>"
|
chris@335
|
67 s << textilizable(project.short_description, :project => project).gsub(/<[^>]+>/, '')
|
chris@335
|
68 s << "</div>"
|
chris@335
|
69 end
|
chris@1139
|
70 s.html_safe
|
chris@335
|
71 end
|
Chris@14
|
72
|
Chris@0
|
73 # Renders a tree of projects as a nested set of unordered lists
|
Chris@0
|
74 # The given collection may be a subset of the whole project tree
|
Chris@0
|
75 # (eg. some intermediate nodes are private and can not be seen)
|
Chris@0
|
76 def render_project_hierarchy(projects)
|
Chris@0
|
77 s = ''
|
Chris@0
|
78 if projects.any?
|
Chris@0
|
79 ancestors = []
|
Chris@0
|
80 original_project = @project
|
Chris@0
|
81 projects.each do |project|
|
Chris@0
|
82 # set the project environment to please macros.
|
Chris@0
|
83 @project = project
|
Chris@0
|
84 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
|
Chris@0
|
85 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
|
Chris@0
|
86 else
|
Chris@0
|
87 ancestors.pop
|
Chris@0
|
88 s << "</li>"
|
Chris@441
|
89 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
|
Chris@0
|
90 ancestors.pop
|
Chris@0
|
91 s << "</ul></li>\n"
|
Chris@0
|
92 end
|
Chris@0
|
93 end
|
Chris@0
|
94 classes = (ancestors.empty? ? 'root' : 'child')
|
Chris@0
|
95 s << "<li class='#{classes}'><div class='#{classes}'>" +
|
Chris@14
|
96 link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}")
|
chris@335
|
97 s << render_project_short_description(project)
|
Chris@0
|
98 s << "</div>\n"
|
Chris@0
|
99 ancestors << project
|
Chris@0
|
100 end
|
Chris@0
|
101 end
|
Chris@909
|
102 s.html_safe
|
Chris@0
|
103 end
|
Chris@0
|
104
|
luisf@68
|
105
|
chris@420
|
106 def render_my_project_in_hierarchy(project)
|
chris@420
|
107
|
chris@420
|
108 s = ''
|
chris@420
|
109
|
chris@420
|
110 if User.current.member_of?(project)
|
chris@420
|
111
|
chris@420
|
112 # set the project environment to please macros.
|
chris@420
|
113 @project = project
|
chris@420
|
114
|
chris@420
|
115 classes = (project.root? ? 'root' : 'child')
|
chris@420
|
116
|
chris@420
|
117 s << "<li class='#{classes}'><div class='#{classes}'>" +
|
chris@420
|
118 link_to_project(project, {}, :class => "project my-project")
|
chris@420
|
119 if project.is_public?
|
chris@939
|
120 s << " <span class='public'>" << l(:field_is_public) << "</span>"
|
chris@420
|
121 else
|
chris@939
|
122 s << " <span class='private'>" << l(:field_is_private) << "</span>"
|
chris@420
|
123 end
|
chris@420
|
124 s << render_project_short_description(project)
|
chris@420
|
125 s << "</div>\n"
|
chris@420
|
126
|
chris@420
|
127 cs = ''
|
chris@420
|
128 project.children.each do |child|
|
chris@420
|
129 cs << render_my_project_in_hierarchy(child)
|
chris@420
|
130 end
|
chris@420
|
131
|
chris@420
|
132 if cs != ''
|
chris@420
|
133 s << "<ul class='projects'>\n" << cs << "</ul>\n";
|
chris@420
|
134 end
|
chris@420
|
135
|
chris@420
|
136 end
|
chris@420
|
137
|
chris@420
|
138 s
|
chris@420
|
139
|
chris@420
|
140 end
|
chris@420
|
141
|
luisf@68
|
142 # Renders a tree of projects where the current user belongs
|
luisf@68
|
143 # as a nested set of unordered lists
|
luisf@68
|
144 # The given collection may be a subset of the whole project tree
|
luisf@68
|
145 # (eg. some intermediate nodes are private and can not be seen)
|
luisf@68
|
146 def render_my_project_hierarchy(projects)
|
chris@420
|
147
|
luisf@68
|
148 s = ''
|
luisf@69
|
149
|
chris@420
|
150 original_project = @project
|
luisf@69
|
151
|
chris@420
|
152 projects.each do |project|
|
chris@420
|
153 if project.root? || !projects.include?(project.parent)
|
chris@420
|
154 s << render_my_project_in_hierarchy(project)
|
chris@420
|
155 end
|
luisf@68
|
156 end
|
luisf@69
|
157
|
chris@420
|
158 @project = original_project
|
chris@420
|
159
|
chris@420
|
160 if s != ''
|
chris@420
|
161 a = ''
|
chris@420
|
162 a << "<ul class='projects root'>\n"
|
luisf@69
|
163 a << s
|
chris@420
|
164 a << "</ul>\n"
|
chris@420
|
165 s = a
|
luisf@69
|
166 end
|
chris@420
|
167
|
chris@1139
|
168 s.html_safe
|
luisf@69
|
169
|
luisf@68
|
170 end
|
luisf@68
|
171
|
chris@995
|
172 # Renders a tree of projects. The given collection may be a subset
|
chris@995
|
173 # of the whole project tree (eg. some intermediate nodes are private
|
chris@995
|
174 # and can not be seen). We are potentially interested in various
|
chris@995
|
175 # things: the project name, description, manager(s), creation date,
|
chris@995
|
176 # last activity date, general activity level, whether there is
|
chris@995
|
177 # anything actually hosted here for the project, etc.
|
chris@124
|
178 def render_project_table(projects)
|
luisf@68
|
179
|
chris@124
|
180 s = ""
|
chris@124
|
181 s << "<div class='autoscroll'>"
|
chris@124
|
182 s << "<table class='list projects'>"
|
chris@124
|
183 s << "<thead><tr>"
|
chris@124
|
184
|
chris@939
|
185 s << sort_header_tag('name', :caption => l(:field_name))
|
chris@939
|
186 s << "<th class='managers'>" << l(:label_managers) << "</th>"
|
chris@124
|
187 s << sort_header_tag('created_on', :default_order => 'desc')
|
chris@124
|
188 s << sort_header_tag('updated_on', :default_order => 'desc')
|
Chris@100
|
189
|
chris@124
|
190 s << "</tr></thead><tbody>"
|
luisf@68
|
191
|
chris@124
|
192 original_project = @project
|
luisf@68
|
193
|
chris@124
|
194 projects.each do |project|
|
chris@205
|
195 s << render_project_in_table(project, cycle('odd', 'even'), 0)
|
luisf@68
|
196 end
|
luisf@69
|
197
|
chris@124
|
198 s << "</table>"
|
luisf@69
|
199
|
chris@124
|
200 @project = original_project
|
chris@124
|
201
|
chris@1139
|
202 s.html_safe
|
luisf@68
|
203 end
|
luisf@68
|
204
|
luisf@68
|
205
|
chris@205
|
206 def render_project_in_table(project, oddeven, level)
|
chris@205
|
207
|
chris@205
|
208 # set the project environment to please macros.
|
chris@205
|
209 @project = project
|
chris@205
|
210
|
chris@205
|
211 classes = (level == 0 ? 'root' : 'child')
|
chris@205
|
212
|
chris@205
|
213 s = ""
|
chris@205
|
214
|
chris@205
|
215 s << "<tr class='#{oddeven} #{classes} level#{level}'>"
|
chris@205
|
216 s << "<td class='firstcol' align=top><div class='name hosted_here"
|
chris@205
|
217 s << " no_description" if project.description.blank?
|
chris@205
|
218 s << "'>" << link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}");
|
chris@205
|
219 s << "</div>"
|
chris@335
|
220 s << render_project_short_description(project)
|
chris@205
|
221
|
chris@205
|
222 s << "<td class='managers' align=top>"
|
chris@205
|
223
|
chris@205
|
224 u = project.users_by_role
|
chris@205
|
225 if u
|
chris@205
|
226 u.keys.each do |r|
|
chris@205
|
227 if r.allowed_to?(:edit_project)
|
chris@205
|
228 mgrs = []
|
chris@205
|
229 u[r].sort.each do |m|
|
chris@205
|
230 mgrs << link_to_user(m)
|
chris@205
|
231 end
|
chris@205
|
232 if mgrs.size < 3
|
chris@205
|
233 s << '<nobr>' << mgrs.join(', ') << '</nobr>'
|
chris@205
|
234 else
|
chris@205
|
235 s << mgrs.join(', ')
|
chris@205
|
236 end
|
chris@205
|
237 end
|
chris@205
|
238 end
|
chris@205
|
239 end
|
chris@205
|
240
|
chris@205
|
241 s << "</td>"
|
chris@205
|
242 s << "<td class='created_on' align=top>" << format_date(project.created_on) << "</td>"
|
chris@205
|
243 s << "<td class='updated_on' align=top>" << format_date(project.updated_on) << "</td>"
|
chris@205
|
244
|
chris@205
|
245 s << "</tr>"
|
chris@205
|
246
|
chris@205
|
247 project.children.each do |child|
|
chris@414
|
248 if child.is_public? or User.current.member_of?(child)
|
chris@414
|
249 s << render_project_in_table(child, oddeven, level + 1)
|
chris@414
|
250 end
|
chris@205
|
251 end
|
chris@205
|
252
|
chris@205
|
253 s
|
chris@205
|
254 end
|
chris@205
|
255
|
luisf@68
|
256
|
Chris@0
|
257 # Returns a set of options for a select field, grouped by project.
|
Chris@0
|
258 def version_options_for_select(versions, selected=nil)
|
Chris@0
|
259 grouped = Hash.new {|h,k| h[k] = []}
|
Chris@0
|
260 versions.each do |version|
|
Chris@0
|
261 grouped[version.project.name] << [version.name, version.id]
|
Chris@0
|
262 end
|
Chris@441
|
263
|
Chris@1464
|
264 selected = selected.is_a?(Version) ? selected.id : selected
|
Chris@0
|
265 if grouped.keys.size > 1
|
Chris@1464
|
266 grouped_options_for_select(grouped, selected)
|
Chris@0
|
267 else
|
Chris@1464
|
268 options_for_select((grouped.values.first || []), selected)
|
Chris@0
|
269 end
|
Chris@0
|
270 end
|
Chris@0
|
271
|
Chris@0
|
272 def format_version_sharing(sharing)
|
Chris@0
|
273 sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
|
Chris@0
|
274 l("label_version_sharing_#{sharing}")
|
Chris@0
|
275 end
|
chris@1192
|
276
|
chris@1192
|
277 def score_maturity(project)
|
chris@1192
|
278 nr_changes = (project.repository.nil? ? 0 : project.repository.changesets.count)
|
chris@1192
|
279 downloadables = [project.attachments,
|
chris@1192
|
280 project.versions.collect { |v| v.attachments },
|
chris@1192
|
281 project.documents.collect { |d| d.attachments }].flatten
|
chris@1192
|
282 nr_downloadables = downloadables.count
|
chris@1192
|
283 nr_downloads = downloadables.map do |d| d.downloads end.sum
|
chris@1192
|
284 nr_members = project.members.count
|
chris@1192
|
285 nr_publications = if project.respond_to? :publications
|
chris@1192
|
286 then project.publications.count else 0 end
|
chris@1192
|
287 Math.log(1 + nr_changes) +
|
chris@1192
|
288 Math.log(1 + nr_downloadables) +
|
chris@1192
|
289 Math.log(1 + nr_downloads) +
|
chris@1192
|
290 Math.sqrt(nr_members > 1 ? (nr_members - 1) : 0) +
|
Chris@1501
|
291 Math.sqrt(nr_publications * 2)
|
chris@1192
|
292 end
|
chris@1192
|
293
|
chris@1192
|
294 def all_maturity_scores()
|
chris@1192
|
295 phash = Hash.new
|
chris@1192
|
296 pp = Project.visible(User.anonymous)
|
chris@1192
|
297 pp.each do |p|
|
chris@1192
|
298 phash[p] = score_maturity p
|
chris@1192
|
299 end
|
chris@1192
|
300 phash
|
chris@1192
|
301 end
|
chris@1194
|
302
|
Chris@1501
|
303 def top_level_maturity_scores()
|
Chris@1501
|
304 phash = Hash.new
|
chris@1503
|
305 pp = Project.visible_roots(User.anonymous)
|
Chris@1501
|
306 pp.each do |p|
|
Chris@1501
|
307 phash[p] = score_maturity p
|
Chris@1501
|
308 end
|
Chris@1501
|
309 phash
|
Chris@1501
|
310 end
|
Chris@1501
|
311
|
chris@1194
|
312 def mature_projects(count)
|
chris@1194
|
313 phash = all_maturity_scores
|
chris@1194
|
314 scores = phash.values.sort
|
chris@1194
|
315 threshold = scores[scores.length / 2]
|
chris@1194
|
316 if threshold == 0 then threshold = 1 end
|
chris@1194
|
317 phash.keys.select { |k| phash[k] > threshold }.sample(count)
|
chris@1194
|
318 end
|
Chris@1501
|
319
|
Chris@1501
|
320 def varied_mature_projects(count)
|
Chris@1501
|
321 phash = top_level_maturity_scores
|
Chris@1501
|
322 scores = phash.values.sort
|
Chris@1501
|
323 threshold = scores[scores.length / 2]
|
Chris@1501
|
324 if threshold == 0 then threshold = 1 end
|
Chris@1506
|
325 mhash = Hash.new
|
chris@1507
|
326 phash.keys.shuffle.select do |k|
|
Chris@1501
|
327 if phash[k] < threshold or k.description == "" then
|
Chris@1501
|
328 false
|
Chris@1501
|
329 else
|
Chris@1506
|
330 u = k.users_by_role
|
Chris@1506
|
331 mgrs = []
|
Chris@1506
|
332 u.keys.each do |r|
|
Chris@1506
|
333 if r.allowed_to?(:edit_project)
|
chris@1507
|
334 u[r].each { |m| mgrs << m }
|
Chris@1506
|
335 end
|
Chris@1506
|
336 end
|
Chris@1506
|
337 novel = (mhash.keys & mgrs).empty?
|
Chris@1506
|
338 mgrs.each { |m| mhash[m] = 1 }
|
Chris@1506
|
339 novel and not mgrs.empty?
|
Chris@1501
|
340 end
|
Chris@1501
|
341 end.sample(count)
|
Chris@1501
|
342 end
|
Chris@1501
|
343
|
Chris@0
|
344 end
|