Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
540 views
in Q2A Core by
Hi,
How to allow moderator can create categories ?

Thanks

1 Answer

+4 votes
by
selected by
 
Best answer

Hi @h.rakotosoa

By following these steps you can allow website-wide moderators to create/delete any category:

  • Step 1: Make 'Admin' → 'Categories' page accessible to moderators. It can be done via a plugin override
  • Step 2: Show 'Categories' sub navigation menu item for moderators as well. It can be done via a plugin override
  • Step 3: Allow moderators to perform recalculation of posts. This operations is necessary when moving posts from one category to another one. It can be done only by hardcoding

By the way, if you want to know what I meant by hardcoding and why developers tend to use this practice, please check out section Tidbits form the answer to this question:

Back to the main point here, let's have some fun by editing few core PHP files, step by step.

Step1: With a text editor open file qa-include/pages/admin/admin-categories.php and scroll down to line 46. This line of code:

if (!qa_admin_check_privileges($qa_content))

needs to be change to this:

if (!qa_moderator_check_privileges($qa_content))

However, there is no function qa_moderator_check_privileges in Question2Answer so we need to add this function as well. It is a modified version of function qa_admin_check_privileges that check for moderator privileges, rather than admin privileges. Lets add this function right below line 30:

function qa_moderator_check_privileges(&$qa_content)
{
    if (!qa_is_logged_in()) {
        require_once QA_INCLUDE_DIR . 'app/format.php';

        $qa_content = qa_content_prepare();

        $qa_content['title'] = qa_lang_html('admin/admin_title');
        $qa_content['error'] = qa_insert_login_links(qa_lang_html('admin/not_logged_in'), qa_request());

        return false;

    } elseif (qa_get_logged_in_level() < QA_USER_LEVEL_MODERATOR) {
        $qa_content = qa_content_prepare();

        $qa_content['title'] = qa_lang_html('admin/admin_title');
        $qa_content['error'] = qa_lang_html('users/no_permission');

        return false;
    }

    return true;
}

In light green is what differs from the original function qa_admin_check_privileges.

We are done with Step 1. Please, do not forget to click the save button before closing the text editor.

Step 2: Open qa-include/app/admin.php and go to line 410; it will look like this:

return $navigation;

and, just above line 410, add the code needed to show the 'Categories' sub navigation item:

if (!isset($navigation['admin/categories']) && qa_using_categories() && $level >= QA_USER_LEVEL_MODERATOR) {
    $new_nav = array(
        'admin/categories' => array(
            'label' => qa_lang_html('admin/categories_title'),
            'url' => qa_path_html('admin/categories')
        )
    );

    $navigation = array_merge($new_nav, $navigation);
}

Save the file and now let's go over the final step.

Step 3: Edit qa-include/ajax/recalc.php on line 26 to replace this line:

if (qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN) {

with this one:

if ($allowed) {

And add the following code above line 26:

if ($state && strpos($state, 'dorecalccategories') === 0) {
    $allowed = qa_get_logged_in_level() >= QA_USER_LEVEL_MODERATOR;
} else {
    $allowed = qa_get_logged_in_level() >= QA_USER_LEVEL_ADMIN;
}

Save it. Let's see how it works:

Animation about how to make moderators create and delete categories.

It works for me and I hope it work for you as well. Slightly Smiling Face


Tidbits

One thing to note is that Question2Answer allows administrator users to grant two types of privileges:

  • Website-wide privileges: Who makes sure that the website rules are not broken; for example by removing any threatening or offensive messages. For instance, see animation below to know how to make a given user moderator:

Animation about how to make a given user website-wide moderator

  • Category-specific privileges: Like website-wide privileges but they can check content only in a given category. Here's how to make a given user moderator for category 'Level-1-1 category':

Animation about how to make a given user category-specific moderator

Another thing to note is the way I wrote this conditional statement here:

if ($state && strpos($state, 'dorecalccategories') === 0) {

instead of just this:

if (strpos($state, 'dorecalccategories') === 0) {

Well, PHP >= 8.1 will throw deprecation messages if $state is null with the latter but not with the former.

Leonardus N. explains why it happens in his article:

To maintain consistency, the internal functions don’t accept null values anymore. Passing null to non-nullable arguments will return a deprecation notice.
...
This deprecation is part of a plan to remove the functionality. According to its RFC, passing null values to non-nullable internal functions will result in TypeError. That will make the internal functions consistent with the current behavior of user-defined functions.

Unfortunately, Question2Answer <= 1.8.6 is plagued with code using the latter patter so if you want to save yourself a few headaches, then just avoid using Q2A <= 1.8.6 with PHP >= 8.1 for now.


Now, this answer assumes you are a developer and you have time for going through the process of making it work. Here are a few suggestions just in case you find it difficult to follow:

  • Add a comment below Backhand index pointing down emoji, I (and other community members) can try to help you out
  • Find a developer friend and ask him to implement it for you
  • Get paid consultation from one of the service providers or from me (send me a private message); specially if quick solutions (only half a day) are needed so you do not have to wait 16 days for someone to give you an answer.

I hope this answer is helpful. Stay safe and healthy!

Bye! Waving Hand Sign Emoji

by
Thanks, I was looking for a similar answer.
...