$tab_saisie = array();  // [eleve_id][rubrique_id][prof_id] => array(prof_info,appreciation,note);
$tab_signature = array(0=>NULL);  // [prof_id] => array(contenu,format,largeur,hauteur);
$tab_assiduite = array_fill_keys( $tab_eleve_id , array( 'absence' => NULL , 'absence_nj' => NULL , 'retard' => NULL , 'retard_nj' => NULL ) );  // [eleve_id] => array(absence,absence_nj,retard,retard_nj);
$tab_moyenne_exception_matieres = ( ($BILAN_TYPE!='bulletin') || !$_SESSION['OFFICIEL']['BULLETIN_MOYENNE_EXCEPTION_MATIERES'] ) ? array() : explode(',',$_SESSION['OFFICIEL']['BULLETIN_MOYENNE_EXCEPTION_MATIERES']) ;
$tab_prof_id = array();
$DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves( $BILAN_TYPE , $periode_id , $liste_eleve_id , 0 /*prof_id*/ , FALSE /*with_rubrique_nom*/ , FALSE /*with_periodes_avant*/ , FALSE /*only_synthese_generale*/ );
foreach($DB_TAB as $DB_ROW)
{
  $prof_info = ($DB_ROW['prof_id']) ? afficher_identite_initiale( $DB_ROW['user_nom'] , FALSE , $DB_ROW['user_prenom'] , TRUE , $DB_ROW['user_genre'] ) : '' ;
  $note = in_array($DB_ROW['rubrique_id'],$tab_moyenne_exception_matieres) ? NULL : $DB_ROW['saisie_note'] ;
  $tab_saisie[$DB_ROW['eleve_id']][$DB_ROW['rubrique_id']][$DB_ROW['prof_id']] = array( 'prof_info'=>$prof_info , 'appreciation'=>$DB_ROW['saisie_appreciation'] , 'note'=>$note );
  $tab_signature[$DB_ROW['prof_id']] = NULL ; // Initialisé
  $tab_prof_id[$DB_ROW['prof_id']] = $DB_ROW['prof_id']; // Pour savoir ensuite la liste des profs à chercher
}
$DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_classe( $BILAN_TYPE , $periode_id , $classe_id , 0 /*prof_id*/ , FALSE /*with_periodes_avant*/ , FALSE /*only_synthese_generale*/ );
foreach($DB_TAB as $DB_ROW)
{
  $prof_info = ($DB_ROW['prof_id']) ? afficher_identite_initiale( $DB_ROW['user_nom'] , FALSE , $DB_ROW['user_prenom'] , TRUE , $DB_ROW['user_genre'] ) : '' ;
  $note = in_array($DB_ROW['rubrique_id'],$tab_moyenne_exception_matieres) ? NULL : $DB_ROW['saisie_note'] ;
  $tab_saisie[0][$DB_ROW['rubrique_id']][$DB_ROW['prof_id']] = array( 'prof_info'=>$prof_info , 'appreciation'=>$DB_ROW['saisie_appreciation'] , 'note'=>$note );
}

// Récupérer les signatures numériques

