Add each ThreadEntry, where you want it displayed. * -> Add ThreadSummary where you want summary displayed. Each calculates and displays required data using original output format and formula. * @author Grizly, using Masino Sinaga's original code. * * Last update Fri Nov 8 2013 * * TODO: Need to test for re-opened tickets * TODO: Figure out what the feck is wrong with old PHP versions and that is_date code.. */ /** * The format of the timestamp, must not change! * @var string */ define('MOD_DURATION_DATEFORMAT', "Y-m-d H:i:s"); class MOD_Duration { /** * Current number of thread entries. * @var int */ private $entrycount; /** * Timestamp of creation of ticket * @var DateTime */ private $created; /** * Timestamp of ticket closure * @var DateTime */ private $closed; /** * Timestamp of ticket last update * @var DateTime */ private $updated; /** * Timestamp of ticket due date * @var DateTime */ private $duedate; /** * Timestamp of the last entry to be added. (used to calculate the entries duration) * @var DateTime */ private $last; /** * @var bool */ private $isopen; /** * osTicket MOD. * * Used to calculate time spent waiting between updates to a tickets thread, and to produce a summary at the end indicating * the amount of time in total spent on the ticket. * * @param object the ticket object! */ public function MOD_Duration($ticket){ $this->isopen = $ticket->isOpen(); if(!$this->isopen){ $this->closed = $ticket->getCloseDate(); } $this->duedate = $ticket->getDueDate(); } /** * Calculate the time difference between two times. * Mostly Masino Sinaga's original code! * @param Date $a * @param Date $b * @return string Formatted string useful for printing on a ticket thread. Or an empty string if called incorrectly. */ function getDiff($a, $b) { $diff = intval(strtotime($b)) - intval(strtotime($a)); $diffday = round(intval(floor($diff/86400))); $modday = ($diff%86400); $diffhour = round(intval(floor($modday/3600)),0); $diffminute = round(intval(floor(($modday%3600)/60)),0); $diffsecond = round(($modday%60),0); //>0 will add to output by not being 0.. ;-) $o = ''; $o.= ($diffday>0) ? $diffday .'d, ' : ''; $o.= ($diffhour>0) ? $diffhour .'h, ' : ''; $o.= ($diffminute>0) ? $diffminute.'m, ' : ''; $o.= ($diffsecond>0) ? $diffsecond.'s.' : '.'; return $o; //make it nice and small, with no zero's //uncomment to get the original output. // return round($diffday)." Day(s), ".round($diffhour)." Hour(s), ".round($diffminute,0)." Minute(s), ".round($diffsecond,0)." Second(s)."; } /** * Grab the values from the thead-entry, this should be called once for each entry. * Currently only tested using showNotesInline mode.. so you might want to check for the other. * * // I like it in the heading, between title and poster: addThreadEntry($entry); ?> * * @param array $entry * @param bool if you want it to print the output, or return it. * @return string formatted value for end of thread entry. */ public function addThreadEntry($entry,$print=false){ $this->entrycount++; if(1==$this->entrycount){ //First item is always 0 duration.. ;-) $this->updated = $this->created = ($this->is_date($entry['created'])) ? $entry['created'] : false; //don't print time, just save it. }else{ $this->last = $this->updated; //save last value for each entry, so each new entry gets to show the time spent only on that entry. $this->updated = ($this->is_date($entry['created'])) ? $entry['created'] : false; $out = $this->getDiff($this->last, $this->updated); if($print) print $out; return $out; } } /** * Generate a summary of the time spent on the thread, prints it out too. * * Add something like this to the very bottom of the page:
Total: post(s), printThreadSummary(); ?> } else { echo '

Error fetching ticket thread - get technical help.

'; */ public function printThreadSummary(){ //sanity check if(!$this->is_date($this->created)){ print 'Unable to compute time.'; return; } //Original had different formatting based on whether the ticket was open or not. if($this->isopen){ print "Ticket open for ".$this->getDiff($this->created, date(MOD_DURATION_DATEFORMAT)); if($this->is_date($this->duedate)){ //TODO: Make this some form of passive-aggressive RED or something should it be overdue? print "\n
Ticket due in: ".$this->getDiff(date(MOD_DURATION_DATEFORMAT),$this->duedate); } }else{ print "Total duration: " . $this->getDiff($this->created, $this->closed); } } /** * checks if a given value is actually a date. * much love to: http://stackoverflow.com/a/11745522/276663 * @param TIMESTAMP $timestamp * @return boolean */ function is_date($timestamp){ $time = strtotime($timestamp); if ($time !== false) { //$keyword is a parsable date! return true; } return false; } } /** CLI Test mode * Simply run from command line: * php /path/to/include/class.duration.php * Output will be something like: Testing ticket_id: 1 7d, 0h, 3m, 38s.7d, 0h, 3m, 38s. 3d, 0h, 15m, 41s.3d, 0h, 15m, 41s. 1d, 20h, 27m, 50s.1d, 20h, 27m, 50s. Total duration: 11d, 20h, 47m, 9s. Testing ticket_id: 2 8m, 54s.8m, 54s. 2d, 15h, 14m, 46s.2d, 15h, 14m, 46s. 26m, 14s.26m, 14s. 1d, 3h, 34m, 37s.1d, 3h, 34m, 37s. ... */ if((php_sapi_name() == 'cli')){ require_once('../main.inc.php'); // WILL NOT WORK ON DELETED TICKETS.. seriously for($i=9269;$i<9279;$i++){ echo "\nTesting ticket_id: $i\n"; $ticket = new Ticket($i);// obviously pick a ticket number with more than one entry in the message thread. $mod_duration = new MOD_Duration($ticket); if(($thread=$ticket->getThreadEntries($types))) { foreach($thread as $entry) { echo $mod_duration->addThreadEntry($entry). "\n"; } } $mod_duration->printThreadSummary(); echo "\n"; } }