Yesterday, @Jonathan Wallace asked me how I would make a Secret Santa drawing program in the Wolfram Language, and to my surprise it turned out to be a really simple CloudDeploy of a FormFunction. Here is how I made it:
1. To create a FormFunction with a group of double fields to enter names and emails that can be added and deleted I used RepeatingElement with CompoundElement:
FormFunction["participants"->RepeatingElement[CompoundElement[<|"Name"->"String","Email"->"EmailAddress"|>]]]
2. The random pairing algorithm is really simple:
Thread[Rule[list = RandomSample[Range[10], 10], RotateLeft[list, 1]]]
{3->8,8->9,9->6,6->2,2->4,4->10,10->1,1->7,7->5,5->3}
3. Then I created a title for the FormFunction using the Santa popular curve:
title=Rasterize@Labeled[Entity["PopularCurve","SantaCurve"]["Image"]/.{Line->Polygon,RGBColor[0.24720000000000014`,0.24`,0.6`]->Opacity@.5,True->False},
Style["Secret Santa Generator","Title"],Top]
4. Then to deliver the drawings to the participants I Map the following SendMail function to the values obtained from the submitted form:
SendMail["To" -> #[[1, 2]], "Subject" -> #[[1, 1]] <> " BUYS FOR " <> #[[2, 1]],
"Body" -> #[[1, 1]] <> " BUYS FOR " <> #[[2, 1]] <> "\n\nSecret Santa Generator powered by the Wolfram Language."]
5. The final FormFunction that combines steps 1 to 4 should look like this:
form=FormFunction[
"participants"->RepeatingElement[CompoundElement[<|"Name"->"String","Email"->"EmailAddress"|>]],
(Map[SendMail["To"->#[[1,2]],"Subject"->#[[1,1]]<>" BUYS FOR "<>#[[2,1]],
"Body"->#[[1,1]]<>" BUYS FOR "<>#[[2,1]]<>"\n\nSecret Santa Generator \nhttps://wolfr.am/99kaasSX \n\n\t\t\t\t powered by the Wolfram Language."]&,
Thread[Rule[list=RandomSample[#participants,Length[#participants]],RotateLeft[list,1]]]];
"Thank you! The Secret Santa drawings have been delivered to all the participants!")&,
AppearanceRules-><|"Title"->title,
"Description"->"Enter the names and email addresses for the gift exchange. The Wolfram Secret Santa Generator will handle the rest!"|>,
FormTheme->"Red"]
6. Which can be shared with all the gift exchange organizers by uploading it to the Wolfram Cloud:
CloudDeploy[form, "SecretSantaGenerator", Permissions -> "Public"]
Once the organizers have submitted the list of participants, the participants will receive immediately an email with their match:
Happy Secret Santa Week!
Attachments: