blob: 1c41567f9c9fe7063495100f2ffcd3249c66b3f1 [file] [log] [blame]
Andreuabbcb7e2019-09-21 18:22:14 +02001<?php
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +01002 require_once('db.php');
Andreu20af6c22019-09-24 18:33:50 +02003
Andreuae44f202019-09-23 23:15:56 +02004 function nomcurs($curs) {
5 if ($curs == 1) return "1r";
6 if ($curs == 2) return "2n";
7 if ($curs == 3) return "3r";
8 if ($curs == 4) return "4t";
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +01009 if ($curs == 5) return "5è";
10 if ($curs == 6) return "6è";
11 if ($curs == 7) return "7è";
12 if ($curs == 8) return "8è";
13 if ($curs > 8) return "Eternal";
14 return "?";
Andreuae44f202019-09-23 23:15:56 +020015 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010016
Andreuae44f202019-09-23 23:15:56 +020017 function nomgrau($grau) {
18 if ($grau == 0) return "MAT";
19 if ($grau == 1) return "EST";
20 if ($grau == 2) return "MAMME";
21 }
22
Andreuabbcb7e2019-09-21 18:22:14 +020023 class User{
24 public $id;
25 public $nomcomplet;
26 public $curs;
27 public $grau;
28 public $quimata;
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010029
Andreuabbcb7e2019-09-21 18:22:14 +020030 public function nom() {
31 $noms = explode(" ", $this->nomcomplet);
32 return $noms[0];
33 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010034
Andreu4b2fbd92019-09-22 22:30:25 +020035 public function nomcurs() {
Andreuae44f202019-09-23 23:15:56 +020036 return nomcurs($this->curs);
Andreu4b2fbd92019-09-22 22:30:25 +020037 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010038
Andreu4b2fbd92019-09-22 22:30:25 +020039 public function nomgrau() {
Andreuae44f202019-09-23 23:15:56 +020040 return nomgrau($this->grau);
Andreu4b2fbd92019-09-22 22:30:25 +020041 }
Andreuabbcb7e2019-09-21 18:22:14 +020042 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010043
Andreu20cbd1d2019-09-22 00:00:57 +020044 function query($query) {
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010045 global $conn;
46
Andreu20cbd1d2019-09-22 00:00:57 +020047 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020048 $result = $conn->query($query);
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010049
Andreu20cbd1d2019-09-22 00:00:57 +020050 // Return result of query
51 return $result;
52 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010053
Andreudd6cfb42019-09-22 19:52:39 +020054 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020055 $users = [];
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010056
Andreu20af6c22019-09-24 18:33:50 +020057 $credentials = new Credentials();
58 $usersdb = $credentials->usersdb;
59 $mortsdb = $credentials->mortsdb;
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010060
Andreu20cbd1d2019-09-22 00:00:57 +020061 // Prepare the query
Andreu20af6c22019-09-24 18:33:50 +020062 $query = "SELECT * FROM $usersdb";
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010063 if ($id > 0) $query .= " WHERE id=".(int)$id;
Andreuabbcb7e2019-09-21 18:22:14 +020064
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 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010091
Andreuabbcb7e2019-09-21 18:22:14 +020092 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 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010098
Andreuabbcb7e2019-09-21 18:22:14 +020099 if ($id > 0) return $users[0];
100 else return $users;
101 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100102
Andreu779c3b72019-09-24 03:14:35 +0200103 // 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?>