<commit_before><?php

/*
   ------------------------------------------------------------------------
   TimelineTicket
   Copyright (C) 2013-2016 by the TimelineTicket Development Team.

   https://github.com/pluginsGLPI/timelineticket
   ------------------------------------------------------------------------

   LICENSE

   This file is part of TimelineTicket project.

   TimelineTicket plugin is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   TimelineTicket plugin is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with TimelineTicket plugin. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   TimelineTicket plugin
   @copyright Copyright (c) 2013-2016 TimelineTicket team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      https://github.com/pluginsGLPI/timelineticket
   @since     2013

   ------------------------------------------------------------------------
 */

//Options for GLPI 0.71 and newer : need slave db to access the report
$USEDBREPLICATE        = 1;
$DBCONNECTION_REQUIRED = 1;

include("../../../../inc/includes.php");

// Instantiate Report with Name
$report = new PluginReportsAutoReport(__("statSpentTimeProcessingByGroup_report_title", "timelineticket"));
//Report's search criterias
$dateYear = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y") - 1));
$lastday  = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));

if (date("d") == $lastday) {
   $dateMonthend   = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
   $dateMonthbegin = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
} else {
   $lastday        = cal_days_in_month(CAL_GREGORIAN, date("m") - 1, date("Y"));
   $dateMonthend   = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, $lastday, date("Y")));
   $dateMonthbegin = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
}
$endDate = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));


$date = new PluginReportsDateIntervalCriteria($report, '`glpi_tickets`.`closedate`', __('Closing date'));
$date->setStartDate($dateMonthbegin);
$date->setEndDate($dateMonthend);

$type        = new PluginReportsTicketTypeCriteria($report, 'type', __('Type'));
$category    = new PluginReportsTicketCategoryCriteria($report, 'itilcategories_id', __('Category'));
$requesttype = new PluginReportsRequestTypeCriteria($report, 'requesttypes_id', __('Request source'));

//Display criterias form is needed
$report->displayCriteriasForm();

$columns = array('id'                => array('sorton' => 'id'),
                 'date'              => array('sorton' => 'date'),
                 'closedate'         => array('sorton' => 'closedate'),
                 'priority'          => array('sorton' => 'priority'),
                 'type'              => array('sorton' => 'type'),
                 'requesttypes_id'   => array('sorton' => 'requesttypes_id'),
                 'itilcategories_id' => array('sorton' => 'itilcategories_id'),
                 'slts_ttr_id'       => array('sorton' => 'slts_ttr_id'),
);

$output_type = Search::HTML_OUTPUT;

if (isset($_POST['list_limit'])) {
   $_SESSION['glpilist_limit'] = $_POST['list_limit'];
   unset($_POST['list_limit']);
}
if (!isset($_REQUEST['sort'])) {
   $_REQUEST['sort']  = "entity";
   $_REQUEST['order'] = "ASC";
}

$limit = $_SESSION['glpilist_limit'];

if (isset($_POST["display_type"])) {
   $output_type = $_POST["display_type"];
   if ($output_type < 0) {
      $output_type = -$output_type;
      $limit       = 0;
   }
}

//Report title
$title = $report->getFullTitle();

// SQL statement
$query = "SELECT glpi_tickets.*  
               FROM `glpi_tickets`
               WHERE `glpi_tickets`.`status` = '" . Ticket::CLOSED . "'";
$query .= getEntitiesRestrictRequest('AND', "glpi_tickets", '', '', false);
$query .= $date->getSqlCriteriasRestriction();
$query .= $category->getSqlCriteriasRestriction();
$query .= $requesttype->getSqlCriteriasRestriction();

$query .= getOrderBy('closedate', $columns);

$res   = $DB->query($query);
$nbtot = ($res ? $DB->numrows($res) : 0);
if ($limit) {
   $start = (isset($_GET["start"]) ? $_GET["start"] : 0);
   if ($start >= $nbtot) {
      $start = 0;
   }
   if ($start > 0 || $start + $limit < $nbtot) {
      $res = $DB->query($query . " LIMIT $start,$limit");
   }
} else {
   $start = 0;
}

