blob: abd519e034c51aa93aa56903516fdef97f967c80 [file] [log] [blame]
Andreuabbcb7e2019-09-21 18:22:14 +02001<?php
Andreuae44f202019-09-23 23:15:56 +02002 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
Andreuabbcb7e2019-09-21 18:22:14 +020015 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 }
Andreu4b2fbd92019-09-22 22:30:25 +020026
27 public function nomcurs() {
Andreuae44f202019-09-23 23:15:56 +020028 return nomcurs($this->curs);
Andreu4b2fbd92019-09-22 22:30:25 +020029 }
30
31 public function nomgrau() {
Andreuae44f202019-09-23 23:15:56 +020032 return nomgrau($this->grau);
Andreu4b2fbd92019-09-22 22:30:25 +020033 }
Andreuabbcb7e2019-09-21 18:22:14 +020034 }
35
Andreu20cbd1d2019-09-22 00:00:57 +020036 function query($query) {
Andreuabbcb7e2019-09-21 18:22:14 +020037 // Create connection
Andreu2457e402019-09-22 00:52:41 +020038 $credentials = new Credentials();
39 $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname);
Andreuabbcb7e2019-09-21 18:22:14 +020040 if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
41 $conn->set_charset("utf8");
42
Andreu20cbd1d2019-09-22 00:00:57 +020043 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020044 $result = $conn->query($query);
Andreu20cbd1d2019-09-22 00:00:57 +020045
46 // Close the connection
47 $conn->close();
48
49 // Return result of query
50 return $result;
51 }
52
Andreudd6cfb42019-09-22 19:52:39 +020053 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020054 $users = [];
55
56 // Prepare the query
Andreuabbcb7e2019-09-21 18:22:14 +020057 $query = "SELECT * FROM users";
58 if ($id > 0) $query .= " WHERE id=".$id;
59
60 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +020061 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +020062 while ($row = $result->fetch_row()) {
Andreudd6cfb42019-09-22 19:52:39 +020063 if ($getAsObjects) {
64 $user = new User();
Andreu4b2fbd92019-09-22 22:30:25 +020065 $user->id = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020066 $user->nomcomplet = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020067 $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];
Andreudd6cfb42019-09-22 19:52:39 +020072 $user->md5password = $row[7];
Andreu779c3b72019-09-24 03:14:35 +020073 $user->bits = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +020074 } else {
75 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +020076 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020077 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020078 $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];
Andreudd6cfb42019-09-22 19:52:39 +020083 $user["md5password"] = $row[7];
Andreu779c3b72019-09-24 03:14:35 +020084 $user["bits"] = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +020085 }
Andreuabbcb7e2019-09-21 18:22:14 +020086
87 array_push($users, $user);
88 }
89 $result->close();
90 } else {
Andreu2a26cd72019-09-22 01:58:23 +020091 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020092 }
Andreuabbcb7e2019-09-21 18:22:14 +020093
94 if ($id > 0) return $users[0];
95 else return $users;
96 }
Andreu779c3b72019-09-24 03:14:35 +020097
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 }
Andreuabbcb7e2019-09-21 18:22:14 +0200104?>