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 @ 1534:b31caaed9d4d

History | View | Annotate | Download (11.7 KB)

1
# encoding: utf-8
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2014  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=''>&nbsp;</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_action_links
55
    links = []
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)
57
    links << link_to(l(:label_issue_view_all), issues_path) if User.current.allowed_to?(:view_issues, nil, :global => true)
58
    links << link_to(l(:label_overall_spent_time), time_entries_path) if User.current.allowed_to?(:view_time_entries, nil, :global => true)
59
    links << link_to(l(:label_overall_activity), { :controller => 'activities', :action => 'index', :id => nil })
60
    links.join(" | ").html_safe
61
  end
62

    
63
  def render_project_short_description(project)
64
    s = ''
65
    if (project.short_description)
66
      s << "<div class='description'>"
67
      s << textilizable(project.short_description, :project => project).gsub(/<[^>]+>/, '')
68
      s << "</div>"
69
    end
70
    s.html_safe
71
  end
72
  
73
  # Renders a tree of projects as a nested set of unordered lists
74
  # The given collection may be a subset of the whole project tree
75
  # (eg. some intermediate nodes are private and can not be seen)
76
  def render_project_hierarchy(projects)
77
    s = ''
78
    if projects.any?
79
      ancestors = []
80
      original_project = @project
81
      projects.each do |project|
82
        # set the project environment to please macros.
83
        @project = project
84
        if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
85
          s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
86
        else
87
          ancestors.pop
88
          s << "</li>"
89
          while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
90
            ancestors.pop
91
            s << "</ul></li>\n"
92
          end
93
        end
94
        classes = (ancestors.empty? ? 'root' : 'child')
95
        s << "<li class='#{classes}'><div class='#{classes}'>" +
96
               link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}")
97
        s << render_project_short_description(project)
98
        s << "</div>\n"
99
        ancestors << project
100
      end
101
    end
102
    s.html_safe
103
  end
104

    
105

    
106
  def render_my_project_in_hierarchy(project)
107
 
108
    s = ''
109

    
110
    if User.current.member_of?(project)
111

    
112
      # set the project environment to please macros.
113
      @project = project
114

    
115
      classes = (project.root? ? 'root' : 'child')
116
      
117
      s << "<li class='#{classes}'><div class='#{classes}'>" +
118
        link_to_project(project, {}, :class => "project my-project")
119
      if project.is_public?
120
        s << " <span class='public'>" << l(:field_is_public) << "</span>"
121
      else
122
        s << " <span class='private'>" << l(:field_is_private) << "</span>"
123
      end
124
      s << render_project_short_description(project)
125
      s << "</div>\n"
126

    
127
      cs = ''
128
      project.children.each do |child|
129
        cs << render_my_project_in_hierarchy(child)
130
      end
131

    
132
      if cs != ''
133
        s << "<ul class='projects'>\n" << cs << "</ul>\n";
134
      end
135

    
136
    end
137

    
138
    s
139

    
140
  end
141

    
142
  # Renders a tree of projects where the current user belongs
143
  # as a nested set of unordered lists
144
  # The given collection may be a subset of the whole project tree
145
  # (eg. some intermediate nodes are private and can not be seen)
146
  def render_my_project_hierarchy(projects)
147

    
148
    s = ''
149

    
150
    original_project = @project
151

    
152
    projects.each do |project|
153
      if project.root? || !projects.include?(project.parent)
154
        s << render_my_project_in_hierarchy(project)
155
      end
156
    end
157

    
158
    @project = original_project
159

    
160
    if s != ''
161
      a = ''
162
      a << "<ul class='projects root'>\n"
163
      a << s
164
      a << "</ul>\n"
165
      s = a
166
    end
167

    
168
    s.html_safe
169
    
170
  end
171

    
172
  # Renders a tree of projects.  The given collection may be a subset
173
  # of the whole project tree (eg. some intermediate nodes are private
174
  # and can not be seen).  We are potentially interested in various
175
  # things: the project name, description, manager(s), creation date,
176
  # last activity date, general activity level, whether there is
177
  # anything actually hosted here for the project, etc.
178
  def render_project_table(projects)
179

    
180
    s = ""
181
    s << "<div class='autoscroll'>"
182
    s << "<table class='list projects'>"
183
    s << "<thead><tr>"
184
    
185
    s << sort_header_tag('name', :caption => l(:field_name))
186
    s << "<th class='managers'>" << l(:label_managers) << "</th>"
187
    s << sort_header_tag('created_on', :default_order => 'desc')
188
    s << sort_header_tag('updated_on', :default_order => 'desc')
189

    
190
    s << "</tr></thead><tbody>"
191

    
192
    original_project = @project
193

    
194
    projects.each do |project|
195
      s << render_project_in_table(project, cycle('odd', 'even'), 0)
196
    end
197

    
198
    s << "</table>"
199

    
200
    @project = original_project
201

    
202
    s.html_safe