if ($nbtot == 0) {
   if (!$HEADER_LOADED) {
      Html::header($title, $_SERVER['PHP_SELF'], "utils", "report");
      Report::title();
   }
   echo "<div class='center red b'>" . __('No item found') . "</div>";
   Html::footer();
} else if ($output_type == Search::PDF_OUTPUT_PORTRAIT
           || $output_type == Search::PDF_OUTPUT_LANDSCAPE
) {
   include(GLPI_ROOT . "/lib/ezpdf/class.ezpdf.php");
} else if ($output_type == Search::HTML_OUTPUT) {
   if (!$HEADER_LOADED) {
      Html::header($title, $_SERVER['PHP_SELF'], "utils", "report");
      Report::title();
   }

   echo "<div class='center'>";

   echo "<table class='tab_cadre_fixe'>";
   echo "<tr><th>$title</th></tr>\n";

   echo "<tr class='tab_bg_2 center'><td class='center'>";
   echo "<form method='POST' action='" . $_SERVER["PHP_SELF"] . "?start=$start'>\n";

   $param = "";
   foreach ($_POST as $key => $val) {
      if (is_array($val)) {
         foreach ($val as $k => $v) {
            echo "<input type='hidden' name='" . $key . "[$k]' value='$v' >";
            if (!empty($param)) {
               $param .= "&";
            }
            $param .= $key . "[" . $k . "]=" . urlencode($v);
         }
      } else {
         echo "<input type='hidden' name='$key' value='$val' >";
         if (!empty($param)) {
            $param .= "&";
         }
         $param .= "$key=" . urlencode($val);
      }
   }
   Dropdown::showOutputFormat();
   Html::closeForm();
   echo "</td></tr>";
   echo "</table></div>";

   Html::printPager($start, $nbtot, $_SERVER['PHP_SELF'], $param);
}

if ($res && $nbtot > 0) {

   $mylevels = array();
   $restrict = getEntitiesRestrictRequest('', "glpi_plugin_timelineticket_grouplevels", '', '', true);
   $restrict .= "ORDER BY rank";
   $levels = getAllDatasFromTable("glpi_plugin_timelineticket_grouplevels", $restrict);
   if (!empty($levels)) {
      foreach ($levels as $level) {
         $mylevels[$level["name"]] = json_decode($level["groups"], true);
      }
   }

   $nbCols = $DB->num_fields($res);
   $nbrows = $DB->numrows($res);
   $num    = 1;
   $link   = $_SERVER['PHP_SELF'];
   $order  = 'ASC';
   $issort = false;

   echo Search::showHeader($output_type, $nbrows, $nbCols, false);

   echo Search::showNewLine($output_type);
   showTitle($output_type, $num, __('id'), 'id', true);
   showTitle($output_type, $num, __('Opening date'), 'date', true);
   showTitle($output_type, $num, __('Closing date'), 'closedate', true);
   showTitle($output_type, $num, __('Priority'), 'priority', true);
   showTitle($output_type, $num, __('Type'), 'type', true);
   showTitle($output_type, $num, __('Request source'), 'requesttypes_id', true);
   showTitle($output_type, $num, __('Category'), 'itilcategories_id', true);
   showTitle($output_type, $num, __('SLT'), 'slts_ttr_id', true);


   if (!empty($mylevels)) {
      foreach ($mylevels as $key => $val) {
         showTitle($output_type, $num, __('Duration by "in progress"', 'timelineticket') . "&nbsp;" . $key, '', false);
      }
   }
   echo Search::showEndLine($output_type);

   $row_num = 1;
   while ($data = $DB->fetch_assoc($res)) {

      $ticket = new Ticket();
      $ticket->getFromDB($data['id']);

      $timelevels = array();
      if (!empty($mylevels)) {
         foreach ($mylevels as $key => $val) {
            if (is_array($val)) {
               foreach ($val as $group => $groups_id) {

                  $a_details = getDetails($ticket, $groups_id);
                  $a_status = array();
                  foreach ($a_details as $time) {
                     if ($time['Status'] == Ticket::ASSIGNED || $time['Status'] == Ticket::PLANNED) {
                        if (isset($timelevels[$key])) {
                           $timelevels[$key] += ($time['End'] - $time['Start']);
                        } else {
                           $timelevels[$key] = ($time['End'] - $time['Start']);
                        }
                     }

                  }
               }
            }
         }
      }

      $row_num++;
      $num = 1;
      echo Search::showNewLine($output_type);
      echo Search::showItem($output_type, $data['id'], $num, $row_num);
      echo Search::showItem($output_type, Html::convDateTime($data['date']), $num, $row_num);
      echo Search::showItem($output_type, Html::convDateTime($data['closedate']), $num, $row_num);
      echo Search::showItem($output_type, Ticket::getPriorityName($data['priority']), $num, $row_num);
      echo Search::showItem($output_type, Ticket::getTicketTypeName($data['type']), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName('glpi_requesttypes', $data["requesttypes_id"]), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName("glpi_itilcategories", $data["itilcategories_id"]), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName('glpi_slts', $data["slts_ttr_id"]), $num, $row_num);
<commit_msg>Minor fixes on statSpentTimeProcessingByGroup.php<commit_after><?php

/*
   ------------------------------------------------------------------------
   TimelineTicket
   Copyright (C) 2013-2016 by the TimelineTicket Development Team.

   https://github.com/pluginsGLPI/timelineticket
   ------------------------------------------------------------------------

   LICENSE

   This file is part of TimelineTicket project.

   TimelineTicket plugin is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   TimelineTicket plugin is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with TimelineTicket plugin. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   TimelineTicket plugin
   @copyright Copyright (c) 2013-2016 TimelineTicket team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      https://github.com/pluginsGLPI/timelineticket
   @since     2013

   ------------------------------------------------------------------------
 */

//Options for GLPI 0.71 and newer : need slave db to access the report
$USEDBREPLICATE        = 1;
$DBCONNECTION_REQUIRED = 1;

include("../../../../inc/includes.php");

// Instantiate Report with Name
$report = new PluginReportsAutoReport(__("statSpentTimeProcessingByGroup_report_title", "timelineticket"));
//Report's search criterias
$dateYear = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y") - 1));
$lastday  = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));

