Advertisement
Guest User

Untitled

a guest
Apr 4th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.97 KB | None | 0 0
  1. <?php
  2.  
  3.     $servername = "localhost";
  4.     $username = "USERNAME";
  5.     $password = "PASSWORD";
  6.  
  7.     $conn = new mysqli($servername, $username, $password);
  8.    
  9.     if ($conn->connect_error)
  10.     {
  11.         die("Connection failed: " . $conn->connect_error);
  12.     }
  13.  
  14. ?>
  15. <!DOCTYPE html>
  16. <html>
  17.     <head>
  18.         <title>IVR Statistics</title>
  19.        
  20.         <link rel="stylesheet" href="FREEPBXFQDN/styles/pbxstyles.css" />
  21.        
  22.         <style>
  23.             td { padding: 2px 10px; }
  24.             #callsDiv { position: fixed; height: 50%; width: 99%; overflow-y: scroll; overflow-x: hidden; }
  25.             .callgood { background: #d1fbc6; }
  26.             .callerrel { background: #f8fb7e; }
  27.             .callivrrel { background: #f09e9e; }
  28.             select { font-size: 1em; }
  29.         </style>
  30.        
  31.         <script>
  32.             function filterIVR()
  33.             {
  34.                 proccalls = 0;
  35.                 var callerleft = 0;
  36.                 var ivrleft = 0;
  37.                 var table = document.getElementById("callsTbl");
  38.                 tr = table.getElementsByTagName("tr");
  39.                 var whichIVR = document.getElementById("ivr-filter").value;
  40.                
  41.                 for (var i = 1; i < tr.length; i++)
  42.                 {  
  43.                     tr[i].style.display = "none";
  44.                    
  45.                     td = tr[i].getElementsByTagName("td");
  46.  
  47.                     for (var j = 0; j < td.length; j++)
  48.                     {
  49.                         cell = tr[i].getElementsByTagName("td")[j];
  50.                        
  51.                         if (cell)
  52.                         {
  53.                             if (whichIVR == "All" || cell.innerHTML.indexOf(whichIVR) > -1)
  54.                             {
  55.                                 proccalls++;
  56.                                
  57.                                 if (tr[i].innerHTML.indexOf("Released by caller") > -1)
  58.                                 {
  59.                                     callerleft++;
  60.                                    
  61.                                     tr[i].className = "callerrel";
  62.                                 }
  63.                                 else if (tr[i].innerHTML.indexOf("Released by IVR") > -1)
  64.                                 {
  65.                                     ivrleft++;
  66.                                    
  67.                                     tr[i].className = "callivrrel";
  68.                                 }
  69.                                
  70.                                 tr[i].style.display = "";
  71.                                
  72.                                 break;
  73.                             }
  74.                         }
  75.                     }
  76.                    
  77.                     document.getElementById("currivr").innerText = whichIVR;
  78.                     document.getElementById("proccalls").innerText = proccalls.toString();
  79.                     document.getElementById("callerrel").innerText = callerleft.toString();
  80.                     document.getElementById("ivrrel").innerText = ivrleft.toString();
  81.                 }
  82.             }
  83.         </script>
  84.     </head>
  85. <body onload="filterIVR()">
  86.     <h1 style="text-align: center">IVR Statistics</h1>
  87.         <span style="font-size: 1.1em">Filter by IVR:</span>
  88.         <span style="width: 60px">&nbsp;</span>
  89.         <select id="ivr-filter" onchange="filterIVR()">
  90.             <option value="All" default>-----All IVRs-----</option>
  91.             <?php
  92.            
  93.                 $sql = "SELECT description FROM asterisk.ivr_details WHERE description NOT LIKE '%IVR%'";
  94.                
  95.                 $result = $conn->query($sql);
  96.                
  97.                 if ($result->num_rows > 0)
  98.                 {
  99.                     while ($row = $result->fetch_assoc())
  100.                     {
  101.                         echo "<option value='" . $row["description"] . "'>" . $row["description"] . "</option>";
  102.                     }
  103.                 }
  104.                 else
  105.                 {
  106.                     echo "No IVRs to display.";
  107.                 }
  108.            
  109.             ?>
  110.         </select>
  111.         <p>&nbsp;</p>
  112.     <hr>
  113.     <h3>
  114.         Current IVR: <span id="currivr" style="text-decoration: underline">&nbsp;</span><br />
  115.         Processed calls: <span id="proccalls">&nbsp;</span><br />
  116.         Released by caller: <span id="callerrel">&nbsp;</span><br />
  117.         Released by IVR: <span id="ivrrel">&nbsp;</span>
  118.     </h3>
  119.    
  120.     <div id="callsDiv">
  121.         <table border="1" id="callsTbl">
  122.             <tr class="header">
  123.                 <th>Menu:</th>
  124.                 <th>Timestamp:</th>
  125.                 <th>Caller:</th>
  126.                 <th>Called Number:</th>
  127.                 <th>Call Length:</th>
  128.                 <th>Call Result:</th>
  129.             </tr>
  130.    
  131.             <?php
  132.            
  133.                 function secToHMS($seconds)
  134.                 {
  135.                     $hours = floor($seconds / 3600);
  136.                     $minutes = floor(($seconds / 60) % 60);
  137.                     $seconds = $seconds % 60;
  138.                    
  139.                     $hours = str_pad($hours, 2, "0", STR_PAD_LEFT);
  140.                     $minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT);
  141.                     $seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
  142.                    
  143.                     return "$hours:$minutes:$seconds";
  144.                 }
  145.                
  146.                 $sq1 = "
  147.                     SELECT
  148.                         asterisk.ivr_details.description AS ivrname,
  149.                         ivrid,
  150.                         tstamp,
  151.                         caller,
  152.                         callednum,
  153.                         calllength,
  154.                         CASE
  155.                             WHEN strcmp(sq2.selected, 's') = 0
  156.                                 THEN 'Released by caller'
  157.                             WHEN strcmp(sq2.selected, 't') = 0
  158.                                 THEN 'Released by IVR'
  159.                             WHEN char_length(sq2.selected) = 4
  160.                                 THEN concat(ie.selection, ' -> ', sq2.selected)
  161.                             ELSE
  162.                                 concat(ie.selection, ' -> ', substr(ie.dest, 17, 4))
  163.                         END AS endresult
  164.                         FROM (
  165.                             SELECT DISTINCT
  166.                                 calldate AS tstamp,
  167.                                 substr(asterisk.incoming.destination, 5, 1) AS ivrid,
  168.                                 asterisk.incoming.extension AS ivrext,
  169.                                 src AS caller,
  170.                                 did AS callednum,
  171.                                 dst AS selected,
  172.                                 billsec AS calllength
  173.  
  174.                             FROM asteriskcdrdb.cdr
  175.                            
  176.                             JOIN
  177.                                 asterisk.incoming ON asterisk.incoming.extension = did
  178.                         )sq2
  179.  
  180.                     JOIN
  181.                         asterisk.ivr_entries ie ON (substr(ie.dest, 17, 4) = sq2.selected OR
  182.                         char_length(sq2.selected) = 1) AND
  183.                         ie.ivr_id = sq2.ivrid
  184.                     JOIN
  185.                         asterisk.ivr_details ON asterisk.ivr_details.id = sq2.ivrid
  186.                        
  187.                     WHERE
  188.                         sq2.callednum IN (ivrext)
  189.  
  190.                     GROUP BY tstamp
  191.                     ORDER BY tstamp DESC
  192.                 ";
  193.                 $result = $conn->query($sq1);
  194.                
  195.                 if ($result->num_rows > 0)
  196.                 {
  197.                     while ($row = $result->fetch_assoc())
  198.                     {
  199.                         if ($row["endresult"] == "Released by caller")
  200.                         {
  201.                             $callerleft++;
  202.                         }
  203.                         else if ($row["endresult"] == "Released by IVR")
  204.                         {
  205.                             $ivrterm++;
  206.                         }
  207.                        
  208.                         echo "<tr class='callgood'><td><a href='FREEPBXFQDN/admin/config.php?display=ivr&action=edit&id=" . $row["ivrid"] . "' target='new'>" . $row["ivrname"] . "</a></td>";
  209.                         echo "<td>" . $row["tstamp"] . "</td>";
  210.                         echo "<td>" . $row["caller"] . "</td>";
  211.                         echo "<td><a href='FREEPBXFQDN/admin/config.php?display=did&view=form&extdisplay=" . $row["callednum"] . "%2F' target='new'>" . $row["callednum"] . "</a></td>";
  212.                         echo "<td>" . secToHMS($row["calllength"]) . "</td>";
  213.                         echo "<td>" . $row["endresult"] . "</td>";
  214.                     }
  215.                 }
  216.                 else
  217.                 {
  218.                     echo "0 results from sq1";
  219.                 }
  220.  
  221.             ?>
  222.            
  223.         </table>
  224.     </div> 
  225. </body>
  226. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement