Skip to content

Commit

Permalink
Evarisk#3785 [Risk] add: list of risks vy danger categories for gp an…
Browse files Browse the repository at this point in the history
…d also clean code by adding function
  • Loading branch information
evarisk-charles authored and evarisk-micka committed Apr 24, 2024
1 parent e77e36a commit 47c5ff2
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions class/riskanalysis/risk.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,15 @@ public function load_dashboard(): array

if (preg_match('/digiriskelement_informations/', $urlTab)) {
$arrayRisksByDangerCategories = $this->getRisksByDangerCategories('single');
$arrayGetDataList = $this->getDataList();
} else {
$arrayRisksByDangerCategories = $this->getRisksByDangerCategories();
$arrayTotalRisksByDangerCategories = $this->getTotalRisksByDangerCategories();
$getRiskListsByDangerCategories = $this->getRiskListsByDangerCategories();
}

$array['graphs'] = [$arrayRisksByCotation, $arrayRisksByDangerCategories, $arrayTotalRisksByDangerCategories ?? []];
$array['lists'] = [$getRiskListsByDangerCategories];
$array['lists'] = [$getRiskListsByDangerCategories, $arrayGetDataList];

return $array;
}
Expand Down Expand Up @@ -707,7 +708,7 @@ public function getTotalRisksByDangerCategories(): array
}

/**
* Get risk lists by danger categories
* Get list of risks by danger categories
*
* @return array
* @throws Exception
Expand Down Expand Up @@ -741,7 +742,7 @@ public function getRiskListsByDangerCategories() : array

$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['value'] = is_array($risks) && !empty($risks) ? count($risks) : 0;
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['morecss'] = 'risk-evaluation-cotation';
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['moreAttr'] = 'style="line-height: 0; border-radius: 0; background-color: #A1467EAA; color: #FFF;"';
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['moreAttr'] = '"style="line-height: 0; border-radius: 0; background-color: #A1467EAA; color: #FFF;"';
$arrayRiskLists[$dangerCategory['position']]['percentage']['value'] = is_array($risks) && !empty($risks) ? price2num((count($risks) / count($totalRisks)) * 100, 2) . ' %' : 0;

for ($i = 1; $i <= 4; $i++) {
Expand All @@ -750,10 +751,60 @@ public function getRiskListsByDangerCategories() : array
$cotationStart = $this->cotations[$i]['start'];
$cotationEnd = $this->cotations[$i]['end'];
$riskAssessments = saturne_fetch_all_object_type('RiskAssessment', '', '', 0, 0, ['customsql' => 't.status = ' . RiskAssessment::STATUS_VALIDATED . ' AND r.category = ' . $dangerCategory['position'] . ' AND t.cotation >= ' . $cotationStart . ' AND t.cotation <= ' . $cotationEnd], 'AND', false, true, false, $join);
$arrayRiskLists = $this->getValueForRisks($riskAssessments, $arrayRiskLists, $dangerCategory['position'], $i);
}
}
}

$array['data'] = $arrayRiskLists;

return $array;
}

/**
* Get list of risks by danger categories for GP
*
* @return array
* @throws Exception
*/
public function getDataList() : array
{
global $langs;

$id = GETPOST('id');
$filter = 't.fk_element = ' . $id . ' AND ';

// Graph Title parameters
$array['title'] = $langs->transnoentities('RiskListsByDangerCategories');
$array['picto'] = $this->picto;

// Graph parameters
$array['width'] = '100%';
$array['type'] = 'list';

$totalRisks = $this->fetchAll('', '', 0, 0, ['customsql' => $filter . 't.status = ' . self::STATUS_VALIDATED]);

$array['labels']['Ref'] = $langs->transnoentities('DangerCategories');
$array['labels']['numberOfRisks'] = $langs->transnoentities('NumberOfRisks') . ' : ' . '<span class="badge badge-info">' . (is_array($totalRisks) && !empty($totalRisks) ? count($totalRisks) : 0) . '</span>';
$array['labels']['percentage'] = $langs->transnoentities('Percentage');

$arrayRiskLists[$dangerCategory['position']][$i]['value'] = is_array($riskAssessments) && !empty($riskAssessments) ? count($riskAssessments) : 0;
$arrayRiskLists[$dangerCategory['position']][$i]['morecss'] = 'risk-evaluation-cotation';
$arrayRiskLists[$dangerCategory['position']][$i]['moreAttr'] = 'data-scale = ' . $i . ' style="line-height: 0; border-radius: 0;"';
$arrayRiskLists = [];
$join = ' LEFT JOIN ' . MAIN_DB_PREFIX . $this->table_element . ' as r ON r.rowid = t.fk_risk';
$dangerCategories = $this->getDangerCategories();
if (is_array($dangerCategories) && !empty($dangerCategories) && is_array($totalRisks) && !empty($totalRisks)) {
foreach ($dangerCategories as $dangerCategory) {
$arrayRiskLists[$dangerCategory['position']]['Ref']['value'] = $dangerCategory['name'];
$risks = $this->fetchAll('', '', 0, 0, ['customsql' => $filter .'t.status = ' . self::STATUS_VALIDATED . ' AND t.category = ' . $dangerCategory['position']]);
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['value'] = is_array($risks) && !empty($risks) ? count($risks) : 0;
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['morecss'] = 'risk-evaluation-cotation';
$arrayRiskLists[$dangerCategory['position']]['numberOfRisks']['moreAttr'] = 'style="line-height: 0; border-radius: 0; background-color: #A1467EAA; color: #FFF;"';
$arrayRiskLists[$dangerCategory['position']]['percentage']['value'] = is_array($risks) && !empty($risks) ? price2num((count($risks) / count($totalRisks)) * 100, 2) . ' %' : 0;
for ($i = 1; $i <= 4; $i++) {
$array['labels'][$i] = $this->cotations[$i]['label'];
$cotationStart = $this->cotations[$i]['start'];
$cotationEnd = $this->cotations[$i]['end'];
$riskAssessments = saturne_fetch_all_object_type('RiskAssessment', '', '', 0, 0, ['customsql' => 'r.fk_element = ' . $id . ' AND t.status = ' . RiskAssessment::STATUS_VALIDATED . ' AND r.category = ' . $dangerCategory['position'] . ' AND t.cotation >= ' . $cotationStart . ' AND t.cotation <= ' . $cotationEnd], 'AND', false, true, false, $join);
$arrayRiskLists = $this->getValueForRisks($riskAssessments, $arrayRiskLists, $dangerCategory['position'], $i);
}
}
}
Expand Down Expand Up @@ -788,4 +839,19 @@ public function getTriggerDescription(SaturneObject $object): string

return $ret;
}

/**
* @param $riskAssessments
* @param array $arrayRiskLists
* @param $position
* @param int $i
* @return array
*/
public function getValueForRisks($riskAssessments, array $arrayRiskLists, $position, int $i): array
{
$arrayRiskLists[$position][$i]['value'] = is_array($riskAssessments) && !empty($riskAssessments) ? count($riskAssessments) : 0;
$arrayRiskLists[$position][$i]['morecss'] = 'risk-evaluation-cotation';
$arrayRiskLists[$position][$i]['moreAttr'] = 'data-scale = ' . $i . ' style="line-height: 0; border-radius: 0;"';
return $arrayRiskLists;
}
}

0 comments on commit 47c5ff2

Please sign in to comment.