stats.ctp 7.81 KB
<?php


//use SebastianBergmann\CodeCoverage\Report\PHP;

/*
$avg['CREATED'] = 10;
$avg['VALIDATED'] = 7;
$avg['ARCHIVED'] = 1;

$tot['CREATED'] = 100;
$tot['VALIDATED'] = 70;
$tot['TOBEARCHIVED'] = 10;
$tot['ARCHIVED'] = 10;

$year = 2020;

$years = [];
for ($y=$year ; $y>$year-5 ; $y--) {
    $years[$y]['created'] = 8;
    $years[$y]['validated'] = 8;
    $years[$y]['archived'] = 8;
}

$suivis = [];
$prets = [];
$suivis['tot'] = 100;
$suivis['avg'] = 10;
for ($y=$current_year ; $y>=$year_min ; $y--) $suivis[$y] = 8;
$prets = $suivis;
*/



// Parameters passed by controller
$current_year = $current_year;
$year_min = $year_min;
$nbyears = $nbyears;
$avg = $avg;
$tot = $tot;
//$tot2 = $tot2;
$years = $years;
//debug($years);
//debug($tot);





function echoAsPctAndBold($nbelem, $total=null, $is_bold=false) {
    if ($is_bold) echo '<b>';
    //if ($total !== null && $total>0) {
    if ($total !== null) {
        // Eviter division par zero
        if ($nbelem==0) $total=1;
        $pct = round( ($nbelem / $total) * 100 , 1 );
        $nbelem = "$pct% ($nbelem)";
        //$nbelem = "$nbelem ($pct%)";
        //$nbelem = "$pct%";
    }   echo $nbelem;
    if ($is_bold) echo '</b>';
}


// LIGNE VIDE DE SÉPARATION
function displayEmptyLine() {
    echo "
    <tr style='height:5px ; background-color:black'>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>";
}

// 100 * (current - prev) / prev
function computeEvolutionFromPrevToCurrent($prev,$current) {
    // On ne fait pas le calcul si l'un des 2 paramètres est nul (ou n'existe pas), car inutile de comparer qqch à un nul (ou inexistant)
    //if ($year_curr_tot==0 || $year_prev_tot==0) return '/';
    if ($prev<=0 || $current<=0) return '/';
    $evol_from_year_prev = round ( 100 * ($current-$prev) / $prev );
    if ($evol_from_year_prev >= 0) $evol_from_year_prev = '+'.$evol_from_year_prev;
    return $evol_from_year_prev.'%';
}
//function displayStatsLine($title, $line, $suivis, $prets, $bold=false, $year_prev=null, $avg=null) {
function displayStatsLine($title, $line, $is_bold=false, $year_prev_tot=-1, $avg=-1) {
    //debug($line);
    ?>
    <tr>
        
        <!-- Titre -->
        <td><b><?=$title?></b></td>

		<?php 
        $year_curr_tot = $line['CREATED'];
		?>        

        <!-- Evolution \ N-1 -->
        <?php
        //$evol_from_year_prev = '/';
        $evol_from_year_prev = computeEvolutionFromPrevToCurrent($year_prev_tot, $year_curr_tot);
        //$evol_from_year_prev = ($year_curr_tot>0 && $year_prev_tot>0) ? computeEvolutionFromPrevToCurrent($year_prev_tot, $year_curr_tot) : '/';
        /* inutile, on fait pas des maths ici
        // - Si prev = 0 => éviter division par zéro, résultat = +/- INFINI
        if ($year_prev_tot==0) {
            if ($year_curr_tot==0) $evol_from_year_prev = '+0';
            else $evol_from_year_prev = $year_curr_tot>0 ? '+INF' : '-INF';
        }
        // - Si prev <> 0 => Cas nominal
        else {
        $evol_from_year_prev = round ( 100 * ($year_curr_tot-$year_prev_tot) / $year_prev_tot );
        if ($evol_from_year_prev >= 0) $evol_from_year_prev = '+'.$evol_from_year_prev;
        $evol_from_year_prev .= '%';
        */
        ?>
        <td><?=$evol_from_year_prev?></td>
        
        <!-- Evolution \ moyenne -->
        <?php
        //$evol_from_avg = '/';
        $evol_from_avg = computeEvolutionFromPrevToCurrent($avg, $year_curr_tot);
        /*
        if ($avg) {
            $evol_from_avg = round (($year_curr_tot-$avg['CREATED'])/$avg['CREATED'] * 100);
            if ($evol_from_avg>=0) $evol_from_avg = '+'.$evol_from_avg;
            $evol_from_avg .= '%';
        }
        */
        ?>
        <td><?=$evol_from_avg?></td>

        <!-- TOTAL -->
        <td><?=echoAsPctAndBold($year_curr_tot, null, $is_bold)?></td>
        
        <!-- VALIDATED -->
        <td><?=echoAsPctAndBold($line['VALIDATED'], $year_curr_tot, $is_bold)?></td>
    	
        <!-- TBA -->
        <!-- 
        <td><echoAsPctAndBold($line['TOBEARCHIVED'], null, $is_bold)></td>
         -->
        <td><?=echoAsPctAndBold($line['TOBEARCHIVED'], $year_curr_tot, $is_bold)?></td>

        <!-- ARCHIVED -->
        <td><?=echoAsPctAndBold($line['ARCHIVED'], $year_curr_tot, $is_bold)?></td>

        <!-- SUIVIS -->
        <td><?=echoAsPctAndBold($line['suivis'], $year_curr_tot, $is_bold)?></td>

        <!-- EMPRUNTS -->
        <td><?=echoAsPctAndBold($line['prets'], $year_curr_tot, $is_bold)?></td>
    	
    </tr>
    <?php
}
	
