Kunena forum. Setting special rank to a new user using custom plugin

132 Views Asked by At

everybody! Could you please help me with the following problem.

Kunena 4.0.1. I have created several special user ranks. Some users have a permission to create new users. The task is to set rank of the new user differ to the default value but one of the special ranks. I've tried to do it using a custom plugin related to the "onUserAfterSave" event. But it didn't work. What I’m doing wrong?

 function onUserAfterSave ($user, $isnew) 
  {

  if ($isnew)
    {
    $NewKUser = KunenaFactory::getUser(intval($user['id']));
    $NewKUser->rank['rank_id'] = 11;                
    $NewKUser->save(true);
    }
  }
2

There are 2 best solutions below

0
On

Also I got another solution, a one liner code but you need to edit existing plugin. So when a user registers in joomla, then it will be automatically assigned a rank.

For that you need to edit the kunena system plugin, which you can find at plugins->system->kunena. if you open kunena.php you will find a function

public function onUserAfterSave($user, $isnew, $success, $msg){
....
....
if ($isnew && intval($user ['id']))
        {
            $kuser = KunenaFactory::getUser(intval($user ['id']));
            $kuser->save();
        }

Change it or modify it by adding a single line $kuser->rank

public function onUserAfterSave($user, $isnew, $success, $msg){
    ....
    ....
if ($isnew && intval($user ['id']))
        {
            $kuser = KunenaFactory::getUser(intval($user ['id']));
            $kuser->rank=11; // The rank which you want
            $kuser->save();
        }

Also its better to create a copy of that plugin before editing.

0
On

Problem solved:

function onUserAfterSave ($user, $isnew) {
   if ($isnew) {
    /*Current user rank*/
    $SelfUser = KunenaUserHelper::getMyself();
    $SelfUserRank = $SelfUser->getRank(0, false);
     /*If current user has custom rank...*/
    if ($SelfUserRank->rank_id >=11) {
        /*Setting new user's rank equal to the current user's rank*/
        $NewKUser = KunenaUser::getInstance(intval($user['id']), false);
    $NewKUser->rank = $SelfUserRank->rank_id;
    $NewKUser->save();
    }
   }