203
  end
204

    
205

    
206
  def render_project_in_table(project, oddeven, level)
207

    
208
    # set the project environment to please macros.
209
    @project = project
210

    
211
    classes = (level == 0 ? 'root' : 'child')
212

    
213
    s = ""
214
    
215
    s << "<tr class='#{oddeven} #{classes} level#{level}'>"
216
    s << "<td class='firstcol' align=top><div class='name hosted_here"
217
    s << " no_description" if project.description.blank?
218
    s << "'>" << link_to_project(project, {}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}");
219
    s << "</div>"
220
    s << render_project_short_description(project)
221
      
222
    s << "<td class='managers' align=top>"
223

    
224
    u = project.users_by_role
225
    if u
226
      u.keys.each do |r|
227
        if r.allowed_to?(:edit_project)
228
          mgrs = []
229
          u[r].sort.each do |m|
230
            mgrs << link_to_user(m)
231
          end
232
          if mgrs.size < 3
233
            s << '<nobr>' << mgrs.join(', ') << '</nobr>'
234
          else
235
            s << mgrs.join(', ')
236
          end
237
        end
238
      end
239
    end
240

    
241
    s << "</td>"
242
    s << "<td class='created_on' align=top>" << format_date(project.created_on) << "</td>"
243
    s << "<td class='updated_on' align=top>" << format_date(project.updated_on) << "</td>"
244
    
245
    s << "</tr>"
246

    
247
    project.children.each do |child|
248
      if child.is_public? or User.current.member_of?(child)
249
        s << render_project_in_table(child, oddeven, level + 1)
250
      end
251
    end
252
    
253
    s
254
  end
255

    
256

    
257
  # Returns a set of options for a select field, grouped by project.
258
  def version_options_for_select(versions, selected=nil)
259
    grouped = Hash.new {|h,k| h[k] = []}
260
    versions.each do |version|
261
      grouped[version.project.name] << [version.name, version.id]
262
    end
263

    
264
    selected = selected.is_a?(Version) ? selected.id : selected
265
    if grouped.keys.size > 1
266
      grouped_options_for_select(grouped, selected)
267
    else
268
      options_for_select((grouped.values.first || []), selected)
269
    end
270
  end
271

    
272
  def format_version_sharing(sharing)
273
    sharing = 'none' unless Version::VERSION_SHARINGS.include?(sharing)
274
    l("label_version_sharing_#{sharing}")
275
  end
276

    
277
  def score_maturity(project)
278
    nr_changes = (project.repository.nil? ? 0 : project.repository.changesets.count)
279
    downloadables = [project.attachments,
280
                     project.versions.collect { |v| v.attachments },
281
                     project.documents.collect { |d| d.attachments }].flatten
282
    nr_downloadables = downloadables.count
283
    nr_downloads = downloadables.map do |d| d.downloads end.sum
284
    nr_members = project.members.count
285
    nr_publications = if project.respond_to? :publications
286
                      then project.publications.count else 0 end
287
    Math.log(1 + nr_changes) +
288
      Math.log(1 + nr_downloadables) +
289
      Math.log(1 + nr_downloads) +
290
      Math.sqrt(nr_members > 1 ? (nr_members - 1) : 0) +
291
      Math.sqrt(nr_publications * 2)
292
  end
293

    
294
  def all_maturity_scores()
295
    phash = Hash.new
296
    pp = Project.visible(User.anonymous)
297
    pp.each do |p| 
298
      phash[p] = score_maturity p
299
    end
300
    phash
301
  end
302

    
303
  def top_level_maturity_scores()
304
    phash = Hash.new
305
    pp = Project.visible_roots(User.anonymous)
306
    pp.each do |p| 
307
      phash[p] = score_maturity p
308
    end
309
    phash
310
  end
311

    
312
  def mature_projects(count)
313
    phash = all_maturity_scores
314
    scores = phash.values.sort
315
    threshold = scores[scores.length / 2]
316
    if threshold == 0 then threshold = 1 end
317
    phash.keys.select { |k| phash[k] > threshold }.sample(count)
318
  end
319

    
320
  def varied_mature_projects(count)
321
    phash = top_level_maturity_scores
322
    scores = phash.values.sort
323
    threshold = scores[scores.length / 2]
324
    if threshold == 0 then threshold = 1 end
325
    mhash = Hash.new
326
    phash.keys.shuffle.select do |k|
327
      if phash[k] < threshold or k.description == "" then
328
        false
329
      else
330
        u = k.users_by_role
331
        mgrs = []
332
        u.keys.each do |r|
333
          if r.allowed_to?(:edit_project)
334
            u[r].each { |m| mgrs << m }
335
          end
336
        end
337
        novel = (mhash.keys & mgrs).empty?
338
        mgrs.each { |m| mhash[m] = 1 }
339
        novel and not mgrs.empty?
340
      end
341
    end.sample(count)
342
  end
343

    
344
end