/**
  * recuperer_arborescence_synthese
  * Retourner l'arborescence des items travaillés par des élèves selectionnés, durant la période choisie => pour la synthèse matière ou multi-matières
  *
  * @param string $liste_eleve_id   id des élèves séparés par des virgules ; il peut n'y avoir qu'un id, en particulier si c'est un élève qui demande un bilan
  * @param int    $matiere_id       id de la matière ; 0 pour toutes les matières
  * @param int    $only_socle       1 pour ne retourner que les items reliés au socle, 0 sinon
  * @param int    $only_niveau      0 pour tous les niveaux, autre pour un niveau donné
  * @param string $mode_synthese    'predefini' ou 'domaine' ou 'theme'
  * @param int    $fusion_niveaux   1 pour ne pas indiquer le niveau dans l'intitulé et fusionner les synthèses de même intitulé, 0 sinon
  * @param string $date_mysql_debut
  * @param string $date_mysql_fin
  * @return array
  */
 public static function DB_recuperer_arborescence_synthese($liste_eleve_id, $matiere_id, $only_socle, $only_niveau, $mode_synthese = 'predefini', $fusion_niveaux, $date_mysql_debut, $date_mysql_fin)
 {
     $select_matiere = !$matiere_id ? 'matiere_id , matiere_nom , matiere_nb_demandes , ' : '';
     $select_synthese = $mode_synthese == 'predefini' ? ', referentiel_mode_synthese AS mode_synthese ' : '';
     $where_eleve = strpos($liste_eleve_id, ',') ? 'eleve_id IN(' . $liste_eleve_id . ') ' : 'eleve_id=' . $liste_eleve_id . ' ';
     // Pour IN(...) NE PAS passer la liste dans $DB_VAR sinon elle est convertie en nb entier
     $where_matiere = $matiere_id ? 'AND matiere_id=:matiere ' : 'AND matiere_active=1 ';
     $where_socle = $only_socle ? 'AND entree_id!=0 ' : '';
     $where_niveau = $only_niveau ? 'AND niveau_id=' . $only_niveau . ' ' : 'AND niveau_actif=1 ';
     $where_date_debut = $date_mysql_debut ? 'AND saisie_date>=:date_debut ' : '';
     $where_date_fin = $date_mysql_fin ? 'AND saisie_date<=:date_fin ' : '';
     $where_synthese = $mode_synthese == 'predefini' ? 'AND referentiel_mode_synthese IN("domaine","theme") ' : '';
     $order_matiere = !$matiere_id ? 'matiere_ordre ASC, matiere_nom ASC, ' : '';
     $DB_SQL = 'SELECT item_id , ';
     $DB_SQL .= 'CONCAT(matiere_ref,".",niveau_ref,".",domaine_ref,theme_ordre,item_ordre) AS item_ref , ';
     $DB_SQL .= 'item_nom , item_coef , item_cart , entree_id AS item_socle , item_lien , ';
     $DB_SQL .= 'theme_id , theme_nom , ';
     $DB_SQL .= 'domaine_id , domaine_nom , ';
     $DB_SQL .= $select_matiere;
     $DB_SQL .= 'niveau_nom , ';
     $DB_SQL .= 'referentiel_calcul_methode AS calcul_methode , referentiel_calcul_limite AS calcul_limite , referentiel_calcul_retroactif AS calcul_retroactif ' . $select_synthese;
     $DB_SQL .= 'FROM sacoche_saisie ';
     $DB_SQL .= 'LEFT JOIN sacoche_referentiel_item USING (item_id) ';
     $DB_SQL .= 'LEFT JOIN sacoche_referentiel_theme USING (theme_id) ';
     $DB_SQL .= 'LEFT JOIN sacoche_referentiel_domaine USING (domaine_id) ';
     $DB_SQL .= 'LEFT JOIN sacoche_matiere USING (matiere_id) ';
     $DB_SQL .= 'LEFT JOIN sacoche_niveau USING (niveau_id) ';
     $DB_SQL .= 'LEFT JOIN sacoche_referentiel USING (matiere_id,niveau_id) ';
     $DB_SQL .= 'WHERE ' . $where_eleve . $where_matiere . $where_socle . $where_niveau . $where_date_debut . $where_date_fin . $where_synthese;
     $DB_SQL .= 'GROUP BY item_id ';
     $DB_SQL .= 'ORDER BY ' . $order_matiere . 'niveau_ordre ASC, domaine_ordre ASC, theme_ordre ASC, item_ordre ASC';
     $DB_VAR = array(':matiere' => $matiere_id, ':date_debut' => $date_mysql_debut, ':date_fin' => $date_mysql_fin);
     $DB_TAB = DB::queryTab(SACOCHE_STRUCTURE_BD_NAME, $DB_SQL, $DB_VAR, TRUE);
     // Traiter le résultat de la requête pour en extraire des sous-tableaux $tab_synthese et éventuellement $tab_matiere
     $tab_synthese = array();
     $tab_matiere = array();
     foreach ($DB_TAB as $item_id => $tab) {
         foreach ($tab as $key => $DB_ROW) {
             if (!$matiere_id) {
                 $prefixe_matiere = $DB_ROW['matiere_id'];
                 $tab_matiere[$DB_ROW['matiere_id']] = array('matiere_nom' => $DB_ROW['matiere_nom'], 'matiere_nb_demandes' => $DB_ROW['matiere_nb_demandes']);
                 unset($DB_TAB[$item_id][$key]['matiere_nom'], $DB_TAB[$item_id][$key]['matiere_nb_demandes']);
             } else {
                 $prefixe_matiere = $matiere_id;
             }
             if ($mode_synthese == 'predefini') {
                 $prefixe_synthese = $DB_ROW['mode_synthese'];
                 unset($DB_TAB[$item_id][$key]['mode_synthese']);
             } else {
                 $prefixe_synthese = $mode_synthese;
             }
             if ($fusion_niveaux) {
                 $synthese_ref = $prefixe_matiere . '_' . Clean::id($DB_ROW[$prefixe_synthese . '_nom']);
                 $synthese_nom = $DB_ROW[$prefixe_synthese . '_nom'];
             } else {
                 $synthese_ref = $prefixe_synthese . '_' . $DB_ROW[$prefixe_synthese . '_id'];
                 $synthese_nom = $DB_ROW['niveau_nom'] . ' - ' . $DB_ROW[$prefixe_synthese . '_nom'];
             }
             $tab_synthese[$synthese_ref] = $synthese_nom;
             $DB_TAB[$item_id][$key]['synthese_ref'] = $synthese_ref;
             unset($DB_TAB[$item_id][$key]['niveau_nom'], $DB_TAB[$item_id][$key]['domaine_id'], $DB_TAB[$item_id][$key]['domaine_nom'], $DB_TAB[$item_id][$key]['theme_id'], $DB_TAB[$item_id][$key]['theme_nom']);
         }
     }
     if ($matiere_id) {
         return array($DB_TAB, $tab_synthese);
     } else {
         return array($DB_TAB, $tab_synthese, $tab_matiere);
     }
 }
  echo'</ul>'.NL;
}
else
{
  // On récupère les données
  $tab_matiere = array();
  $tab_colonne = array();
  foreach($DB_TAB as $DB_ROW)
  {
    if(!isset($tab_matiere[$DB_ROW['matiere_id']]))
    {
      $matiere_droit = test_user_droit_specifique( $_SESSION['DROIT_GERER_RESSOURCE'] , $DB_ROW['jointure_coord'] /*matiere_coord_or_groupe_pp_connu*/ );
      $icone_action  = ($matiere_droit) ? '<q class="modifier" title="Modifier les ressources de ce référentiel."></q>' : '<q class="modifier_non" title="Droit d\'accès : '.$texte_profil.'."></q>' ;
      $tab_matiere[$DB_ROW['matiere_id']] = array(
        'matiere_nom' => html($DB_ROW['matiere_nom']),
        'matiere_ref' => Clean::id($DB_ROW['matiere_ref']),
        'matiere_act' => $icone_action,
      );
    }
    $tab_colonne[$DB_ROW['matiere_id']][$DB_ROW['niveau_id']] = '<td>'.html($DB_ROW['niveau_nom']).'</td><td class="nu" id="td_'.$DB_ROW['matiere_id'].'_'.$DB_ROW['niveau_id'].'">'.$tab_matiere[$DB_ROW['matiere_id']]['matiere_act'].'</td>';
  }
  // On construit et affiche le tableau résultant
  $affichage = '<table class="vm_nug"><thead>'.NL.'<tr><th>Matière</th><th>Niveau</th><th class="nu"></th></tr>'.NL.'</thead><tbody>'.NL;
  foreach($tab_matiere as $matiere_id => $tab)
  {
    $rowspan = count($tab_colonne[$matiere_id]);
    foreach($tab_colonne[$matiere_id] as $niveau_id => $cellules)
    {
      if($rowspan)
      {
        $affichage .= '<tr class="tr_'.$tab['matiere_ref'].'"><td rowspan="'.$rowspan.'">'.$tab['matiere_nom'].'</td>'.$cellules.'</tr>'.NL;
// On récupère la liste des référentiels des matières auxquelles le professeur est rattaché, et s'il en est coordonnateur
$DB_TAB = DB_STRUCTURE_PROFESSEUR::DB_lister_matieres_niveaux_referentiels_professeur($_SESSION['USER_ID']);
if (empty($DB_TAB)) {
    echo '<ul class="puce">' . NL;
    echo '<li><span class="danger">Aucun référentiel présent parmi les matières qui vous sont rattachées !</span></li>' . NL;
    echo '<li><span class="astuce">Commencer par <a href="./index.php?page=professeur_referentiel&amp;section=gestion">créer ou importer un référentiel</a>.</span></li>' . NL;
    echo '</ul>' . NL;
} else {
    // On récupère les données
    $tab_matiere = array();
    $tab_colonne = array();
    foreach ($DB_TAB as $DB_ROW) {
        if (!isset($tab_matiere[$DB_ROW['matiere_id']])) {
            $matiere_droit = test_user_droit_specifique($_SESSION['DROIT_GERER_RESSOURCE'], $DB_ROW['jointure_coord']);
            $icone_action = $matiere_droit ? '<q class="modifier" title="Modifier les ressources de ce référentiel."></q>' : '<q class="modifier_non" title="Droit d\'accès : ' . $texte_profil . '."></q>';
            $tab_matiere[$DB_ROW['matiere_id']] = array('matiere_nom' => html($DB_ROW['matiere_nom']), 'matiere_ref' => Clean::id($DB_ROW['matiere_ref']), 'matiere_act' => $icone_action);
        }
        $tab_colonne[$DB_ROW['matiere_id']][$DB_ROW['niveau_id']] = '<td>' . html($DB_ROW['niveau_nom']) . '</td><td class="nu" id="td_' . $DB_ROW['matiere_id'] . '_' . $DB_ROW['niveau_id'] . '">' . $tab_matiere[$DB_ROW['matiere_id']]['matiere_act'] . '</td>';
    }
    // On construit et affiche le tableau résultant
    $affichage = '<table class="vm_nug"><thead>' . NL . '<tr><th>Matière</th><th>Niveau</th><th class="nu"></th></tr>' . NL . '</thead><tbody>' . NL;
    foreach ($tab_matiere as $matiere_id => $tab) {
        $rowspan = count($tab_colonne[$matiere_id]);
        foreach ($tab_colonne[$matiere_id] as $niveau_id => $cellules) {
            if ($rowspan) {
                $affichage .= '<tr class="tr_' . $tab['matiere_ref'] . '"><td rowspan="' . $rowspan . '">' . $tab['matiere_nom'] . '</td>' . $cellules . '</tr>' . NL;
                $rowspan = 0;
            } else {
                $affichage .= '<tr class="tr_' . $tab['matiere_ref'] . '">' . $cellules . '</tr>' . NL;
            }
        }