Skip to content

Commit de473b9

Browse files
committed
#2 [Dashboard] add: digiboard getDigiRiskStatsList
1 parent 061b1ab commit de473b9

File tree

8 files changed

+343
-3
lines changed

8 files changed

+343
-3
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# EditorConfig is awesome: https://editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
charset = utf-8
9+
end_of_line = lf
10+
indent_style = space
11+
indent_size = 2
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true
14+
15+
[*.php]
16+
indent_size = 4
17+
18+
[*.md]
19+
trim_trailing_whitespace = false

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
/.project
1818
# Other
1919
*.back
20-
/.editorconfig
2120
/.gitattributes
2221
node_modules
2322
package-lock.json

class/actions_digiboard.class.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/* Copyright (C) 2024 EVARISK <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* \file class/actions_digiboard.class.php
20+
* \ingroup digiboard
21+
* \brief DigiBoard hook overload
22+
*/
23+
24+
/**
25+
* Class ActionsDigiboard
26+
*/
27+
class ActionsDigiboard
28+
{
29+
/**
30+
* @var DoliDB Database handler
31+
*/
32+
public DoliDB $db;
33+
34+
/**
35+
* @var string Error code (or message)
36+
*/
37+
public string $error = '';
38+
39+
/**
40+
* @var array Errors.
41+
*/
42+
public array $errors = [];
43+
44+
/**
45+
* @var array Hook results. Propagated to $hookmanager->resArray for later reuse
46+
*/
47+
public array $results = [];
48+
49+
/**
50+
* @var string|null String displayed by executeHook() immediately after return
51+
*/
52+
public ?string $resprints;
53+
54+
/**
55+
* Constructor
56+
*
57+
* @param DoliDB $db Database handler
58+
*/
59+
public function __construct(DoliDB $db)
60+
{
61+
$this->db = $db;
62+
}
63+
64+
/**
65+
* Overloading the addHtmlHeader function : replacing the parent's function with the one below
66+
*
67+
* @param array $parameters Hook metadata (context, etc...)
68+
* @return int 0 < on error, 0 on success, 1 to replace standard code
69+
*/
70+
public function addHtmlHeader(array $parameters): int
71+
{
72+
if (strpos($parameters['context'], 'digiboardindex') !== false) {
73+
$resourcesRequired = [
74+
'css' => '/custom/digiriskdolibarr/css/digiriskdolibarr.min.css',
75+
];
76+
77+
$out = '<!-- Includes CSS added by module digiriskdolibarr -->';
78+
$out .= '<link rel="stylesheet" type="text/css" href="' . dol_buildpath($resourcesRequired['css'], 1) . '">';
79+
80+
$this->resprints = $out;
81+
}
82+
83+
return 0; // or return 1 to replace standard code
84+
}
85+
}

