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