Badge query for each time posting on a new topic in a category?

I have a badge that my members can earn multiple times, it is a “Book Club” badge - they are awarded it for discussing the book of the month.

Each month, a new topic is created about the next book, and everyone replying should get the badge - this is currently done manually and is a real pain.

I was wondering if someone could help me craft a Badge SQL to fix this.

The user should receive maximum of one badge per topic, the first time they post in that topic. It’s the same badge every time, which is earned multiple times.

Is that possible? Thanks!

2 Likes

Can anyone help with this? Or is there a guide anywhere about how to craft Badge SQL?

Did you read through Topics tagged sql-triggered-badge?

Yes I’ve read all of those.

It’s a bit daunting to just try something out as a badly formed SQL query on a live site could go very wrong.

Is there a test mode or anything?

I use a staging server for experiments.

Did you get any further with this?

How are the book club topics defined? Are they in a separate category, or grouped by a tag?

No further. I’m stumped by the multiple granting factor.

All the topics are in the same category. That category is locked down so only staff can create posts there, so it’s safe to auto grant to any replies in the category.

But I want it to:

  • Grant a badge for the first reply by each person in each topic, but only once per topic
  • A user can get multiple of the same badge each time they post a reply to a different topic of the same category

I think something like this could do it:

WITH book_club_first_posts AS (

SELECT 
    p.topic_id,
    p.user_id,
    MIN(p.id) AS post_id
FROM posts p
  JOIN topics t ON t.id = p.topic_id
WHERE t.category_id = 5  -- replace with the category_id of your book club
    AND p.deleted_at ISNULL
    AND t.deleted_at ISNULL
    AND p.post_type = 1
    AND p.post_number <> 1
    AND p.user_id > 0
GROUP BY p.topic_id, p.user_id

)

SELECT bcfp.user_id, bcfp.post_id, p.created_at granted_at
FROM book_club_first_posts bcfp
  JOIN posts p ON p.id = bcfp.post_id
WHERE (:backfill OR p.id IN (:post_ids))

badge configuration

1 Like

I’ll try it soon thanks.

1 Like

Works great, thank you!!

1 Like