%%%
% Gestion Score
%%%
\def\filedateGestionScore{2026/04/04}%
\def\fileversionGestionScore{0.1a}%
\message{-- \filedateGestionScore\space v\fileversionGestionScore}%
%
\definecolor{PfCInsuffisant}{HTML}{B45309}%
\edef\PfC@SeuilI{24.9}%
\definecolor{PfCFragile}{HTML}{CA8A04}%
\edef\PfC@SeuilF{49.9}%
\definecolor{PfCSatisfaisant}{HTML}{16A34A}%
\edef\PfC@SeuilS{74.9}%
\definecolor{PfCTbien}{HTML}{2493EF}%

\makeatletter
\setKVdefault[GestionScore]{SeuilI=24.9,SeuilF=49.9,SeuilS=74.9,CouleurI=PfCInsuffisant,CouleurF=PfCFragile,CouleurS=PfCSatisfaisant,CouleurTB=PfCTbien,Fichier={}}%

\NewDocumentCommand\PfC@ColoreScore{m}{%
  \edef\RetiensArrondi{\fpeval{#1*10}}%
  \ifnum\RetiensArrondi>0\relax%
    \ifnum\RetiensArrondi<\PfC@SeuilRetenuI\relax%
      \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurI}}%
    \else%
      \ifnum\RetiensArrondi<\PfC@SeuilRetenuF\relax%
        \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurF}}%
      \else\ifnum\RetiensArrondi<\PfC@SeuilRetenuS\relax%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurS}}%
        \else%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurTB}}%
        \fi%
      \fi%
    \fi%
    \textcolor{CouleurScore}{\SI{#1}{\percent}}%
  \else%
    --%
  \fi%
}%

\NewDocumentCommand\PfC@ColoreEcart{m}{%
  \ifnum\fpeval{\Score*10}=0\relax%
    --%
  \else%
    \edef\RetiensArrondi{\fpeval{(#1)*10}}%
    \ifnum\RetiensArrondi<0\relax%
      \colorlet{CouleurEcart}{DarkRed}%
    \else%
      \colorlet{CouleurEcart}{DarkGreen}%
    \fi%
    \textcolor{CouleurEcart}{\num{\fpeval{#1}}}%
  \fi%
}%

\NewDocumentCommand\PfC@AutoAppreciation{m}{%
  \edef\RetiensArrondi{\fpeval{#1*10}}%
  \ifnum\RetiensArrondi>0\relax%
    \ifnum\RetiensArrondi<\PfC@SeuilRetenuI\relax%
      \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurI}}%
      \textcolor{CouleurScore}{Maîtrise insuffisante}%
    \else%
      \ifnum\RetiensArrondi<\PfC@SeuilRetenuF\relax%
        \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurF}}%
        \textcolor{CouleurScore}{Maîtrise fragile}%
      \else\ifnum\RetiensArrondi<\PfC@SeuilRetenuS\relax%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurS}}%
          \textcolor{CouleurScore}{Maîtrise satisfaisante}%
        \else%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurTB}}%
          \textcolor{CouleurScore}{Très bonne maîtrise}%
        \fi%
      \fi%
    \fi%
  \else%
    --%
  \fi%
}%

\NewDocumentCommand\PfC@AutoMoyenneGenerale{m}{%
  \edef\RetiensArrondi{\fpeval{#1*10}}%
  \ifnum\RetiensArrondi>0\relax%
    \ifnum\RetiensArrondi<\PfC@SeuilRetenuI\relax%
      \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurI}!15}%
    \else%
      \ifnum\RetiensArrondi<\PfC@SeuilRetenuF\relax%
        \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurF}!15}%
      \else\ifnum\RetiensArrondi<\PfC@SeuilRetenuS\relax%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurS}!15}%
        \else%
          \colorlet{CouleurScore}{\useKV[GestionScore]{CouleurTB}!15}%
        \fi%
      \fi%
    \fi%
  \else%
    \colorlet{CouleurScore}{White}%
  \fi%
  \begin{tcolorbox}[colback=CouleurScore,colframe=CouleurScore!50!black,width=0.25\linewidth,nobeforeafter,valign=center,box align=center,height=75pt]
    \begin{center}
      \Large\bfseries%
      \SI{#1}{\percent}\par%
      \ifnum\RetiensArrondi>0\relax%
        \ifnum\RetiensArrondi<\PfC@SeuilRetenuI\relax%
          Maîtrise insuffisante%
        \else%
          \ifnum\RetiensArrondi<\PfC@SeuilRetenuF\relax%
            Maîtrise fragile%
          \else\ifnum\RetiensArrondi<\PfC@SeuilRetenuS\relax%
              Maîtrise satisfaisante%
            \else%
              Très bonne maîtrise%
            \fi%
          \fi%
        \fi%
      \else%
        --%
      \fi%
    \end{center}
  \end{tcolorbox}
}%

