# Discourse-Math badge

**URL:** https://meta.discourse.org/t/discourse-math-badge/160728
**Category:** Support
**Created:** [13 augustus 2020 om 12:32 UTC](https://meta.discourse.org/t/discourse-math-badge/160728 "2020-08-13T12:32:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [13 augustus 2020 om 12:32 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/1 "2020-08-13T12:32:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [13 augustus 2020 om 12:58 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/2 "2020-08-13T12:58:28Z")

</div>

> [@agf1997](#):
>
> No idea where to start on this

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.

---

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [13 augustus 2020 om 17:51 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/3 "2020-08-13T17:51:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [13 augustus 2020 om 19:34 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/4 "2020-08-13T19:34:07Z")

</div>

> [@agf1997](#):
>
> I was just looking for a simple way to highlight people using the math typesetting feature to encourage others to use it also.

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.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [13 augustus 2020 om 20:11 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/5 "2020-08-13T20:11:51Z")

</div>

> [@agf1997](#):
>
> 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?

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](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894) on your site.

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

```sql
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

```

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [14 augustus 2020 om 06:18 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/6 "2020-08-14T06:18:35Z")

</div>

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:

> [@\[Superseded\] What cool badge queries have you come up with?](https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978):
>
> warning Enabling Badge SQL entails security and performance risks so it is not available by default. For more information, see: [Enable Badge SQL](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894) This topic became too long to be easily searched and has been broken down into separate topics. If you have any questions about how to write a badge query, or have one to share, please start a new topic in the #data & reporting category and tag it with the #sql-triggered-badge tag. +1 More ideas: [Some common badge queries](https://meta.discourse.org/t/some-common-badge-queries-idea/31859)

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:

> [@Creating triggered custom badge queries](https://meta.discourse.org/t/triggered-custom-badge-queries/19336):
>
> bookmark This guide explains how to create triggered custom badge queries in Discourse, including the types of badges, constraints for triggered badges, and an example query. person_raising_hand Required user level: Administrator warning This feature is disabled by default. To enable it, follow [this guide](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894). When defining badges in Discourse, you’ll encounter a “Trigger” option with the following choices: Update daily When a user acts on post When a user edits or creates a post When…

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!

---

<div class="post-metadata">

### Author: ![agf1997](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@agf1997](https://meta.discourse.org/u/agf1997)
#### Post date: [21 augustus 2020 om 17:58 UTC](https://meta.discourse.org/t/discourse-math-badge/160728/7 "2020-08-21T17:58:59Z")

</div>

@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

```plaintext
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))

```
