User Resource Report Part 1

Reporting the resource-related activity for a particular user.

By Bob Ray  |  October 22, 2024  |  5 min read
User Resource Report Part 1

In the last few articles, we saw how to display user information for the user-related fields of a single resource. In this series of articles, we'll go in a slightly different direction and look at how to display information about a collection of resources acted on by the current user. As we did in the previous article, we'll use a Tpl chunk to format the output.

Getting the Resources

This couldn't be much simpler. We can get the current user's ID with this code:

$userId = $modx->user->get('id');

As we saw in the previous articles, the user-related fields of a resource (createdby, editedby, publishedby) hold the ID of the user who performed each action. It's a short step from there to getting a collection of the user's resources. For example, if we want all the resources created by the user, we can do this:

$userId = $modx->user->get('id');
$docs = $modx->getCollection('modResource', array('createdby' => $userId));

We'll need a Tpl chunk to display the output and some more code to process each resource, but first, let's look at an easy way to create the associative array we need for the placeholders in the Tpl chunk.

toArray()

Every MODX object is descended from the basic xPDO object. That object has a method called toArray(), which means that every MODX object (including resources, users, chunks, snippets, plugins, templates, template variables, and lots more) inherits that method. Once you have the object, you can call its toArray() method.

The toArray() method creates exactly the kind of associative array that $modx->getChunk() is expecting. (If you need to review how $modx->getChunk() works, take a look at my previous article.) Basically, toArray() returns an array where the keys are the fields of the object and the values are the contents of those fields. Here's an abbreviated version of the array toArray() returns for a resource:


array( 'pagetitle' => 'MyPage', 'longtitle' => 'This is My Page', 'description' => 'This is my first page', 'alias' => 'mypage', 'createdby' => 22, 'content' => 'This is the content of my first page', );

The Snippet Tag

We'll call our snippet UserResourceReport, so the tag will look like this:

[[!UserResourceReport]]

The Tpl Chunk

The Tpl chunk will contain placeholder tags for the resource fields we want to show. The snippet will actually set placeholders for all the fields returned by toArray(), so you can add or remove fields to show just by editing the Tpl chunk. All the resource fields will be in the array, but if you don't set a placeholder for a given field, MODX will ignore it. We'll call our Tpl chunk ResourceReportTpl:

<h3>[[+pagetitle]]</h3>
<p class="page_field">Description: [[+description]]
<p class="page_field">Summary: [[+introtext]]

You can have as many resource fields as you like and format them however you want See this page for a full list of the resource fields.

The Code

Here's the code to show the list of resources created by the current user:

/* UserResourceReport snippet */
$userId = $modx->user->get('id');
$docs = $modx->getCollection('modResource', array('createdby' => $userId));
$output = '';

foreach($docs as $doc) {
   $fields = $doc->toArray();
   $output .= $modx->getChunk('ResourceReportTpl', $fields);
}

return $output;

As you can see, there's not much to the snippet. It really demonstrates the power of xPDO and MODX. It makes a single query to the database and will quickly produce a display of information about all the resources the user has created on the site. You may want to exclude deleted and unpublished resources. That's easily done by changing the criteria used by getCollection():

$c = array(
    'createdby' => $userId,
    'deleted' => false,
    'published' => true,
);

$modx->getCollection('modResource', $c);

Another improvement to the code is to tell toArray() to use the "raw values" of the fields. The second argument to toArray() tells it whether to use the raw values or to call the object's get() method for each field. Often, you want the get() method called because the raw values of the fields are not what you need. In our case, though, the fields we need are all user IDs or simple strings, so the raw values are fine. We can speed the snippet up significantly by telling it to skip the call to get() for each field:

$fields = $doc->toArray('', true);

The first argument above is used to create a prefix for each field. We don't want that, so we send an empty string.

Coming Up

If we want to change from resources created by the user, to resources published or edited by the user we need to change the code of our snippet. In the next article, we'll send the field we want to use as an argument to the snippet.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.