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

Adding Karma Points to DiscussionFilterModule

I'm helping out @okhawaja with some things and one is extending the functionality of the Karma Bank. I just wanted to share this if anybody is interested. Adding this to a custom theme (or a plugin) will add a link to the Karma Bank profile page to the discussion filter module (below "My Drafts").

Personally I do not think that this is a good place for this because Karma Points simply are no discussions, but I think making the balance permanently visible is a good thing.

So here is the code:

    /**
     * Adds a link to Karma Bank to DiscussionFilterModule.
     *
     * @param gardenController $sender Instance of the calling class.
     * @return void.
     */
    public function base_afterDiscussionFilters_handler($sender) {
        // Only show this if KarmaBank is enabled.
        if (c('Plugins.KarmaBank.Enabled', false) != true) {
            return;
        }

        // Stop on profile controller to prevent showing the link twice.
        if ($sender->ControllerName === 'profilecontroller') {
            return;
        }

        // Echo html and return if markup is already cached.
        $html = Gdn::cache()->get('KarmaBankDiscussionFilterLink');
        if ($html !== Gdn_Cache::CACHEOP_FAILURE) {
            echo $html;
            return;
        }

        // Prepare link text.
        $karmaBankLink = t('Karma Bank');
        if (c('Vanilla.Discussions.ShowCounts', true)) {
            $karmaBankModel = new KarmaBankModel(Gdn::session()->UserID);
            $balance = $karmaBankModel->getBalance();
            // Convert ugly floating point number format.
            $balance = sprintf(t('KarmaBank.NumberFormat', '%d'), $balance->Balance);
            $karmaBankLink .= filterCountString($balance);
        }

        $html = wrap(
            anchor(
                sprite('SpKarmaBank').' '.$karmaBankLink,
                '/profile/karmabank'
            ),
            'li',
            ['class' => 'KarmaBank']
        );

        // Store markup in cache for not hammering the db for the karma balance.
        Gdn::cache()->store(
            'KarmaBankDiscussionFilterLink',
            $html,
            [Gdn_Cache::FEATURE_EXPIRY => 180] // 3 minutes
        );

        echo $html;
    }

By the way: the Karma Bank plugin is written very well and can be extended easily!
e.g.: one thing I don't like is that Karma is presented as floating point values. But x00 made it easy to customize that. If anybody prefers another number format, it can be solved by using a custom translation.

Create following file: /conf/locale.php and add following code in there

<?php
$Definition['KarmaBank.NumberFormat'] = '%d';

This is the format that the php function printf uses to fomat Karma points when they are rendered to screen and in my case I choose %d to see integer values.

Comments

  • how do you create a theme hook file? I am using Bittersweet theme and inside the theme/bittersweet I created a file called "class.bittersweetthemehooks" and then I entered this code:

    <?php

    class bittersweetThemeHooks implements Gdn_IPlugin {

    //entered your code in discussion here

    }

    Is this how you create a theme hook if your theme doesn't have one? my MinusBaseline theme has a themehook file, but Bittersweet doesn't so I am not sure for Bittersweet how to create a themehook file.

  • RiverRiver MVP
    edited July 2016

    @okhawaja said:
    how do you create a theme hook file? I am using Bittersweet theme and inside the theme/bittersweet I created a file called "class.bittersweetthemehooks" and then I entered this code:

    <?php

    class bittersweetThemeHooks implements Gdn_IPlugin {

    //entered your code in discussion here

    }

    Is this how you create a theme hook if your theme doesn't have one? my MinusBaseline theme has a themehook file, but Bittersweet doesn't so I am not sure for Bittersweet how to create a themehook file.

    why are you changing core files?

    Much better to create a new theme with your own name. and create a "themehooks" file with a class name and file name that matches your newly created theme in your newly created theme folder.

    Documentation - http://docs.vanillaforums.com/theming/hooks/

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • R_JR_J Admin

    I bet he is doing that because he isn't aware that even adding a file could be seen as changing.

    In this case you, okhawaja, would loose your work if the next update of Vanilla would come with its own themehook file for bittersweet.
    Also it would be hard to help if e.g. you tell people you deactivated all plugins and only use the bittersweet theme: a themehook file is nothing else than a plugin.

    So the right way to add a themehook file is first to create your own theme. This could simply be done by copying the bittersweet folder to "myBittersweet". Afterwards you have to reflect this new name in /themes/myBittersweet/about.php
    Look at these to lines in this file:

    $ThemeInfo['bittersweet'] = array(
        'Name' => 'Bitter Sweet',
    

    replace "bittersweet" with "myBittersweet" and "Bitter Sweet" with "My Bitter Sweet". That's all you have to do to create your own theme where you can change, add, delete anything you like!


    The way you've added the themehooks file was absolutely correct. Do it the same way (with the new name everywhere) in your new themes folder. There s only one part missing in your themehooks file: every themehook file must have a method setup(), but this doesn't have to do anything. So simply add this to your themehooks file:

        public function setup() {
            return true;
        }
    

    Now it should work!

Sign In or Register to comment.