blob: 9598a271071b851914543897c781fc962ce4c2f4 [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) {
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +01005 $codi = (int)$curs - 1;
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +01006 $codiany = intdiv($codi, 3);
7 $coditorn = $codi % 3;
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +01008
9 $any = "";
10 switch ($codiany) {
11 case 0:
12 $any = "2018 o abans";
13 break;
14 case 1:
15 $any = "2019";
16 break;
17 case 2:
18 $any = "2020";
19 break;
20 case 3:
21 $any = "2021";
22 break;
23 case 4:
24 $any = "2022";
25 break;
26 default:
27 $any = "?";
28 }
29
30 $torn = "";
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010031 switch ($coditorn) {
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +010032 case 0:
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010033 $torn = "Matins";
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +010034 break;
35 case 1:
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010036 $torn = "Mixt";
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +010037 break;
38 case 2:
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010039 $torn = "Tardes";
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +010040 break;
41 default:
42 $torn = "?";
43 }
44
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010045 return "$any - $torn";
Andreuae44f202019-09-23 23:15:56 +020046 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010047
Andreuae44f202019-09-23 23:15:56 +020048 function nomgrau($grau) {
Adrià Vilanova Martíneza7082722022-12-10 20:30:53 +010049 switch ($grau) {
50 case 1:
51 return "Física";
52 case 2:
53 return "Química";
54 case 3:
55 return "Electrònica";
56 case 4:
57 return "Materials";
58 case 5:
59 return "Màster astrofísica i partícules";
60 case 6:
61 return "Màster fotònica";
62 default:
63 return "?";
64 }
Andreuae44f202019-09-23 23:15:56 +020065 }
66
Andreuabbcb7e2019-09-21 18:22:14 +020067 class User{
68 public $id;
69 public $nomcomplet;
70 public $curs;
71 public $grau;
72 public $quimata;
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010073
Andreuabbcb7e2019-09-21 18:22:14 +020074 public function nom() {
75 $noms = explode(" ", $this->nomcomplet);
76 return $noms[0];
77 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010078
Andreu4b2fbd92019-09-22 22:30:25 +020079 public function nomcurs() {
Andreuae44f202019-09-23 23:15:56 +020080 return nomcurs($this->curs);
Andreu4b2fbd92019-09-22 22:30:25 +020081 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010082
Andreu4b2fbd92019-09-22 22:30:25 +020083 public function nomgrau() {
Andreuae44f202019-09-23 23:15:56 +020084 return nomgrau($this->grau);
Andreu4b2fbd92019-09-22 22:30:25 +020085 }
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +010086
87 public function pista() {
88 return $this->nomgrau()." - ".$this->nomcurs();
89 }
Andreuabbcb7e2019-09-21 18:22:14 +020090 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010091
Andreu20cbd1d2019-09-22 00:00:57 +020092 function query($query) {
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010093 global $conn;
94
Andreu20cbd1d2019-09-22 00:00:57 +020095 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020096 $result = $conn->query($query);
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010097
Andreu20cbd1d2019-09-22 00:00:57 +020098 // Return result of query
99 return $result;
100 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100101
Andreudd6cfb42019-09-22 19:52:39 +0200102 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +0200103 $users = [];
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100104
Andreu20af6c22019-09-24 18:33:50 +0200105 $credentials = new Credentials();
Adrià Vilanova Martínez60524332022-11-20 02:33:56 +0100106 $usersdb = $credentials->usersdb();
107 $mortsdb = $credentials->mortsdb();
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100108
Andreu20cbd1d2019-09-22 00:00:57 +0200109 // Prepare the query
Andreu20af6c22019-09-24 18:33:50 +0200110 $query = "SELECT * FROM $usersdb";
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100111 if ($id > 0) $query .= " WHERE id=".(int)$id;
Andreuabbcb7e2019-09-21 18:22:14 +0200112
113 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +0200114 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +0200115 while ($row = $result->fetch_row()) {
Andreudd6cfb42019-09-22 19:52:39 +0200116 if ($getAsObjects) {
117 $user = new User();
Andreu4b2fbd92019-09-22 22:30:25 +0200118 $user->id = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +0200119 $user->nomcomplet = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +0200120 $user->curs = (int)$row[2];
121 $user->grau = (int)$row[3];
122 $user->quimata = (int)$row[4];
123 $user->requested = (int)$row[5];
124 $user->mort = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +0200125 $user->md5password = $row[7];
Andreu779c3b72019-09-24 03:14:35 +0200126 $user->bits = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +0200127 } else {
128 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +0200129 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +0200130 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +0200131 $user["curs"] = (int)$row[2];
132 $user["grau"] = (int)$row[3];
133 $user["quimata"] = (int)$row[4];
134 $user["requested"] = (int)$row[5];
135 $user["mort"] = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +0200136 $user["md5password"] = $row[7];
Andreu779c3b72019-09-24 03:14:35 +0200137 $user["bits"] = $row[8];
Adrià Vilanova Martínez4861da62022-12-10 21:58:42 +0100138 $user["pista"] = nomgrau($row[3])." - ".nomcurs($row[2]);
Andreudd6cfb42019-09-22 19:52:39 +0200139 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100140
Andreuabbcb7e2019-09-21 18:22:14 +0200141 array_push($users, $user);
142 }
143 $result->close();
144 } else {
Andreu2a26cd72019-09-22 01:58:23 +0200145 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +0200146 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100147
Andreuabbcb7e2019-09-21 18:22:14 +0200148 if ($id > 0) return $users[0];
149 else return $users;
150 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100151
Andreu779c3b72019-09-24 03:14:35 +0200152 // Number n to XXXXXXXXX with X = {0,1} binary format
153 function dec2bits($code) {
154 $bits = decbin($code);
155 while (strlen($bits) < 9) $bits = '0' . $bits;
156 return $bits;
157 }
Andreuabbcb7e2019-09-21 18:22:14 +0200158?>