<?php
# Snippet to read the MODX ManagerLog and spill out your work time on the site
# USAGE: [[malte? &timeout=`15`]]
# author: [email protected]
# Phase 1) read Manager Log - Phase 2) also read some folders for check for file changes (css for example)
# Let me know if you add or change things, maybe I can add them to the package in a later version!
//$modx->setDebug(true);
// thank you, sottwell!
$user = $modx->getOption('user', $scriptProperties, $modx->getUser());
if (!($user instanceof modUser)) {
$key = is_numeric($user) ? 'id' : 'username';
$user = $modx->getObject('modUser', array($key => $user));
if (!($user instanceof modUser)) {
return 'Defined user was not found.';
}
}
// Snippet properties
$timeout = $modx->getOption('timeout', $scriptProperties, 15); // gap (in minutes) before closing the current workunit
$detail = $modx->getOption('detail', $scriptProperties, false);
// Processor properties
$properties = array(
'user' => $user->get('id'),
'limit' => $modx->getOption('limit', $scriptProperties, 0), // Unlimited
'start' => $modx->getOption('start', $scriptProperties, 0),
'dateStart' => $modx->getOption('dateStart', $scriptProperties, false),
'dateEnd' => $modx->getOption('dateEnd', $scriptProperties, false),
'actionType' => $modx->getOption('actionType', $scriptProperties, false),
'sort' => $modx->getOption('sort', $scriptProperties, 'occurred'),
'dir' => $modx->getOption('dir', $scriptProperties, 'ASC'),
'dateFormat' => 'Y-m-d H:i:s',
);
// Fetch log data
$response = $modx->runProcessor('system/log/getlist', $properties);
if ($response->hasMessage()) {
$message = $response->getMessage();
}
$success = true;
$errors = array();
if ($response->isError()) {
foreach ($response->getFieldErrors() as $error) {
$errors[] = $error->message;
}
$success = false;
}
if ($success) {
$results = $modx->fromJSON($response->getResponse());
$output = '<h4>'.$user->get('username').'</h4>';
$timeoutsec = $timeout * 60;
$output .= '<p>';
foreach ($results['results'] as $log) {
$thisTime = $log['occurred'];
if (isset($lastTime)) {
$date1 = new DateTime($thisTime);
$date2 = new DateTime($lastTime);
$diff = strtotime($thisTime) - strtotime($lastTime);
}
if ($diff >= $timeoutsec) {
// the next step is more than $timeout seconds after than the current.
// It's a new workunit. Something like yesterday - today or "before lunch"
$output .= '<div>Current workunit: '.timeformat($workunit).'</div>';
$totalseconds += $workunit;
$output .= '<div>Total time: '.timeformat($totalseconds).'</div>';
$output .= '</p><p><div>New Workunit</div>';
$workunit = 0;
} else {
$workunit += $diff;
}
// mute the next line to save a lot of space on the output
if ($detail) {
$output .= "<br/>Entry #{$log['id']} with timestamp $thisTime which is $diff seconds later";
}
$lastTime = $log['occurred'];
}
$output .= '</p>';
return $output;
} else {
$log = $message.' '.print_r($errors, true);
$modx->log(modX::LOG_LEVEL_ERROR, $log);
return $log;
}
function timeformat($seconds)
{
return sprintf('%d days %02d:%02d:%02d', $seconds / 60 / 60 / 24, ($seconds / 60 / 60) % 24, ($seconds / 60) % 60, $seconds % 60);
}
]]>