if (date("d") == $lastday) {
   $dateMonthend   = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
   $dateMonthbegin = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
} else {
   $lastday        = cal_days_in_month(CAL_GREGORIAN, date("m") - 1, date("Y"));
   $dateMonthend   = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, $lastday, date("Y")));
   $dateMonthbegin = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
}
$endDate = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));


$date = new PluginReportsDateIntervalCriteria($report, '`glpi_tickets`.`closedate`', __('Closing date'));
$date->setStartDate($dateMonthbegin);
$date->setEndDate($dateMonthend);

$type        = new PluginReportsTicketTypeCriteria($report, 'type', __('Type'));
$category    = new PluginReportsTicketCategoryCriteria($report, 'itilcategories_id', __('Category'));
$requesttype = new PluginReportsRequestTypeCriteria($report, 'requesttypes_id', __('Request source'));

//Display criterias form is needed
$report->displayCriteriasForm();

$columns = array('id'                => array('sorton' => 'id'),
                 'date'              => array('sorton' => 'date'),
                 'closedate'         => array('sorton' => 'closedate'),
                 'priority'          => array('sorton' => 'priority'),
                 'type'              => array('sorton' => 'type'),
                 'requesttypes_id'   => array('sorton' => 'requesttypes_id'),
                 'itilcategories_id' => array('sorton' => 'itilcategories_id'),
                 'slts_ttr_id'       => array('sorton' => 'slts_ttr_id'),
);

$output_type = Search::HTML_OUTPUT;

if (isset($_POST['list_limit'])) {
   $_SESSION['glpilist_limit'] = $_POST['list_limit'];
   unset($_POST['list_limit']);
}
if (!isset($_REQUEST['sort'])) {
   $_REQUEST['sort']  = "entity";
   $_REQUEST['order'] = "ASC";
}

$limit = $_SESSION['glpilist_limit'];

if (isset($_POST["display_type"])) {
   $output_type = $_POST["display_type"];
   if ($output_type < 0) {
      $output_type = -$output_type;
      $limit       = 0;
   }
}

//Report title
$title = $report->getFullTitle();

// SQL statement
$query = "SELECT glpi_tickets.*  
               FROM `glpi_tickets`
               WHERE `glpi_tickets`.`status` = '" . Ticket::CLOSED . "'";
$query .= getEntitiesRestrictRequest('AND', "glpi_tickets", '', '', false);
$query .= $date->getSqlCriteriasRestriction();
$query .= $category->getSqlCriteriasRestriction();
if (isset($_POST['requesttypes_id']) && $_POST['requesttypes_id'] > 0) {
   $query .= $requesttype->getSqlCriteriasRestriction();
}
$query .= getOrderBy('closedate', $columns);

$res   = $DB->query($query);
$nbtot = ($res ? $DB->numrows($res) : 0);
if ($limit) {
   $start = (isset($_GET["start"]) ? $_GET["start"] : 0);
   if ($start >= $nbtot) {
      $start = 0;
   }
   if ($start > 0 || $start + $limit < $nbtot) {
      $res = $DB->query($query . " LIMIT $start,$limit");
   }
} else {
   $start = 0;
}

