Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.1k views
in Q2A Core by
Hi,

I want to add a Page for Reporting the number of questions asked, and answers given, in a specific period of time (say past 1 hour, or past 1 month).

I've decided to make a plugin for the same. What I need though is, when I add my plugin page as a Tab, the Tab is shown only to a Moderator or Administrator (Just like the "Admin" Tab).

There's no option however for the same in the "Pages" Section of Admin Interface. Can anyone tell me how the same can be achieved? I guess I need to change some php page for the same, but which one and what?
by
I had a similar problem. And due to this and other things I had to do an external php. I think It would be nice a new function, for the next release, for the plugins to add a tab in admin/moderator menu.

1 Answer

+1 vote
by

There is currently no easy way to do this.

But it is possible using a layer in your plugin, which checks the permission level of the currently logged in user, and removes the page from the navigation menus if it's not high enough. Override the nav_item() function, check the $key parameter, and only call through to the base theme class if it doesn't match your page's slug.

by
Thanks gigreen for replying. I do have an idea though. I just saw that q&a uses some minimal jQuery. I can add some "custom" code to my page, having a small jquery statement, to "hide" the Tab. I am still exploring q2a, and will have to see if its feasable or not, but this seems a "dirty shortcut"... your view?
by
You can hide the tab even more simply with some custom CSS that is output in the page <HEAD>, since the tab will have its own specific class (look in HTML source). This can be done by a layer.
by
true....
but I would need to write some php code to check the User level, and then decide whether to hide it or not.
Anyway, thanks for the help. I am somewhat new to PHP, so may be I am not really in a position to write a layer. Still, I am going through the Help Page of Layers, and trying to figure out how I can do the same, as you explained in your first reply.
by
Hi gidgreen,
Thanks for your suggestion. I made an advanced Theme and my work is done. I would like to post the code snippet here, so that anyone who wants to do the same can be benefited.

My Plugin Page had a small code to prevent the Page being viewed directly by unathorized people:

function process_request($request) {
    if($level == null || $level < QA_USER_LEVEL_MODERATOR)
        qa_fatal_error('Only Moderators/Admins can access this page');
    ....
    ....
}

in the advanced theme, qa-theme.php:
class qa_html_theme extends qa_html_theme_base {
    function nav_item($key, $navlink, $class, $level=null) {
        $user_level = qa_get_logged_in_level();
        if($user_level == null || $user_level < QA_USER_LEVEL_MODERATOR) {
            if(isset($navlink['url']) && $navlink['url'] == './qa-rg-reporting-page')
                return;
        }
        qa_html_theme_base::nav_item($key, $navlink, $class, $level);
    }
}

}

Here "./qa-rg-reporting-page" is the location of my page. I tried using the Key parameter, but it returns a Value like "custom-5" or something, which may not be a good idea to check, so I used the url part.
...