Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 1 | <?php |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 2 | |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 3 | function nomcurs($curs) { |
| 4 | if ($curs == 1) return "1r"; |
| 5 | if ($curs == 2) return "2n"; |
| 6 | if ($curs == 3) return "3r"; |
| 7 | if ($curs == 4) return "4t"; |
| 8 | } |
| 9 | |
| 10 | function nomgrau($grau) { |
| 11 | if ($grau == 0) return "MAT"; |
| 12 | if ($grau == 1) return "EST"; |
| 13 | if ($grau == 2) return "MAMME"; |
| 14 | } |
| 15 | |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 16 | class User{ |
| 17 | public $id; |
| 18 | public $nomcomplet; |
| 19 | public $curs; |
| 20 | public $grau; |
| 21 | public $quimata; |
| 22 | |
| 23 | public function nom() { |
| 24 | $noms = explode(" ", $this->nomcomplet); |
| 25 | return $noms[0]; |
| 26 | } |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 27 | |
| 28 | public function nomcurs() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 29 | return nomcurs($this->curs); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | public function nomgrau() { |
Andreu | ae44f20 | 2019-09-23 23:15:56 +0200 | [diff] [blame] | 33 | return nomgrau($this->grau); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 34 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 37 | function query($query) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 38 | // Create connection |
Andreu | 2457e40 | 2019-09-22 00:52:41 +0200 | [diff] [blame] | 39 | $credentials = new Credentials(); |
| 40 | $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 41 | if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); |
| 42 | $conn->set_charset("utf8"); |
| 43 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 44 | // Execute query and save result |
Andreu | 9950105 | 2019-09-22 20:09:08 +0200 | [diff] [blame] | 45 | $result = $conn->query($query); |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 46 | |
| 47 | // Close the connection |
| 48 | $conn->close(); |
| 49 | |
| 50 | // Return result of query |
| 51 | return $result; |
| 52 | } |
| 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 = []; |
| 56 | |
Andreu | 20af6c2 | 2019-09-24 18:33:50 +0200 | [diff] [blame] | 57 | $credentials = new Credentials(); |
| 58 | $usersdb = $credentials->usersdb; |
| 59 | $mortsdb = $credentials->mortsdb; |
| 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"; |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 63 | if ($id > 0) $query .= " WHERE id=".$id; |
| 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 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 91 | |
| 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 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 98 | |
| 99 | if ($id > 0) return $users[0]; |
| 100 | else return $users; |
| 101 | } |
Andreu | 779c3b7 | 2019-09-24 03:14:35 +0200 | [diff] [blame] | 102 | |
| 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 | ?> |