Select mod log with php object

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
Hello,

First I am new to PHP object programmation, that's why I'm asking you such an easy thing.

I made a little XenForo add-on, that list all my staff users.

I would like to know how, in the actionIndex() function of my PublicController Addon, call a method that would count the number of action the user have done.

Regards.
 
Hi Elly,

You'd want to check out library/XenForo/Model/Log.php

Specifically there's a function in there called countModeratorLogEntries.

The code in your controller would look something like this:

PHP:
$logModel = XenForo_Model::create('XenForo_Model_Log');
foreach ($staff AS $staffMember)
{
$staffMember['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
}

Be weary of this, though. That will create one database query on the page load for each member of staff. 30 staff members? That'll be an additional 30 queries when the page loads. That isn't ideal.

With that in mind, you will want to look at trying to load that periodically (maybe via a cron entry) and then loading it from a cache.

For an example of this, you could check out my Profile View Counter add-on. This employs a similar method in the cron entry.
 
You'd want to check out library/XenForo/Model/Log.php

Specifically there's a function in there called countModeratorLogEntries.

The code in your controller would look something like this:

PHP:
$logModel = XenForo_Model::create('XenForo_Model_Log');
foreach ($staff AS $staffMember)
{
$staffMember['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
}

Thanks for the awnser Chris, I tried but have the following error :
PHP:
Cannot use a scalar value as an array

And how would I then send the datas to my template ? I'm actually returning my staff list, but how can I put each staff's actions appened to the correct user ?

Actually, I don't really care of performances ;) Just trying to understand a little more :) But I will think about the cache later.
 
Hi Elly,

You'd want to check out library/XenForo/Model/Log.php

Specifically there's a function in there called countModeratorLogEntries.

The code in your controller would look something like this:

PHP:
$logModel = XenForo_Model::create('XenForo_Model_Log');
foreach ($staff AS $staffMember)
{
$staffMember['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
}

Be weary of this, though. That will create one database query on the page load for each member of staff. 30 staff members? That'll be an additional 30 queries when the page loads. That isn't ideal.

With that in mind, you will want to look at trying to load that periodically (maybe via a cron entry) and then loading it from a cache.

For an example of this, you could check out my Profile View Counter add-on. This employs a similar method in the cron entry.

I got it working :) My only problem is to return it to my template.
 
Hi Elly,
You'd want to check out library/XenForo/Model/Log.php

I am stuck to send it to the template :( Here is my template :
Code:
<xen:foreach loop="$staffmembers" value="$user">           
    <xen:username user="$user" rich="true" /> // username is showing up correctly
    {$user.username} // again username is showing up fine
    {$user.actionCount} // nothing is showing up for actionCount !
</xen:foreach>
 
Sorry it took me a while (I read your PM while I was out).

Can you post your controller code, I can then instruct where to put the code to pass the data to the template.
 
Sorry it took me a while (I read your PM while I was out).

Can you post your controller code, I can then instruct where to put the code to pass the data to the template.

Here is my Controller file :
PHP:
class XenModeration_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
    {
 
        public function actionIndex()
          {
              $staffGroupList = $this->getModelFromCache('XenModeration_Model_SelectStaff');
              $userInfoModel = $this->getModelFromCache('XenForo_Model_User');
              $logModel = $this->getModelFromCache('XenForo_Model_Log');
 
            $staffGroups = $staffGroupList->getStaffMembersIdsList();
            $staff = $userInfoModel->getUsersByIds($staffGroups, $staffOptions = array());
           
            foreach ($staff AS $staffMember)
            {
            $staffMember['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
            }
           
            $viewParams = array(
            'staffmembers' => $staff
            );
           
            return $this->responseView('Leaderboars_ViewPublic_Home', 'leaderboards', $viewParams);
        }
 
    }
 
I have a feeling this will work... So a slightly different foreach statement in your controller.

PHP:
foreach ($staff AS $key => $staffMember)
{
	$staff[$key]['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
}
 
I have a feeling this will work... So a slightly different foreach statement in your controller.

PHP:
foreach ($staff AS $key => $staffMember)
{
$staff[$key]['actionCount'] = $logModel->countModeratorLogEntries($staffMember['user_id']);
}

It's working amazing thanks you very much, I was on this problem for hours now ;)

I don't really got the difference, was it that key ?
 
Basically $key is the array key of the current array element as the foreach cycles through.

Setting $staffMember['actionCount'] will not actually set the ['actionCount'] value on the parent array ($staff).

With that in mind, if you know the key of that element in the array, you can write stuff to it.

Not sure if that makes sense or not, but basically it is sometimes necessary so if you're a trial and error sort of person, try doing a foreach loop normally and if it doesn't work, try including the $key parameter and using it in this way.
 
Basically $key is the array key of the current array element as the foreach cycles through.

Setting $staffMember['actionCount'] will not actually set the ['actionCount'] value on the parent array ($staff).

With that in mind, if you know the key of that element in the array, you can write stuff to it.

Not sure if that makes sense or not, but basically it is sometimes necessary so if you're a trial and error sort of person, try doing a foreach loop normally and if it doesn't work, try including the $key parameter and using it in this way.

Thanks you very much for your patience Chris, I think I understand very well :)

Thanks again (y) .
 
Top Bottom