Someone please test my code/logic to make sure it does what I intended. I wanted moderators to receive email notification when a new message is posted to a forum in which they are a moderator via group (role) membership. That way I can create groups for moderators and assign certain groups of people to moderate particular forums.
Here is how it works. When you post a new message to a moderated forum then there is a section of code in postmessage.aspx.cs that already checks to see if the message is approved. Its line 382 for me in version 1.9.1.8.
// Create notification emails
if ( bApproved )
{
[/code]
If it's 🅱not[/b] approved then the 🅱'else' portion[/b] of the if statement is where we want our new code to be. Should be about line 400. There is already some code in the 'else' portion of the if statement that redirects the page to tell the user their post is not visible until a moderator approves it. You will need to place this new code right above that. Here is the new code:
[code]
//Send email to moderators of this PageForumID
using (DataTable dt = DB.forum_moderator_emails(PageForumID))
{
foreach (DataRow row in dt.Rows)
{
string subject = Server.HtmlEncode(Subject.Text);
string body = Utils.ReadTemplate("moderatenotification.txt");
body = body.Replace("{link}", String.Format("{0}{1}", ServerURL, Forum.GetLink(Pages.moderate_forum, "f={0}", PageForumID)));
body = body.Replace("{forumname}", BoardSettings.Name);
body = body.Replace("{topic}", String.Format("{0}", subject));
Utils.SendMail(this, BoardSettings.ForumEmail, (string)row["Email"], String.Format("{0} Post Needs Approved", BoardSettings.Name), body);
}
}
This retrieves the email addresses of the members of the groups that are moderators of the forum the message was posted to and loops through them sending emails to each. Now for this to work we must also add a new stored procedure to retrieve the emails. This is where I really need someone to make sure I did this correctly. Here it is:
USE [eus_yaf]
GO
/****** Object: StoredProcedure [dbo].[yaf_forum_moderator_emails] Script Date: 07/21/2008 16:39:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[yaf_forum_moderator_emails](
@ForumID INT)
AS
BEGIN
SELECT DISTINCT e.Email
FROM yaf_ForumAccess AS a INNER JOIN
yaf_Group AS b ON a.GroupID = b.GroupID INNER JOIN
yaf_AccessMask AS c ON a.AccessMaskID = c.AccessMaskID INNER JOIN
yaf_UserGroup AS d ON b.GroupID = d.GroupID INNER JOIN
yaf_User AS e ON e.UserID = d.UserID
WHERE (c.Flags & 64 <> 0) AND (a.ForumID = @ForumID)
END
Now there is two more mods necessary to complete the process. Notice the email message uses a template. Create a new template text file in the templates folder called moderatenotification.txt. Here is the text I used. Modify to suite your taste.
A new message has been posted in the thread '{topic}' on the forum: {forumname} and needs approved by a moderator.
To approve the message, open the following link in your browser:
<{link}>
And lastly we need to create the DataTable code in the DB.cs file. I placed it in the bottom of the yaf_Forum region. Here it is:
static public DataTable forum_moderator_emails(object ForumID)
{
using (SqlCommand cmd = new SqlCommand("yaf_forum_moderator_emails"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ForumID", ForumID);
return GetData(cmd);
}
}
That's it. Please try it and make sure it works. Thanks!
Edited by user
2008-07-22T05:46:35Z
|
Reason: Not specified