Adapt user insertion and logic to Física UB

Change-Id: Iece1abb1e2611c7fc77f379dca89ce3aead1a2d7
diff --git a/admin/index.php b/admin/index.php
index bf6078c..f376dd3 100644
--- a/admin/index.php
+++ b/admin/index.php
@@ -9,6 +9,7 @@
 <form action="insert.php" method="POST" enctype="multipart/form-data">
 	<label>Insert CSV: <input type="file" name="csvname" /></label>
 	<input type="text" name="dbname" placeholder="Nom de la base de dades" value="<?=isset($_GET['dbname']) ? $_GET['dbname'] : ''?>" />
+	<input type="text" name="forcepos" placeholder="Posicions forçades (ex: [{'id': 0, 'pos': 2}])">
 	<input type="password" name="token" placeholder="Token">
 	<input type="submit" />
 </form>
diff --git a/admin/inscrits.csv b/admin/inscrits.csv
index a72196d..831587f 100644
--- a/admin/inscrits.csv
+++ b/admin/inscrits.csv
@@ -1,101 +1,3 @@
-nom,curs,grau
-Leyla Rosenbarg,4,0
-Conny Swinford,2,0
-Nadya Gillio,4,1
-Tillie Care,4,0
-Lynnett Perceval,3,1
-Hunter Kneeland,4,0
-Eileen Fells,3,1
-Jessamine Gowler,2,0
-Cleve Ebbers,4,0
-Josee Harrap,3,1
-Frederik Merchant,3,1
-Leo Mattedi,4,0
-Cynde Bickell,3,1
-Peyton Fenne,4,1
-Noella Turneux,4,1
-Lib Langeley,3,1
-Benjie Hopewell,2,0
-Valerie Sherlaw,4,0
-Mira Willgoose,3,1
-Cory Gullam,4,0
-Amalita Melmoth,4,1
-Laverne Paxforde,4,0
-Caroline Burfitt,4,0
-Moses Glawsop,3,0
-Cristionna Hallede,4,1
-Vinnie De Brett,1,0
-Tripp Jeannin,4,1
-Netta Lejeune,3,1
-Hyacinth Grigorio,1,1
-Germain Groucock,4,1
-Merle Demanche,2,1
-Donica Chalmers,1,1
-Shel Lassen,2,1
-Quint Enocksson,3,1
-Codie Tomik,3,1
-Jessalin Land,2,1
-Woodman Pattinson,1,0
-Matias Arnull,1,1
-Rafael Coburn,3,0
-Maiga Nurden,3,1
-Latrena McDowell,4,1
-Dael Sammes,3,0
-Maribeth Oldnall,3,1
-Duncan Beacham,4,0
-Adelaide Knewstub,2,1
-Prince Gotter,3,0
-Mignonne Reiglar,4,1
-Doll Esland,3,0
-Penelope Capner,3,0
-Mina Abelovitz,2,1
-Damaris Gerrelt,3,0
-Waverly Pockey,1,1
-Portie Crewther,4,0
-Esmaria Girodin,1,1
-Homerus Keeton,1,0
-Georgiana Rallin,4,0
-Ammamaria Churn,3,0
-Kirby Grunwald,1,1
-Kenna Meadway,4,1
-Evania Dri,4,0
-Colas Pettiford,4,0
-Raven Gorrick,1,1
-Sunny Renouf,3,0
-Darice MacCarter,3,1
-Evyn Bloxsum,4,0
-Willette Lantoph,4,1
-Wells Murtell,3,1
-Paxon Poley,4,1
-Jamaal Barenskie,4,0
-Damon Driffe,4,1
-Danya Holborn,2,0
-Emmi Fairney,1,1
-Kare Ottley,3,0
-Marysa Carbine,4,1
-Ginni Hilhouse,4,1
-Lisa Lunt,4,0
-Vivyan Goldstein,2,1
-Dalt Goaley,4,1
-Antony Poulgreen,4,1
-Courtney Jodkowski,1,0
-Joela Yerrill,4,1
-Armstrong Shillito,4,0
-Garry Vaudin,3,0
-Margaretha Cartmell,1,1
-Wendall Clorley,2,0
-Pablo Janusz,3,1
-Neddy Pirouet,4,0
-Ardene Rhydderch,2,0
-Cassey Pech,3,0
-Dody Happert,2,0
-Lilas Sach,3,0
-Winthrop Sidon,4,0
-Wainwright Ginley,3,0
-Colas Ardern,4,1
-Grace Monni,1,1
-Husain Buglar,4,1
-Andres Gay,2,0
-Krissie Ledwith,4,1
-Lloyd Laval,4,0
-Sharyl Wooffitt,4,0
+nom,curs,grau,contrasenya,correu
+Persona 1,3,1,123456,persona1@example.com
+Persona 2,1,4,123456,persona2@example.com
diff --git a/admin/insert.php b/admin/insert.php
index 3a22ca4..5a50a8f 100644
--- a/admin/insert.php
+++ b/admin/insert.php
@@ -13,16 +13,46 @@
 	// Read from CSV
 	$inscrits = array_map('str_getcsv', file($csvname));
 	array_shift($inscrits); // remove header
+
+	foreach ($inscrits as $key => &$inscrit) {
+		$inscrit["key"] = $key;
+	}
+
 	shuffle($inscrits); // shuffle randomly
-	
+
+	$forceposraw = $_POST['forcepos'] ?? "[]";
+	$forcepos = json_decode($forceposraw, true);
+	foreach ($forcepos as $pos) {
+		if (!isset($pos["id"]) || !isset($pos["pos"])) continue;
+
+		// Switch entry with key |$pos["id"]| to position |$pos["pos"]|.
+		$key = -1;
+		foreach ($inscrits as $itKey => &$inscrit) {
+			if ($inscrit["key"] == $pos["id"]) {
+				$key = $itKey;
+				break;
+			}
+		}
+		if ($key === -1) continue;
+
+		// Switch places
+		$tmp = $inscrits[$pos["pos"]];
+		$inscrits[$pos["pos"]] = $inscrits[$key];
+		$inscrits[$key] = $tmp;
+	}
+
 	$start = 1;
 	$i = $start;
 	foreach ($inscrits as $user) {
 		$i = ($i + 1) % ($start + count($inscrits));
 		if ($i == 0) $i = $i + 1;
+		$nom = mysqli_real_escape_string($conn, $user[0]);
+		$curs = mysqli_real_escape_string($conn, $user[1]);
+		$grau = mysqli_real_escape_string($conn, $user[2]);
+		$password = mysqli_real_escape_string($conn, password_hash($user[3], PASSWORD_DEFAULT));
 		// Input values into SQL values
 		$template = "INSERT INTO `$dbname` (`id`, `nom`, `curs`, `grau`, `quimata`, `requested`, `mort`, `password`, `bits`)" .
-					" VALUES (NULL, '$user[0]', '$user[1]', '$user[2]', $i, 0, 0, '', ".(int)rand(1,512).")";
+					" VALUES (NULL, '$nom', '$curs', '$grau', $i, 0, 0, '$password', ".(int)rand(1,512).")";
 
 		if (!query($template)) die("An error ocurred." . $template);
 	}