blob: 49ccbb70b60a2b3d958c331c50a17f1a4949140a [file] [log] [blame]
Andreuabbcb7e2019-09-21 18:22:14 +02001<?php
Andreu20af6c22019-09-24 18:33:50 +02002
Andreuae44f202019-09-23 23:15:56 +02003 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
Andreuabbcb7e2019-09-21 18:22:14 +020016 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 }
Andreu4b2fbd92019-09-22 22:30:25 +020027
28 public function nomcurs() {
Andreuae44f202019-09-23 23:15:56 +020029 return nomcurs($this->curs);
Andreu4b2fbd92019-09-22 22:30:25 +020030 }
31
32 public function nomgrau() {
Andreuae44f202019-09-23 23:15:56 +020033 return nomgrau($this->grau);
Andreu4b2fbd92019-09-22 22:30:25 +020034 }
Andreuabbcb7e2019-09-21 18:22:14 +020035 }
36
Andreu20cbd1d2019-09-22 00:00:57 +020037 function query($query) {
Andreuabbcb7e2019-09-21 18:22:14 +020038 // Create connection
Andreu2457e402019-09-22 00:52:41 +020039 $credentials = new Credentials();
40 $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname);
Andreuabbcb7e2019-09-21 18:22:14 +020041 if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
42 $conn->set_charset("utf8");
43
Andreu20cbd1d2019-09-22 00:00:57 +020044 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020045 $result = $conn->query($query);
Andreu20cbd1d2019-09-22 00:00:57 +020046
47 // Close the connection
48 $conn->close();
49
50 // Return result of query
51 return $result;
52 }
53
Andreudd6cfb42019-09-22 19:52:39 +020054 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020055 $users = [];
56
Andreu20af6c22019-09-24 18:33:50 +020057 $credentials = new Credentials();
58 $usersdb = $credentials->usersdb;
59 $mortsdb = $credentials->mortsdb;
60
Andreu20cbd1d2019-09-22 00:00:57 +020061 // Prepare the query
Andreu20af6c22019-09-24 18:33:50 +020062 $query = "SELECT * FROM $usersdb";
Andreuabbcb7e2019-09-21 18:22:14 +020063 if ($id > 0) $query .= " WHERE id=".$id;
64
65 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +020066 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +020067 while ($row = $result->fetch_row()) {
Andreudd6cfb42019-09-22 19:52:39 +020068 if ($getAsObjects) {
69 $user = new User();
Andreu4b2fbd92019-09-22 22:30:25 +020070 $user->id = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020071 $user->nomcomplet = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020072 $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];
Andreudd6cfb42019-09-22 19:52:39 +020077 $user->md5password = $row[7];
Andreu779c3b72019-09-24 03:14:35 +020078 $user->bits = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +020079 } else {
80 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +020081 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020082 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020083 $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];
Andreudd6cfb42019-09-22 19:52:39 +020088 $user["md5password"] = $row[7];
Andreu779c3b72019-09-24 03:14:35 +020089 $user["bits"] = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +020090 }
Andreuabbcb7e2019-09-21 18:22:14 +020091
92 array_push($users, $user);
93 }
94 $result->close();
95 } else {
Andreu2a26cd72019-09-22 01:58:23 +020096 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020097 }
Andreuabbcb7e2019-09-21 18:22:14 +020098
99 if ($id > 0) return $users[0];
100 else return $users;
101 }
Andreu779c3b72019-09-24 03:14:35 +0200102
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 }
Andreuabbcb7e2019-09-21 18:22:14 +0200109?>