Overview

Packages

  • Horde
    • Form
    • MIME
      • Viewer
    • Scheduler
  • None
  • Whups
    • UnitTests

Classes

  • Horde_Core_Ui_VarRenderer_whups
  • Whups
  • Whups_Ajax_Imple_ContactAutoCompleter
  • Whups_Api
  • Whups_Driver
  • Whups_Driver_Sql
  • Whups_Form_AddComment
  • Whups_Form_Admin_AddAttribute
  • Whups_Form_Admin_AddPriority
  • Whups_Form_Admin_AddQueue
  • Whups_Form_Admin_AddReply
  • Whups_Form_Admin_AddState
  • Whups_Form_Admin_AddType
  • Whups_Form_Admin_AddUser
  • Whups_Form_Admin_AddVersion
  • Whups_Form_Admin_CloneType
  • Whups_Form_Admin_DefaultPriority
  • Whups_Form_Admin_DefaultState
  • Whups_Form_Admin_DeleteAttribute
  • Whups_Form_Admin_DeletePriority
  • Whups_Form_Admin_DeleteQueue
  • Whups_Form_Admin_DeleteReply
  • Whups_Form_Admin_DeleteState
  • Whups_Form_Admin_DeleteType
  • Whups_Form_Admin_DeleteVersion
  • Whups_Form_Admin_EditAttributeStepOne
  • Whups_Form_Admin_EditAttributeStepTwo
  • Whups_Form_Admin_EditPriorityStepOne
  • Whups_Form_Admin_EditPriorityStepTwo
  • Whups_Form_Admin_EditQueueStepOne
  • Whups_Form_Admin_EditQueueStepTwo
  • Whups_Form_Admin_EditReplyStepOne
  • Whups_Form_Admin_EditReplyStepTwo
  • Whups_Form_Admin_EditStateStepOne
  • Whups_Form_Admin_EditStateStepTwo
  • Whups_Form_Admin_EditTypeStepOne
  • Whups_Form_Admin_EditTypeStepTwo
  • Whups_Form_Admin_EditUser
  • Whups_Form_Admin_EditVersionStepOne
  • Whups_Form_Admin_EditVersionStepTwo
  • Whups_Form_InsertBranch
  • Whups_Form_Query_AttributeCriterion
  • Whups_Form_Query_ChooseNameForLoad
  • Whups_Form_Query_ChooseNameForSave
  • Whups_Form_Query_DateCriterion
  • Whups_Form_Query_Delete
  • Whups_Form_Query_GroupCriterion
  • Whups_Form_Query_Parameter
  • Whups_Form_Query_PropertyCriterion
  • Whups_Form_Query_TextCriterion
  • Whups_Form_Query_UserCriterion
  • Whups_Form_Renderer_Comment
  • Whups_Form_Search
  • Whups_Form_SendReminder
  • Whups_Form_Ticket_CreateStepFour
  • Whups_Form_Ticket_CreateStepOne
  • Whups_Form_Ticket_CreateStepThree
  • Whups_Form_Ticket_CreateStepTwo
  • Whups_Form_Ticket_Edit
  • Whups_Form_TicketDetails
  • Whups_LoginTasks_SystemTask_Upgrade
  • Whups_Mail
  • Whups_Query
  • Whups_Query_Manager
  • Whups_Reports
  • Whups_Ticket
  • Whups_View_Base
  • Whups_View_Results
  • Whups_View_SavedQueries
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Whups_Reports:: class.
  4:  *
  5:  *
  6:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file LICENSE for license information (BSD). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 10:  *
 11:  * @author  Chuck Hagenbuch <chuck@horde.org>
 12:  * @package Whups
 13:  */
 14: class Whups_Reports
 15: {
 16:     /**
 17:      * @var Whups_Driver
 18:      */
 19:     protected $_backend;
 20: 
 21:     /**
 22:      * Local cache of open tickets
 23:      *
 24:      * @var array
 25:      */
 26:     protected $_opentickets;
 27: 
 28:     /**
 29:      * Local cache of closed tickets
 30:      *
 31:      * @var array
 32:      */
 33:     protected $_closedtickets;
 34: 
 35:     /**
 36:      * Local cache of ticket sets
 37:      *
 38:      * @var array
 39:      */
 40:     protected $_alltickets;
 41: 
 42:     /**
 43:      * Constructor
 44:      *
 45:      * @param Whups_Driver $whups_driver  The backend driver
 46:      *
 47:      * @return Whups_Reports
 48:      */
 49:     function __construct(Whups_Driver $whups_driver)
 50:     {
 51:         $this->_backend = $whups_driver;
 52:     }
 53: 
 54:     /**
 55:      * Get the data set
 56:      *
 57:      * @param string $report  The report
 58:      *
 59:      * @return array  The dataset
 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:         // Perform any necessary post-processing on the dataset - process
103:         // averages, for example.
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:         // Sort
113:         ksort($dataset);
114: 
115:         // Return the final data.
116:         return $dataset;
117:     }
118: 
119:     /**
120:      * Update the dataset
121:      *
122:      * @param array $dataset     The dataset.
123:      * @param integer $index     The index to update.
124:      * @param mixed $newdata     The new data to insert.
125:      * @param string $operation  The operation being performed.
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:      * Returns a time (max, min, avg) that tickets are in a particular state
158:      * (open, assigned, etc.).
159:      *
160:      * @param string $operation  One of 'max', 'min', or 'avg'.
161:      * @param string $state      The state to measure - 'open', etc.
162:      * @param string $group_by   A ticket property by which to group the
163:      *                           results.
164:      *
165:      * @return integer|array  The time value requested, or an array of values,
166:      *                        if the $group_by parameter has been specified.
167:      *
168:      * @throws Whups_Exception
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:      * Loads a set of tickets, and cache the result inside the Whups_Reports::
236:      * object to save on database access.
237:      *
238:      * @param string $type       'open', 'closed', or 'all' - the set of
239:      *                           tickets to fetch. A previously cached set
240:      *                           will be returned if it is available.
241:      * @param boolean $expanded  List tickets once for each owner of the
242:      *                           ticket?
243:      *
244:      * @return array  The ticket set.
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: 
API documentation generated by ApiGen