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 @ 1330:441b66f148b6

History | View | Annotate | Download (10.2 KB)

1
# encoding: utf-8
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2012  Jean-Philippe Lang
5
#
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
#
11
# 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
#
16
# 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

    
26
  def project_settings_tabs
27
    tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural},
28
            {:name => 'overview', :action => :edit_project, :partial => 'projects/settings/overview', :label => :label_welcome_page},
29
            {: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
            {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural},
34
            {: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
    tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)}
38
  end
39

    
40
  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

    
48
    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
    content_tag('select', options.html_safe, :name => 'project[parent_id]', :id => 'project_parent_id')
52
  end
53

    
54
  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
    s.html_safe
62
  end
63
  
64
  # 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
  def render_project_hierarchy(projects)
68
    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
        s << render_project_short_description(project)
89
        s << "</div>\n"
90
        ancestors << project
91
      end
92
    end
93
    s.html_safe
94
  end
95

    
96

    
97
  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
        s << " <span class='public'>" << l(:field_is_public) << "</span>"
112
      else
113
        s << " <span class='private'>" << l(:field_is_private) << "</span>"
114
      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
  # 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

    
139
    s = ''
140

    
141
    original_project = @project
142

    
143
    projects.each do |project|
144
      if project.root? || !projects.include?(project.parent)
145
        s << render_my_project_in_hierarchy(project)
146
      end
147
    end
148

    
149
    @project = original_project
150

    
151
    if s != ''
152
      a = ''
153
      a << "<ul class='projects root'>\n"
154
      a << s
155
      a << "</ul>\n"
156
      s = a
157
    end
158

    
159
    s.html_safe
160
    
161
  end
162

    
163
  # 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
  def render_project_table(projects)
170

    
171
    s = ""
172
    s << "<div class='autoscroll'>"
173
    s << "<table class='list projects'>"
174
    s << "<thead><tr>"
175
    
176
    s << sort_header_tag('name', :caption => l(:field_name))
177
    s << "<th class='managers'>" << l(:label_managers) << "</th>"
178
    s << sort_header_tag('created_on', :default_order => 'desc')
179
    s << sort_header_tag('updated_on', :default_order => 'desc')
180

    
181
    s << "</tr></thead><tbody>"
182

    
183
    original_project = @project
184

    
185
    projects.each do |project|
186
      s << render_project_in_table(project, cycle('odd', 'even'), 0)
187
    end
188

    
189
    s << "</table>"
190

    
191
    @project = original_project
192

    
193
    s.html_safe
194
  end
195

    
196

    
197
  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
    s << render_project_short_description(project)
212
      
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
      if child.is_public? or User.current.member_of?(child)
240
        s << render_project_in_table(child, oddeven, level + 1)
241
      end
242
    end
243
    
244
    s
245
  end
246

    
247

    
248
  # 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

    
255
    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

    
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

    
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
end