| <?php |
| require_once('db.php'); |
| |
| function nomcurs($curs) { |
| $codi = (int)$curs - 1; |
| $codiany = $codi % 3; |
| $coditorn = intdiv($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 ($torn) { |
| 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); |
| } |
| } |
| |
| 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]; |
| } |
| |
| 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; |
| } |
| ?> |