22
33namespace App \Form \Type ;
44
5+ use App \Entity \Clarification ;
56use App \Entity \ContestProblem ;
67use App \Entity \Team ;
78use App \Service \ConfigurationService ;
89use App \Service \DOMJudgeService ;
910use Doctrine \ORM \EntityManagerInterface ;
1011use Symfony \Component \Form \AbstractType ;
1112use Symfony \Component \Form \Extension \Core \Type \ChoiceType ;
13+ use Symfony \Component \Form \Extension \Core \Type \HiddenType ;
1214use Symfony \Component \Form \Extension \Core \Type \TextareaType ;
1315use Symfony \Component \Form \FormBuilderInterface ;
1416use Symfony \Component \OptionsResolver \OptionsResolver ;
1517use Symfony \Component \Validator \Constraints \NotEqualTo ;
18+ use Symfony \Component \Validator \Constraints \Callback ;
19+ use Symfony \Component \Validator \Context \ExecutionContextInterface ;
1620
1721class JuryClarificationType extends AbstractType
1822{
1923 public const RECIPIENT_MUST_SELECT = 'domjudge-must-select ' ;
2024
25+ /** @var int The clarification entity id if the entity exists in the database */
26+ private $ clarid ;
27+
2128 public function __construct (
2229 private readonly EntityManagerInterface $ em ,
2330 private readonly ConfigurationService $ config ,
@@ -26,6 +33,7 @@ public function __construct(
2633
2734 public function buildForm (FormBuilderInterface $ builder , array $ options ): void
2835 {
36+ $ this ->clarid = $ options ['clarid ' ];
2937 $ recipientOptions = [
3038 '(select...) ' => static ::RECIPIENT_MUST_SELECT ,
3139 'ALL ' => '' ,
@@ -104,11 +112,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
104112 'cols ' => 85 ,
105113 ],
106114 ]);
115+
116+ $ builder ->add ('jurymember ' , HiddenType::class, [
117+ 'constraints ' => [
118+ new Callback ([$ this , 'checkJuryMember ' ])
119+ ]
120+ ]);
107121 }
108122
109123 public function configureOptions (OptionsResolver $ resolver ): void
110124 {
111125 $ resolver ->setDefault ('limit_to_team ' , null );
126+ $ resolver ->setDefault ('clarid ' , null );
112127 }
113128
114129 private function getTeamLabel (Team $ team ): string
@@ -119,4 +134,25 @@ private function getTeamLabel(Team $team): string
119134
120135 return sprintf ('%s (%s) ' , $ team ->getEffectiveName (), $ team ->getExternalId ());
121136 }
137+
138+ public function checkJuryMember (mixed $ value , ExecutionContextInterface $ context , mixed $ payload ): void
139+ {
140+ if ($ this ->clarid ) {
141+ $ juryMember = $ this ->em ->createQueryBuilder ()
142+ ->select ('clar.jury_member ' )
143+ ->from (Clarification::class, 'clar ' )
144+ ->where ('clar.clarid = :clarid ' )
145+ ->setParameter ('clarid ' , $ this ->clarid )
146+ ->getQuery ()
147+ ->getSingleResult ()['jury_member ' ];
148+
149+ // If jury member changed, and we are not currently assigned, warn.
150+ if ($ value !== $ juryMember && $ this ->dj ->getUser ()->getUserIdentifier () !== $ juryMember ) {
151+ $ context ->buildViolation ("Jury Member '%jury%' claimed this clarification in the meantime.
152+ Please resubmit if you want to continue. " )
153+ ->setParameter ('%jury% ' , $ juryMember )
154+ ->addViolation ();
155+ }
156+ }
157+ }
122158}
0 commit comments