694 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
696 |
jpm |
3 |
* Service fournissant des statistiques de l'application CEL au format texte (JSON).
|
694 |
jpm |
4 |
* Encodage en entrée : utf8
|
|
|
5 |
* Encodage en sortie : utf8
|
|
|
6 |
*
|
|
|
7 |
* Cas d'utilisation :
|
696 |
jpm |
8 |
* /CelStatistiqueTxt/TypeDeStat : retourne les statistiques demandées
|
694 |
jpm |
9 |
* Paramêtres :
|
696 |
jpm |
10 |
* utilisateur=courriel : retourne les statistiques d'un utilisateur donné.
|
694 |
jpm |
11 |
*
|
696 |
jpm |
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
15 |
* @version $Id$
|
|
|
16 |
* @copyright Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
|
694 |
jpm |
17 |
*/
|
|
|
18 |
class CelStatistiqueTxt extends Cel {
|
696 |
jpm |
19 |
|
694 |
jpm |
20 |
/**
|
|
|
21 |
* Méthode appelée avec une requête de type GET.
|
|
|
22 |
*/
|
696 |
jpm |
23 |
public function getElement($ressources) {
|
694 |
jpm |
24 |
$graph = null;
|
|
|
25 |
$serveur = '';
|
|
|
26 |
|
696 |
jpm |
27 |
if (isset($ressources[0])) {
|
|
|
28 |
$this->analyserParametresUrl();
|
|
|
29 |
$stat_demande = array_shift($ressources);
|
694 |
jpm |
30 |
$methode = 'get'.$stat_demande;
|
|
|
31 |
if (method_exists($this, $methode)) {
|
702 |
jpm |
32 |
$this->ressources = $ressources;
|
696 |
jpm |
33 |
$stats = $this->$methode($ressources);
|
694 |
jpm |
34 |
} else {
|
|
|
35 |
$this->messages[] = "Ce type de statistiques '$stat_demande' n'est pas disponible.";
|
|
|
36 |
}
|
|
|
37 |
} else {
|
696 |
jpm |
38 |
$this->messages[] = "La ressource du service CEL StatistiqueTxt doit indiquer le type de statistique. Ex. : .../CelStatistiqueTxt/Nombres";
|
694 |
jpm |
39 |
}
|
|
|
40 |
|
|
|
41 |
if (!is_null($stats)) {
|
|
|
42 |
$this->envoyerJson($stats);
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
696 |
jpm |
46 |
private function analyserParametresUrl() {
|
702 |
jpm |
47 |
$this->parametres['utilisateur'] = isset($_GET['utilisateur']) ? $this->bdd->quote($this->verifierSecuriteParametreUrl($_GET['utilisateur'])) : null;
|
|
|
48 |
$this->parametres['num_taxon'] = isset($_GET['num_taxon']) ? $this->bdd->quote($this->verifierSecuriteParametreUrl($_GET['num_taxon'])) : null;
|
|
|
49 |
$this->parametres['taxon'] = isset($_GET['taxon']) ? $this->bdd->quote($this->verifierSecuriteParametreUrl($_GET['taxon'].'%')) : null;
|
707 |
jpm |
50 |
$this->parametres['tag'] = isset($_GET['tag']) ? $this->verifierSecuriteParametreUrl($_GET['tag']) : null;
|
702 |
jpm |
51 |
$this->parametres['start'] = isset($_GET['start']) ? $this->verifierSecuriteParametreUrl($_GET['start']) : null;
|
|
|
52 |
$this->parametres['limit'] = isset($_GET['limit']) ? $this->verifierSecuriteParametreUrl($_GET['limit']) : null;
|
696 |
jpm |
53 |
}
|
|
|
54 |
|
702 |
jpm |
55 |
private function getListeUtilisateursNbrePhotos() {
|
707 |
jpm |
56 |
$liste = array();
|
|
|
57 |
|
702 |
jpm |
58 |
$requete = $this->construireRequeteListeUtilisateurNbrePhoto();
|
707 |
jpm |
59 |
if ($requete != null) {
|
|
|
60 |
$resultats = $this->executerRequete($requete);
|
|
|
61 |
|
|
|
62 |
if ($resultats != false) {
|
|
|
63 |
foreach ($resultats as $resultat) {
|
|
|
64 |
$liste[$resultat['identifiant']] = $resultat['nbre'];
|
|
|
65 |
}
|
702 |
jpm |
66 |
}
|
|
|
67 |
}
|
|
|
68 |
return $liste;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function construireRequeteListeUtilisateurNbrePhoto() {
|
|
|
72 |
$select = 'SELECT identifiant, COUNT(DISTINCT ci_id_image) AS nbre ';
|
|
|
73 |
$from = 'FROM cel_inventory '.
|
|
|
74 |
' LEFT JOIN cel_obs_images ON (coi_ce_observation = ordre AND coi_ce_utilisateur = identifiant) '.
|
|
|
75 |
' LEFT JOIN cel_images ON (coi_ce_image = ci_id_image) ';
|
|
|
76 |
$where = 'WHERE transmission = 1 ';
|
|
|
77 |
$groupBy = 'GROUP BY identifiant ';
|
|
|
78 |
$orderBy = 'ORDER BY nbre DESC ';
|
|
|
79 |
$limitSql = 'LIMIT 0,150 ';
|
|
|
80 |
|
707 |
jpm |
81 |
$zero_images = false;
|
970 |
aurelien |
82 |
|
702 |
jpm |
83 |
if (count($this->parametres) != 0) {
|
|
|
84 |
extract($this->parametres);
|
|
|
85 |
|
|
|
86 |
$filtres = array();
|
|
|
87 |
|
|
|
88 |
if (isset($utilisateur)) {
|
|
|
89 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
90 |
}
|
|
|
91 |
if (isset($num_taxon)) {
|
|
|
92 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
93 |
}
|
|
|
94 |
if (isset($taxon)) {
|
|
|
95 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
96 |
}
|
707 |
jpm |
97 |
if (isset($tag)) {
|
|
|
98 |
$limitation = $this->construireWhereTags();
|
|
|
99 |
if ($limitation != null) {
|
|
|
100 |
$filtres[] = $limitation;
|
|
|
101 |
} else {
|
|
|
102 |
$zero_images = true;
|
|
|
103 |
}
|
|
|
104 |
}
|
702 |
jpm |
105 |
$where .= ((count($filtres) > 0) ? 'AND '.implode(' AND ', $filtres) : '');
|
|
|
106 |
|
|
|
107 |
if (isset($start)) {
|
|
|
108 |
$limitSql = str_replace('0,', "$start,", $limitSql);
|
|
|
109 |
}
|
|
|
110 |
if (isset($limit)) {
|
|
|
111 |
$limitSql = str_replace('150', $limit, $limitSql);
|
|
|
112 |
}
|
|
|
113 |
}
|
707 |
jpm |
114 |
if ($zero_images) {
|
|
|
115 |
$requete = null;
|
|
|
116 |
} else {
|
|
|
117 |
$requete = $select.$from.$where.$groupBy.$orderBy.$limitSql;
|
|
|
118 |
//echo $requete;
|
|
|
119 |
}
|
702 |
jpm |
120 |
return $requete;
|
|
|
121 |
}
|
|
|
122 |
|
707 |
jpm |
123 |
private function construireWhereTags() {
|
|
|
124 |
$where = null;
|
|
|
125 |
if (isset($this->parametres['tag'])) {
|
|
|
126 |
$tag = $this->parametres['tag'];
|
|
|
127 |
$tag_encode = $this->bdd->quote($this->encoderMotCle(trim($tag)));
|
|
|
128 |
|
|
|
129 |
// Construction de la requête
|
|
|
130 |
$requete = 'SELECT cmc_id_mot_cle_utilisateur, cmc_id_proprietaire '.
|
|
|
131 |
'FROM cel_mots_cles_images '.
|
|
|
132 |
"WHERE cmc_id_mot_cle_general = $tag_encode ";
|
|
|
133 |
$elements = $this->executerRequete($requete);
|
|
|
134 |
|
|
|
135 |
if ($elements != false && count($elements) > 0) {
|
|
|
136 |
// Pré-construction du where de la requête
|
|
|
137 |
$tpl_where = '(ci_meta_mots_cles LIKE "%%%s%%" AND ci_ce_utilisateur = %s )';
|
|
|
138 |
$requete_where = array();
|
|
|
139 |
foreach ($elements as $occurence) {
|
|
|
140 |
$requete_where[] = sprintf($tpl_where, $occurence['cmc_id_mot_cle_utilisateur'], $this->bdd->quote($occurence['cmc_id_proprietaire']));
|
|
|
141 |
}
|
|
|
142 |
$where = ' ('.implode(" \nOR ", $requete_where).') ';
|
|
|
143 |
} else {
|
|
|
144 |
$this->debug[] = "Aucune image ne correspond à ce mot clé.";
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
return $where;
|
|
|
148 |
}
|
|
|
149 |
|
702 |
jpm |
150 |
private function getListeTaxonsNbrePhotos() {
|
|
|
151 |
$requete = $this->construireRequeteListeTaxonNbrePhoto();
|
|
|
152 |
$resultats = $this->executerRequete($requete);
|
|
|
153 |
$liste = array();
|
|
|
154 |
if ($resultats != false) {
|
|
|
155 |
foreach ($resultats as $resultat) {
|
|
|
156 |
$liste[$resultat['nom_ret']] = $resultat['nbre'];
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
return $liste;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
private function construireRequeteListeTaxonNbrePhoto() {
|
|
|
163 |
$select = 'SELECT nom_ret, COUNT(DISTINCT ci_id_image) AS nbre ';
|
|
|
164 |
$from = 'FROM cel_inventory '.
|
|
|
165 |
' LEFT JOIN cel_obs_images ON (coi_ce_observation = ordre AND coi_ce_utilisateur = identifiant) '.
|
|
|
166 |
' LEFT JOIN cel_images ON (coi_ce_image = ci_id_image) ';
|
|
|
167 |
$where = 'WHERE transmission = 1 '.
|
|
|
168 |
" AND nom_ret != '' ";
|
|
|
169 |
$groupBy = 'GROUP BY nom_ret ';
|
|
|
170 |
$orderBy = 'ORDER BY nbre DESC ';
|
|
|
171 |
$limitSql = 'LIMIT 0,150 ';
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
if (count($this->parametres) != 0) {
|
|
|
175 |
extract($this->parametres);
|
|
|
176 |
|
|
|
177 |
$filtres = array();
|
|
|
178 |
|
|
|
179 |
if (isset($utilisateur)) {
|
|
|
180 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
181 |
}
|
|
|
182 |
if (isset($num_taxon)) {
|
|
|
183 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
184 |
}
|
|
|
185 |
if (isset($taxon)) {
|
|
|
186 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
187 |
}
|
|
|
188 |
$where .= ((count($filtres) > 0) ? 'AND '.implode(' AND ', $filtres) : '');
|
|
|
189 |
|
|
|
190 |
if (isset($start)) {
|
|
|
191 |
$limitSql = str_replace('0,', "$start,", $limitSql);
|
|
|
192 |
}
|
|
|
193 |
if (isset($limit)) {
|
|
|
194 |
$limitSql = str_replace('150', $limit, $limitSql);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
$requete = $select.$from.$where.$groupBy.$orderBy.$limitSql;
|
|
|
199 |
return $requete;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
private function getNombres() {
|
696 |
jpm |
203 |
|
|
|
204 |
$requete = $this->construireRequeteNbreObs();
|
|
|
205 |
$info['observations'] = (int) $this->executerRequete($requete, 'Column');
|
694 |
jpm |
206 |
|
696 |
jpm |
207 |
$requete = $this->construireRequeteNbreObsPubliques();
|
|
|
208 |
$info['observationsPubliques'] = (int) $this->executerRequete($requete, 'Column');
|
|
|
209 |
|
|
|
210 |
$requete = $this->construireRequeteNbreImg();
|
|
|
211 |
$info['images'] =(int) $this->executerRequete($requete, 'Column');
|
|
|
212 |
|
|
|
213 |
$requete = $this->construireRequeteNbreImgLiees();
|
|
|
214 |
$info['imagesLiees'] =(int) $this->executerRequete($requete, 'Column');
|
|
|
215 |
|
|
|
216 |
$requete = $this->construireRequeteNbreObsLiees();
|
|
|
217 |
$info['observationsLiees'] = (int) $this->executerRequete($requete, 'Column');
|
|
|
218 |
|
697 |
jpm |
219 |
$info['moyImagesParObs'] = ($info['observationsLiees'] > 0 ? round($info['imagesLiees']/$info['observationsLiees'], 2) : '');
|
696 |
jpm |
220 |
|
|
|
221 |
$requete = $this->construireRequeteNbreObsParCommune();
|
697 |
jpm |
222 |
$info['communes'] = ($resultats = $this->executerRequete($requete)) ? count($resultats) : '' ;
|
|
|
223 |
$info['observationsParCommunesMin'] = 1000;
|
|
|
224 |
$info['observationsParCommunesMax'] = 0;
|
|
|
225 |
$info['observationsParCommunesTotal'] = 0;
|
|
|
226 |
foreach ($resultats as $resultat) {
|
|
|
227 |
if ($resultat['nbre'] < $info['observationsParCommunesMin']) {
|
|
|
228 |
$info['observationsParCommunesMin'] = $resultat['nbre'];
|
|
|
229 |
}
|
|
|
230 |
if ($resultat['nbre'] > $info['observationsParCommunesMax']) {
|
|
|
231 |
$info['observationsParCommunesMax'] = $resultat['nbre'];
|
|
|
232 |
}
|
|
|
233 |
$info['observationsParCommunesTotal'] += $resultat['nbre'];
|
|
|
234 |
}
|
702 |
jpm |
235 |
$info['observationsParCommunesMoyenne'] = ($info['communes'] > 0 ) ? round($info['observationsParCommunesTotal'] / $info['communes'], 2) : 0;
|
696 |
jpm |
236 |
|
|
|
237 |
return $info;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
private function construireRequeteNbreObs() {
|
694 |
jpm |
241 |
$requete = 'SELECT COUNT(id) AS nbre '.
|
696 |
jpm |
242 |
'FROM cel_inventory ';
|
|
|
243 |
|
|
|
244 |
if (count($this->parametres) != 0) {
|
|
|
245 |
$filtres = array();
|
|
|
246 |
extract($this->parametres);
|
|
|
247 |
|
|
|
248 |
if (isset($utilisateur)) {
|
|
|
249 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
250 |
}
|
|
|
251 |
if (isset($num_taxon)) {
|
|
|
252 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
253 |
}
|
702 |
jpm |
254 |
if (isset($taxon)) {
|
|
|
255 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
256 |
}
|
696 |
jpm |
257 |
|
|
|
258 |
$requete .= ((count($filtres) > 0) ? 'WHERE '.implode(' AND ', $filtres) : '');
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
return $requete;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
private function construireRequeteNbreObsPubliques() {
|
|
|
265 |
$requete = 'SELECT COUNT(id) AS nbre '.
|
694 |
jpm |
266 |
'FROM cel_inventory '.
|
696 |
jpm |
267 |
"WHERE transmission = 1 ";
|
|
|
268 |
|
|
|
269 |
if (count($this->parametres) != 0) {
|
|
|
270 |
$filtres = array();
|
|
|
271 |
extract($this->parametres);
|
|
|
272 |
|
|
|
273 |
if (isset($utilisateur)) {
|
|
|
274 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
275 |
}
|
|
|
276 |
if (isset($num_taxon)) {
|
|
|
277 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
278 |
}
|
702 |
jpm |
279 |
if (isset($taxon)) {
|
|
|
280 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
281 |
}
|
696 |
jpm |
282 |
|
|
|
283 |
$requete .= ((count($filtres) > 0) ? 'AND '.implode(' AND ', $filtres) : '');
|
|
|
284 |
}
|
694 |
jpm |
285 |
|
696 |
jpm |
286 |
return $requete;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
private function construireRequeteNbreObsParCommune() {
|
|
|
290 |
$requete = 'SELECT COUNT(id) AS nbre '.
|
|
|
291 |
'FROM cel_inventory '.
|
|
|
292 |
"WHERE location != '000null' ".
|
|
|
293 |
" AND id_location != '000null' ";
|
|
|
294 |
$groupBy = 'GROUP BY location, id_location';
|
|
|
295 |
|
|
|
296 |
if (count($this->parametres) != 0) {
|
|
|
297 |
$filtres = array();
|
|
|
298 |
extract($this->parametres);
|
|
|
299 |
|
|
|
300 |
if (isset($utilisateur)) {
|
|
|
301 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
302 |
}
|
|
|
303 |
if (isset($num_taxon)) {
|
|
|
304 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
305 |
}
|
702 |
jpm |
306 |
if (isset($taxon)) {
|
|
|
307 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
308 |
}
|
696 |
jpm |
309 |
|
|
|
310 |
$requete .= ((count($filtres) > 0) ? 'AND '.implode(' AND ', $filtres) : '');
|
|
|
311 |
}
|
|
|
312 |
$requete .= $groupBy;
|
694 |
jpm |
313 |
|
696 |
jpm |
314 |
return $requete;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
private function construireRequeteNbreImg() {
|
|
|
318 |
$select = 'SELECT COUNT(DISTINCT ci_id_image) AS nbre ';
|
|
|
319 |
$from = 'FROM cel_images ';
|
694 |
jpm |
320 |
|
696 |
jpm |
321 |
if (count($this->parametres) != 0) {
|
|
|
322 |
$filtres = array();
|
|
|
323 |
|
|
|
324 |
extract($this->parametres);
|
|
|
325 |
|
|
|
326 |
if (isset($utilisateur)) {
|
|
|
327 |
$filtres[] = "ci_ce_utilisateur = $utilisateur ";
|
|
|
328 |
}
|
|
|
329 |
if (isset($num_taxon)) {
|
702 |
jpm |
330 |
|
696 |
jpm |
331 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
332 |
}
|
702 |
jpm |
333 |
if (isset($taxon)) {
|
|
|
334 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
335 |
}
|
|
|
336 |
if (isset($num_taxon) || isset($taxon)) {
|
|
|
337 |
$from .= 'LEFT JOIN cel_obs_images ON (coi_ce_image = ci_id_image) '.
|
|
|
338 |
'LEFT JOIN cel_inventory ON (coi_ce_observation = ordre AND coi_ce_utilisateur = identifiant) ';
|
|
|
339 |
}
|
696 |
jpm |
340 |
|
|
|
341 |
$where = ((count($filtres) > 0) ? 'WHERE '.implode(' AND ', $filtres) : '');
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
$requete = $select.$from.$where;
|
|
|
345 |
return $requete;
|
694 |
jpm |
346 |
}
|
696 |
jpm |
347 |
|
|
|
348 |
private function construireRequeteNbreImgLiees() {
|
|
|
349 |
$select = 'SELECT COUNT(DISTINCT ci_id_image) AS nbre ';
|
|
|
350 |
$from = 'FROM cel_obs_images '.
|
|
|
351 |
' LEFT JOIN cel_images ON (coi_ce_image = ci_id_image) ';
|
|
|
352 |
|
|
|
353 |
if (count($this->parametres) != 0) {
|
|
|
354 |
$filtres = array();
|
|
|
355 |
|
|
|
356 |
extract($this->parametres);
|
|
|
357 |
|
|
|
358 |
if (isset($utilisateur)) {
|
|
|
359 |
$filtres[] = "coi_ce_utilisateur = $utilisateur ";
|
|
|
360 |
}
|
|
|
361 |
if (isset($num_taxon)) {
|
|
|
362 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
363 |
}
|
702 |
jpm |
364 |
if (isset($taxon)) {
|
|
|
365 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
366 |
}
|
696 |
jpm |
367 |
|
702 |
jpm |
368 |
if (isset($num_taxon) || isset($taxon)) {
|
|
|
369 |
$from .= 'LEFT JOIN cel_inventory ON (coi_ce_observation = ordre AND coi_ce_utilisateur = identifiant) ';
|
|
|
370 |
}
|
|
|
371 |
|
696 |
jpm |
372 |
$where = ((count($filtres) > 0) ? 'WHERE '.implode(' AND ', $filtres) : '');
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
$requete = $select.$from.$where;
|
|
|
376 |
return $requete;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
private function construireRequeteNbreObsLiees() {
|
|
|
380 |
$select = 'SELECT COUNT(DISTINCT coi_ce_observation) AS nbre ';
|
|
|
381 |
$from = 'FROM cel_obs_images '.
|
|
|
382 |
' LEFT JOIN cel_inventory ON (coi_ce_observation = ordre AND coi_ce_utilisateur = identifiant) ';
|
|
|
383 |
|
|
|
384 |
if (count($this->parametres) != 0) {
|
|
|
385 |
$filtres = array();
|
|
|
386 |
|
|
|
387 |
extract($this->parametres);
|
|
|
388 |
|
|
|
389 |
if (isset($utilisateur)) {
|
|
|
390 |
$filtres[] = "identifiant = $utilisateur ";
|
|
|
391 |
}
|
|
|
392 |
if (isset($num_taxon)) {
|
|
|
393 |
$filtres[] = "num_taxon = $num_taxon ";
|
|
|
394 |
}
|
702 |
jpm |
395 |
if (isset($taxon)) {
|
|
|
396 |
$filtres[] = "nom_ret LIKE $taxon ";
|
|
|
397 |
}
|
696 |
jpm |
398 |
|
|
|
399 |
$where = ((count($filtres) > 0) ? 'WHERE '.implode(' AND ', $filtres) : '');
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
$requete = $select.$from.$where;
|
|
|
403 |
|
|
|
404 |
return $requete;
|
|
|
405 |
}
|
694 |
jpm |
406 |
}
|
|
|
407 |
?>
|