if ($nbtot == 0) {
   if (!$HEADER_LOADED) {
      Html::header($title, $_SERVER['PHP_SELF'], "utils", "report");
      Report::title();
   }
   echo "<div class='center red b'>" . __('No item found') . "</div>";
   Html::footer();
} else if ($output_type == Search::PDF_OUTPUT_PORTRAIT
           || $output_type == Search::PDF_OUTPUT_LANDSCAPE
) {
   include(GLPI_ROOT . "/lib/ezpdf/class.ezpdf.php");
} else if ($output_type == Search::HTML_OUTPUT) {
   if (!$HEADER_LOADED) {
      Html::header($title, $_SERVER['PHP_SELF'], "utils", "report");
      Report::title();
   }

   echo "<div class='center'>";

   echo "<table class='tab_cadre_fixe'>";
   echo "<tr><th>$title</th></tr>\n";

   echo "<tr class='tab_bg_2 center'><td class='center'>";
   echo "<form method='POST' action='" . $_SERVER["PHP_SELF"] . "?start=$start'>\n";

   $param = "";
   foreach ($_POST as $key => $val) {
      if (is_array($val)) {
         foreach ($val as $k => $v) {
            echo "<input type='hidden' name='" . $key . "[$k]' value='$v' >";
            if (!empty($param)) {
               $param .= "&";
            }
            $param .= $key . "[" . $k . "]=" . urlencode($v);
         }
      } else {
         echo "<input type='hidden' name='$key' value='$val' >";
         if (!empty($param)) {
            $param .= "&";
         }
         $param .= "$key=" . urlencode($val);
      }
   }
   Dropdown::showOutputFormat();
   Html::closeForm();
   echo "</td></tr>";
   echo "</table></div>";

   Html::printPager($start, $nbtot, $_SERVER['PHP_SELF'], $param);
}

if ($res && $nbtot > 0) {

   $mylevels = array();
   $restrict = getEntitiesRestrictRequest('', "glpi_plugin_timelineticket_grouplevels", '', '', true);
   $restrict .= "ORDER BY rank";
   $levels = getAllDatasFromTable("glpi_plugin_timelineticket_grouplevels", $restrict);
   if (!empty($levels)) {
      foreach ($levels as $level) {
         $mylevels[$level["name"]] = json_decode($level["groups"], true);
      }
   }

   $nbCols = $DB->num_fields($res);
   $nbrows = $DB->numrows($res);
   $num    = 1;
   $link   = $_SERVER['PHP_SELF'];
   $order  = 'ASC';
   $issort = false;

   echo Search::showHeader($output_type, $nbrows, $nbCols, false);

   echo Search::showNewLine($output_type);
   showTitle($output_type, $num, __('id'), 'id', true);
   showTitle($output_type, $num, __('Opening date'), 'date', true);
   showTitle($output_type, $num, __('Closing date'), 'closedate', true);
   showTitle($output_type, $num, __('Priority'), 'priority', true);
   showTitle($output_type, $num, __('Type'), 'type', true);
   showTitle($output_type, $num, __('Request source'), 'requesttypes_id', true);
   showTitle($output_type, $num, __('Category'), 'itilcategories_id', true);
   showTitle($output_type, $num, __('SLT'), 'slts_ttr_id', true);


   if (!empty($mylevels)) {
      foreach ($mylevels as $key => $val) {
         showTitle($output_type, $num, __('Duration by "in progress"', 'timelineticket') . "&nbsp;" . $key, '', false);
      }
   }
   echo Search::showEndLine($output_type);

   $row_num = 1;
   while ($data = $DB->fetch_assoc($res)) {

      $ticket = new Ticket();
      $ticket->getFromDB($data['id']);

      $timelevels = array();
      if (!empty($mylevels)) {
         foreach ($mylevels as $key => $val) {
            if (is_array($val)) {
               foreach ($val as $group => $groups_id) {

                  $a_details = getDetails($ticket, $groups_id);
                  $a_status = array();
                  foreach ($a_details as $time) {
                     if ($time['Status'] == Ticket::ASSIGNED || $time['Status'] == Ticket::PLANNED) {
                        if (isset($timelevels[$key])) {
                           $timelevels[$key] += ($time['End'] - $time['Start']);
                        } else {
                           $timelevels[$key] = ($time['End'] - $time['Start']);
                        }
                     }

                  }
               }
            }
         }
      }

      $row_num++;
      $num = 1;
      echo Search::showNewLine($output_type);
      
      $link = "<a href='".$CFG_GLPI["root_doc"].
                "/front/ticket.form.php?id=".$data["id"]."'>".$data['id']."</a>";
      echo Search::showItem($output_type, $link, $num, $row_num);
      echo Search::showItem($output_type, Html::convDateTime($data['date']), $num, $row_num);
      echo Search::showItem($output_type, Html::convDateTime($data['closedate']), $num, $row_num);
      echo Search::showItem($output_type, Ticket::getPriorityName($data['priority']), $num, $row_num);
      echo Search::showItem($output_type, Ticket::getTicketTypeName($data['type']), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName('glpi_requesttypes', $data["requesttypes_id"]), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName("glpi_itilcategories", $data["itilcategories_id"]), $num, $row_num);
      echo Search::showItem($output_type, Dropdown::getDropdownName('glpi_slts', $data["slts_ttr_id"]), $num, $row_num);

{'fraction_non_alphanumeric': 0.21233388084216814, 'fraction_numerical': 0.006818973669802399, 'mean_word_length': 2.8226788432267886, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 118, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3438078', 'n_tokens_mistral': 6650, 'n_tokens_neox': 6419, 'n_words': 1385}