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) { |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 5 | $codi = (int)$curs - 1; |
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 6 | $codiany = intdiv($codi, 3); |
7 | $coditorn = $codi % 3; | ||||
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 8 | |
9 | $any = ""; | ||||
10 | switch ($codiany) { | ||||
11 | case 0: | ||||
12 | $any = "2018 o abans"; | ||||
13 | break; | ||||
14 | case 1: | ||||
15 | $any = "2019"; | ||||
16 | break; | ||||
17 | case 2: | ||||
18 | $any = "2020"; | ||||
19 | break; | ||||
20 | case 3: | ||||
21 | $any = "2021"; | ||||
22 | break; | ||||
23 | case 4: | ||||
24 | $any = "2022"; | ||||
25 | break; | ||||
26 | default: | ||||
27 | $any = "?"; | ||||
28 | } | ||||
29 | |||||
30 | $torn = ""; | ||||
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 31 | switch ($coditorn) { |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 32 | case 0: |
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 33 | $torn = "Matins"; |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 34 | break; |
35 | case 1: | ||||
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 36 | $torn = "Mixt"; |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 37 | break; |
38 | case 2: | ||||
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 39 | $torn = "Tardes"; |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 40 | break; |
41 | default: | ||||
42 | $torn = "?"; | ||||
43 | } | ||||
44 | |||||
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 45 | return "$any - $torn"; |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 46 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 47 | |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 48 | function nomgrau($grau) { |
Adrià Vilanova Martínez | a708272 | 2022-12-10 20:30:53 +0100 | [diff] [blame] | 49 | switch ($grau) { |
50 | case 1: | ||||
51 | return "Física"; | ||||
52 | case 2: | ||||
53 | return "Química"; | ||||
54 | case 3: | ||||
55 | return "Electrònica"; | ||||
56 | case 4: | ||||
57 | return "Materials"; | ||||
58 | case 5: | ||||
59 | return "Màster astrofísica i partícules"; | ||||
60 | case 6: | ||||
61 | return "Màster fotònica"; | ||||
62 | default: | ||||
63 | return "?"; | ||||
64 | } | ||||
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 65 | } |
66 | |||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 67 | class User{ |
68 | public $id; | ||||
69 | public $nomcomplet; | ||||
70 | public $curs; | ||||
71 | public $grau; | ||||
72 | public $quimata; | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 73 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 74 | public function nom() { |
75 | $noms = explode(" ", $this->nomcomplet); | ||||
76 | return $noms[0]; | ||||
77 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 78 | |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 79 | public function nomcurs() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 80 | return nomcurs($this->curs); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 81 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 82 | |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 83 | public function nomgrau() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 84 | return nomgrau($this->grau); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 85 | } |
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 86 | |
87 | public function pista() { | ||||
88 | return $this->nomgrau()." - ".$this->nomcurs(); | ||||
89 | } | ||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 90 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 91 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 92 | function query($query) { |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 93 | global $conn; |
94 | |||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 95 | // Execute query and save result |
Andreu | 9950105 | 2019-09-22 20:09:08 +0200 | [diff] [blame] | 96 | $result = $conn->query($query); |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 97 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 98 | // Return result of query |
99 | return $result; | ||||
100 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 101 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 102 | function get_users($id = 0, $getAsObjects = true) { |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 103 | $users = []; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 104 | |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 105 | $credentials = new Credentials(); |
Adrià Vilanova Martínez | 6052433 | 2022-11-20 02:33:56 +0100 | [diff] [blame] | 106 | $usersdb = $credentials->usersdb(); |
107 | $mortsdb = $credentials->mortsdb(); | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 108 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 109 | // Prepare the query |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 110 | $query = "SELECT * FROM $usersdb"; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 111 | if ($id > 0) $query .= " WHERE id=".(int)$id; |
Adrià Vilanova Martínez | bcc4f6e | 2022-12-12 00:05:29 +0100 | [diff] [blame^] | 112 | $query .= " ORDER BY nom"; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 113 | |
114 | // Fetch the information of the user | ||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 115 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 116 | while ($row = $result->fetch_row()) { |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 117 | if ($getAsObjects) { |
118 | $user = new User(); | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 119 | $user->id = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 120 | $user->nomcomplet = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 121 | $user->curs = (int)$row[2]; |
122 | $user->grau = (int)$row[3]; | ||||
123 | $user->quimata = (int)$row[4]; | ||||
124 | $user->requested = (int)$row[5]; | ||||
125 | $user->mort = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 126 | $user->md5password = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 127 | $user->bits = $row[8]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 128 | } else { |
129 | $user = []; | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 130 | $user["id"] = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 131 | $user["nomcomplet"] = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 132 | $user["curs"] = (int)$row[2]; |
133 | $user["grau"] = (int)$row[3]; | ||||
134 | $user["quimata"] = (int)$row[4]; | ||||
135 | $user["requested"] = (int)$row[5]; | ||||
136 | $user["mort"] = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 137 | $user["md5password"] = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 138 | $user["bits"] = $row[8]; |
Adrià Vilanova Martínez | 4861da6 | 2022-12-10 21:58:42 +0100 | [diff] [blame] | 139 | $user["pista"] = nomgrau($row[3])." - ".nomcurs($row[2]); |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 140 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 141 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 142 | array_push($users, $user); |
143 | } | ||||
144 | $result->close(); | ||||
145 | } else { | ||||
Andreu | 2a26cd7 | 2019-09-22 01:58:23 +0200 | [diff] [blame] | 146 | die("Query failed: " . $query); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 147 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 148 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 149 | if ($id > 0) return $users[0]; |
150 | else return $users; | ||||
151 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 152 | |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 153 | // Number n to XXXXXXXXX with X = {0,1} binary format |
154 | function dec2bits($code) { | ||||
155 | $bits = decbin($code); | ||||
156 | while (strlen($bits) < 9) $bits = '0' . $bits; | ||||
157 | return $bits; | ||||
158 | } | ||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 159 | ?> |