class/digiboarddashboard.class.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/* Copyright (C) 2024 EVARISK <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* \file class/digiboarddashboard.class.php
20+
* \ingroup digiboard
21+
* \brief Class file for manage DigiBoardDashboard
22+
*/
23+
24+
/**
25+
* Class for DigiBoardDashboard
26+
*/
27+
class DigiboardDashboard
28+
{
29+
/**
30+
* @var DoliDB Database handler
31+
*/
32+
public DoliDB $db;
33+
34+
/**
35+
* Constructor
36+
*
37+
* @param DoliDB $db Database handler
38+
*/
39+
public function __construct(DoliDB $db)
40+
{
41+
$this->db = $db;
42+
}
43+
44+
/**
45+
* Load dashboard info
46+
*
47+
* @return array
48+
* @throws Exception
49+
*/
50+
public function load_dashboard(): array
51+
{
52+
$array['lists'] = [];
53+
54+
if (isModEnabled('digiriskdolibarr')) {
55+
$getDigiRiskStatsList = $this->getDigiRiskStatsList();
56+
$array['digiriskdolibarr']['lists'] = [$getDigiRiskStatsList];
57+
}
58+
59+
return $array;
60+
}
61+
62+
/**
63+
* Get digirisk stats list with API
64+
*
65+
* @return array Graph datas (label/color/type/title/data etc..)
66+
* @throws Exception
67+
*/
68+
public function getDigiRiskStatsList(): array
69+
{
70+
global $conf, $db, $mc, $langs;
71+
72+
// Graph Title parameters
73+
$array['title'] = $langs->transnoentities('DigiRiskStatsList');
74+
$array['picto'] = $this->picto;
75+
76+
// Graph parameters
77+
$array['type'] = 'list';
78+
$array['labels'] = ['Site', 'Siret', 'RiskAssessmentDocument', 'DelayGenerateDate', 'NbEmployees', 'NbEmployeesInvolved', 'GreyRisk', 'OrangeRisk', 'RedRisk', 'BlackRisk', 'NbPresquAccidents', 'NbAccidents', 'NbAccidentsByEmployees', 'NbAccidentInvestigations', 'WorkStopDays', 'FrequencyIndex', 'FrequencyRate', 'GravityRate'];
79+
80+
require_once __DIR__ . '/../../digiriskdolibarr/class/digiriskdolibarrdocuments/riskassessmentdocument.class.php';
81+
require_once __DIR__ . '/../../digiriskdolibarr/class/evaluator.class.php';
82+
require_once __DIR__ . '/../../digiriskdolibarr/class/digiriskelement.class.php';
83+
require_once __DIR__ . '/../../digiriskdolibarr/class/riskanalysis/risk.class.php';
84+
require_once __DIR__ . '/../../digiriskdolibarr/class/accident.class.php';
85+
require_once __DIR__ . '/../../digiriskdolibarr/class/accidentinvestigation.class.php';
86+
87+
$riskAssessmentDocument = new RiskAssessmentDocument($this->db);
88+
$evaluator = new Evaluator($this->db);
89+
$digiriskElement = new DigiriskElement($this->db);
90+
$risk = new Risk($this->db);
91+
$accident = new Accident($this->db);
92+
$accidentInvestigation = new AccidentInvestigation($this->db);
93+
94+
$riskAssessmentDocument->ismultientitymanaged = 0;
95+
$accident->ismultientitymanaged = 0;
96+
$entities = $mc->getEntitiesList(false, false, true);
97+
98+
$arrayDigiRiskStatsList = [];
99+
$currentEntity[] = 1;
100+
$riskAssessmentCotation = [1 => 'GreyRisk', 2 => 'OrangeRisk', 3 => 'RedRisk', 4 => 'BlackRisk'];
101+
$sharingEntities = $mc->sharings['digiriskstats'];
102+
$sharingEntities = array_unique(array_merge($currentEntity, $sharingEntities));
103+
if (!empty($sharingEntities)) {
104+
foreach ($sharingEntities as $key => $sharingEntity) {
105+
$arrayDigiRiskStatsList[$key]['Site']['value'] = $entities[$sharingEntity];
106+
$arrayDigiRiskStatsList[$key]['Site']['morecss'] = 'left bold';
107+
$arrayDigiRiskStatsList[$key]['Siret']['value'] = dolibarr_get_const($db, 'MAIN_INFO_SIRET', $sharingEntity);
108+
109+
$moreParam['entity'] = $sharingEntity;
110+
$moreParam['filter'] = ' AND t.entity = ' . $sharingEntity;
111+
$arrayGetGenerationDateInfos = $riskAssessmentDocument->getGenerationDateInfos($moreParam);
112+
$arrayDigiRiskStatsList[$key]['RiskAssessmentDocument']['value'] = $arrayGetGenerationDateInfos['lastgeneratedate'] . $arrayGetGenerationDateInfos['moreContent'];
113+
$arrayDigiRiskStatsList[$key]['DelayGenerateDate']['value'] = $arrayGetGenerationDateInfos['delaygeneratedate'];
114+
115+
$employees = $evaluator->getNbEmployees();
116+
$arrayDigiRiskStatsList[$key]['NbEmployees']['value'] = $employees['nbemployees'];
117+
$arrayDigiRiskStatsList[$key]['NbEmployeesInvolved']['value'] = $evaluator->getNbEmployeesInvolved()['nbemployeesinvolved'];
118+
119+
$filter = $digiriskElement->getTrashExclusionSqlFilter();
120+
$filter .= $moreParam['filter'];
121+
$getRisksByCotation = $risk->getRisksByCotation($filter)['data'];
122+
for ($i = 1; $i <= 4; $i++) {
123+
$arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['value'] = $getRisksByCotation[$i];
124+
$arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['morecss'] = 'risk-evaluation-cotation';
125+
$arrayDigiRiskStatsList[$key][$riskAssessmentCotation[$i]]['moreAttr'] = 'data-scale=' . $i . ' style="line-height: 0; border-radius: 0;"';
126+
}
127+
128+
$join = ' LEFT JOIN ' . MAIN_DB_PREFIX . $accident->table_element . ' as a ON a.rowid = t.fk_accident';
129+
$accidentsWithWorkStops = saturne_fetch_all_object_type('AccidentWorkStop', 'DESC', 't.rowid', 0, 0, ['customsql' => 't.entity = ' . $sharingEntity], 'AND', false, false, false, $join);
130+
$accidents = $accident->fetchAll('', '', 0, 0, ['customsql' => ' t.status > ' . Accident::STATUS_DRAFT . ' AND t.entity = ' . $sharingEntity]);
131+
if (empty($accidents) && !is_array($accidents)) {
132+
$accidents = [];
133+
}
134+
if (empty($accidentsWithWorkStops) && !is_array($accidentsWithWorkStops)) {
135+
$accidentsWithWorkStops = [];
136+
}
137+
138+
$arrayDigiRiskStatsList[$key]['NbPresquAccidents']['value'] = $accident->getNbPresquAccidents()['nbpresquaccidents'];
139+
$arrayDigiRiskStatsList[$key]['NbAccidents']['value'] = $accident->getNbAccidents($accidents, $accidentsWithWorkStops)['data']['accidents'];
140+
$arrayDigiRiskStatsList[$key]['NbAccidentsByEmployees']['value'] = $accident->getNbAccidentsByEmployees($accidents, $accidentsWithWorkStops, $employees)['nbaccidentsbyemployees'];
141+
$arrayDigiRiskStatsList[$key]['NbAccidentInvestigations']['value'] = $accident->getNbAccidentInvestigations()['nbaccidentinvestigations'];
142+
$arrayDigiRiskStatsList[$key]['WorkStopDays']['value'] = $accident->getNbWorkstopDays($accidentsWithWorkStops)['nbworkstopdays'];
143+
$arrayDigiRiskStatsList[$key]['FrequencyIndex']['value'] = $accident->getFrequencyIndex($accidentsWithWorkStops, $employees)['frequencyindex'];
144+
$arrayDigiRiskStatsList[$key]['FrequencyRate']['value'] = $accident->getFrequencyRate($employees)['frequencyrate'];
145+
$arrayDigiRiskStatsList[$key]['GravityRate']['value'] = $accident->getGravityRate($employees)['gravityrate'];
146+
}
147+
}
148+
$array['data'] = $arrayDigiRiskStatsList;
149+
150+
return $array;
151+
}
152+
}

