2013-08-01 10:33:49 +08:00
# encoding: utf-8
#
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module WelcomeHelper
2013-11-29 10:48:16 +08:00
2013-12-16 14:39:02 +08:00
include CoursesHelper
2014-03-07 20:34:17 +08:00
include ProjectsHelper
2013-12-07 09:47:54 +08:00
2014-06-16 09:19:21 +08:00
def get_timestamp ( obj )
if obj . respond_to? :updated_on
:updated_on
elsif obj . respond_to? :updated_at
:updated_at
elsif obj . respond_to? :created_on
:created_on
elsif obj . respond_to? :created_at
:created_at
else
Time . now . to_i . to_s . to_sym
end
end
def cache_key_for_project ( obj )
timestamp = get_timestamp ( obj )
" welcome_index_project_ul_ #{ obj . class } _li_ #{ obj . id } _ #{ obj . __send__ timestamp } "
end
def cache_key_for_event ( obj )
timestamp = get_timestamp ( obj )
" welcome_index_event_ul_ #{ obj . class } _li_ #{ obj . id } _ #{ obj . __send__ timestamp } "
end
def cache_key_for_topic ( obj )
timestamp = get_timestamp ( obj )
" welcome_index_topic_ul_ #{ obj . class } _li_ #{ obj . id } _ #{ obj . __send__ timestamp } "
end
2014-03-12 17:09:26 +08:00
def welcome_join_in_course ( project , user )
if ( user . logged? &&
2014-03-20 15:28:36 +08:00
! ( course_endTime_timeout? project ) &&
2014-03-12 17:09:26 +08:00
( project . course_extra . teacher . id != user . id )
)
join_in_course ( project , user )
end
end
def get_course_avatar project
if get_avatar? ( project )
url_to_avatar ( project )
else
'../images/avatars/Project/course.jpg'
end
end
2014-03-24 10:18:42 +08:00
def get_project_avatar project
if get_avatar? ( project )
url_to_avatar ( project )
else
2014-03-25 09:34:43 +08:00
'../images/avatars/Project/0'
2014-03-24 10:18:42 +08:00
end
end
2014-03-08 11:08:48 +08:00
# 前略·天国の首页君/Earth has been unable stop to welcomePage's.
# sum - 要搜索的项目数量
# max_rate - 新项目所占所有项目的比重, 10分制
#
# Examples
#
# find_miracle_course(10, 7)
# # => 前7个项目为新课程, 后面三个是参与人数最多的
#
# Returns project&courses array
2014-07-03 15:24:06 +08:00
# 原来的取课程逻辑
def find_miracle_course_base ( sum = 10 , max_rate = 7 , school_id )
2014-04-23 21:20:03 +08:00
if User . current . user_extensions . school . nil? and school_id . nil?
Project . active . visible . course_entities .
joins ( :course_extra ) .
joins ( :memberships ) .
group ( 'members.project_id' ) .
reorder ( " courses.time DESC, COUNT(members.project_id) DESC " ) . take sum
# elseif school_id.nil?
else
if school_id . nil?
Project . active . visible . course_entities .
joins ( :course_extra ) .
joins ( :memberships ) .
where ( " #{ Course . table_name } .school_id = ? " , User . current . user_extensions . school . id ) .
group ( 'members.project_id' ) .
reorder ( " courses.time DESC, COUNT(members.project_id) DESC " ) . take sum
else
if school_id == " 0 "
Project . active . visible . course_entities .
joins ( :course_extra ) .
joins ( :memberships ) .
group ( 'members.project_id' ) .
reorder ( " courses.time DESC, COUNT(members.project_id) DESC " ) . take sum
else
Project . active . visible . course_entities .
joins ( :course_extra ) .
joins ( :memberships ) .
where ( " #{ Course . table_name } .school_id = ? " , school_id ) .
group ( 'members.project_id' ) .
reorder ( " courses.time DESC, COUNT(members.project_id) DESC " ) . take sum
end
end
end
# else
# Project.active.visible.course_entities.
# joins(:course_extra).
# joins(:memberships).
# where("#{Course.table_name}.school_id = ?", school_id).
# group('members.project_id').
# reorder("courses.time DESC, COUNT(members.project_id) DESC").take sum
# end
2014-03-28 18:12:12 +08:00
# max = sum*(max_rate.to_f/10)
# c1 = find_new_course(sum).to_a.dup
# c2 = find_all_hot_course(sum).to_a.dup
# c2 = c2 - c1
# (c1.take(max)+c2).take(sum)
2014-03-08 11:08:48 +08:00
end
2014-05-10 15:33:18 +08:00
2014-07-03 15:24:06 +08:00
#获取课程列表
# add by nwb
def find_miracle_course ( sum = 10 , max_rate = 7 , school_id )
if User . current . user_extensions . school . nil? and school_id . nil?
Course . active . visible .
joins ( :memberships ) .
group ( 'members.course_id' ) .
2014-07-05 09:33:52 +08:00
reorder ( " courses.created_at DESC, COUNT(members.course_id) DESC " ) . take sum
2014-07-03 15:24:06 +08:00
else
if school_id . nil?
Course . active . visible .
joins ( :memberships ) .
where ( " #{ Course . table_name } .school_id = ? " , User . current . user_extensions . school . id ) .
group ( 'members.course_id' ) .
2014-07-05 09:33:52 +08:00
reorder ( " courses.created_at DESC, COUNT(members.course_id) DESC " ) . take sum
2014-07-03 15:24:06 +08:00
else
if school_id == " 0 "
Course . active . visible .
joins ( :memberships ) .
group ( 'members.course_id' ) .
2014-07-05 09:33:52 +08:00
reorder ( " courses.created_at DESC, COUNT(members.course_id) DESC " ) . take sum
2014-07-03 15:24:06 +08:00
else
Course . active . visible .
joins ( :memberships ) .
where ( " #{ Course . table_name } .school_id = ? " , school_id ) .
group ( 'members.course_id' ) .
2014-07-05 09:33:52 +08:00
reorder ( " courses.created_at DESC, COUNT(members.course_id) DESC " ) . take sum
2014-07-03 15:24:06 +08:00
end
end
end
# else
# Project.active.visible.course_entities.
# joins(:course_extra).
# joins(:memberships).
# where("#{Course.table_name}.school_id = ?", school_id).
# group('members.project_id').
# reorder("courses.time DESC, COUNT(members.project_id) DESC").take sum
# end
# max = sum*(max_rate.to_f/10)
# c1 = find_new_course(sum).to_a.dup
# c2 = find_all_hot_course(sum).to_a.dup
# c2 = c2 - c1
# (c1.take(max)+c2).take(sum)
end
2014-05-10 15:33:18 +08:00
#查找所有学校按每个学校开设课程数量降序排序
#page 分页查询开始条数的编号,从0开始
#limit 分页查询的数量
def find_maxmin_course_school page , limit
School . find_by_sql ( " SELECT *,(SELECT COUNT(*) FROM courses WHERE school_id = schools.id) AS a
FROM schools
ORDER BY a DESC LIMIT #{page},#{limit}")
#School.where(" id IN (SELECT school_id FROM courses GROUP BY school_id)").limit limit;
#School.order("#{School.course_count}").limit(limit).all
#@school = School.all.sort
#@school.each do |s|
# s.courses.count
#end
#result = []
#@school = School.all.to_ary
#i = 1
#for i in i < School.count
# j = i - 1
# for j in j > 0
# if @school[j].courses.count >
# end
#end
end
2014-05-12 16:01:13 +08:00
2014-03-24 17:12:28 +08:00
def find_miracle_project ( sum , max_rate )
2014-06-23 09:42:08 +08:00
#max = sum*(max_rate.to_f/10)
#c1 = find_new_project(sum).to_a.dup
#c2 = find_all_hot_project(sum).to_a.dup
#(c2.take(sum-max)+c1.take(max)).take(sum)
find_all_hot_project ( sum ) . to_a . dup
2014-03-24 10:18:42 +08:00
end
2014-03-07 11:28:52 +08:00
def find_new_course limit = 15
Project . visible . joins ( :course_extra ) . where ( " #{ Project . table_name } .project_type = ? " , 1 ) . order ( " courses.time DESC, #{ Project . table_name } .created_on DESC " ) . limit ( limit ) . all
end
2014-03-24 10:18:42 +08:00
def find_new_project limit = 15
2014-03-24 17:12:28 +08:00
Project . visible . where ( " #{ Project . table_name } .project_type = ? " , 0 ) . order ( " #{ Project . table_name } .updated_on DESC, #{ Project . table_name } .created_on DESC " ) . limit ( limit ) . all
2014-03-24 10:18:42 +08:00
end
2014-03-07 11:28:52 +08:00
2014-02-22 09:44:14 +08:00
def find_all_hot_project limit = 15
sort_project_by_hot limit
2013-11-29 10:48:16 +08:00
end
2014-02-22 09:44:14 +08:00
def find_all_hot_course limit = 15
sort_course_by_hot limit
2013-11-29 10:48:16 +08:00
end
2014-07-03 10:12:30 +08:00
# modif by nwb
2014-05-19 16:53:38 +08:00
def find_all_new_hot_course limit = 9 , school_id = 0
2014-05-13 10:30:15 +08:00
#sort_project_by_hot_rails 1, 'course_ac_para DESC', limit
time_now = Time . new . strftime ( " %Y " ) ;
2014-07-04 09:10:17 +08:00
if school_id
2014-07-03 17:28:13 +08:00
courses = Course . visible . joins ( :course_status ) . where ( " #{ Course . table_name } .created_at like '% #{ time_now } %' and #{ Course . table_name } .school_id <>
2014-07-03 10:12:30 +08:00
?" , school_id ) . order ( " course_ac_para DESC " ) . limit ( limit ) . all
2014-07-04 09:10:17 +08:00
else
courses = Course . visible . joins ( :course_status ) . where ( " #{ Course . table_name } .created_at like '% #{ time_now } %' and #{ Course . table_name } .school_id is not NULL
" ).order( " course_ac_para DESC " ).limit(limit).all
end
2014-07-03 17:28:13 +08:00
courses
2014-05-13 10:30:15 +08:00
end
2013-11-29 10:48:16 +08:00
def find_all_hot_bid
sort_bid_by_hot
end
2014-04-11 09:00:00 +08:00
def find_all_hot_contest limit = 10
2014-04-25 10:10:36 +08:00
Contest . reorder ( " created_on DESC " ) . all . take limit
# mix_bid = []
# mix_bid += Contest.reorder("created_on DESC").take(limit).to_a
# mix_bid += Bid.visible.where('reward_type = ?', 2).reorder('bids.created_on desc').take(limit).to_a
# mix_bid.sort do |older, newer|
# newer.created_on - older.created_on
# end
# mix_bid.take limit
2013-11-29 10:48:16 +08:00
end
2014-04-16 17:32:58 +08:00
def find_all_hot_softapplication limit = 10
Softapplication . reorder ( " created_at DESC " ) . all . take limit
end
2013-12-04 16:46:16 +08:00
def cal_memos_count event
return nil if event . parent_id
event . replies_count
rescue NoMethodError
nil
end
def cal_issues_count event
event . journals . count
rescue NoMethodError
nil
end
2013-11-30 10:41:14 +08:00
2013-12-09 18:31:08 +08:00
def topic_last_time topic
2013-12-11 16:14:51 +08:00
return topic . event_datetime if ( ! ( topic . methods . to_s =~ %r[ last_reply ] ) || topic . last_reply . nil? )
2013-12-09 18:31:08 +08:00
topic . last_reply . event_datetime
end
2013-11-29 10:48:16 +08:00
def time_tag_welcome time
text = distance_of_time_in_words ( Time . now , time )
content_tag ( 'span' , text , :title = > format_time ( time ) )
end
def show_grade project
grade = 0
begin
2014-07-02 11:32:49 +08:00
#ActiveRecord::Base.connection.execute("CALL sp_project_status_cursor();")#执行存储过程速度慢
2013-11-29 10:48:16 +08:00
grade = project . project_status . grade if project && project . project_status
rescue Exception = > e
2014-01-13 16:24:13 +08:00
logger . error " Logger.Error [WelcomeHelper] ===> #{ e } "
2013-11-29 10:48:16 +08:00
end
2013-12-05 15:26:29 +08:00
" 项目评分: " . html_safe << grade . to_s
2013-11-29 10:48:16 +08:00
end
2013-11-30 10:41:14 +08:00
def show_user_content event
2013-12-12 09:19:31 +08:00
str = ' ' . html_safe
2013-11-30 10:41:14 +08:00
case event . event_type
2014-01-06 19:29:18 +08:00
when 'news'
str << content_tag ( " span " , " 发表了 " ) <<
content_tag ( " span " , find_all_event_type ( event ) ) <<
': ' . html_safe <<
link_to ( truncate ( strip_tags ( event . event_description ) . gsub ( / / , '' ) , length : 30 , omission : '...' ) , event . event_url )
when 'issue' , 'message' , 'bid' , 'wiki-page' , 'document'
str << content_tag ( " span " , " 发表了 " ) <<
content_tag ( " span " , find_all_event_type ( event ) ) <<
': ' . html_safe <<
link_to ( truncate ( event . event_title , length : 30 , omission : '...' ) , event . event_url )
2013-12-12 10:10:30 +08:00
when 'reply' , 'Reply' , 'Memo'
2013-12-28 10:48:22 +08:00
str << content_tag ( " span " , " 发表了 " ) <<
content_tag ( " span " , find_all_event_type ( event ) ) <<
': ' . html_safe <<
2014-01-06 19:29:18 +08:00
link_to ( truncate ( strip_tags ( event . event_description ) . gsub ( / / , '' ) , length : 30 , omission : '...' ) , event . event_url )
2013-12-12 10:10:30 +08:00
when 'attachment'
2014-01-06 19:29:18 +08:00
str << content_tag ( 'span' , '上传了' ) <<
content_tag ( 'span' , find_all_event_type ( event ) ) <<
': ' . html_safe <<
link_to ( truncate ( event . event_title , length : 30 , omission : '...' ) , event . event_url ) <<
2014-06-03 16:58:32 +08:00
link_to ( ( ' [' . html_safe + l ( :label_downloads_list ) . to_s << ']' ) , project_files_path ( event . container . project ) , :class = > " attachments_list_color " )
2013-11-30 10:41:14 +08:00
else
2014-01-06 19:29:18 +08:00
str << content_tag ( " span " , " 更新了 " ) <<
content_tag ( " span " , find_all_event_type ( event ) ) <<
': ' . html_safe << link_to ( truncate ( event . event_title , length : 30 , omission : '...' ) , event . event_url )
2013-11-30 10:41:14 +08:00
end
str
2013-12-12 10:10:30 +08:00
rescue Exception = > e
str << content_tag ( " span " , '未知内容' )
2013-11-30 10:41:14 +08:00
end
2013-12-05 15:26:29 +08:00
def show_event_reply event
str = " 回复( "
case event . event_type
2014-01-06 19:29:18 +08:00
when 'news'
str << link_to ( event . comments . count , news_path ( event ) ) << " ) "
2013-12-05 15:26:29 +08:00
when " issue "
2014-01-06 19:29:18 +08:00
str << link_to ( cal_issues_count ( event ) , issue_path ( event ) ) << " ) "
2013-12-05 15:26:29 +08:00
when " Memo "
2014-01-06 19:29:18 +08:00
str << link_to ( cal_memos_count ( event ) , forum_memo_path ( event . forum_id , event . id ) ) << " ) "
2013-12-05 15:26:29 +08:00
else
str = " "
end
str . html_safe
end
2013-12-16 14:39:02 +08:00
2013-12-13 16:08:29 +08:00
def find_new_forum_topics limit = 7
2013-12-27 13:54:57 +08:00
# Memo.where('memos.parent_id IS NULL').reorder('memos.created_at DESC').limit(limit)
2013-12-11 16:15:10 +08:00
# activity = Redmine::Activity::Fetcher.new(nil)
# activity.scope=['memos']
# activity.events_welcome(nil, nil, {:limit => limit})
2013-12-27 13:54:57 +08:00
2014-01-02 21:20:57 +08:00
resultSet = Memo . where ( 'memos.parent_id IS NULL' ) . includes ( :last_reply ) . order ( 'COALESCE (last_replies_memos.created_at, memos.created_at) DESC' ) . limit ( limit )
2013-12-27 13:54:57 +08:00
# resultSet += Message.where('messages.parent_id IS NULL').includes(:last_reply).order('COALESCE (last_replies_messages.created_on, messages.created_on) DESC').limit(limit)
2014-01-02 21:20:57 +08:00
# resultSet = Memo.includes(:children).where('parent_id IS NULL').order('updated_at DESC').limit(limit)
# resultSet += Message.includes(:children).where('parent_id IS NULL').order('updated_on DESC').limit(limit)
# resultSet.sort! {|x,y| y.event_datetime <=> x.event_datetime}
# resultSet = resultSet.to_a
# for i in 0..(resultSet.size-1)
# resultSet[i] = resultSet[i].children.last if resultSet[i].children.count > 0
# end
# resultSet.take(limit)
2013-12-10 19:45:38 +08:00
end
2013-12-09 18:31:08 +08:00
2013-11-29 10:48:16 +08:00
private
2014-02-22 09:44:14 +08:00
def sort_project_by_hot limit = 15
sort_project_by_hot_rails 0 , 'grade DESC' , limit
2013-11-29 10:48:16 +08:00
end
2014-02-22 09:44:14 +08:00
def sort_course_by_hot limit = 15
sort_project_by_hot_rails 1 , 'course_ac_para DESC' , limit
2013-11-29 10:48:16 +08:00
end
def sort_bid_by_hot
sort_bid_by_hot_rails 1
end
def sort_contest_by_hot
sort_bid_by_hot_rails 2
end
2014-04-19 13:10:25 +08:00
#new added by linchun
def sort_contest_by_time
sort_bid_by_time 2
end
2013-11-29 10:48:16 +08:00
#取得所有活动
2014-06-23 14:19:01 +08:00
def find_all_activities limit = 6
2013-11-29 10:48:16 +08:00
# users = []
# activities = Activity.find_by_sql("select distinct user_id from activities order by id DESC limit #{limit}" )
# activities.each { |activity|
# users << activity.user_id
# }
# user_objs = User.find_by_sql("SELECT * FROM users WHERE (users.id IN #{"(" << users.join(',') << ")"} )")
activity = Redmine :: Activity :: Fetcher . new ( nil )
2014-03-13 22:06:49 +08:00
has = { # TODO: 待完成
" show_issues " = > true ,
" show_files " = > true ,
" show_documents " = > true ,
" show_messages " = > true ,
" show_news " = > true ,
2014-06-03 09:27:07 +08:00
" show_bids " = > true ,
" show_contest " = > true
2014-03-13 22:06:49 +08:00
}
2014-06-03 09:27:07 +08:00
activity . scope_select { | t | [ 'changesets' , 'documents' , 'memos' , 'messages' , 'journals_for_messages' , 'bids' , 'news' , 'contestnotification' ] . include? ( t ) ? nil : 'You may think you know what the following code does, may be. but why don"t you close this file and go play with something else, Now?' }
2013-12-04 15:31:18 +08:00
activity . events_welcome ( nil , nil , { :limit = > limit , :types = > 'welcome' } )
2013-11-29 10:48:16 +08:00
end
#取得论坛数据
2013-12-05 15:26:29 +08:00
def find_hot_forum_topics limit = 9
2013-12-02 17:27:15 +08:00
## 以下语句会内链接自身查询出最后一条回复时间,没有回复的帖子不会显示
# Memo.find_by_sql("
# SELECT memos.*, reply.created_at AS last_reply_date FROM memos AS memos
# INNER JOIN memos
# AS reply ON memos.last_reply_id=reply.id
# WHERE memos.parent_id IS NULL
# ORDER BY memos.replies_count DESC, memos.created_at DESC
# LIMIT #{limit}")
2013-12-06 11:19:15 +08:00
#Memo.order('replies_count DESC').where('replies_count <> 0').limit(limit)
resultSet = Memo . order ( 'replies_count DESC, created_at DESC' ) . where ( 'parent_id IS NULL' ) . limit ( limit )
resultSet += Message . order ( 'replies_count DESC, created_on DESC' ) . where ( 'parent_id IS NULL' ) . limit ( limit )
resultSet . sort! { | x , y | ( y . replies_count < = > x . replies_count ) . nonzero? || ( y . event_datetime < = > x . event_datetime ) }
resultSet . take ( limit )
2013-11-29 10:48:16 +08:00
end
def sort_project_by_hot_rails project_type = 0 , order_by = 'grade DESC' , limit = 15
2013-12-17 16:59:07 +08:00
# Project.find_by_sql("
2013-12-16 19:08:00 +08:00
# SELECT p.id, p.name, p.description, p.identifier, t.project_id
2013-12-17 16:59:07 +08:00
# FROM projects AS p LEFT OUTER JOIN (
# SELECT project_id,grade FROM project_statuses
# WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} ) AS t ON p.id = t.project_id ")
Project . visible . joins ( :project_status ) . where ( " #{ Project . table_name } .project_type = ? " , project_type ) . order ( order_by ) . limit ( limit ) . all
2013-11-29 10:48:16 +08:00
end
2013-11-28 09:29:59 +08:00
2013-11-29 10:48:16 +08:00
def sort_bid_by_hot_rails reward_type , limit = 10
Bid . visible . where ( 'reward_type = ?' , reward_type ) . reorder ( 'bids.commit desc' ) . limit ( limit )
end
2013-11-30 10:41:14 +08:00
2014-04-19 13:10:25 +08:00
def sort_bid_by_time reward_type , limit = 10
Bid . visible . where ( 'reward_type = ?' , reward_type ) . reorder ( 'bids.created_on desc' ) . limit ( limit )
end
2013-11-30 10:41:14 +08:00
def find_all_event_type event
case event . event_type
when 'news'
'新闻'
when 'issue'
'缺陷'
when 'attachment'
'附件'
when 'message'
'主题'
2013-12-12 08:40:18 +08:00
when 'Reply' , 'reply'
2013-11-30 10:41:14 +08:00
'回复'
when 'bid'
'作业'
2013-12-05 15:26:29 +08:00
when 'Memo'
'主题'
when 'document'
'文件'
2013-12-11 20:34:29 +08:00
when 'changeset'
'版本库'
2013-12-12 08:40:18 +08:00
when 'issue-note'
'问题说明'
2013-11-30 10:41:14 +08:00
else
event . event_type
end
end
2013-12-07 09:47:54 +08:00
2014-02-24 10:40:45 +08:00
def newbie_send_path
create_new_forum_path '新手讨论'
end
def suggestion_send_path
create_new_forum_path '网站建议'
end
private
def create_new_forum_path name
# 没有论坛则返回'#' 不能发帖
# 否则到指定论坛里发帖
# 没有找到置顶论坛就跑默认第一个论坛发帖
forum_relation = create_find_undefine_forum name
backUrl = '#'
backUrl = new_forum_memo_path ( Forum . first ) if Forum . count > 0
backUrl = new_forum_memo_path ( forum_relation . first ) if ! forum_relation . empty?
return backUrl
end
def create_find_undefine_forum name
Forum . where ( " name LIKE \' % #{ name } % \' " )
end
2013-08-01 10:33:49 +08:00
end