blob: cc37c84475c0c9b00c475caa6be976a1f6883b9d [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;
6 $codiany = $codi % 3;
7 $coditorn = intdiv($codi, 3);
8
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 = "";
31 switch ($torn) {
32 case 0:
33 $torn = "matins";
34 break;
35 case 1:
36 $torn = "mixt";
37 break;
38 case 2:
39 $torn = "tardes";
40 break;
41 default:
42 $torn = "?";
43 }
44
45 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 }
Andreuabbcb7e2019-09-21 18:22:14 +020086 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010087
Andreu20cbd1d2019-09-22 00:00:57 +020088 function query($query) {
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010089 global $conn;
90
Andreu20cbd1d2019-09-22 00:00:57 +020091 // Execute query and save result
Andreu99501052019-09-22 20:09:08 +020092 $result = $conn->query($query);
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010093
Andreu20cbd1d2019-09-22 00:00:57 +020094 // Return result of query
95 return $result;
96 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +010097
Andreudd6cfb42019-09-22 19:52:39 +020098 function get_users($id = 0, $getAsObjects = true) {
Andreu20cbd1d2019-09-22 00:00:57 +020099 $users = [];
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100100
Andreu20af6c22019-09-24 18:33:50 +0200101 $credentials = new Credentials();
Adrià Vilanova Martínez60524332022-11-20 02:33:56 +0100102 $usersdb = $credentials->usersdb();
103 $mortsdb = $credentials->mortsdb();
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100104
Andreu20cbd1d2019-09-22 00:00:57 +0200105 // Prepare the query
Andreu20af6c22019-09-24 18:33:50 +0200106 $query = "SELECT * FROM $usersdb";
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100107 if ($id > 0) $query .= " WHERE id=".(int)$id;
Andreuabbcb7e2019-09-21 18:22:14 +0200108
109 // Fetch the information of the user
Andreu20cbd1d2019-09-22 00:00:57 +0200110 if ($result = query($query)) {
Andreuabbcb7e2019-09-21 18:22:14 +0200111 while ($row = $result->fetch_row()) {
Andreudd6cfb42019-09-22 19:52:39 +0200112 if ($getAsObjects) {
113 $user = new User();
Andreu4b2fbd92019-09-22 22:30:25 +0200114 $user->id = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +0200115 $user->nomcomplet = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +0200116 $user->curs = (int)$row[2];
117 $user->grau = (int)$row[3];
118 $user->quimata = (int)$row[4];
119 $user->requested = (int)$row[5];
120 $user->mort = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +0200121 $user->md5password = $row[7];
Andreu779c3b72019-09-24 03:14:35 +0200122 $user->bits = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +0200123 } else {
124 $user = [];
Andreu4b2fbd92019-09-22 22:30:25 +0200125 $user["id"] = (int)$row[0];
Andreudd6cfb42019-09-22 19:52:39 +0200126 $user["nomcomplet"] = $row[1];
Andreu4b2fbd92019-09-22 22:30:25 +0200127 $user["curs"] = (int)$row[2];
128 $user["grau"] = (int)$row[3];
129 $user["quimata"] = (int)$row[4];
130 $user["requested"] = (int)$row[5];
131 $user["mort"] = (int)$row[6];
Andreudd6cfb42019-09-22 19:52:39 +0200132 $user["md5password"] = $row[7];
Andreu779c3b72019-09-24 03:14:35 +0200133 $user["bits"] = $row[8];
Andreudd6cfb42019-09-22 19:52:39 +0200134 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100135
Andreuabbcb7e2019-09-21 18:22:14 +0200136 array_push($users, $user);
137 }
138 $result->close();
139 } else {
Andreu2a26cd72019-09-22 01:58:23 +0200140 die("Query failed: " . $query);
Andreuabbcb7e2019-09-21 18:22:14 +0200141 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100142
Andreuabbcb7e2019-09-21 18:22:14 +0200143 if ($id > 0) return $users[0];
144 else return $users;
145 }
Adrià Vilanova Martínez13cf0cd2022-11-20 01:02:20 +0100146
Andreu779c3b72019-09-24 03:14:35 +0200147 // Number n to XXXXXXXXX with X = {0,1} binary format
148 function dec2bits($code) {
149 $bits = decbin($code);
150 while (strlen($bits) < 9) $bits = '0' . $bits;
151 return $bits;
152 }
Andreuabbcb7e2019-09-21 18:22:14 +0200153?>