User Resource Report Part 4

UserResourceReport—a full report for the current user

By Bob Ray  |  November 12, 2024  |  3 min read
User Resource Report Part 4

In the previous article, we produced a sorted list of resources acted on by the current user. In this one, we'll see how 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:

[[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:

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

The Code

/* 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,
    'published:=' => true,
    array(
        'createdby:=' => $userId,
        'OR:publishedby:=' => $userId,
        'OR:editedby:=' => $userId,
    ),
);
/* Use the criteria to set the query parameters */
$query->where($criteria);

/* 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;

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

$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:

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.

All User Report

Suppose that you want the report above for all users. You can use most of the code above. You just need to get all users, then step through them and perform the actions above:

$users = $modx->getCollection('modUser');
foreach ($users as $user) {
    $userId = $user->get(id);

    /* The code above goes here */
}

Important: Be sure to remove the line that sets the user ID to the current user.

The only other changes would be adding a username placeholder to the Tpl chunk, and setting that placeholder inside the loop in the code above.


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.