alpacapilot
  • Posts: 2
  • Joined: 22/07/2008
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 )
{

If it's not approved then the 'else' portion 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:

                //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!
Sponsor
alpacapilot
  • Posts: 2
  • Joined: 22/07/2008
This appears to be working as expected for users who are granted moderator access via a group. However I forgot about those individuals assigned as moderators directly. Simple fix though. Just alter the stored procedure yaf_forum_moderator_emails to the following and it will then include their email addresses in the record set.

USE [eus_yaf]
GO
/****** Object:  StoredProcedure [dbo].[yaf_forum_moderator_emails]    Script Date: 07/23/2008 08:44:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[yaf_forum_moderator_emails](
			@ForumID	INT)
AS
    BEGIN
		SELECT     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)
		UNION
		SELECT     a.Email
		FROM         yaf_User AS a INNER JOIN
			yaf_UserForum AS b ON b.UserID = a.UserID INNER JOIN
			yaf_AccessMask AS c ON c.AccessMaskID = b.AccessMaskID
		WHERE     (b.ForumID = @ForumID) AND (c.Flags & 64 <> 0)
    END
eyeseetee2
  • Posts: 8
  • Joined: 27/08/2008
This doesnt seem to work for me, I cannot recieve an e-mail
Forum Jump  
  • You cannot post new topics in this forum.
  • You cannot reply to topics in this forum.
  • You cannot delete your posts in this forum.
  • You cannot edit your posts in this forum.
  • You cannot create polls in this forum.
  • You cannot vote in polls in this forum.

About Us

The YAF.NET is an open source .NET forum project. YAF.NET is supported by an team of international developers who are build community by building community software.

Powered by Resharper Donate with PayPal button

Project Twitter Updates

Copyright © YetAnotherForum.NET & Ingo Herbote. All rights reserved