-
Notifications
You must be signed in to change notification settings - Fork 4
/
manage_unites.inc.php
208 lines (190 loc) · 5.25 KB
/
manage_unites.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
function cmpunits($a, $b) {
return strnatcmp( strtolower(trim($a->nickname)), strtolower(trim($b->nickname)));
}
function unitsList($all_sections = false)
{
$units = array();
if($all_sections || !isset($_SESSION['all_units']))
{
if($all_sections || isSuperUser())
$sql = "SELECT * FROM ".units_db." ORDER BY LOWER(nickname) ASC;";
else
$sql = "SELECT * FROM ".units_db." WHERE `code`!= \"\" && `section`='". real_escape_string(currentSection())."' ORDER BY LOWER(nickname) ASC;";
// $sql = "SELECT * FROM ".units_db." WHERE `section`='". real_escape_string(currentSection())."' OR `section`=\"0\" ORDER BY nickname ASC;";
// $sql = "SELECT * FROM ".units_db." WHERE `section`=\"0\" ORDER BY LOWER(nickname) ASC;";
if($result= sql_request($sql))
while ($row = mysqli_fetch_object($result))
if($row->code != "")
$units[$row->code] = $row;
$maxsize = 0;
foreach($units as $unit)
$maxsize = max($maxsize, strlen($unit->nickname));
foreach($units as $unit)
{
$l = strlen($unit->nickname);
$unit->prettyname = str_replace(" "," ", $unit->nickname);
$unit->prettyname .= str_pad("", $maxsize +10 - $l , " ")."- ".$unit->code;
}
uasort($units, 'cmpunits');
if(!$all_sections || isSuperUser())
$_SESSION['all_units'] = $units;
}
else
$units = $_SESSION['all_units'];
return $units;
}
function unitExists($code)
{
$liste = unitsList();
/* foreach($liste as $key => $value)
echo $key."<br/>";*/
return isset($liste[$code]);
}
function unitDSIExists($code)
{
$sql = "SELECT * FROM ".dsidbname.".".dsi_units_db." WHERE CODEUNITE=\"".$code."\";";
$res = sql_request($sql);
while(mysqli_fetch_object($res))
return true;
return false;
}
function createUnitIfNeeded($code)
{
if($code == "") return;
if(!unitExists($code))
{
try
{
if(unitDSIExists($code))
{
$sql = "SELECT * FROM ".dsidbname.".".dsi_units_db." WHERE CODEUNITE=\"".$code."\";";
$res = sql_request($sql);
while($row = mysqli_fetch_object($res))
{
if($row->SIGLEUNI == "") $row->SIGLEUNI = $row->CODEUNITE;
addUnit($row->SIGLEUNI,$row->CODEUNITE,$row->INTUNI,$row->NOM_DIR_UNI." ".$row->PRN_DIR_UNI);
break;
}
}
else
{
addUnit($code,$code,$code,"");
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
}
/*
function updateUnitData($unite, $data)
{
if($unite == "") return;
if(isSuperUser() && !isset($data->section))
{
echo "Superuser cannot update lab with generic section";
return;
}
global $fieldsUnitsDB;
if(unitExists($unite))
{
$sql = "";
foreach($data as $field => $value)
if($field != "section" && isset($fieldsUnitsDB[$field]) && $value != "")
$sql .= " $field='$value' ";
if($sql != "")
{
$sql = "UPDATE ".units_db." SET ".$sql;
if(isSuperUser())
$sql .= " WHERE code='$unite' AND `section`='". real_escape_string($data->section).";";
else
$sql .= " WHERE code='$unite' AND `section`='". real_escape_string($_SESSION['filter_section']).";";
sql_request($sql);
}
}
else
{
$sql = "INSERT INTO ".reports_db." ($sqlfields) VALUES ($sqlvalues);";
}
}
*/
function simpleUnitsList($short = false)
{
$units = unitsList();
$result = array();
foreach($units as $unit => $row)
$result[$unit] = $short ? $row->nickname : $row->prettyname;
return $result;
}
function addUnit($nickname, $code, $fullname, $directeur)
{
if($code == "")
throw new Exception("Cannot create unit with empty code");
$sql = "SELECT * FROM ".units_db." WHERE `code`=\"".real_escape_string($code)."\";";
$result = sql_request($sql);
if($row = mysqli_fetch_object($result))
{
if($nickname == "") $nickname = $row->nickname;
if($fullname == "") $fullname = $row->fullname;
if($directeur =="" ) $directeur = $row->directeur;
}
else
{
/* if nickname has been set we dont delete it */
if($nickname == "")
$nickname = $code;
if($fullname == "")
$fullname = $nickname;
}
unset($_SESSION['all_units']);
$sql = "DELETE FROM ".units_db." WHERE code = \"".$code."\" and section =\"".currentSection()."\";";
sql_request($sql);
$values = "\"".real_escape_string($nickname)."\",";
$values .= "\"".str_replace(' ','',real_escape_string($code))."\",";
$values .= "\"".real_escape_string($fullname)."\",";
$values .= "\"".real_escape_string($directeur)."\",";
$values .= "\"".real_escape_string(currentSection())."\"";
$sql = "INSERT INTO ".units_db." (nickname, code, fullname, directeur, section) VALUES ($values);";
sql_request($sql);
}
function deleteUnit($code)
{
unset($_SESSION['all_units']);
$sql = "DELETE FROM ".units_db." WHERE code = \"".$code."\" and section=\"".currentSection()."\";";
sql_request($sql);
}
function delete_all_units()
{
unset($_SESSION['all_units']);
if(isSuperUser())
$sql = "DELETE FROM ".units_db." WHERE 1;";
else if(isSecretaire())
$sql = "DELETE FROM ".units_db." WHERE section=\"".currentSection()."\";";
sql_request($sql);
}
/*
* Unit can be code or nickname
*/
function fromunittocode($unitdata)
{
$units = unitsList();
if(key_exists($unitdata, $units))
{
$answer = $unitdata;
return $unitdata;
}
foreach($units as $unit)
{
if(strcasecmp($unit->nickname,$unitdata) == 0 )
{
$answer = $unit->code;
return $unit->code;
}
}
addUnit($unitdata, $unitdata,$unitdata,"");
$answer = $unitdata;
return $unitdata;
}
?>