Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
244 views
in Q2A Core by
edited by

I need to create some custom javascript that uses the user role. 

I can easily get the user ID from mark-up.. 

        var q2aUserID = $('.qa-user-link').text();

..but is there a way to push the user role (expert, editor, moderator, etc) into a JS variable?

 

Many thanks 

Q2A version: typo

1 Answer

+2 votes
by
selected by
 
Best answer

You can do this in a custom theme. Override the head_script() method, get the user level there and output in a JS variable. The function qa_get_logged_in_level() only gives a number so you'll have to compare to the predefined constants like this

    public function head_script()
    {
        parent::head_script();

        $levels = array(
            QA_USER_LEVEL_BASIC => 'basic',
            QA_USER_LEVEL_APPROVED => 'approved',
            QA_USER_LEVEL_EXPERT => 'expert',
            QA_USER_LEVEL_EDITOR => 'editor',
            QA_USER_LEVEL_MODERATOR => 'moderator',
            QA_USER_LEVEL_ADMIN => 'admin',
            QA_USER_LEVEL_SUPER => 'super',
        );

        $userlevel = qa_get_logged_in_level();
        $levelstring = isset($levels[$userlevel]) ? $levels[$userlevel] : 'unknown';

        $this->output_raw('<script>');
        $this->output_raw('var q2aLevel = '.qa_js($levelstring));
        $this->output_raw('</script>');
    }

Hope that helps!

 

by
I dropped that code into my theme and ... KABOOM !  .. works like a champion.
now I can change the UI based on each user's role level

many thanks..   awesome skills !
...