\NewDocumentCommand\PfC@BarrePositionMoyenne{mm}{%
  \mplibforcehmode%
  \begin{mplibcode}
    u:=5mm;
    pair A,B,C,D,M[],N[];
    eb=3pt;
    A=(0,0);
    B-A=u*(5,0);
    C-B=u*(0,1);
    D-C=A-B;
    path barrescore[];
    barrescore[1]=(A+eb*(1,0))--(B-eb*(1,0)){dir 0}..{dir 90}(B+eb*(0,1))--(C-eb*(0,1)){dir 90}..{dir 180}(C-eb*(1,0))--(D+eb*(1,0)){dir 180}..(D-eb*(0,1)){dir -90}--(A+eb*(0,1)){dir -90}..cycle;
    fill barrescore[1] withcolor LightSteelBlue;
    M1=(#1/100)[A,B];
    M2=(#1/100)[D,C];
    N1=(#2/100)[A,B];
    N2=(#2/100)[D,C];
    barrescore[2]=(A+eb*(1,0))--(M1-eb*(1,0)){dir 0}..{dir 90}(M1+eb*(0,1))--(M2-eb*(0,1)){dir 90}..{dir 180}(M2-eb*(1,0))--(D+eb*(1,0)){dir 180}..(D-eb*(0,1)){dir -90}--(A+eb*(0,1)){dir -90}..cycle;
    color CouleurBarre;
    if #1<ceiling(\useKV[GestionScore]{SeuilI}):
      CouleurBarre:=\mpcolor{\useKV[GestionScore]{CouleurI}};
    elseif #1<ceiling(\useKV[GestionScore]{SeuilF}):
      CouleurBarre:=\mpcolor{\useKV[GestionScore]{CouleurF}};
    elseif #1<ceiling(\useKV[GestionScore]{SeuilS}):
      CouleurBarre:=\mpcolor{\useKV[GestionScore]{CouleurS}};
    else:
      CouleurBarre:=\mpcolor{\useKV[GestionScore]{CouleurTB}};
    fi;
    if #1>0:
      fill barrescore[2] withcolor CouleurBarre;
    fi;
    trace 1.1[N1,N2]--1.1[N2,N1] withpen pencircle scaled 1.2;
 \end{mplibcode}
}%

\NewDocumentCommand\PfC@BuildGraphiqueScore{mm}{%
  \mplibforcehmode%
  \begin{mplibcode}%
    pair M[],Moy[];
    nb:=0;
    scoree=0;
    vardef Lecture(text t)=
      for p_=t:
        nb:=nb+1;
        kk:=nb mod 3;
        if kk=1:
          scoree:=scoree+1;
          if numeric p_:
            ScoreEleve[scoree]=p_;
          else:
            ScoreEleve[scoree]=-1;
          fi;
        elseif kk=2:
          Bareme[scoree]=p_;
        else:
          Moyenne[scoree]=p_;
        fi;
      endfor;
    enddef;

    Lecture(#1);

    alpha:=1;
    
    vardef PointsPlaces=
      nbp:=0;
      for k=0 upto scoree-1:
        Moy[k+1]=u*(alpha*k,5*Moyenne[k+1]/100);
        if ScoreEleve[k+1]<0:
        else:
          nbp:=nbp+1;
          M[nbp]=u*(alpha*k,5*ScoreEleve[k+1]/Bareme[k+1]);
        fi;
      endfor;
    enddef;
    %
    PointsPlaces;
    %
    path Eleve,FillEleve,Moyenne;
    % Eleve=M[1]{dir angle(M[2]-M[1])} for k=2 upto nbp-1:..{dir 0.25angle(M[k]-M[k-1])}M[k]{dir 0.25angle(M[k+1]-M[k])} endfor..M[nbp];
    % Eleve=M[1]{dir angle(M[2]-M[1])} for k=2 upto nbp-1:..{dir angle(M[k+1]-M[k-1])}M[k]{dir angle(M[k+1]-M[k-1])} endfor..M[nbp];
    Eleve=M[1] for k=2 upto nbp:--M[k] endfor;
    Moyenne=Moy[1] for k=2 upto scoree:--Moy[k] endfor;
    FillEleve=(xpart(M[1]),0)--Eleve--(xpart(M[nbp]),0)--cycle;
    fill FillEleve withcolor 0.8[LightSteelBlue,white];
    trace Eleve withpen pencircle scaled 1.2 withcolor LightSteelBlue;
    trace Moyenne withpen pencircle scaled 0.5 dashed evenly;
    % Repere
    for k=0 upto 5:
    label.lft(TEX("\tiny\SI{"&decimal(k*20)&"}{\percent}"),u*(0,k));
    draw (u*(0,k))--(u*(alpha*scoree,k)) withpen pencircle scaled 0.5 withcolor 0.8white;
    endfor;
    for k=0 upto scoree-1:
    label.bot(TEX("\tiny E"&decimal(k+1)),u*(alpha*k,0));
    endfor;
    for k=0 upto scoree:
    draw (u*(alpha*k,0))--(u*(alpha*k,5)) withpen pencircle scaled 0.5 withcolor 0.8white;
    endfor;
    marque_p:="creux";
    drawoptions(withcolor LightSteelBlue);
    for k=1 upto nbp:
    pointe(M[k]);
    endfor;
    draw (u*(0,-0.75)--u*(1,-0.75)) withpen pencircle scaled 1.2 withcolor LightSteelBlue;
    pointe(u*(0.5,-0.75));
    drawoptions();
    for k=1 upto scoree:
    pointe(Moy[k]);
    endfor;
    draw (u*(0,-1.25)--u*(1,-1.25)) withpen pencircle scaled 0.5 dashed evenly;
    pointe(u*(0.5,-1.25));
    draw (0,0)--(u*(alpha*scoree,0));
    draw (0,0)--u*(0,5.5);
    label.rt(TEX("\scriptsize\ListeNomsEleves[#2]"),(u*(1,-0.75)));
    label.rt(TEX("\scriptsize Moyenne classe"),(u*(1,-1.25)));
  \end{mplibcode}%
}%

\NewDocumentCommand\GestionScore{om}{%
  \useKVdefault[GestionScore]%
  \setKV[GestionScore]{#1}%
  \ifemptyKV[GestionScore]{Fichier}{}{%
    %
    \edef\PfC@SeuilRetenuI{\fpeval{ceil(\useKV[GestionScore]{SeuilI})*10}}%
    \edef\PfC@SeuilRetenuF{\fpeval{ceil(\useKV[GestionScore]{SeuilF})*10}}%
    \edef\PfC@SeuilRetenuS{\fpeval{ceil(\useKV[GestionScore]{SeuilS})*10}}%
    \readarraysepchar{,}%
    \readdef{\useKV[GestionScore]{Fichier}}{\DataScore}%
    \readarray\DataScore\twoD[-,\ncols]%
    % 
    \edef\NomEvaluation{}%
    \xintFor* ##1 in{\xintSeq{2}{\ncols}}\do{%
      \ifnum##1=2\relax%
        \edef\NomEvaluation{\twoD[1,##1]}%
      \else%
        \edef\NomEvaluation{\NomEvaluation,\twoD[1,##1]}%
      \fi%
    }%
    % Les évaluations portent le nom de \NomEvaluation
    % 
    \edef\NomDescriptif{}%
    \xintFor* ##1 in{\xintSeq{2}{\ncols}}\do{%
      \ifnum##1=2\relax%
        \edef\NomDescriptif{\twoD[2,##1]}%
      \else%
        \edef\NomDescriptif{\NomDescriptif,\twoD[2,##1]}%
      \fi%
    }%
    % Les descriptifs portent le nom de \NomDescriptif
    % 
    \edef\NomBareme{}
    \xintFor* ##1 in{\xintSeq{2}{\ncols}}\do{%
      \ifnum##1=2\relax%
        \edef\NomBareme{\twoD[3,##1]}%
      \else%
        \edef\NomBareme{\NomBareme,\twoD[3,##1]}%
      \fi%
    }%
    % Les barèmes sont \NomBareme
    % 
    \edef\NomEleve{}%
    \xintFor* ##1 in{\xintSeq{4}{\nrows}}\do{%
      \ifnum##1=4\relax%
        \edef\NomEleve{\twoD[##1,1]}%
      \else%
        \edef\NomEleve{\NomEleve,\twoD[##1,1]}%
      \fi%
    }%
    % Les élèves sont \NomEleve
    % 
    \edef\NoteEvaluation{}%
    \xintFor* ##2 in{\xintSeq{2}{\ncols}}\do{%
      \edef\RecapMoyenne{}%
      \ifnum ##2=2\relax
        \xintFor* ##1 in{\xintSeq{4}{\nrows}}\do{%
          \IfDecimal{\twoD[##1,##2]}{\edef\RecapMoyenne{\RecapMoyenne,\twoD[##1,##2]}}{}%
          \ifnum##1=4\relax%
            \edef\NoteEvaluation{\twoD[##1,##2]}%
          \else%
            \edef\NoteEvaluation{\NoteEvaluation/\twoD[##1,##2]}%
          \fi%
        }
      \else%
        \xintFor* ##1 in{\xintSeq{4}{\nrows}}\do{%
          \IfDecimal{\twoD[##1,##2]}{\edef\RecapMoyenne{\RecapMoyenne,\twoD[##1,##2]}}{}%
          \ifnum##1=4\relax%
            \edef\NoteEvaluation{\NoteEvaluation,\twoD[##1,##2]}%
          \else%
            \edef\NoteEvaluation{\NoteEvaluation/\twoD[##1,##2]}%
          \fi%
        }%
      \fi%
      \Stat[Liste]{\RecapMoyenne}%
      \edef\NoteEvaluation{\NoteEvaluation/\Moyenne}%
    }%
    % 
    % Les notes des évaluations sont \NoteEvaluation
    % 
    % Les noms et notes des élèves sont
    % 
    \edef\RecapPerso{}%
    \xintFor* ##1 in{\xintSeq{4}{\nrows}}\do{%
      \ifnum ##1=4\relax%
        \xintFor* ##2 in{\xintSeq{1}{\ncols}}\do{%
          \ifnum ##2=1\relax%
            \edef\RecapPerso{\twoD[##1,##2]}%
          \else%
            \edef\RecapPerso{\RecapPerso/\twoD[##1,##2]}%
          \fi%
        }%
      \else%
        \xintFor* ##2 in{\xintSeq{1}{\ncols}}\do{%
          \ifnum ##2=1\relax%
            \edef\RecapPerso{\RecapPerso,\twoD[##1,##2]}%
          \else%
            \edef\RecapPerso{\RecapPerso/\twoD[##1,##2]}%
          \fi%
        }%
      \fi%
    }%
    % Les récapitulatifs perso sont \RecapPerso
    \setsepchar[*]{,*/}\reademptyitems%
    \readlist*\ListeNomsEleves{\NomEleve}%
    \readlist*\ListeNomsEvaluations{\NomEvaluation}%
    \readlist*\ListeNomsDescriptifs{\NomDescriptif}%
    \readlist*\ListeNomsBaremes{\NomBareme}%
    \readlist*\ListeNomsNotesPersos{\RecapPerso}%
    \readlist*\ListeNomsNotesEvaluations{\NoteEvaluation}%
    \reademptyitems%
    %
    \xintFor* ##2 in{\xintSeq{1}{\ListeNomsEleveslen}}\do{%
      \clearpage%
      \xdef\MoyenneTotale{}%
      \xintFor* ##1 in{\xintSeq{1}{\ListeNomsBaremeslen}}\do{%
        \IfDecimal{\ListeNomsNotesPersos[##2,\fpeval{##1+1}]}{\xdef\MoyenneTotale{\MoyenneTotale,\fpeval{round(100*\ListeNomsNotesPersos[##2,\fpeval{##1+1}]/\ListeNomsBaremes[##1],1,1)}}}{}%
      }%
      \Stat[Liste]{\MoyenneTotale}%
      \begin{tcolorbox}[colback=Cornsilk!15,colframe=Cornsilk!75!black,height=75pt,width=0.7\linewidth,box align=center,nobeforeafter]
        \ListeNomsEleves[##2]\par
        Bilan des automatismes\par
        \today\par
        {%
          \begin{Geometrie}[Cadre="aucun"]
            fill (unitsquare scaled 2mm) withcolor \mpcolor{\useKV[GestionScore]{CouleurI}};
            trace (unitsquare scaled 2mm);
          \end{Geometrie}
          \footnotesize Maîtrise insuffisante \begin{Geometrie}[Cadre="aucun"]
            fill (unitsquare scaled 2mm) withcolor \mpcolor{\useKV[GestionScore]{CouleurF}};
            trace (unitsquare scaled 2mm);
          \end{Geometrie} Maîtrise fragile \begin{Geometrie}[Cadre="aucun"]
            fill (unitsquare scaled 2mm) withcolor \mpcolor{\useKV[GestionScore]{CouleurS}};
            trace (unitsquare scaled 2mm);
          \end{Geometrie} Maîtrise satisfaisante \begin{Geometrie}[Cadre="aucun"]
            fill (unitsquare scaled 2mm) withcolor \mpcolor{\useKV[GestionScore]{CouleurTB}};
            trace (unitsquare scaled 2mm);
          \end{Geometrie} Très bonne maîtrise%
        }%
      \end{tcolorbox}
      \hfill\PfC@AutoMoyenneGenerale{\fpeval{round(\Moyenne,1,1)}}%
      \par%\smallskip\par%
      \begin{center}
        \renewcommand{\arraystretch}{1.5}%
        \begin{longtable}{m{160pt}ccc>{\centering\arraybackslash}m{70pt}r}
          \multicolumn{3}{l}{\scriptsize Le trait noir sur la barre indique le score moyen de la classe.}\\
          \hline
          \rowcolor{LightSteelBlue!75!NavyBlue}Épreuve&Score&Moy.classe&Écart&Barre&Niveau\\
          \hline
          \xintFor* ##1 in{\xintSeq{1}{\ListeNomsEvaluationslen}}\do{%
            \IfDecimal{\ListeNomsNotesPersos[##2,\fpeval{##1+1}]}{\xdef\Score{\fpeval{round(100*\ListeNomsNotesPersos[##2,\fpeval{##1+1}]/\ListeNomsBaremes[##1],1,1)}}}{\xdef\Score{0}}%
            \xdef\MoyenneClasse{\fpeval{round(100*\ListeNomsNotesEvaluations[##1,\fpeval{\ListeNomsEleveslen+1}]/\ListeNomsBaremes[##1],1,1)}}%
            \ifnum ##1=1\relax%
              \IfDecimal{\ListeNomsNotesPersos[##2,\fpeval{##1+1}]}{%
                \xdef\RecapPourGraphique{\ListeNomsNotesPersos[##2,\fpeval{##1+1}],\ListeNomsBaremes[##1],\MoyenneClasse}%
              }{\xdef\RecapPourGraphique{-1,\ListeNomsBaremes[##1],\MoyenneClasse}%
              }%
            \else%
              \IfDecimal{\ListeNomsNotesPersos[##2,\fpeval{##1+1}]}{%
                \xdef\RecapPourGraphique{\RecapPourGraphique,\ListeNomsNotesPersos[##2,\fpeval{##1+1}],\ListeNomsBaremes[##1],\MoyenneClasse}%
              }{%
                \xdef\RecapPourGraphique{\RecapPourGraphique,-1,\ListeNomsBaremes[##1],\MoyenneClasse}%
              }%
            \fi%
            \xdef\EcartAMoyenne{\fpeval{\Score-\MoyenneClasse}}%
            \ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\ListeNomsEvaluations[##1] (/\ListeNomsBaremes[##1])&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\\
            \ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\ListeNomsDescriptifs[##1]&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\multirow{-2}{*}{\PfC@ColoreScore{\Score}}&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\multirow{-2}{*}{\SI{\MoyenneClasse}{\percent}}&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\multirow{-2}{*}{\PfC@ColoreEcart{\EcartAMoyenne}}&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\multirow{-2}{*}{\PfC@BarrePositionMoyenne{\Score}{\MoyenneClasse}}&\ifodd##1\relax\else\cellcolor{LightSteelBlue!15}\fi\multirow{-2}{*}{\PfC@AutoAppreciation{\Score}}\\
            \hline
          }
        \end{longtable}
        
        \PfC@BuildGraphiqueScore{\RecapPourGraphique}{##2}
      \end{center}
    }%
  }%
}%
\makeatother