Skip to content

Commit

Permalink
#91[Dashboard] add: timespent indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Jan 13, 2023
1 parent 42036e1 commit 420587b
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 5 deletions.
190 changes: 190 additions & 0 deletions class/dashboarddolisirhstats.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php
/* Copyright (C) 2022 EOXIA <dev@eoxia.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* \file class/dashboarddolisirhstats.class.php
* \ingroup dolisirh
* \brief Fichier de la classe de gestion des stats du dashboard
*/

require_once DOL_DOCUMENT_ROOT . '/core/class/dolgraph.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/class/html.form.class.php';

require_once __DIR__ . '/dolisirhstats.class.php';

/**
* Class to manage stats for dashboard
*/
class DashboardDoliSIRHStats extends DoliSIRHStats
{
const DASHBOARD_TIMESPENT = 0;

/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}

/**
* Load dashboard info.
*
* @param int $load_timespent Load dashboard timespent info
*
* @return array
* @throws Exception
*/
public function load_dashboard($load_timespent = 1)
{
$dolisirh_dashboard = DoliSIRHStats::load_dashboard();

$dashboard_data = array(
'widgets' => array(
'timespent' => ($load_timespent) ? $dolisirh_dashboard['widgets'] : array()
),
// 'graphs' => array(
// 'risk' => ($load_risk) ? $risk_dashboard['graphs'] : array(),
// 'task' => ($load_task) ? $digirisktask_dashboard['graphs'] : array(),
// 'accident' => ($load_accident) ? $accident_dashboard['graphs'] : array()
// )
);

return $dashboard_data;
}

/**
* Show dashboard.
*
* @return void
* @throws Exception
*/
public function show_dashboard($load_timespent = 1)
{
global $conf, $form, $langs, $user;

$WIDTH = DolGraph::getDefaultGraphSizeForStats('width');
$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height');

$dashboard_data = $this->load_dashboard($load_timespent);

print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '" class="dashboard" id="dashBoardForm">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="view">';

$disableWidgetList = json_decode($user->conf->DOLISIRH_DISABLED_DASHBOARD_INFO);

if (!empty($dashboard_data['widgets'])) {
foreach ($dashboard_data['widgets'] as $dashboardLine) {
foreach ($dashboardLine as $key => $dashboardLinesingle) {
if (isset($disableWidgetList->$key) && $disableWidgetList->$key == 0) {
$dashboardLinesArray[$key] = $dashboardLinesingle['widgetName'];
}
}
}
}

print '<div class="add-widget-box" style="' . (!empty((array)$disableWidgetList) ? '' : 'display:none') . '">';
print Form::selectarray('boxcombo', $dashboardLinesArray, -1, $langs->trans("ChooseBoxToAdd") . '...', 0, 0, '', 1, 0, 0, 'DESC', 'maxwidth150onsmartphone hideonprint add-dashboard-widget', 0, 'hidden selected', 0, 1);
if (!empty($conf->use_javascript_ajax)) {
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
print ajax_combobox("boxcombo");
}
print '</div>';
print '<div class="fichecenter">';

if (!empty($dashboard_data['widgets'])) {
$openedDashBoard = '';
foreach ($dashboard_data['widgets'] as $dashboardLine) {
foreach ($dashboardLine as $key => $dashboardLinesingle) {
if (!isset($disableWidgetList->$key) && is_array($dashboardLinesingle) && !empty($dashboardLinesingle)) {
$openedDashBoard .= '<div class="box-flex-item"><div class="box-flex-item-with-margin">';
$openedDashBoard .= '<div class="info-box info-box-sm">';
$openedDashBoard .= '<span class="info-box-icon">';
$openedDashBoard .= '<i class="' . $dashboardLinesingle["picto"] . '"></i>';
$openedDashBoard .= '</span>';
$openedDashBoard .= '<div class="info-box-content">';
$openedDashBoard .= '<div class="info-box-title" title="' . $langs->trans("Close") . '">';
$openedDashBoard .= '<span class="close-dashboard-widget" data-widgetname="' . $key . '"><i class="fas fa-times"></i></span>';
$openedDashBoard .= '</div>';
$openedDashBoard .= '<div class="info-box-lines">';
$openedDashBoard .= '<div class="info-box-line" style="font-size : 20px;">';
for ($i = 0; $i < count($dashboardLinesingle['label']); $i++) {
$openedDashBoard .= '<span class=""><strong>' . $dashboardLinesingle["label"][$i] . ' : ' . '</strong>';
$openedDashBoard .= '<span class="classfortooltip badge badge-info" title="' . $dashboardLinesingle["label"][$i] . ' : ' . $dashboardLinesingle["content"][$i] . '" >' . $dashboardLinesingle["content"][$i] . '</span>';
$openedDashBoard .= (!empty($dashboardLinesingle["tooltip"][$i]) ? $form->textwithpicto('', $langs->transnoentities($dashboardLinesingle["tooltip"][$i])) : '') . '</span>';
$openedDashBoard .= '<br>';
}
$openedDashBoard .= '</div>';
$openedDashBoard .= '</div><!-- /.info-box-lines --></div><!-- /.info-box-content -->';
$openedDashBoard .= '</div><!-- /.info-box -->';
$openedDashBoard .= '</div><!-- /.box-flex-item-with-margin -->';
$openedDashBoard .= '</div>';
}
}
}
print '<div class="opened-dash-board-wrap"><div class="box-flex-container">' . $openedDashBoard . '</div></div>';
}

print '<div class="box-flex-container">';

if (is_array($dashboard_data['graphs']) && !empty($dashboard_data['graphs'])) {
foreach ($dashboard_data['graphs'] as $keyelement => $datagraph) {
$nbdata = 0;
if (is_array($datagraph['data']) && !empty($datagraph['data'])) {
foreach ($datagraph['data'] as $datagraphsingle) {
$nbdata += $datagraphsingle;
}
if ($nbdata > 0) {
$arraykeys = array_keys($datagraph['data']);
foreach ($arraykeys as $key) {
$data[$keyelement][] = array(
0 => $langs->trans($datagraph['labels'][$key]['label']),
1 => $datagraph['data'][$key]
);
$datacolor[$keyelement][] = $langs->trans($datagraph['labels'][$key]['color']);
}

$filename[$keyelement] = $keyelement . '.png';
$fileurl[$keyelement] = DOL_URL_ROOT . '/viewimage.php?modulepart=dolisirh&file=' . $keyelement . '.png';

$graph = new DolGraph();
$graph->SetData($data[$keyelement]);
$graph->SetDataColor($datacolor[$keyelement]);
$graph->SetType(array('pie'));
$graph->SetWidth($WIDTH);
$graph->SetHeight($HEIGHT);
$graph->setShowLegend(2);
$graph->draw($filename[$keyelement], $fileurl[$keyelement]);
print '<div class="box-flex-item">';
print '<div class="titre inline-block">';
print $datagraph['picto'] . ' ' . $datagraph['title'];
print '</div>';
print $graph->show();
print '</div>';
}
}
}
}

print '</div></div></div>';
print '</form>';
}
}

88 changes: 88 additions & 0 deletions class/dolisirhstats.php → class/dolisirhstats.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,92 @@ protected function _getAmountByYear(string $sql): array
}
return $result;
}

/**
* Load dashboard info dolisirh
*
* @return array
* @throws Exception
*/
public function load_dashboard()
{
global $langs;

$timeSpendingInfos = $this->getTimeSpendingInfos();

$array['widgets'] = array(
0 => array(
'label' => array($timeSpendingInfos['planned']['label'], $timeSpendingInfos['passed']['label'], $timeSpendingInfos['spent']['label'], $timeSpendingInfos['difference']['label']),
'content' => array($timeSpendingInfos['planned']['content'], $timeSpendingInfos['passed']['content'], $timeSpendingInfos['spent']['content'], $timeSpendingInfos['difference']['content']),
'picto' => 'fas fa-clock',
'widgetName' => $langs->transnoentities('TimeSpent')
),
);

return $array;
}

/**
* Get all timespent infos.
*
* @return array
* @throws Exception
*/
public function getTimeSpendingInfos()
{
require_once __DIR__ . '/../lib/dolisirh_function.lib.php';

global $db, $langs, $user;

$firstdaytoshow = dol_get_first_day(date('Y'), date('m'));
$lastdayofmonth = strtotime(date('Y-m-t', $firstdaytoshow));

$currentMonth = date('m', dol_now());
if ($currentMonth == date('m')) {
$lastdaytoshow = dol_now();
} else {
$lastdaytoshow = $lastdayofmonth;
}

$daysInMonth = num_between_day($firstdaytoshow, $lastdayofmonth, 1);

$isavailable = array();
for ($idw = 0; $idw < $daysInMonth; $idw++) {
$dayInLoop = dol_time_plus_duree($firstdaytoshow, $idw, 'd');
if (isDayAvailable($dayInLoop, $user->id)) {
$isavailable[$dayInLoop] = array('morning'=>1, 'afternoon'=>1);
} else if (date('N', $dayInLoop) >= 6) {
$isavailable[$dayInLoop] = array('morning'=>false, 'afternoon'=>false, 'morning_reason'=>'week_end', 'afternoon_reason'=>'week_end');
} else {
$isavailable[$dayInLoop] = array('morning'=>false, 'afternoon'=>false, 'morning_reason'=>'public_holiday', 'afternoon_reason'=>'public_holiday');
}
}

$workinghours = new Workinghours($db);
$workingHours = $workinghours->fetchCurrentWorkingHours($user->id, 'user');

$timeSpendingInfos = loadTimeSpendingInfosWithinRange($firstdaytoshow, dol_time_plus_duree($lastdaytoshow, 1, 'd'), $workingHours, $isavailable, $user->id);

// Planned working time
$planned_working_time = loadPlannedTimeWithinRange($firstdaytoshow, dol_time_plus_duree($lastdayofmonth, 1, 'd'), $workingHours, $isavailable);
$array['planned']['label'] = $langs->trans("Total") . ' - ' . $langs->trans('ExpectedWorkedHoursMonth', dol_print_date(dol_mktime(0, 0, 0, date('m'), date('d'), date('Y')), '%B %Y'));
$array['planned']['content'] = (($planned_working_time['minutes'] != 0) ? convertSecondToTime($planned_working_time['minutes'] * 60, 'allhourmin') : '00:00');

// Hours passed
$passed_working_time = $timeSpendingInfos['passed'];
$array['passed']['label'] = $langs->trans("Total") . ' - ' . $langs->trans('SpentWorkedHoursMonth', dol_print_date($firstdaytoshow, 'dayreduceformat'), dol_print_date($lastdaytoshow, 'dayreduceformat'));
$array['passed']['content'] = (($passed_working_time['minutes'] != 0) ? convertSecondToTime($passed_working_time['minutes'] * 60, 'allhourmin') : '00:00');

//Worked hours
$worked_time = $timeSpendingInfos['spent'];
$array['spent']['label'] = $langs->trans("Total") . ' - ' . $langs->trans('ConsumedWorkedHoursMonth', dol_print_date($firstdaytoshow, 'dayreduceformat'), dol_print_date($lastdaytoshow, 'dayreduceformat'));
$array['spent']['content'] = convertSecondToTime($worked_time['total'], 'allhourmin');

//Difference between passed and worked hours
$difftotaltime = $timeSpendingInfos['difference'] * 60;
$array['difference']['label'] = $langs->trans('Total') . ' - ' . $langs->trans('DiffSpentAndConsumedWorkedHoursMonth', dol_print_date($firstdaytoshow, 'dayreduceformat'), dol_print_date($lastdaytoshow, 'dayreduceformat'));
$array['difference']['content'] = (($difftotaltime != 0) ? convertSecondToTime(abs($difftotaltime), 'allhourmin') : '00:00');

return $array;
}
}
2 changes: 1 addition & 1 deletion class/facturerecstats.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* \ingroup dolisirh
* \brief Recurring invoice class to manage statistics reports
*/
include_once DOL_DOCUMENT_ROOT.'/custom/dolisirh/class/dolisirhstats.php';
include_once DOL_DOCUMENT_ROOT . '/custom/dolisirh/class/dolisirhstats.class.php';
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
//include_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
Expand Down
40 changes: 40 additions & 0 deletions core/tpl/dolisirh_dashboard.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* Actions
*/

