HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Award badge when user visits URL

Heya! Would it be possible (by modification possibly) to award a badge to a user once he/she visits a certain URL? I'd love to use this to award special badges which people need to click a link on a totally different website.

Comments

  • Your page is A and you want to keep track if a user clicked a link on page B which lead to page C, where B and C are pages that you have no access to? Generally spoken, that is not possible.

    You can check by JS if a users browsers have already visited page C and page B with JavaScript
    But the result you get is unreliable. Further more if you can get it via JavaScript you would have to send that information to your server to award the badge. This call, where the information about the visited page is sent, can easily be spoofed so that anyone with js knowledge could get that badge without ever having visited page B and page C.

    If you speak about a link that you provide, it would be easier. Because instead of simply using www.example.com, you would be able to provide this as yourforum/plugin/badgeredirect?target=www.example.com

  • Oh yeah that's way more advanced, here's what I had in mind;

    You'll set a URL/code in YAGA, say, www.example.com/unlock?key=E2e2ag and whenever a user goes to that page (while signed-in) it will unlock a badge for them once.

  • Does the user have to visit the link starting from your page? That would be possible.

  • Well, no, as long as they have the URL they'd just be able to go to there from any page.

  • Sorry, I think I haven't understood your plan completely. Do you want to assign a badge to anybody who have ever visited www.example. com at any time or do you want to award a badge only for those who visit www.example.com by following a link on your page?

    So to give an example: I was browsing page www.example.com for 10 minutes and afterwards I visit your forum. D you want me to get a badge? The "have been at www.example.com at any point in time"-badge?

    Or could it be that I was browsing www.example.com, then visit your forum and click on a link that leads to www.example.com and I receive the "followed our link"-badge after clicking the link on your page?

    Only the last case would be possible.

  • Hah no worries! Alright, trying to describe even easier:

    Say my Vanilla forum is www.myforum.com. In YAGA settings I set a new rule for awarding a badge which is visiting ?award=code. Now, when the user visits www.myforum.com?award=code he/she will receive the badge (if the user didn't have it already). That's it basically!

  • That in fact is quite easy. Are you interested in doing it by yourself with some help?

  • Yeah absolutely! I have some knowledge of PHP, I've already written a custom class to allow the rule to be added to YAGA. Just need a bit of help with; creating a new page in Vanilla, checking if the URL parameter is a YAGA rule, give the signed-in user a badge. Pointers to where to look are totally fine, no problem in doing a bit of research myself.

    I'd also be willing to share the final code as a plug-in, if it's decent enough.

  • To be honest: I would have been very lazy about the YAGA integration. To my opinion there is only one setting screen needed which allows admin to determine which badge should be assigned and the link to which the user should be directed.
    Then you will need an endpoint that you could insert your forum which will redirect users and assign them their badge.

    Do you already have a skeleton plugin? If not take a look at this plugin:
    https://github.com/vanilla/addons/blob/master/plugins/example/class.example.plugin.php

    You create a new Vanilla page simply by adding a public function someGardenController_nameOfThePage_create($sender, $args) {} inside your plugin.

    I will call your plugin "linkBadge" so that it is easier to talk about it. The example plugin suggests using the /plugin/linkbadge as a dispatcher so that /plugin/linkbadge would lead to the settings page and /plugin/linkbadge/redirect would do the redirection.

    I prefer using public function settingsController_linkBadge_create($sender, $args) for the settings screen.
    Vanilla has a beatiful helper for setting screens. Take a look at this discussion: https://open.vanillaforums.com/discussion/25253/simple-setting-screens-with-configurationmodule

    With this discussion you should be able to create a setting screen in no time!

    The link will be stored in the config file with the name you provide. I would suggest "linkBadge.Target" but feel free to choose another name - I just need something to refer to.

    One addition to that mysterious $sender, $args. When you create a page by adding a method controllerName_linkName_create($sender, $args), Vanilla will pass the class of the chosen controller as first argument and the url parameters as the second parameter.

    If you are admin and you are on a test environment try decho($args); in your new method and try visiting /settings/linkbadge/one/two/three.


    For redirecting you will not need much. Simply create a public function pluginController_linkBadge_create($sender, $args) {}.
    Search through \library\core\functions.general.php and you will find a function that does redirections.

    If you have saved the link in the config setting, you will be able to retrieve it with c('linkBadge.Target');

    With this information you should be able to create a plugin with a configurable link and an endpoint which will redirect users to this link.

    All that is left is the YAGA badge right now, but one step after the other... ;)

  • So after you have your plugin without YAGA up and running, lets take care for the badges. YAGA has been written very closely to the standards of Vanilla. So without taking a further look at the code, I bet there is a folder called "models" and in there will be a "class.badgemodel.php". This will be of interest.

    If you want to provide a list of badges in your settings, you need to query all badges. The badge model extends Gardens model. As such you have a method called "get()" which will give back all badges out of the box, but there might be an own implementation of "get()" in the badge model. Looking at the source is always helpful, so just see which methods the badge model offers...

    In the discussion I have linked in my previous comment, there is the dropdown form element mentioned. You should be able to use the result of the get() call to fill the dropdown and in the naming scheme I've chosen, I would save the config setting as "linkBadge.BadgeID".

    If you need more help on the dropdown, you can take a look at the form class (it is a core class, see if you can find it by yourself). Furthermore I would suggest you download a good amount of plugins and use fulltext search quite often to find examples in the code written by others.
    I still do so very often and together with plugins I have started to write but never finished, plugins that I have downloaded although I know they are broken or outdated, I have 295 (sic!) plugins in my test installation. You can find nearly anything there...


    Next thing would be how to assign a badge to a user.

  • I must admit that I will have to look at YAGA this time. But before I do, I tell you what I will do: I will take a look at the models folder in YAGA and search for anything which has to do with users.

    There is another possibility: if there is nothing there, I will take a look at the settings folder. Since assigning badges to users can be seen as something which belongs to the user, the functionality might be implemented as a number of hooks into the user model. Applications can use a "class.hooks.php" file in which they define hooks into other classes.

    Instead of looking by myself, just take a look if you find the class and the method which will assign a badge to a user by yourself. If you found it, I don't think I have to tell you anything more and you will be able to write your plugin. If not, just drop a note what needs to be clarified

    Good luck! :+1:

  • Just out of curiosity, @Kenney, how is your project going on?

Sign In or Register to comment.