?>






<!-- 
----------------------------------------
-------------- PAGE WEB HTML ----------- 
----------------------------------------
-->

<style>
table.stats {
    border: 5px solid #333;
}
table.stats td {
    border: 2px solid #333;
}

table.stats thead th {
    border: 2px solid #333;
    text-align: center;
    #background-color: blue;
    #color: white;
}
</style>

<h2>
	<!-- 
	<i class="icon-print"></i> 
	 -->
	<center>STATISTIQUES SUR LES MATÉRIELS</center>
</h2>

<p>
<table>
<tr>
<td><b>Prendre en compte les</b></td>
<td> 
<?php
echo $this->Form->create('Statistiques', [
 'type' => 'get',
]);
    //echo '<input name="s_all_2" class="search-input" type="text" id="MaterielSAll">';
    echo $this->Form->control('nbyears', [
    'id' => 'nby',
    //'class' => 'search-input',
    'label' => '',
    //'type' => 'text',
    'default' => $nbyears
    ]);
    /*
    echo $this->Form->submit('Chercher', [
    'name' => 'MaterielSAll',
    'id' => 'MaterielSAll',
    'style' => 'margin: 0px;'
    ]);
    */
echo $this->Form->end();
?>

</td>
<td><b>dernières années (<?=$current_year.'-'.$year_min?>)</b></td>
</tr>
</table>
</p>


<table class="stats">

    <!-- TITRES COLONNES sur 2 lignes -->
	<thead>
    	
    	<!-- 
    	<tr>
    		<th> </th>
    		<th>Evolution \ N-1</th>
    		<th>Evolution \ moyenne</th>
    		<th>Matériels acquis</th>
    		<th>Validés</th>
    		<th>En demande d'archivage</th>
    		<th>Archivés</th>
    		<th>Matériels suivis</th>
    		<th>Matériels empruntés</th>
    	</tr>
    	 -->
    	<tr>
    		<th> </th>
    		<th colspan="3" align="center">Matériels acquis</th>
    		<!-- 
    		<th></th>
    		<th></th>
    		 -->
    		<th colspan="3">Répartition par statut</th>
    		<!-- 
    		<th></th>
    		<th></th>
    		 -->
    		<th colspan="2">Suivis et Emprunts</th>
    	</tr>
    	
    	<tr>
    		<th> </th>
    		
    		<th>Evol. \ N-1</th>
    		<th>Evol. \ moyenne</th>
    		<th>Total</th>
    		
    		<th>Validés</th>
    		<th>A archiver</th>
    		<th>Archivés</th>
    		
    		<th>Matériels suivis</th>
    		<th>Matériels prêtés</th>
    	</tr>
	
	</thead>

	<!-- LIGNES DATA -->
	<tbody>
	
    	<?php 
    	
    	displayEmptyLine();
    	
    	displayStatsLine("Moyenne annuelle", $avg, true);
    	//displayStatsLine("Moyenne annuelle (sur les $nbyears dernières années)", $avg, true);
    	//displayStatsLine("Moyenne annuelle (sur toutes les années sauf 1ère et dernière)", $avg, true);
    
    	displayStatsLine('Total Général', $tot, true);
    	//displayStatsLine('Total Sauf 1ère et dernière années', $tot2, true);
    	
    	// LIGNE VIDE DE SÉPARATION
    	displayEmptyLine();

    	// DATA, ANNÉE PAR ANNÉE (1 ligne par année)
    	/*
    	foreach ($years as $y=>$val) {
        	$year_title = $y;
        	if ($y==$year) $year_title = "Année en cours ($y)";
        	displayStatsLine($year_title, $val);
    	}
    	*/
    	if ($nbyears>0)
    	for ($y=$current_year ; $y>=$year_min ; $y--) {
    	    $val = $years[$y];
    	    $year_title = $y;
    	    if ($y==$current_year) $year_title = "Année en cours ($y)";
    	    $year_prev_tot = $y==$year_min ? -1 : $years[$y-1]['CREATED'];
    	    displayStatsLine($year_title, $val, false, $year_prev_tot, $avg['CREATED']);
    	}
    	
    	?>

	</tbody>
	
</table>

<?php 

?>