MODX User Report III

In the previous article, we produced a list of resources acted on byMODX logo the current user. In this one, we’ll see how to sort that list.




We could sort the resources after they are retrieved, but it’s faster and easier to let the database engine do it for us as they are retrieved. In this example, we’ll use an xPDO query to set the criteria and specify a sort order.

The Snippet Tag

We’ll add two more properties to set the sort field and the direction of the sort. With the added properties, our snippet tag will look like this:

[code language=”html”]
[[UserResourceReport? &searchField=`createdby` &sortBy=`pagetitle` &sortDir=`ASC`]]


The Tpl Chunk

The Tpl chunk (ResourceReportTpl) will take the same form it did before:

[code language=”html”]
<p class="page_field">Description: [[+description]]</p>
<p class="page_field">Summary: [[+summary]]</p>

The Code

Here’s the code of our snippet, modified to use the sort fields. In order to do the sort, we have to create an extra variable for the xPDO query so we can use its sortby() method. We’ll also use its where() method to set the criteria for the search:

[code language=”php”]
/* UserResourceReport snippet */

/* Get the variables we’ll need */
$userId = $modx->user->get(‘id’);
$searchField = $modx->getOption(‘searchField’, $scriptProperties, ‘createdby’);
$sortBy = $modx->getOption(‘sortBy’, $scriptProperties, ‘pagetitle’);
$sortDir = $modx->getOption(‘sortDir’, $scriptProperties, ‘ASC’);

/* Create the query */
$query = $modx->newQuery(‘modResource’);
$criteria = array(
$searchField => $userId,
‘deleted’ => false,
/* Use the criteria to set the query parameters */

/* Set the query’s sort field and direction */
$query->sortby($sortBy, $sortDir);

/* Query the database for the resources */

$docs = $modx->getCollection(‘modResource’, $query);
$output = ”;

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

return $output;

Setting up the query is a little more complex, but once it’s done, the rest of the code is the same as it was before. If you wanted to sort in the opposite direction (as you might if you used publishedon as the search field) you could use &sortDir=`DESC`.


A Complete Report

Because our snippet now sorts the resources, each of the following sections will present the resources in the order you specify.

[code language=”html”]
<h2>Resources You’ve Created</h2>
[[!UserResourceReport? &searchField=`createdby`]]

<h2>Resources You’ve Published</h2>
[[!UserResourceReport? &searchField=`publishedby`]]

<h2>Resources You’ve Edited</h2>
[[!UserResourceReport? &searchField=`publishedby`]]


Coming Up

It may have occurred to you that our complete report above calls the snippet three times and makes three queries to the database. This is not ideal. It’s possible to get all the resources for the report in a single query by changing the search criteria to something like this:

[code language=”php”]
$criteria = array(
‘deleted’ => false,
‘createdby’ => $userId,
‘OR:publishedby’ => $userId,
‘OR:editedby’ => $userId,

Unfortunately, that would give us an array of all the resources the user did anything with. If you just want to display them all, that’s fine, but it doesn’t tell us which ones the user did what with. If you want to report on which ones the user edited, published, and/or created, you’d have to loop through all the resources, check those three fields, create a string to describe the users actions, and use it to set another placeholder. We’ll look at how to do that in the next article.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *