Developer forum

Forum » CMS - Standard features » Dynamicweb Comments module

Dynamicweb Comments module

Martin Nielsen
Reply

Hey,

 

I'm currently setting up comments on a website, and it's working quite well.

But the customer has requested that they need to approve all comments from users before they are shown on the site, is this possible in standard DW?

 

Or is there a notification that we can use to archieve this?

 

Regards

    Martin


Replies

 
Mikkel Ricky
Reply

Currently, there is no built-in way to let an administrator approve comments before they are published on the site. 

 

However, with clever use of notifications you can actually make something that may be usable: First, subscribe to "Dynamicweb.Notifications.Commenting.OnAfterComment" and mark new comments in some way. In the code samples below, we add the string "[not approved]" before the Name on new comments.

 

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Commenting.OnAfterComment)]
public class CommentingOnAfterCommentObserver1 : Dynamicweb.Extensibility.NotificationSubscriber {
	public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args) {
		Dynamicweb.Notifications.Commenting.CommentArgs commentArgs = args as Dynamicweb.Notifications.Commenting.CommentArgs;
		
		// Mark comment as not approved
		var comment = commentArgs.Comment;
		comment.Name = string.Format("[not approved]", comment.Name);
		comment.Save();
	}
}

Then, subscribe to "Dynamicweb.Notifications.Commenting.OnBeforeRenderComments" and remove comments that are not yet approved, i.e. the ones with a Name starting with the string "[not approved]".

[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Commenting.OnBeforeRenderComments)]
public class CommentingOnBeforeRenderCommentsObserver1 : Dynamicweb.Extensibility.NotificationSubscriber {
	public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args) {
		Dynamicweb.Notifications.Commenting.OnBeforeRenderCommentsArgs onBeforeRenderCommentsArgs = args as Dynamicweb.Notifications.Commenting.OnBeforeRenderCommentsArgs;
		
		var nonApprovedComments = new Dynamicweb.Content.Commenting.CommentCollection();
		
		// Get non-approved comments
		foreach (var comment in onBeforeRenderCommentsArgs.Comments) {
			if (comment.Name.StartsWith("[not approved]")) {
				nonApprovedComments.Add(comment);
			}
		}
		
		// Remove all non-approved comments
		foreach (var comment in nonApprovedComments) {
			onBeforeRenderCommentsArgs.Comments.Remove(comment);
		}
	}
}

Now an administrator can edit comments and remove "[not approved]" from the Name to approved comments. This is a bit of a hack, but that doesn't mean that it's not useful. 

 

Note: The template tag <!--@Comments.Count--> doesn't reflect the number of comments after filtering, but, if need be, this can be worked around by counting comments using JavaScript. 

 

Best regards,

Mikkel

 
Martin Nielsen
Reply

Hi Mikkel,

 

Yes, i went with that approach aswell after consulting Pavel.

The Extensibility API is quite nifty for hacking stuff like this :-)

 

Thank for your approach.

 

// Martin

 

 

 

 

 

 

You must be logged in to post in the forum