if($_SESSION['OFFICIEL']['TAMPON_SIGNATURE']!='sans')
{
  if( ($_SESSION['OFFICIEL']['TAMPON_SIGNATURE']=='tampon') || (!count($tab_prof_id)) )
  {
    $listing_prof_id = '0';
  }
            $liste_eleve_id = implode(',', $tab_eleve_id_tmp);
        }
        calculer_et_enregistrer_moyennes_eleves_bulletin($periode_id, $classe_id, $liste_eleve_id, '', $_SESSION['OFFICIEL']['BULLETIN_RETROACTIF'], $_SESSION['OFFICIEL']['BULLETIN_MOYENNE_CLASSE'], $_SESSION['OFFICIEL']['BULLETIN_MOYENNE_GENERALE']);
    }
}
// Récupérer les saisies déjà effectuées pour le bilan officiel concerné
$tab_saisie = array();
// [eleve_id][rubrique_id][prof_id] => array(prof_info,appreciation,note);
$tab_moyenne_exception_matieres = $BILAN_TYPE != 'bulletin' || !$_SESSION['OFFICIEL']['BULLETIN_MOYENNE_EXCEPTION_MATIERES'] ? array() : explode(',', $_SESSION['OFFICIEL']['BULLETIN_MOYENNE_EXCEPTION_MATIERES']);
$DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves($BILAN_TYPE, $periode_id, $eleve_id, 0, FALSE, FALSE, FALSE);
foreach ($DB_TAB as $DB_ROW) {
    $prof_info = $DB_ROW['prof_id'] ? afficher_identite_initiale($DB_ROW['user_nom'], FALSE, $DB_ROW['user_prenom'], TRUE, $DB_ROW['user_genre']) : '';
    $note = !in_array($DB_ROW['rubrique_id'], $tab_moyenne_exception_matieres) ? $DB_ROW['saisie_note'] : NULL;
    $tab_saisie[$DB_ROW['eleve_id']][$DB_ROW['rubrique_id']][$DB_ROW['prof_id']] = array('prof_info' => $prof_info, 'appreciation' => $DB_ROW['saisie_appreciation'], 'note' => $note);
}
$DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_classe($periode_id, $classe_id, 0, FALSE, FALSE);
foreach ($DB_TAB as $DB_ROW) {
    $prof_info = $DB_ROW['prof_id'] ? afficher_identite_initiale($DB_ROW['user_nom'], FALSE, $DB_ROW['user_prenom'], TRUE, $DB_ROW['user_genre']) : '';
    $note = !in_array($DB_ROW['rubrique_id'], $tab_moyenne_exception_matieres) ? $DB_ROW['saisie_note'] : NULL;
    $tab_saisie[0][$DB_ROW['rubrique_id']][$DB_ROW['prof_id']] = array('prof_info' => $prof_info, 'appreciation' => $DB_ROW['saisie_appreciation'], 'note' => $note);
}
// Récupérer les absences / retards
$affichage_assiduite = $_SESSION['OFFICIEL'][$tab_types[$BILAN_TYPE]['droit'] . '_ASSIDUITE'] ? TRUE : FALSE;
if ($affichage_assiduite && $eleve_id) {
    $DB_ROW = DB_STRUCTURE_OFFICIEL::DB_recuperer_officiel_assiduite($periode_id, $eleve_id);
    $tab_assiduite[$eleve_id] = empty($DB_ROW) ? array('absence' => NULL, 'absence_nj' => NULL, 'retard' => NULL, 'retard_nj' => NULL) : array('absence' => $DB_ROW['assiduite_absence'], 'absence_nj' => $DB_ROW['assiduite_absence_nj'], 'retard' => $DB_ROW['assiduite_retard'], 'retard_nj' => $DB_ROW['assiduite_retard_nj']);
}
// Récupérer les professeurs principaux
$affichage_prof_principal = $_SESSION['OFFICIEL'][$tab_types[$BILAN_TYPE]['droit'] . '_PROF_PRINCIPAL'] ? TRUE : FALSE;
if ($affichage_prof_principal) {
    $DB_TAB = DB_STRUCTURE_OFFICIEL::DB_lister_profs_principaux($classe_id);
       }
     }
   }
 }
 if(!count($tab_donnees_csv))
 {
   exit('Erreur : aucune saisie trouvée dans le fichier transmis !');
 }
 // On compare avec ce qui est enregistré dans la base pour distinguer s'il s'agit d'UPDATE, d'INSERT, ou si cela n'a pas changé.
 // Cette partie de code est inspirée de [code_officiel_archiver.php]
 $liste_eleve_id = implode(',',array_keys($tab_eleve_id));
 if($BILAN_TYPE=='bulletin')
 {
   $DB_TAB = array_merge
   (
     DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_classe( $BILAN_TYPE , $periode_id , $classe_id      , $_SESSION['USER_ID'] , FALSE /*with_periodes_avant*/ , FALSE /*only_synthese_generale*/ ),
     DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves( $BILAN_TYPE , $periode_id , $liste_eleve_id , $_SESSION['USER_ID'] , TRUE /*with_rubrique_nom*/ , FALSE /*with_periodes_avant*/ , FALSE /*only_synthese_generale*/ )
   );
 }
 else
 {
   $DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves( $BILAN_TYPE , $periode_id , $liste_eleve_id , $_SESSION['USER_ID'] , TRUE /*with_rubrique_nom*/ , FALSE /*with_periodes_avant*/ , FALSE /*only_synthese_generale*/ );
 }
 // Les appréciations
 foreach($DB_TAB as $key => $DB_ROW)
 {
   if($DB_ROW['prof_id'])
   {
     if(isset($tab_donnees_csv[$DB_ROW['rubrique_id']][$DB_ROW['eleve_id']]['appreciation']))
     {
       $appreciation = $tab_donnees_csv[$DB_ROW['rubrique_id']][$DB_ROW['eleve_id']]['appreciation']['val'];
                 $appreciation = $with_note ? Clean::appreciation($col4) : Clean::appreciation($col3);
                 if ($appreciation) {
                     $tab_donnees_csv[$rubrique_id][$eleve_id]['appreciation'] = array('val' => mb_substr($appreciation, 0, $longueur_maxi), 'mode' => 'insert');
                 }
             }
         }
     }
 }
 if (!count($tab_donnees_csv)) {
     exit('Erreur : aucune saisie trouvée dans le fichier transmis !');
 }
 // On compare avec ce qui est enregistré dans la base pour distinguer s'il s'agit d'UPDATE, d'INSERT, ou si cela n'a pas changé.
 // Cette partie de code est inspirée de [code_officiel_archiver.php]
 $liste_eleve_id = implode(',', array_keys($tab_eleve_id));
 if ($BILAN_TYPE == 'bulletin') {
     $DB_TAB = array_merge(DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_classe($periode_id, $classe_id, $_SESSION['USER_ID'], FALSE, FALSE), DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves($BILAN_TYPE, $periode_id, $liste_eleve_id, $_SESSION['USER_ID'], TRUE, FALSE, FALSE));
 } else {
     $DB_TAB = DB_STRUCTURE_OFFICIEL::DB_recuperer_bilan_officiel_saisies_eleves($BILAN_TYPE, $periode_id, $liste_eleve_id, $_SESSION['USER_ID'], TRUE, FALSE, FALSE);
 }
 // Les appréciations
 foreach ($DB_TAB as $key => $DB_ROW) {
     if ($DB_ROW['prof_id']) {
         if (isset($tab_donnees_csv[$DB_ROW['rubrique_id']][$DB_ROW['eleve_id']]['appreciation'])) {
             $appreciation = $tab_donnees_csv[$DB_ROW['rubrique_id']][$DB_ROW['eleve_id']]['appreciation']['val'];
             $tab_donnees_csv[$DB_ROW['rubrique_id']][$DB_ROW['eleve_id']]['appreciation']['mode'] = $appreciation == $DB_ROW['saisie_appreciation'] ? 'idem' : 'update';
         }
         unset($DB_TAB[$key]);
     }
 }
 // Les notes
 if ($with_note) {