blob: edd53fa1609e8b551625f9af4ec08b0181d7822b [file] [log] [blame]
Andreuabbcb7e2019-09-21 18:22:14 +02001<?php
2 class User{
3 public $id;
4 public $nomcomplet;
5 public $curs;
6 public $grau;
7 public $quimata;
8
9 public function nom() {
10 $noms = explode(" ", $this->nomcomplet);
11 return $noms[0];
12 }
Andreu4b2fbd92019-09-22 22:30:25 +020013
14 public function nomcurs() {
15 if ($this->curs == 1) return "1er";
16 if ($this->curs == 2) return "2on";
17 if ($this->curs == 3) return "3er";
18 if ($this->curs == 4) return "4rt";
19 }
20
21 public function nomgrau() {
22 if ($this->grau == 0) return "MAT";
23 if ($this->grau == 1) return "EST";
24 if ($this->grau == 2) return "MAMME";
25 }
Andreuabbcb7e2019-09-21 18:22:14 +020026 }
27
Andreu20cbd1d2019-09-22 00:00:57 +020028 function query($query) {
Andreuabbcb7e2019-09-21 18:22:14 +020029 // Create connection
Andreu2457e402019-09-22 00:52:41 +020030 $credentials = new Credentials();
31 $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname);
Andreuabbcb7e2019-09-21 18:22:14 +020032 if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
33 $conn->set_charset("utf8");
34
Andreu20cbd1d2019-09-22 00:00:57 +020035 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020036 $result = $conn->query($query);
Andreu20cbd1d2019-09-22 00:00:57 +020037
38 // Close the connection
39 $conn->close();
40
41 // Return result of query
42 return $result;
43 }
44
Andreudd6cfb42019-09-22 19:52:39 +020045 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020046 $users = [];
47
48 // Prepare the query
Andreuabbcb7e2019-09-21 18:22:14 +020049 $query = "SELECT * FROM users";
50 if ($id > 0) $query .= " WHERE id=".$id;
51
52 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +020053 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +020054 while ($row = $result->fetch_row()) {
Andreudd6cfb42019-09-22 19:52:39 +020055 if ($getAsObjects) {
56 $user = new User();
Andreu4b2fbd92019-09-22 22:30:25 +020057 $user->id = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020058 $user->nomcomplet = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020059 $user->curs = (int)$row[2];
60 $user->grau = (int)$row[3];
61 $user->quimata = (int)$row[4];
62 $user->requested = (int)$row[5];
63 $user->mort = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +020064 $user->md5password = $row[7];
65 } else {
66 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +020067 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +020068 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +020069 $user["curs"] = (int)$row[2];
70 $user["grau"] = (int)$row[3];
71 $user["quimata"] = (int)$row[4];
72 $user["requested"] = (int)$row[5];
73 $user["mort"] = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +020074 $user["md5password"] = $row[7];
75 }
Andreuabbcb7e2019-09-21 18:22:14 +020076
77 array_push($users, $user);
78 }
79 $result->close();
80 } else {
Andreu2a26cd72019-09-22 01:58:23 +020081 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +020082 }
Andreuabbcb7e2019-09-21 18:22:14 +020083
84 if ($id > 0) return $users[0];
85 else return $users;
86 }
87?>