blob: 9598a271071b851914543897c781fc962ce4c2f4 [file] [log] [blame]
<?php
require_once('db.php');
function nomcurs($curs) {
$codi = (int)$curs - 1;
$codiany = intdiv($codi, 3);
$coditorn = $codi % 3;
$any = "";
switch ($codiany) {
case 0:
$any = "2018 o abans";
break;
case 1:
$any = "2019";
break;
case 2:
$any = "2020";
break;
case 3:
$any = "2021";
break;
case 4:
$any = "2022";
break;
default:
$any = "?";
}
$torn = "";
switch ($coditorn) {
case 0:
$torn = "Matins";
break;
case 1:
$torn = "Mixt";
break;
case 2:
$torn = "Tardes";
break;
default:
$torn = "?";
}
return "$any - $torn";
}
function nomgrau($grau) {
switch ($grau) {
case 1:
return "Física";
case 2:
return "Química";
case 3:
return "Electrònica";
case 4:
return "Materials";
case 5:
return "Màster astrofísica i partícules";
case 6:
return "Màster fotònica";
default:
return "?";
}
}
class User{
public $id;
public $nomcomplet;
public $curs;
public $grau;
public $quimata;
public function nom() {
$noms = explode(" ", $this->nomcomplet);
return $noms[0];
}
public function nomcurs() {
return nomcurs($this->curs);
}
public function nomgrau() {
return nomgrau($this->grau);
}
public function pista() {
return $this->nomgrau()." - ".$this->nomcurs();
}
}
function query($query) {
global $conn;
// Execute query and save result
$result = $conn->query($query);
// Return result of query
return $result;
}
function get_users($id = 0, $getAsObjects = true) {
$users = [];
$credentials = new Credentials();
$usersdb = $credentials->usersdb();
$mortsdb = $credentials->mortsdb();
// Prepare the query
$query = "SELECT * FROM $usersdb";
if ($id > 0) $query .= " WHERE id=".(int)$id;
// Fetch the information of the user
if ($result = query($query)) {
while ($row = $result->fetch_row()) {
if ($getAsObjects) {
$user = new User();
$user->id = (int)$row[0];
$user->nomcomplet = $row[1];
$user->curs = (int)$row[2];
$user->grau = (int)$row[3];
$user->quimata = (int)$row[4];
$user->requested = (int)$row[5];
$user->mort = (int)$row[6];
$user->md5password = $row[7];
$user->bits = $row[8];
} else {
$user = [];
$user["id"] = (int)$row[0];
$user["nomcomplet"] = $row[1];
$user["curs"] = (int)$row[2];
$user["grau"] = (int)$row[3];
$user["quimata"] = (int)$row[4];
$user["requested"] = (int)$row[5];
$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);
}
$result->close();
} else {
die("Query failed: " . $query);
}
if ($id > 0) return $users[0];
else return $users;
}
// Number n to XXXXXXXXX with X = {0,1} binary format
function dec2bits($code) {
$bits = decbin($code);
while (strlen($bits) < 9) $bits = '0' . $bits;
return $bits;
}
?>