class/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
//Silence is golden

core/modules/modDigiBoard.class.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ public function __construct($db)
117117
// Set this to relative path of js file if module must load a js on all pages
118118
'js' => [],
119119
// Set here all hooks context managed by module. To find available hook context, make a "grep -r '>initHooks(' *" on source code. You can also set hook context to 'all'
120-
'hooks' => [],
120+
'hooks' => [
121+
'digiboardindex'
122+
],
121123
// Set this to 1 if features of module are opened to external users
122124
'moduleforexternal' => 0
123125
];
@@ -218,7 +220,21 @@ public function __construct($db)
218220
$r = 0;
219221

220222
// Add here entries to declare new menus
221-
$this->menu[$r++] = [];
223+
$this->menu[$r++] = [
224+
'fk_menu' => 'fk_mainmenu=digiriskdolibarr',
225+
'type' => 'left',
226+
'titre' => $langs->transnoentities('DigiBoard'),
227+
'prefix' => '<i class="fas fa-chart-bar pictofixedwidth"></i>',
228+
'mainmenu' => 'digiriskdolibarr',
229+
'leftmenu' => 'digiboardconfig',
230+
'url' => '/digiboard/digiboardindex.php',
231+
'langs' => 'digiboard@digiboard',
232+
'position' => 10 + $r,
233+
'enabled' => '$conf->digiboard->enabled && $conf->digiriskdolibarr->enabled',
234+
'perms' => '$user->rights->digiboard->read && $user->rights->digiriskdolibarr->read ',
235+
'target' => '',
236+
'user' => 0,
237+
];
222238
}
223239

