File: 0.00.1a/plugins/simplereporting/simplereporting.php (View as HTML)

  1: <?php 
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: class simplereporting extends FreeDESK_PIM
 24: {
 25: 
 26: 	function Start()
 27: 	{
 28: 		$this->DESK->PluginManager->RegisterPIMPage("simplereporting", $this->ID);
 29: 		$this->DESK->PluginManager->Register(new Plugin(
 30: 			"Simple Reporting", "0.02", "Reporting" ));
 31: 		$this->DESK->PermissionManager->Register("simple_reporting", false);
 32: 		$jspath = $this->webpath . "simplereporting.js";
 33: 		$this->DESK->PluginManager->RegisterScript($jspath);
 34: 	}
 35: 	
 36: 	function BuildMenu()
 37: 	{	
 38: 		if ($this->DESK->ContextManager->Permission("simple_reporting"))
 39: 		{
 40: 			$currentItems = $this->DESK->ContextManager->MenuItems();
 41: 			if (!isset($currentItems['reporting']))
 42: 			{
 43: 				$repmenu = new MenuItem();
 44: 				$repmenu->tag = "reporting";
 45: 				$repmenu->display = "Reporting";
 46: 				$this->DESK->ContextManager->AddMenuItem("reporting", $repmenu);
 47: 			}
 48: 		
 49: 			$sReport = new MenuItem();
 50: 			$sReport->tag="simplereporting";
 51: 			$sReport->display="Simple Reporting";
 52: 			$sReport->onclick = "DESK.loadSubpage('simplereporting');";
 53: 			$this->DESK->ContextManager->AddMenuItem("reporting",$sReport);
 54: 		}
 55: 	}
 56: 	
 57: 	function Page($page)
 58: 	{
 59: 		if ($page == "simplereporting" && $this->DESK->ContextManager->Permission("simple_reporting"))
 60: 		{
 61: 			echo "<h3>Simple Reporting</h3>\n";
 62: 		
 63: 			$cYear = date("Y");
 64: 			$cMonth = date("m");
 65: 			$cDay = date("d");
 66: 			
 67: 			$sYear = (isset($_REQUEST['sYear'])) ? $_REQUEST['sYear'] : $cYear;
 68: 			$sMonth = (isset($_REQUEST['sMonth'])) ? $_REQUEST['sMonth'] : 1;
 69: 			$sDay = (isset($_REQUEST['sDay'])) ? $_REQUEST['sDay'] : 1;
 70: 			$fYear = (isset($_REQUEST['fYear'])) ? $_REQUEST['fYear'] : ($cYear+1);
 71: 			$fMonth = (isset($_REQUEST['fMonth'])) ? $_REQUEST['fMonth'] : 1;
 72: 			$fDay = (isset($_REQUEST['fDay'])) ? $_REQUEST['fDay'] : 1;
 73: 			
 74: 			if ($sMonth < 10)
 75: 				$sMonth = "0".$sMonth;
 76: 			if ($sDay < 20)
 77: 				$sDay = "0".$sDay;
 78: 			if ($fMonth < 10)
 79: 				$fMonth = "0".$fMonth;
 80: 			if ($fDay < 10)
 81: 				$fDay = "0".$fDay;
 82: 			
 83: 			echo "<form id=\"simplereporting\" onsubmit=\"return false;\">\n";
 84: 			echo "<table>\n";
 85: 			echo "<tr>\n";
 86: 			echo "<td>Start</td>\n";
 87: 			echo "<td>\n";
 88: 			echo "<select name=\"sYear\">";
 89: 			for($i=2011; $i<=($cYear+5); ++$i)
 90: 			{
 91: 				if ($i == $sYear)
 92: 					$s=" selected";
 93: 				else
 94: 					$s="";
 95: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
 96: 			}
 97: 			echo "</select>\n";
 98: 			echo "</td>\n";
 99: 			
100: 			echo "<td>\n";
101: 			echo "<select name=\"sMonth\">";
102: 			for($i=1; $i<=12; ++$i)
103: 			{
104: 				if ($i == $sMonth)
105: 					$s=" selected";
106: 				else
107: 					$s="";
108: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
109: 			}
110: 			echo "</select>\n";
111: 			echo "</td>\n";
112: 			
113: 			echo "<td>\n";
114: 			echo "<select name=\"sDay\">";
115: 			for($i=1; $i<=31; ++$i)
116: 			{
117: 				if ($i == $sDay)
118: 					$s=" selected";
119: 				else
120: 					$s="";
121: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
122: 			}
123: 			echo "</select>\n";
124: 			echo "</td>\n";
125: 			
126: 			echo "</tr>\n";
127: 			
128: 			
129: 			echo "<tr>\n";
130: 			echo "<td>Finish</td>\n";
131: 			echo "<td>\n";
132: 			echo "<select name=\"fYear\">";
133: 			for($i=2011; $i<=($cYear+5); ++$i)
134: 			{
135: 				if ($i == $fYear)
136: 					$s=" selected";
137: 				else
138: 					$s="";
139: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
140: 			}
141: 			echo "</select>\n";
142: 			echo "</td>\n";
143: 			
144: 			echo "<td>\n";
145: 			echo "<select name=\"fMonth\">";
146: 			for($i=1; $i<=12; ++$i)
147: 			{
148: 				if ($i == $fMonth)
149: 					$s=" selected";
150: 				else
151: 					$s="";
152: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
153: 			}
154: 			echo "</select>\n";
155: 			echo "</td>\n";
156: 			
157: 			echo "<td>\n";
158: 			echo "<select name=\"fDay\">";
159: 			for($i=1; $i<=31; ++$i)
160: 			{
161: 				if ($i == $fDay)
162: 					$s=" selected";
163: 				else
164: 					$s="";
165: 				echo "<option value=\"".$i."\"".$s.">".$i."</option>\n";
166: 			}
167: 			echo "</select>\n";
168: 			echo "</td>\n";
169: 			
170: 			echo "</tr>\n";
171: 			
172: 			echo "<tr>\n";
173: 			echo "<td colspan=\"4\">\n";
174: 			echo "<input type=\"submit\" value=\"Run Report\" onclick=\"simpleReporting.runReport('simplereporting');\" />\n";
175: 			echo "</td></tr>";
176: 			
177: 			echo "</table>\n";
178: 			echo "</form>\n";
179: 			
180: 			if (isset($_REQUEST['runreport']))
181: 			{
182: 				$priorities = $this->DESK->RequestManager->GetPriorityList();
183: 				$data = array();
184: 			
185: 				echo "<br /><br />";
186: 				echo "<h3>Report for ".$sYear."-".$sMonth."-".$sDay." to ".$fYear."-".$fMonth."-".$fDay."</h3>\n";
187: 				
188: 				$start=$sYear."-".$sMonth."-".$sDay." 00:00:00";
189: 				$finish=$fYear."-".$fMonth."-".$fDay." 00:00:00";
190: 				
191: 				$q="SELECT ".$this->DESK->Database->Field("openeddt").",".$this->DESK->Database->Field("closeddt").",".$this->DESK->Database->Field("priority").",";
192: 				$q.="TIMESTAMPDIFF(SECOND,".$this->DESK->Database->Field("openeddt").",".$this->DESK->Database->Field("closeddt").") AS diff ";
193: 				$q.="FROM ".$this->DESK->Database->Table("request")." WHERE ".$this->DESK->Database->Field("closeddt").">=".$this->DESK->Database->SafeQuote($start)." AND ";
194: 				$q.=$this->DESK->Database->Field("closeddt")."<=".$this->DESK->Database->SafeQuote($finish);
195: 				
196: 				$r=$this->DESK->Database->Query($q);
197: 				
198: 				//print_r($priorities);
199: 				echo "Found: ".$this->DESK->Database->NumRows($r);
200: 				echo "<br /><br />\n";
201: 				
202: 				while($row = $this->DESK->Database->FetchAssoc($r))
203: 				{
204: 					$diff = $row['diff'];
205: 					//echo $diff."<br />";
206: 					$pri = $row['priority'];
207: 					//echo $pri."<br />";
208: 					//echo $priorities[$pri]['resolutionsla']."<br />";
209: 					if (!isset($priorities[$pri]))
210: 						$pri = 0;
211: 					if (!isset($out[$pri]))
212: 						$out[$pri]=array( "count" => 0, "within" => 0 );
213: 					$out[$pri]["count"]++;
214: 					//echo $pri."<br />";
215: 					if ($pri != 0)
216: 					{
217: 						if ($diff <= $priorities[$pri]['resolutionsla'])
218: 							$out[$pri]["within"]++;
219: 						//echo "(".$diff.") (".$priorities[$pri]['resolutionsla'].")<br />\n";
220: 					}
221: 				}
222: 				
223: 				echo "<table>\n";
224: 				echo "<tr>\n";
225: 				echo "<th>Priority</th>\n";
226: 				echo "<th>Count</th>\n";
227: 				echo "<th>Within (n)</th>\n";
228: 				echo "<th>Within %</th>\n";
229: 				echo "</tr>\n";
230: 				
231: 				foreach($out as $pri => $detail)
232: 				{
233: 					echo "<tr>\n";
234: 					echo "<td>\n";
235: 					if (!isset($priorities[$pri]))
236: 						echo "Unknown";
237: 					else
238: 						echo $priorities[$pri]['priorityname'];
239: 					echo "</td>\n";
240: 					echo "<td>".$detail['count']."</td>\n";
241: 					echo "<td>".$detail['within']."</td>\n";
242: 					if ($detail['count']>0 && $detail['within']>0)
243: 						$perc = ($detail['within']/$detail['count'])*100;
244: 					else
245: 						$perc = "0.00";
246: 					echo "<td>".$perc." %</td>\n";
247: 					echo "</tr>";
248: 				}
249: 				
250: 				echo "</table>\n";
251: 				
252: 				$this->DESK->Database->Free($r);
253: 			}
254: 		}
255: 	}
256: 	
257: 	
258: 
259: }
260: ?>
261: