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; |
6 | $codiany = $codi % 3; | ||||
7 | $coditorn = intdiv($codi, 3); | ||||
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 = ""; | ||||
31 | switch ($torn) { | ||||
32 | case 0: | ||||
33 | $torn = "matins"; | ||||
34 | break; | ||||
35 | case 1: | ||||
36 | $torn = "mixt"; | ||||
37 | break; | ||||
38 | case 2: | ||||
39 | $torn = "tardes"; | ||||
40 | break; | ||||
41 | default: | ||||
42 | $torn = "?"; | ||||
43 | } | ||||
44 | |||||
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 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 86 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 87 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 88 | function query($query) { |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 89 | global $conn; |
90 | |||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 91 | // Execute query and save result |
Andreu | 9950105 | 2019-09-22 20:09:08 +0200 | [diff] [blame] | 92 | $result = $conn->query($query); |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 93 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 94 | // Return result of query |
95 | return $result; | ||||
96 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 97 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 98 | function get_users($id = 0, $getAsObjects = true) { |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 99 | $users = []; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 100 | |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 101 | $credentials = new Credentials(); |
Adrià Vilanova Martínez | 6052433 | 2022-11-20 02:33:56 +0100 | [diff] [blame] | 102 | $usersdb = $credentials->usersdb(); |
103 | $mortsdb = $credentials->mortsdb(); | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 104 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 105 | // Prepare the query |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 106 | $query = "SELECT * FROM $usersdb"; |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 107 | if ($id > 0) $query .= " WHERE id=".(int)$id; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 108 | |
109 | // Fetch the information of the user | ||||
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 110 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 111 | while ($row = $result->fetch_row()) { |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 112 | if ($getAsObjects) { |
113 | $user = new User(); | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 114 | $user->id = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 115 | $user->nomcomplet = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 116 | $user->curs = (int)$row[2]; |
117 | $user->grau = (int)$row[3]; | ||||
118 | $user->quimata = (int)$row[4]; | ||||
119 | $user->requested = (int)$row[5]; | ||||
120 | $user->mort = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 121 | $user->md5password = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 122 | $user->bits = $row[8]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 123 | } else { |
124 | $user = []; | ||||
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 125 | $user["id"] = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 126 | $user["nomcomplet"] = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 127 | $user["curs"] = (int)$row[2]; |
128 | $user["grau"] = (int)$row[3]; | ||||
129 | $user["quimata"] = (int)$row[4]; | ||||
130 | $user["requested"] = (int)$row[5]; | ||||
131 | $user["mort"] = (int)$row[6]; | ||||
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 132 | $user["md5password"] = $row[7]; |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 133 | $user["bits"] = $row[8]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 134 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 135 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 136 | array_push($users, $user); |
137 | } | ||||
138 | $result->close(); | ||||
139 | } else { | ||||
Andreu | 2a26cd7 | 2019-09-22 01:58:23 +0200 | [diff] [blame] | 140 | die("Query failed: " . $query); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 141 | } |
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 142 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 143 | if ($id > 0) return $users[0]; |
144 | else return $users; | ||||
145 | } | ||||
Adrià Vilanova Martínez | 13cf0cd | 2022-11-20 01:02:20 +0100 | [diff] [blame] | 146 | |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 147 | // Number n to XXXXXXXXX with X = {0,1} binary format |
148 | function dec2bits($code) { | ||||
149 | $bits = decbin($code); | ||||
150 | while (strlen($bits) < 9) $bits = '0' . $bits; | ||||
151 | return $bits; | ||||
152 | } | ||||
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 153 | ?> |