MODX User Report IV

In the previous article, we produced a sorted list of resources acted on bMODX logoy the current user. In this one, we’ll see how to get all resources acted on by the user in a single query and add the user’s actions to our report.




The Snippet Tag

We’ll keep the two properties that set the sort field and the direction of the sort, but since we’re making a single query, we no longer need the &searchField property. Our tag now looks like this:

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


The Tpl Chunk

The Tpl chunk (ResourceReportTpl) will take the same form it did before, but with an added placeholder to show the users actions:

[code language=”html”]
<p class="page_field">Description: [[+description]]</p>
<p class="page_field">Summary: [[+summary]]</p>
<p class="page_field">Actions: [[+actions]]</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’);
$sortBy = $modx->getOption(‘sortBy’, $scriptProperties, ‘pagetitle’);
$sortDir = $modx->getOption(‘sortDir’, $scriptProperties, ‘ASC’);

/* Create the query */
$query = $modx->newQuery(‘modResource’);
$criteria = array(
‘deleted’ => false,
‘createdby’ => $userId,
‘OR:publishedby’ => $userId,
‘OR:editedby’ => $userId,
/* 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();

/* See what the user did with the resource */
$actions = array();

if ($doc->get(‘createdby’ == $userId) {
$actions[] = ‘Created’;

if ($doc->get(‘publishedby’ == $userId) {
$actions[] = ‘Published’;

if ($doc->get(‘editedby’ == $userId) {
$actions[] = ‘Edited’;
/* Create the actions string from the actions array */
$userActions = implode(‘, ‘, $actions);

/* Add the actions string to the $fields array */
$fields[‘actions’] = $userActions;

$output .= $modx->getChunk(‘ResourceReportTpl’, $fields);

return $output;


I have to confess that I haven’t actually tested this code. It should work, but I can’t guarantee it.


Why did we create the $actions array? Why not just add the action string to the end of the $userActions string like this?

[code language=”php”]
$userActions .= ‘Created, ‘;

The problem with that approach is that we don’t know what the user did with the resource. If the user only created the resource, you’d get this output:

[code language=”html”]
Actions: Created,

The comma looks bad if there is only one entry. Even if there’s more than one, there will always be an extra comma at the end. By using implode() with ', ' as the "glue" between the elements, the formatting will always be correct, regardless of how many or how few actions there are, and there will never be a comma at the end.

If you’d like to list the actions in a different order, you can just re-order the three if{} statements.


For more information on how to use MODX to create a website, 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 *