box_graph_ticket_by_severity.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /* Module descriptor for ticket system
  3. * Copyright (C) 2013-2016 Jean-François FERRY <hello@librethic.io>
  4. * 2016 Christophe Battarel <christophe@altairis.fr>
  5. * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program 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. * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/boxes/box_graph_ticket_by_severity.php
  22. * \ingroup ticket
  23. * \brief This box shows open tickets by severity
  24. */
  25. require_once DOL_DOCUMENT_ROOT."/core/boxes/modules_boxes.php";
  26. /**
  27. * Class to manage the box
  28. */
  29. class box_graph_ticket_by_severity extends ModeleBoxes
  30. {
  31. public $boxcode = "box_ticket_by_severity";
  32. public $boximg = "ticket";
  33. public $boxlabel;
  34. public $depends = array("ticket");
  35. /**
  36. * @var DoliDB Database handler.
  37. */
  38. public $db;
  39. public $param;
  40. public $info_box_head = array();
  41. public $info_box_contents = array();
  42. public $widgettype = 'graph';
  43. /**
  44. * Constructor
  45. * @param DoliDB $db Database handler
  46. * @param string $param More parameters
  47. */
  48. public function __construct($db, $param = '')
  49. {
  50. global $langs;
  51. $langs->load("boxes");
  52. $this->db = $db;
  53. $this->boxlabel = $langs->transnoentitiesnoconv("BoxTicketSeverity");
  54. }
  55. /**
  56. * Load data into info_box_contents array to show array later.
  57. *
  58. * @param int $max Maximum number of records to load
  59. * @return void
  60. */
  61. public function loadBox($max = 5)
  62. {
  63. global $conf, $user, $langs;
  64. $badgeStatus0 = '#cbd3d3'; // draft
  65. $badgeStatus1 = '#bc9526'; // validated
  66. $badgeStatus1b = '#bc9526'; // validated
  67. $badgeStatus2 = '#9c9c26'; // approved
  68. $badgeStatus3 = '#bca52b';
  69. $badgeStatus4 = '#25a580'; // Color ok
  70. $badgeStatus4b = '#25a580'; // Color ok
  71. $badgeStatus5 = '#cad2d2';
  72. $badgeStatus6 = '#cad2d2';
  73. $badgeStatus7 = '#baa32b';
  74. $badgeStatus8 = '#993013';
  75. $badgeStatus9 = '#e7f0f0';
  76. if (file_exists(DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php')) {
  77. include DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
  78. }
  79. $this->max = $max;
  80. require_once DOL_DOCUMENT_ROOT."/ticket/class/ticket.class.php";
  81. $text = $langs->trans("BoxTicketSeverity", $max);
  82. $this->info_box_head = array(
  83. 'text' => $text,
  84. 'limit' => dol_strlen($text)
  85. );
  86. $listofopplabel = array();
  87. $listofoppcode = array();
  88. $colorseriesstat = array();
  89. if ($user->hasRight('ticket', 'read')) {
  90. $sql = "SELECT cts.rowid, cts.label, cts.code";
  91. $sql .= " FROM " . MAIN_DB_PREFIX . "c_ticket_severity as cts";
  92. $sql .= " WHERE cts.active = 1";
  93. $sql .= $this->db->order('cts.rowid', 'ASC');
  94. $resql = $this->db->query($sql);
  95. if ($resql) {
  96. $num = $this->db->num_rows($resql);
  97. $i = 0;
  98. while ($i < $num) {
  99. $objp = $this->db->fetch_object($resql);
  100. $listofoppcode[$objp->rowid] = $objp->code;
  101. $listofopplabel[$objp->rowid] = $objp->label;
  102. switch ($objp->code) {
  103. case 'LOW':
  104. $colorseriesstat[$objp->rowid] = $badgeStatus4;
  105. break;
  106. case 'NORMAL':
  107. $colorseriesstat[$objp->rowid] = $badgeStatus2;
  108. break;
  109. case 'HIGH':
  110. $colorseriesstat[$objp->rowid] = $badgeStatus1;
  111. break;
  112. case 'BLOCKING':
  113. $colorseriesstat[$objp->rowid] = $badgeStatus8;
  114. break;
  115. default:
  116. break;
  117. }
  118. $i++;
  119. }
  120. } else {
  121. dol_print_error($this->db);
  122. }
  123. $dataseries = array();
  124. $data = array();
  125. $sql = "SELECT t.severity_code, COUNT(t.severity_code) as nb";
  126. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket as t";
  127. $sql .= " WHERE t.fk_statut <> 8";
  128. $sql .= " GROUP BY t.severity_code";
  129. $resql = $this->db->query($sql);
  130. if ($resql) {
  131. $num = $this->db->num_rows($resql);
  132. $i = 0;
  133. while ($i < $num) {
  134. $objp = $this->db->fetch_object($resql);
  135. $data[$objp->severity_code] = $objp->nb;
  136. $i++;
  137. }
  138. foreach ($listofoppcode as $rowid => $code) {
  139. $dataseries[] = array(
  140. 'label' => $langs->getLabelFromKey($this->db, 'TicketSeverityShort' . $code, 'c_ticket_severity', 'code', 'label', $code),
  141. 'data' => (empty($data[$code]) ? 0 : $data[$code])
  142. );
  143. }
  144. } else {
  145. dol_print_error($this->db);
  146. }
  147. $stringtoprint = '';
  148. $stringtoprint .= '<div class="div-table-responsive-no-min ">';
  149. if (!empty($dataseries) && count($dataseries) > 0) {
  150. include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  151. $px1 = new DolGraph();
  152. $mesg = $px1->isGraphKo();
  153. $totalnb = 0;
  154. if (!$mesg) {
  155. //$px1->SetDataColor(array_values($colorseriesstat));
  156. $data = array();
  157. $legend = array();
  158. foreach ($dataseries as $value) {
  159. $data[] = array($value['label'], $value['data']);
  160. $totalnb += $value['data'];
  161. }
  162. $px1->SetData($data);
  163. $px1->setShowLegend(0);
  164. $px1->SetType(array('bars'));
  165. $px1->SetLegend($legend);
  166. $px1->SetMaxValue($px1->GetCeilMaxValue());
  167. //$px1->SetHeight($HEIGHT);
  168. $px1->SetShading(3);
  169. $px1->SetHorizTickIncrement(1);
  170. $px1->SetCssPrefix("cssboxes");
  171. $px1->mode = 'depth';
  172. $px1->draw('idgraphticketseverity');
  173. $stringtoprint .= $px1->show($totalnb ? 0 : 1);
  174. }
  175. $stringtoprint .= '</div>';
  176. $this->info_box_contents[][]=array(
  177. 'td' => 'class="center"',
  178. 'text' => $stringtoprint
  179. );
  180. } else {
  181. $this->info_box_contents[0][0] = array(
  182. 'td' => '',
  183. 'text' => '<span class="opacitymedium">'.$langs->trans("BoxNoTicketSeverity").'</span>'
  184. );
  185. }
  186. } else {
  187. $this->info_box_contents[0][0] = array(
  188. 'td' => '',
  189. 'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>',
  190. );
  191. }
  192. }
  193. /**
  194. * Method to show box
  195. *
  196. * @param array $head Array with properties of box title
  197. * @param array $contents Array with properties of box lines
  198. * @param int $nooutput No print, only return string
  199. * @return string
  200. */
  201. public function showBox($head = null, $contents = null, $nooutput = 0)
  202. {
  203. return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
  204. }
  205. }