To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / helpers / projects_helper.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (10.2 KB)

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