Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 1 | <?php |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 2 | require_once('db.php'); |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 3 | |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 4 | function nomcurs($curs) { |
5 | if ($curs == 1) return "1r"; | ||||
6 | if ($curs == 2) return "2n"; | ||||
7 | if ($curs == 3) return "3r"; | ||||
8 | if ($curs == 4) return "4t"; | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 9 | if ($curs == 5) return "5è"; |
10 | if ($curs == 6) return "6è"; | ||||
11 | if ($curs == 7) return "7è"; | ||||
12 | if ($curs == 8) return "8è"; | ||||
13 | if ($curs > 8) return "Eternal"; | ||||
14 | return "?"; | ||||
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 15 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 16 | |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 17 | function nomgrau($grau) { |
18 | if ($grau == 0) return "MAT"; | ||||
19 | if ($grau == 1) return "EST"; | ||||
20 | if ($grau == 2) return "MAMME"; | ||||
21 | } | ||||
22 | |||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 23 | class User{ |
24 | public $id; | ||||
25 | public $nomcomplet; | ||||
26 | public $curs; | ||||
27 | public $grau; | ||||
28 | public $quimata; | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 29 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 30 | public function nom() { |
31 | $noms = explode(" ", $this->nomcomplet); | ||||
32 | return $noms[0]; | ||||
33 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 34 | |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 35 | public function nomcurs() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 36 | return nomcurs($this->curs); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 37 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 38 | |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 39 | public function nomgrau() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 40 | return nomgrau($this->grau); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 41 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 42 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 43 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 44 | function query($query) { |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 45 | global $conn; |
46 | |||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 47 | // Execute query and save result |
Andreu | 9950105 | 2019-09-22 20:09:08 +0200 | [diff] [blame] | 48 | $result = $conn->query($query); |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 49 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 50 | // Return result of query |
51 | return $result; | ||||
52 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 53 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 54 | function get_users($id = 0, $getAsObjects = true) { |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 55 | $users = []; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 56 | |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 57 | $credentials = new Credentials(); |
Adrià Vilanova Martínez | 6052433 | 2022-11-20 02:33:56 +0100 | [diff] [blame] | 58 | $usersdb = $credentials->usersdb(); |
59 | $mortsdb = $credentials->mortsdb(); | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 60 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 61 | // Prepare the query |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 62 | $query = "SELECT * FROM $usersdb"; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 63 | if ($id > 0) $query .= " WHERE id=".(int)$id; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 64 | |
65 | // Fetch the information of the user | ||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 66 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 67 | while ($row = $result->fetch_row()) { |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 68 | if ($getAsObjects) { |
69 | $user = new User(); | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 70 | $user->id = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 71 | $user->nomcomplet = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 72 | $user->curs = (int)$row[2]; |
73 | $user->grau = (int)$row[3]; | ||||
74 | $user->quimata = (int)$row[4]; | ||||
75 | $user->requested = (int)$row[5]; | ||||
76 | $user->mort = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 77 | $user->md5password = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 78 | $user->bits = $row[8]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 79 | } else { |
80 | $user = []; | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 81 | $user["id"] = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 82 | $user["nomcomplet"] = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 83 | $user["curs"] = (int)$row[2]; |
84 | $user["grau"] = (int)$row[3]; | ||||
85 | $user["quimata"] = (int)$row[4]; | ||||
86 | $user["requested"] = (int)$row[5]; | ||||
87 | $user["mort"] = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 88 | $user["md5password"] = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 89 | $user["bits"] = $row[8]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 90 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 91 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 92 | array_push($users, $user); |
93 | } | ||||
94 | $result->close(); | ||||
95 | } else { | ||||
Andreu | 2a26cd7 | 2019-09-22 01:58:23 +0200 | [diff] [blame] | 96 | die("Query failed: " . $query); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 97 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 98 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 99 | if ($id > 0) return $users[0]; |
100 | else return $users; | ||||
101 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 102 | |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 103 | // Number n to XXXXXXXXX with X = {0,1} binary format |
104 | function dec2bits($code) { | ||||
105 | $bits = decbin($code); | ||||
106 | while (strlen($bits) < 9) $bits = '0' . $bits; | ||||
107 | return $bits; | ||||
108 | } | ||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 109 | ?> |