%%%
% R\'eponses \`a relier
%%%
\def\filedateQRelier{2025/08/26}%
\def\fileversionQRelier{0.1b}%
\message{-- \filedateQRelier\space v\fileversionQRelier}%
%
\setKVdefault[ClesRelie]{Solution=false,LargeurG=5cm,LargeurD=2cm,Stretch=1.5,Ecart=2cm,Couleur=black,Graine={}}%

\newcounter{NbRelie}%
\newcounter{NbRelieD}%

\makeatletter
\NewDocumentCommand\Relie{som}{%
  \useKVdefault[ClesRelie]%
  \setKV[ClesRelie]{#2}%
  \colorlet{PfCRelieCouleurTrace}{\useKV[ClesRelie]{Couleur}}%
  \setsepchar[*]{,*/}\reademptyitems%
  \readlist*\PfM@ListeRelie{#3}%
  \ignoreemptyitems%
  \IfBooleanTF{#1}{%
    \ifemptyKV[ClesRelie]{Graine}{}{\edef\PfCRetiensGraine{\PfCGraineAAfficher}\PfCGraineAlea{\useKV[ClesRelie]{Graine}}}%
    \PfC@buildtabrelieauto%
    \par
    \ifboolKV[ClesRelie]{Solution}{%
      \xintFor* ##1 in {\xintSeq {1}{\PfM@ListeRelielen}}\do{%
        \itemtomacro\PfCListeReponsesMelangees[##1]\PfCNumReponses%
        \itemtomacro\PfM@ListeRelie[\PfCNumReponses,1]\untest%
        % Le test est \untest\\
        \ifx\bla\untest\bla%
        \else%
          \tikz[remember picture,overlay]{\draw[PfCRelieCouleurTrace] (RelieG-\PfCNumReponses) -- (RelieD-##1);}%
        \fi%
      }%
    }{%
    }%
    \ifemptyKV[ClesRelie]{Graine}{}{\PfCGraineAlea{\PfCRetiensGraine}}%
  }{%
    \PfC@buildtabrelie%
    \ifboolKV[ClesRelie]{Solution}{%
      \xintFor* ##1 in {\xintSeq {1}{\PfM@ListeRelielen}}\do{%
        \itemtomacro\PfM@ListeRelie[##1,1]\untest%
        \ifx\bla\untest\bla%
        \else%
          \itemtomacro\PfM@ListeRelie[##1,3]\Foo%
          \setsepchar{-}\ignoreemptyitems%
          \readlist*\PfM@ListeRelieReponses{\Foo}%
          \reademptyitems%
          \foreachitem\compteur\in\PfM@ListeRelieReponses{%
            \tikz[remember picture,overlay]{\draw[PfCRelieCouleurTrace] (RelieG-##1) -- (RelieD-\PfM@ListeRelieReponses[\compteurcnt]);}%
            }%
        \fi%
      }%
    }{%
    }%
  }%
}%

\def\PfC@buildtabrelie{%
  \setcounter{NbRelie}{0}%
  \setcounter{NbRelieD}{0}%
  \renewcommand{\arraystretch}{\useKV[ClesRelie]{Stretch}}%
  \begin{tabular}{p{\useKV[ClesRelie]{LargeurG}}cp{\useKV[ClesRelie]{Ecart}}cp{\useKV[ClesRelie]{LargeurD}}}%
    \xintFor* ##1 in {\xintSeq {1}{\PfM@ListeRelielen}}\do{%
    \PfM@ListeRelie[##1,1]\itemtomacro\PfM@ListeRelie[##1,1]\untest%
    \ifx\bla\untest\bla%                                     
    \uppercase{&}\stepcounter{NbRelie}%
      \else%
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
                   picture,overlay]{\node[name=RelieG-\theNbRelie,inner
                   sep=0pt]{};\fill[]
                   (RelieG-\theNbRelie) circle[radius=1.5pt];}
                   \fi&&
                         \itemtomacro\PfM@ListeRelie[##1,2]\untest
                         \ifx\bla\untest\bla\else\stepcounter{NbRelieD}\tikz[remember
                         picture]{\node[name=RelieD-\theNbRelieD,inner
                         sep=0pt]{};\fill[] (RelieD-\theNbRelieD) circle[radius=1.5pt]}
                         \fi
    &\itemtomacro\PfM@ListeRelie[##1,2]\untest%
                          \ifx\bla\untest\bla
                          \else
                          \PfM@ListeRelie[##1,2]
                          \fi\\}%
  \end{tabular}%
}%

\NewDocumentCommand\PfC@buildtabrelieauto{}{%
  \setcounter{NbRelie}{0}%
  \setcounter{NbRelieD}{0}%
  \xdef\PfCFooListeNbQ{1}%
  \xintFor* ##1 in{\xintSeq{2}{\PfM@ListeRelielen}}\do{%
    \xdef\PfCFooListeNbQ{\PfCFooListeNbQ,##1}%
  }%
  \setsepchar{,}\ignoreemptyitems%
  \MelangeListe{\PfCFooListeNbQ}{\PfM@ListeRelielen}%
  \setsepchar{,}\ignoreemptyitems%
  \readlist*\PfCListeReponsesMelangees{\faa}%
  \renewcommand{\arraystretch}{\useKV[ClesRelie]{Stretch}}%
  \begin{tabular}{p{\useKV[ClesRelie]{LargeurG}}cp{\useKV[ClesRelie]{Ecart}}>{\tikz[remember
          picture]{\node[name=RelieD-\theNbRelie,inner
            sep=0pt]{};\fill[] (RelieD-\theNbRelie) circle[radius=1.5pt]}}cp{\useKV[ClesRelie]{LargeurD}}}%
    \xintFor* ##1 in {\xintSeq {1}{\PfM@ListeRelielen}}\do{%
      {\PfM@ListeRelie[##1,1]}\itemtomacro\PfM@ListeRelie[##1,1]\untest%
      \ifx\bla\untest\bla%                                     
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
        picture,overlay]{\node[name=RelieG-\theNbRelie,inner
          sep=0pt]{};}%
      \else%
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
        picture,overlay]{\node[name=RelieG-\theNbRelie,inner
          sep=0pt]{};\fill[]
        (RelieG-\theNbRelie) circle[radius=1.5pt];}%
                   \fi&&&\itemtomacro\PfCListeReponsesMelangees[##1]\PfCNumReponses{\PfM@ListeRelie[\PfCNumReponses,2]}\\%
    }%
  \end{tabular}%
  \setcounter{NbRelie}{0}%
}%
\makeatother

