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);
 	}
diff --git a/js/autocomplete.js b/js/autocomplete.js
index 3b27deb..d3885d9 100644
--- a/js/autocomplete.js
+++ b/js/autocomplete.js
@@ -1,16 +1,3 @@
-function nomcurs(curs) {
-	if (curs == 1) return "1er";
-	if (curs == 2) return "2on";
-	if (curs == 3) return "3er";
-	if (curs == 4) return "4rt";	
-}
-
-function nomgrau(grau) {
-	if (grau == 0) return "MAT";
-	if (grau == 1) return "EST";
-	if (grau == 2) return "MAMME";
-}
-
 function autocomplete(inp, obj, act) {
   /*the autocomplete function takes two arguments,
   the text field element and an objay of possible autocompleted values:*/
@@ -43,7 +30,7 @@
 
         b.innerHTML += nomNode.substr(parts[0].length, val.length);
         b.innerHTML += "<span style='font-weight: bold; position:relative; z-index:120;'>" + nomNode.substr(parts[0].length + val.length) + "</span>";
-        b.innerHTML += " <span class='autocomplete-year'>(" + nomcurs(obj[node].curs) + " - " + nomgrau(obj[node].grau) + ")</span>";
+        b.innerHTML += " <span class='autocomplete-year'>(" + obj[node].pista + ")</span>";
 
         /*include node id to keep track of which is it*/
         b.dataset.id = node;
diff --git a/php/utils.php b/php/utils.php
index cc37c84..9598a27 100644
--- a/php/utils.php
+++ b/php/utils.php
@@ -3,8 +3,8 @@
 
 	function nomcurs($curs) {
 		$codi = (int)$curs - 1;
-		$codiany = $codi % 3;
-		$coditorn = intdiv($codi, 3);
+		$codiany = intdiv($codi, 3);
+		$coditorn = $codi % 3;
 
 		$any = "";
 		switch ($codiany) {
@@ -28,21 +28,21 @@
 		}
 
 		$torn = "";
-		switch ($torn) {
+		switch ($coditorn) {
 			case 0:
-				$torn = "matins";
+				$torn = "Matins";
 				break;
 			case 1:
-				$torn = "mixt";
+				$torn = "Mixt";
 				break;
 			case 2:
-				$torn = "tardes";
+				$torn = "Tardes";
 				break;
 			default:
 				$torn = "?";
 		}
 
-		return "$any ($torn)";
+		return "$any - $torn";
 	}
 
 	function nomgrau($grau) {
@@ -83,6 +83,10 @@
 		public function nomgrau() {
 			return nomgrau($this->grau);
 		}
+
+		public function pista() {
+			return $this->nomgrau()." - ".$this->nomcurs();
+		}
 	}
 
 	function query($query) {
@@ -131,6 +135,7 @@
 					$user["mort"] = (int)$row[6];
 					$user["md5password"] = $row[7];
 					$user["bits"] = $row[8];
+					$user["pista"] = nomgrau($row[3])." - ".nomcurs($row[2]);
 				}
 
 				array_push($users, $user);
diff --git a/ranking.php b/ranking.php
index c682d64..0bb3d72 100644
--- a/ranking.php
+++ b/ranking.php
@@ -34,7 +34,7 @@
 			<div id="rightlinks"><a href="./main.php">Tornar a la pàgina principal</a></div>
 		</header>
 
-		<p><img src="./bin/images/info.png" width="32px" /> La puntuació de cada jugador es calcula com a una suma ponderada depenent del caràcter de les seves víctimes: companys de <b>c</b>lasse (mateix curs, mateix grau), companys de <b>g</b>rau (mateix grau) i companys de <b>f</b>acultat (diferents grau). Amb puntuacions +100, +150 i +200; respectivament.</p>
+		<p><img src="./bin/images/info.png" width="32px" /> La puntuació de cada jugador es calcula com a una suma ponderada depenent del caràcter de les seves víctimes: companys de <b>c</b>lasse (mateix any d'entrada i torn, mateix grau), companys de <b>g</b>rau (mateix grau) i companys de <b>f</b>acultat (diferents graus). Amb puntuacions +100, +150 i +200; respectivament.</p>
 
 		<div id="table-container">
 			<table id="ranking" cellspacing="0" cellpadding="0">