@ -27,21 +27,104 @@ class Mailer < ActionMailer::Base
{ :host = > Setting . host_name , :protocol = > Setting . protocol }
end
# 贴吧新建贴吧发送邮件
# example Mailer.forum(forum).deliver
def forum_add ( forum )
redmine_headers 'Forum' = > forum . id
@forum = forum
@author = forum . creator
recipients = forum . creator . mail
# cc = wiki_content.page.wiki.watcher_recipients - recipients
@issue_author_url = url_for ( user_activities_url ( @author ) )
@forum_url = url_for ( :controller = > 'forums' , :action = > 'show' , :id = > forum . id )
mail :to = > recipients , :subject = > " [ #{ l ( :label_forum ) } : #{ forum . name } #{ l ( :notice_successful_create ) } ] "
# author: alan
# 发送邀请未注册用户加入项目邮件
# 功能: 在加入项目的同时自动注册用户
def send_invite_in_project ( email , project , invitor )
@subject = " #{ invitor . name } #{ l ( :label_invite_project ) } #{ project . name } "
password = newpass ( 6 )
@project_url = url_for ( :controller = > 'projects' , :action = > 'show' , :id = > project . id ,
:password = > password , :login = > email )
mail :to = > email , :subject = > @subject
end
# author: alan
# 根据用户选择发送个人日报或周报
# 发送内容: 项目【缺陷,讨论区,新闻】,课程【通知,留言,新闻】, 贴吧, 个人留言
def send_for_user_activities ( user , date_to , days )
date_from = date_to - days . days
# 生成token用于直接点击登录
@user = user
token = Token . new ( :user = > user , :action = > 'autologin' )
token . save
@token = token
@user_url = url_for ( my_account_url ( user , :token = > @token . value ) )
# 查询user参加的项目及课程
projects = user . projects
courses = user . courses
project_ids = projects . map { | project | project . id } . join ( " , " )
course_ids = courses . map { | course | course . id } . join ( " , " )
# 查询user的缺陷, 包括发布的, 跟踪的以及被指派的缺陷
@issues = Issue . find_by_sql ( " select DISTINCT i.* from issues i, watchers w
where ( i . assigned_to_id = #{user.id} or i.author_id = #{user.id}
or ( w . watchable_type = 'Issue' and w . watchable_id = i . id and w . user_id = #{user.id}))
and ( i . created_on between '#{date_from}' and '#{date_to}' ) order by i . created_on desc " )
# @bids 查询课程作业, 包括老师发布的作业, 以及user提交作业
# @attachments查询课程课件更新
@attachments || = [ ]
@bids || = [ ] # 老师发布的作业
unless courses . first . nil?
count = courses . count
count = count - 1
for i in 0 .. count do
bids = courses [ i ] . homeworks . where ( " bids.created_on between ' #{ date_from } ' and ' #{ date_to } ' " ) . order ( " bids.created_on desc " )
attachments = courses [ i ] . attachments . where ( " attachments.created_on between ' #{ date_from } ' and ' #{ date_to } ' " ) . order ( 'attachments.created_on DESC' )
@bids += bids if bids . count > 0
@attachments += attachments if attachments . count > 0
end
end
# user 提交的作业
@homeworks = HomeworkAttach . where ( " user_id= #{ user . id } and (created_at between ' #{ date_from } ' and ' #{ date_to } ') " ) . order ( " created_at desc " )
# 查询user在课程。项目中发布的讨论帖子
messages = Message . find_by_sql ( " select DISTINCT * from messages where author_id = #{ user . id } and (created_on between ' #{ date_from } ' and ' #{ date_to } ') order by created_on desc " )
@course_messages || = [ ]
@project_messages || = [ ]
unless messages . first . nil?
messages . each do | msg |
if msg . project
@project_messages << msg
elsif msg . course
@course_messages << msg
end
end
end
# 查询user在课程中发布的通知, 项目中发的新闻
@course_news = News . find_by_sql ( " select DISTINCT n.* from news n
where n . course_id in ( #{course_ids})
and ( created_on between '#{date_from}' and '#{date_to}' ) order by created_on desc " )
@project_news = News . find_by_sql ( " select DISTINCT n.* from news n where n.project_id in ( #{ project_ids } )
and ( created_on between '#{date_from}' and '#{date_to}' ) order by created_on desc " )
# 查询user在课程及个人中留言
@course_journal_messages = JournalsForMessage . find_by_sql ( " select DISTINCT * from journals_for_messages where
jour_type = 'Course' and user_id = #{user.id}
and ( created_on between '#{date_from}' and '#{date_to}' ) order by created_on desc " )
@user_journal_messages = user . journals_for_messages . where ( " m_parent_id IS NULL and (created_on between ' #{ date_from } ' and ' #{ date_to } ') " ) . order ( 'created_on DESC' )
# 查询user新建贴吧或发布帖子
@forums = Forum . find_by_sql ( " select DISTINCT * from forums where creator_id = #{ user . id } and (created_at between ' #{ date_from } ' and ' #{ date_to } ') order by created_at desc " )
@memos = Memo . find_by_sql ( " select DISTINCT m.* from memos m, forums f where (m.author_id = #{ user . id } or (m.forum_id = f.id and f.creator_id = #{ user . id } ))
and ( m . created_at between '#{date_from}' and '#{date_to}' ) order by m . created_at desc " )
if days == 1
subject = " [ #{ user . show_name } : #{ l ( :label_day_mail ) } ] "
@subject = " #{ user . show_name } : #{ date_to - 1 . days } #{ l ( :label_day_mail ) } "
else
subject = " [ #{ user . show_name } : #{ l ( :label_week_mail ) } ] "
@subject = " #{ user . show_name } : #{ l ( :label_week_mail ) } "
end
mail :to = > user . mail , :subject = > subject
end
def forum_message_added ( memo )
@memo = memo
redmine_headers 'Memo' = > memo . id
@ -50,15 +133,12 @@ class Mailer < ActionMailer::Base
@forum_url = url_for ( :controller = > 'forums' , :action = > 'show' , :id = > @forum . id )
@issue_author_url = url_for ( user_activities_url ( @author ) )
recipients || = [ ]
# if !memo.parent_id.nil?
# mems = memo.self_and_siblings
# mems.each do |mem|
# recipients << mem.author.mail unless recipients.include? mem.author.mail
# end
# else
# recipients << memo.author.mail
# end
recipients << @author . mail
if @forum . author . mail_notification != 'day' && @forum . author . mail_notification != 'week'
recipients << @forum . creator . mail
end
if @author . mail_notification != 'day' && @author . mail_notification != 'week'
recipients << @author . mail
end
# cc = wiki_content.page.wiki.watcher_recipients - recipients
@memo_url = url_for ( forum_memo_url ( @forum , ( @memo . parent_id . nil? ? @memo : @memo . parent_id ) ) )
@ -93,6 +173,17 @@ class Mailer < ActionMailer::Base
Rails . logger . error " [Builds a Mail::Message ERROR] journalsForMessage's jour is unkown type, journalsForMessage.id = #{ journals_for_message . id } "
return - 1
end
# 验证用户的收取邮件的方式
recipients || = [ ]
recipients1 || = [ ]
if @mail . mail_notification != 'week' && @mail . mail_notification != 'day'
recipients1 = @mail . mail
end
if journals_for_message . jour . author . mail_notification != 'week' && journals_for_message . jour . author . mail_notification != 'day'
recipients = journals_for_message . jour . author . mail
end
# modify by nwb
#如果是直接留言并且留言对象是课程
if ! journals_for_message . at_user && journals_for_message . jour . class . to_s . to_sym == :Course
@ -103,7 +194,9 @@ class Mailer < ActionMailer::Base
#收件人邮箱
@recipients || = [ ]
@members . each do | teacher |
if teacher . user . mail_notification != 'week' && teacher . user . mail_notification != 'day'
@recipients << teacher . user . mail
end
end
mail :to = > @recipients ,
:subject = > " #{ l ( :label_your_course ) } #{ journals_for_message . jour . name } #{ l ( :label_have_message ) } "
@ -111,14 +204,15 @@ class Mailer < ActionMailer::Base
if ! journals_for_message . jour . author . notify_about? journals_for_message
return - 1
end
mail :to = > journals_for_message . jour . author . mail , :subject = > @title
mail :to = > recipients , :subject = > @title
elsif journals_for_message . jour . class . to_s . to_sym == :Contest
if ! journals_for_message . jour . author . notify_about? journals_for_message
return - 1
end
mail :to = > journals_for_message. jour . author . mail , :subject = > @title
mail :to = > recipients , :subject = > @title
else
mail :to = > @mail . mail , :subject = > @title
mail :to = > recipients1 , :subject = > @title
end
@ -140,7 +234,6 @@ class Mailer < ActionMailer::Base
@author = issue . author
@issue = issue
user = User . find_by_mail ( recipients )
token = Token . new ( :user = > user , :action = > 'autologin' )
token . save
@token = token
@ -196,12 +289,6 @@ class Mailer < ActionMailer::Base
@user_url = url_for ( my_account_url ( user , :token = > @token . value ) )
@issue_url = url_for ( :controller = > 'issues' , :action = > 'show' , :id = > issue . id , :anchor = > " change- #{ journal . id } " , :token = > @token . value )
s = " [ #{ issue . project . name } - #{ issue . tracker . name } # #{ issue_id } ] "
s << " ( #{ issue . status . name } ) " if journal . new_value_for ( 'status_id' )
s << issue . subject
@ -752,4 +839,13 @@ class Mailer < ActionMailer::Base
end
end
end
# author: alan
# 功能: 生成len位随机字符串
def newpass ( len )
chars = ( " a " .. " z " ) . to_a + ( " A " .. " Z " ) . to_a + ( " 0 " .. " 9 " ) . to_a
newpass = " "
1 . upto ( len ) { | i | newpass << chars [ rand ( chars . size - 1 ) ] }
return newpass
end
end