blob: 8148c92c4379ecd8542e684581fd20177e4969de [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];
73 } else {
74 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +020075 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020076 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020077 $user["curs"] = (int)$row[2];
78 $user["grau"] = (int)$row[3];
79 $user["quimata"] = (int)$row[4];
80 $user["requested"] = (int)$row[5];
81 $user["mort"] = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +020082 $user["md5password"] = $row[7];
83 }
Andreuabbcb7e2019-09-21 18:22:14 +020084
85 array_push($users, $user);
86 }
87 $result->close();
88 } else {
Andreu2a26cd72019-09-22 01:58:23 +020089 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020090 }
Andreuabbcb7e2019-09-21 18:22:14 +020091
92 if ($id > 0) return $users[0];
93 else return $users;
94 }
95?>