User Resource Report IV

Get all resources acted on by the user in a single query

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:

<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 used it's where() method to set the criteria for the search:

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

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

Comments (0)

Please login to comment.