1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Whups_Reports
15: {
16: 17: 18:
19: protected $_backend;
20:
21: 22: 23: 24: 25:
26: protected $_opentickets;
27:
28: 29: 30: 31: 32:
33: protected $_closedtickets;
34:
35: 36: 37: 38: 39:
40: protected $_alltickets;
41:
42: 43: 44: 45: 46: 47: 48:
49: function __construct(Whups_Driver $whups_driver)
50: {
51: $this->_backend = $whups_driver;
52: }
53:
54: 55: 56: 57: 58: 59: 60:
61: public function getDataSet($report)
62: {
63: $operation = 'inc';
64: $state = null;
65: list($type, $field) = explode('|', $report);
66: if (substr($type, 0, 1) == '@') {
67: list($type, $operation, $state) = explode(':', substr($type, 1));
68: }
69: $tickets = $this->_getTicketSet($type, ($field == 'owner'));
70:
71: if (substr($field, 0, 7) == 'user_id' || $field == 'owner') {
72: $user = true;
73: } else {
74: $user = false;
75: }
76:
77: $dataset = array();
78: foreach ($tickets as $info) {
79: switch ($state) {
80: case 'open':
81: $date1 = new Horde_Date($info['date_resolved']);
82: $newdata = $date1->diff(new Horde_Date($info['timestamp']));
83: break;
84:
85: default:
86: $newdata = 1;
87: }
88:
89: if (empty($info[$field])) {
90: $this->_updateDataSet($dataset, _("None"), $newdata, $operation);
91: } else {
92: if ($user) {
93: $col = Whups::formatUser($info[$field], false);
94: } else {
95: $col = $info[$field];
96: }
97:
98: $this->_updateDataSet($dataset, $col, $newdata, $operation);
99: }
100: }
101:
102:
103:
104: switch ($operation) {
105: case 'avg':
106: foreach ($dataset as $index => $data) {
107: $dataset[$index] = number_format(array_sum($data) / count($data), 2);
108: }
109: break;
110: }
111:
112:
113: ksort($dataset);
114:
115:
116: return $dataset;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126:
127: function _updateDataSet(&$dataset, $index, $newdata, $operation)
128: {
129: if (isset($dataset[$index])) {
130: switch ($operation) {
131: case 'inc':
132: $dataset[$index] += $newdata;
133: break;
134:
135: case 'max':
136: case 'min':
137: $dataset[$index] = $operation($newdata, $dataset[$index]);
138: break;
139:
140: case 'avg':
141: $dataset[$index][] = $newdata;
142: break;
143: }
144: } else {
145: switch ($operation) {
146: case 'avg':
147: $dataset[$index] = array($newdata);
148: break;
149:
150: default:
151: $dataset[$index] = $newdata;
152: }
153: }
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169:
170: public function getTime($stat, $group_by = null)
171: {
172: list($operation, $state) = explode('|', $stat);
173:
174: $tickets = $this->_getTicketSet('closed');
175: if (!count($tickets)) {
176: throw new Whups_Exception(_("There is no data for this report."));
177: }
178:
179: $dataset = array();
180: if (empty($group_by)) {
181: $dataset[0] = array();
182: }
183: foreach ($tickets as $info) {
184: if (is_null($info['date_resolved'])) {
185: continue;
186: }
187:
188: switch ($state) {
189: case 'open':
190: $date1 = new Horde_Date($info['date_resolved']);
191: $diff = $date1->diff(new Horde_Date($info['timestamp']));
192: if (empty($group_by)) {
193: $dataset[0][] = $diff;
194: } else {
195: if (!isset($info[$group_by])) {
196: continue;
197: }
198: if (!isset($dataset[$info[$group_by]])) {
199: $dataset[$info[$group_by]] = array();
200: }
201: $dataset[$info[$group_by]][] = $diff;
202: }
203:
204: break;
205: }
206: }
207:
208: if (!count($dataset) || (is_null($group_by) && !count($dataset[0]))) {
209: return 'N/A';
210: }
211:
212: switch ($operation) {
213: case 'min':
214: case 'max':
215: foreach (array_keys($dataset) as $group) {
216: $dataset[$group] = $operation($dataset[$group]);
217: }
218: break;
219:
220: case 'avg':
221: foreach (array_keys($dataset) as $group) {
222: $dataset[$group] = round(array_sum($dataset[$group]) / count($dataset[$group]), 2);
223: }
224: break;
225: }
226:
227: if (empty($group_by)) {
228: $dataset = $dataset[0];
229: }
230:
231: return $dataset;
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245:
246: protected function &_getTicketSet($type, $expanded = false)
247: {
248: $queues = array_keys(Whups::permissionsFilter($this->_backend->getQueues(), 'queue'));
249: $expanded = (int)$expanded;
250: switch ($type) {
251: case 'open':
252: if (is_null($this->_opentickets[$expanded])) {
253: $this->_opentickets[$expanded] = $this->_backend->getTicketsByProperties(array('nores' => true, 'queue' => $queues), true, $expanded);
254: }
255: return $this->_opentickets[$expanded];
256:
257: case 'closed':
258: if (is_null($this->_closedtickets[$expanded])) {
259: $this->_closedtickets[$expanded] = $this->_backend->getTicketsByProperties(array('res' => true, 'queue' => $queues), true, $expanded);
260: }
261: return $this->_closedtickets[$expanded];
262:
263: case 'all':
264: if (is_null($this->_alltickets[$expanded])) {
265: $this->_alltickets[$expanded] = $this->_backend->getTicketsByProperties(array('queue' => $queues), true, $expanded);
266: }
267: return $this->_alltickets[$expanded];
268: }
269: }
270:
271: }
272: