Discourse-Math badge

Can anyone think of a way i could automatically grant a custom badge for making a post that renders an equation using the discourse-math plugin?

No idea where to start on this

5 Likes

Hi @agf1997

Agree the Discourse math plugin is well done and a great addition for a site.

One approach would be to pay a Discourse plugin developer to modify the math plugin for you to update a custom field in the DB when a forum member uses the plugin.

Then, you could create an easy DB query (in the badge admin area) to issue badges.

Hope this helps.

3 Likes

It’s certainly not a mission critical feature. I was just looking for a simple way to highlight people using the math typesetting feature to encourage others to use it also.

1 Like

Hi @agf1997

You will need plugin code for that, as I mentioned.

This would indicate you have two choices:

  1. Modify or extend the math plugin yourself
  2. Hire someone to modify or extend the math plugin for you

What ever direction you decide to go in you need to modify or extend the math plugin to update a custom field in the DB when a forum member uses the plugin.

Hope this helps.

2 Likes

Yes, you can have badges for successful Math posts without having to modify the plugin.

First you will need to enable Badge SQL can no longer be edited by default on your site.

After that create a new badge that triggers on the following query:

SELECT user_id, MIN(id) AS post_id, current_timestamp AS granted_at 
FROM badge_posts
WHERE cooked LIKE '%<span class="math">%' AND (:backfill OR id IN (:post_ids) )
GROUP BY user_id
6 Likes

Hey @Falco

That is a really nice badge query.

Question:

Do you have a list of all the Discourse :symbols that can be used in a badge query to help trigger?

The reason I ask is that I see you used :backfill and :post_id and when I look at this link, I see other Ruby :symbols used:

Is there a concise reference for the Discourse Ruby symbols that can be used in a badge query?

I did find this, related to badge triggers (after your post above), which was very helpful:

In that Discourse article, mentioned are these trigger helpers:

  • :user_ids
  • :post_ids
  • :backfill

Are these three “all of them”?

Thanks so much for your help!

3 Likes

@Falco Awesome! Quick question … should this work on previous posts? When I click “Preview granted badges” I get “No badges to be assigned.”

Clicking “Preview with query plan” gives me

GroupAggregate  (cost=2021.68..2021.70 rows=1 width=16)
  Group Key: p.user_id
  ->  Sort  (cost=2021.68..2021.68 rows=1 width=8)
        Sort Key: p.user_id
        ->  Nested Loop  (cost=0.28..2021.67 rows=1 width=8)
              Join Filter: (t.category_id = c.id)
              ->  Nested Loop  (cost=0.28..2017.97 rows=1 width=12)
                    ->  Seq Scan on posts p  (cost=0.00..2009.66 rows=1 width=12)
                          Filter: ((deleted_at IS NULL) AND (cooked ~~ '%<span class="math">%'::text) AND (post_type = ANY ('{1,2,3}'::integer[])))
                    ->  Index Scan using topics_pkey on topics t  (cost=0.28..8.30 rows=1 width=8)
                          Index Cond: (id = p.topic_id)
                          Filter: ((deleted_at IS NULL) AND visible)
              ->  Seq Scan on categories c  (cost=0.00..3.35 rows=28 width=4)
                    Filter: (allow_badges AND (NOT read_restricted))