/usr/share/horde/whups/lib/Reports.php is in php-horde-whups 3.0.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | <?php
/**
* Whups_Reports:: class.
*
*
* Copyright 2002-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Whups
*/
class Whups_Reports
{
/**
* @var Whups_Driver
*/
protected $_backend;
/**
* Local cache of open tickets
*
* @var array
*/
protected $_opentickets;
/**
* Local cache of closed tickets
*
* @var array
*/
protected $_closedtickets;
/**
* Local cache of ticket sets
*
* @var array
*/
protected $_alltickets;
/**
* Constructor
*
* @param Whups_Driver $whups_driver The backend driver
*
* @return Whups_Reports
*/
function __construct(Whups_Driver $whups_driver)
{
$this->_backend = $whups_driver;
}
/**
* Get the data set
*
* @param string $report The report
*
* @return array The dataset
*/
public function getDataSet($report)
{
$operation = 'inc';
$state = null;
list($type, $field) = explode('|', $report);
if (substr($type, 0, 1) == '@') {
list($type, $operation, $state) = explode(':', substr($type, 1));
}
$tickets = $this->_getTicketSet($type, ($field == 'owner'));
if (substr($field, 0, 7) == 'user_id' || $field == 'owner') {
$user = true;
} else {
$user = false;
}
$dataset = array();
foreach ($tickets as $info) {
switch ($state) {
case 'open':
$date1 = new Horde_Date($info['date_resolved']);
$newdata = $date1->diff(new Horde_Date($info['timestamp']));
break;
default:
$newdata = 1;
}
if (empty($info[$field])) {
$this->_updateDataSet($dataset, _("None"), $newdata, $operation);
} else {
if ($user) {
$col = Whups::formatUser($info[$field], false);
} else {
$col = $info[$field];
}
$this->_updateDataSet($dataset, $col, $newdata, $operation);
}
}
// Perform any necessary post-processing on the dataset - process
// averages, for example.
switch ($operation) {
case 'avg':
foreach ($dataset as $index => $data) {
$dataset[$index] = number_format(array_sum($data) / count($data), 2);
}
break;
}
// Sort
ksort($dataset);
// Return the final data.
return $dataset;
}
/**
* Update the dataset
*
* @param array $dataset The dataset.
* @param integer $index The index to update.
* @param mixed $newdata The new data to insert.
* @param string $operation The operation being performed.
*/
function _updateDataSet(&$dataset, $index, $newdata, $operation)
{
if (isset($dataset[$index])) {
switch ($operation) {
case 'inc':
$dataset[$index] += $newdata;
break;
case 'max':
case 'min':
$dataset[$index] = $operation($newdata, $dataset[$index]);
break;
case 'avg':
$dataset[$index][] = $newdata;
break;
}
} else {
switch ($operation) {
case 'avg':
$dataset[$index] = array($newdata);
break;
default:
$dataset[$index] = $newdata;
}
}
}
/**
* Returns a time (max, min, avg) that tickets are in a particular state
* (open, assigned, etc.).
*
* @param string $operation One of 'max', 'min', or 'avg'.
* @param string $state The state to measure - 'open', etc.
* @param string $group_by A ticket property by which to group the
* results.
*
* @return integer|array The time value requested, or an array of values,
* if the $group_by parameter has been specified.
*
* @throws Whups_Exception
*/
public function getTime($stat, $group_by = null)
{
list($operation, $state) = explode('|', $stat);
$tickets = $this->_getTicketSet('closed');
if (!count($tickets)) {
throw new Whups_Exception(_("There is no data for this report."));
}
$dataset = array();
if (empty($group_by)) {
$dataset[0] = array();
}
foreach ($tickets as $info) {
if (is_null($info['date_resolved'])) {
continue;
}
switch ($state) {
case 'open':
$date1 = new Horde_Date($info['date_resolved']);
$diff = $date1->diff(new Horde_Date($info['timestamp']));
if (empty($group_by)) {
$dataset[0][] = $diff;
} else {
if (!isset($info[$group_by])) {
continue;
}
if (!isset($dataset[$info[$group_by]])) {
$dataset[$info[$group_by]] = array();
}
$dataset[$info[$group_by]][] = $diff;
}
break;
}
}
if (!count($dataset) || (is_null($group_by) && !count($dataset[0]))) {
return 'N/A';
}
switch ($operation) {
case 'min':
case 'max':
foreach (array_keys($dataset) as $group) {
$dataset[$group] = $operation($dataset[$group]);
}
break;
case 'avg':
foreach (array_keys($dataset) as $group) {
$dataset[$group] = round(array_sum($dataset[$group]) / count($dataset[$group]), 2);
}
break;
}
if (empty($group_by)) {
$dataset = $dataset[0];
}
return $dataset;
}
/**
* Loads a set of tickets, and cache the result inside the Whups_Reports::
* object to save on database access.
*
* @param string $type 'open', 'closed', or 'all' - the set of
* tickets to fetch. A previously cached set
* will be returned if it is available.
* @param boolean $expanded List tickets once for each owner of the
* ticket?
*
* @return array The ticket set.
*/
protected function &_getTicketSet($type, $expanded = false)
{
$queues = array_keys(Whups::permissionsFilter($this->_backend->getQueues(), 'queue'));
$expanded = (int)$expanded;
switch ($type) {
case 'open':
if (is_null($this->_opentickets[$expanded])) {
$this->_opentickets[$expanded] = $this->_backend->getTicketsByProperties(array('nores' => true, 'queue' => $queues), true, $expanded);
}
return $this->_opentickets[$expanded];
case 'closed':
if (is_null($this->_closedtickets[$expanded])) {
$this->_closedtickets[$expanded] = $this->_backend->getTicketsByProperties(array('res' => true, 'queue' => $queues), true, $expanded);
}
return $this->_closedtickets[$expanded];
case 'all':
if (is_null($this->_alltickets[$expanded])) {
$this->_alltickets[$expanded] = $this->_backend->getTicketsByProperties(array('queue' => $queues), true, $expanded);
}
return $this->_alltickets[$expanded];
}
}
}
|