if ($action == 'adddashboardinfo') {
$data = json_decode(file_get_contents('php://input'), true);

$dashboardWidgetName = $data['dashboardWidgetName'];

$visible = json_decode($user->conf->DOLISIRH_DISABLED_DASHBOARD_INFO);
unset($visible->$dashboardWidgetName);

$tabparam['DOLISIRH_DISABLED_DASHBOARD_INFO'] = json_encode($visible);

dol_set_user_param($db, $conf, $user, $tabparam);
$action = '';
}

if ($action == 'closedashboardinfo') {
$data = json_decode(file_get_contents('php://input'), true);

$dashboardWidgetName = $data['dashboardWidgetName'];


$visible = json_decode($user->conf->DOLISIRH_DISABLED_DASHBOARD_INFO);
$visible->$dashboardWidgetName = 0;

$tabparam['DOLISIRH_DISABLED_DASHBOARD_INFO'] = json_encode($visible);


dol_set_user_param($db, $conf, $user, $tabparam);
$action = '';
}

/*
* View
*/

$stats->show_dashboard();
10 changes: 10 additions & 0 deletions dolisirhindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
}

// Libraries
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';

require_once __DIR__ . '/core/modules/modDoliSIRH.class.php';
require_once __DIR__ . '/class/dashboarddolisirhstats.class.php';

// Global variables definitions
global $conf, $db, $langs, $user;
Expand All @@ -39,6 +42,10 @@

// Initialize technical objects
$dolisirh = new modDoliSIRH($db);
$stats = new DashboardDolisirhStats($db);

// Get parameters
$action = GETPOST('action', 'alpha');

// Security check
if (!$user->rights->dolisirh->lire) accessforbidden();
Expand All @@ -64,6 +71,9 @@
</div>
</div>
<?php endif;

require_once __DIR__ . '/core/tpl/dolisirh_dashboard.tpl.php';

// End of page
llxFooter();
$db->close();
Loading

0 comments on commit 420587b

Please sign in to comment.