224240
/**
@@ -242,6 +258,34 @@ public function init($options = ''): int
242258
dolibarr_set_const($this->db, 'DIGIBOARD_VERSION', $this->version, 'chaine', 0, '', $conf->entity);
243259
dolibarr_set_const($this->db, 'DIGIBOARD_DB_VERSION', $this->version, 'chaine', 0, '', $conf->entity);
244260

261+
$params = [
262+
'digiboard' => [ // nom informatif du module externe qui apporte ses paramètres
263+
'sharingelements' => [ // section des paramètres 'element' et 'object'
264+
'digiriskstats' => [ // Valeur utilisée dans getEntity()
265+
'type' => 'element', // element: partage d'éléments principaux (thirdparty, product, member, etc...)
266+
'icon' => 'chart-bar', // Font Awesome icon
267+
'lang' => 'digiboard@digiboard', // Fichier de langue contenant les traductions
268+
'tooltip' => 'DigiriskStatsSharedTooltip', // Message Tooltip (ne pas mettre cette clé si pas de tooltip)
269+
'enable' => '!empty($conf->digiboard->enabled) && !empty($conf->digiriskdolibarr->enabled)', // Conditions d'activation du partage
270+
'input' => [ // input : Paramétrage de la réaction du bouton on/off
271+
'global' => [ // global : réaction lorsqu'on désactive l'option de partage global
272+
'showhide' => true, // showhide : afficher/cacher le bloc de partage lors de l'activation/désactivation du partage global
273+
'hide' => true, // hide : cache le bloc de partage lors de la désactivation du partage global
274+
'del' => true // del : suppression de la constante du partage lors de la désactivation du partage global
275+
]
276+
]
277+
]
278+
],
279+
'sharingmodulename' => [ // correspondance des noms de modules pour le lien parent ou compatibilité (ex: 'productsupplierprice' => 'product')
280+
'digiriskstats' => 'digiboard',
281+
]
282+
]
283+
];
284+
285+
$externalModule = json_decode(getDolGlobalString('MULTICOMPANY_EXTERNAL_MODULES_SHARING'), true);
286+
$externalModule = !empty(getDolGlobalString('MULTICOMPANY_EXTERNAL_MODULES_SHARING')) ? array_merge($externalModule, $params) : $params;
287+
dolibarr_set_const($this->db, 'MULTICOMPANY_EXTERNAL_MODULES_SHARING', json_encode($externalModule), 'json', 0, '', 0);
288+
245289
return $this->_init($sql, $options);
246290
}
247291

digiboardindex.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/* Copyright (C) 2024 EVARISK <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
/**
19+
* \file digiboardindex.php
20+
* \ingroup digiboard
21+
* \brief Home page of digiboard top menu
22+
*/
23+
24+
// Load DigiBoard environment
25+
if (file_exists('digiboard.main.inc.php')) {
26+
require_once __DIR__ . '/digiboard.main.inc.php';
27+
} elseif (file_exists('../digiboard.main.inc.php')) {
28+
require_once __DIR__ . '/../digiboard.main.inc.php';
29+
} else {
30+
die('Include of digiboard main fails');
31+
}
32+
33+
require_once __DIR__ . '/../saturne/core/tpl/index/index_view.tpl.php';

langs/fr_FR/digiboard.lang

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ ModuleDigiBoardDesc = Tableau de bord pour les applications DigiRisk, DigiQ
2323
DigiBoardDescription = Tableau de bord pour les applications DigiRisk, DigiQuali et bien plus ...
2424
DigiBoardDescriptionLong = Tableau de bord pour les applications DigiRisk, DigiQuali et bien plus ...
2525
EnableDigiBoard = Veuillez activer le module DigiBoard pour accéder à cette page
26+
27+
ShareDigiriskstats = Activer le partage des statistiques de DigiRisk
28+
DigiriskStatsSharedTooltip = Partagez vos statistiques avec les autres entités de DigiRisk
29+
DIGIRISKSTATSSharing = Partage des statistiques DigiRisk
30+
DIGIRISKSTATSSharingDescription = Sélection des entités qui partageront leurs statistiques DigiRisk avec cette entité.
31+
DigiRiskStatsList = Statistiques DigiRisk

0 commit comments

Comments
 (0)