1593 |
samuel |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Le web service image récupère toutes les données de la table del_obs_images
|
|
|
4 |
* pour retourner une liste d'images associée à la détermination la plus probable
|
|
|
5 |
* avec la possibilité de ne renvoyer que les images les mieux notées pour un protocole donné
|
|
|
6 |
*
|
|
|
7 |
* @category php 5.2
|
|
|
8 |
* @package del
|
|
|
9 |
* @subpackage images
|
|
|
10 |
* @author Raphaël Droz <raphael@tela-botanica.org>
|
|
|
11 |
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
|
|
|
12 |
* @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
|
|
|
13 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
14 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
15 |
* @see http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=ApiIdentiplante01Images
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
class Changements {
|
|
|
19 |
|
|
|
20 |
private $indexImagesIds = array();
|
|
|
21 |
private $conteneur;
|
|
|
22 |
private $navigation;
|
|
|
23 |
private $masque;
|
|
|
24 |
private $gestionBdd;
|
|
|
25 |
private $bdd;
|
|
|
26 |
private $parametres = array();
|
|
|
27 |
private $ressources = array();
|
|
|
28 |
private $date_defaut = '1900-01-01';
|
|
|
29 |
|
|
|
30 |
public function __construct(Conteneur $conteneur = null) {
|
|
|
31 |
|
|
|
32 |
/* restore_exception_handler(); */
|
|
|
33 |
/* restore_error_handler(); */
|
|
|
34 |
/* ini_set("display_errors", "1"); */
|
|
|
35 |
|
|
|
36 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
37 |
$this->conteneur->chargerConfiguration('config_plantnet.ini');
|
|
|
38 |
$this->navigation = $conteneur->getNavigation();
|
|
|
39 |
$this->masque = $conteneur->getMasque();
|
|
|
40 |
$this->gestionBdd = $conteneur->getGestionBdd();
|
|
|
41 |
$this->bdd = $this->gestionBdd->getBdd();
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Méthode principale de la classe.
|
|
|
46 |
* Lance la récupération des images dans la base et les place dans un objet ResultatService
|
|
|
47 |
* pour l'afficher.
|
|
|
48 |
* @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
|
|
|
49 |
* @param array $parametres les paramètres situés après le ? dans l'url
|
|
|
50 |
* */
|
|
|
51 |
public function consulter($ressources, $parametres) {
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
// initialiserRessourcesEtParametres()
|
|
|
55 |
$this->ressources = $ressources;
|
|
|
56 |
$this->parametres = $parametres;
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
if(!isset($parametres['date'])) {
|
|
|
60 |
$this->parametres['date'] = $this->date_defaut;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
// Lancement du service
|
|
|
65 |
$liaisons = $this->chargerLiaisons();
|
|
|
66 |
$images = array();
|
|
|
67 |
$total = 0;
|
|
|
68 |
|
|
|
69 |
if($liaisons) {
|
|
|
70 |
$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
|
|
|
71 |
$total = (int) $compte['nbre'];
|
|
|
72 |
|
|
|
73 |
//$images = $this->chargerImages($liaisons);
|
|
|
74 |
$obs = $this->regrouperObs($liaisons);
|
|
|
75 |
//$images = $this->chargerPropositionPlusProbable($obs);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$this->navigation->setTotal($total);
|
|
|
79 |
|
|
|
80 |
// Mettre en forme le résultat et l'envoyer pour affichage
|
|
|
81 |
$resultat = new ResultatService();
|
|
|
82 |
//$resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $images);
|
|
|
83 |
$resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $obs);
|
|
|
84 |
|
|
|
85 |
return $resultat;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
/*-------------------------------------------------------------------------------
|
|
|
90 |
CHARGEMENT DES IMAGES
|
|
|
91 |
--------------------------------------------------------------------------------*/
|
|
|
92 |
private function chargerLiaisons() {
|
|
|
93 |
|
|
|
94 |
$date_debut = $this->parametres['date'];
|
|
|
95 |
$limite = @min(intval($this->parametres['navigation.limite']), 1000);
|
|
|
96 |
$limite = $limite ? $limite : 10; // 0 => 10
|
|
|
97 |
$depart = intval(@$this->parametres['navigation.depart']);
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
$requete_sql =
|
|
|
101 |
'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
|
|
|
102 |
|
|
|
103 |
'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
|
|
|
104 |
'vdi.ce_zone_geo, vdi.zone_geo, vdi.lieudit, vdi.station, vdi.milieu, '.
|
|
|
105 |
'vdi.date_observation, vdi.date_transmission, '.
|
|
|
106 |
'vdi.mots_cles_texte as mots_cles_texte, '.
|
|
|
107 |
'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
|
|
|
108 |
|
|
|
109 |
'vdi.ce_utilisateur as ce_utilisateur, '.
|
|
|
110 |
'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image, '.
|
|
|
111 |
|
|
|
112 |
'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
|
|
|
113 |
|
|
|
114 |
'GREATEST(vdi.date_creation, vdi.date_modification, MAX(del_image_tag.date), '.
|
|
|
115 |
'MAX(del_image_tag.date_modification), MAX(del_image_vote.date), '.
|
|
|
116 |
'IFNULL(MAX(del_commentaire.date),0), IFNULL(MAX(del_commentaire_vote.date),0)) as modif_date '.
|
|
|
117 |
|
|
|
118 |
'from v_del_image as vdi '.
|
|
|
119 |
|
|
|
120 |
'left join del_image_vote on del_image_vote.ce_image=id_image '.
|
|
|
121 |
'left join del_image_tag on del_image_tag.ce_image=id_image '.
|
|
|
122 |
'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
|
|
|
123 |
'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
|
|
|
124 |
|
|
|
125 |
'where ce_protocole=3 and actif=1 '.
|
|
|
126 |
|
|
|
127 |
'group by id_image, id_observation '.
|
|
|
128 |
|
|
|
129 |
'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
|
|
|
130 |
' MAX(vdi.date_modification) >= '.$date_debut.' or '.
|
|
|
131 |
' MAX(del_image_tag.date) >= '.$date_debut.' or '.
|
|
|
132 |
' MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
|
|
|
133 |
' MAX(del_image_vote.date) >= '.$date_debut.' or '.
|
|
|
134 |
' MAX(del_commentaire.date) >= '.$date_debut.' or '.
|
|
|
135 |
' MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
|
|
|
136 |
'order by modif_date DESC '.
|
|
|
137 |
'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
|
|
|
138 |
|
|
|
139 |
return $this->bdd->recupererTous($requete_sql);
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
// GROUP BY (très couteux) car multiples observations associées à une image
|
|
|
148 |
// eg: 16150,16185,16245,16246,118995,129989
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Retourner un tableau d'images formaté en fonction des liaisons trouvées
|
|
|
154 |
* @param $liaisons les liaisons de la table del_obs_images
|
|
|
155 |
* */
|
|
|
156 |
private function regrouperObs(&$liaisons) {
|
|
|
157 |
|
|
|
158 |
// regroupe les observations
|
|
|
159 |
$obs = array();
|
|
|
160 |
foreach ($liaisons as $img) {
|
|
|
161 |
$idobs = $img['id_observation'];
|
|
|
162 |
|
|
|
163 |
if (!isset($obs[$idobs])) {
|
|
|
164 |
$obs[$idobs] = array();
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
$obs[$idobs]['id_observation'] = $idobs;
|
|
|
168 |
|
|
|
169 |
if (!isset($obs[$idobs]['images'])) {
|
|
|
170 |
$obs[$idobs]['images'] = array();
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
$obs[$idobs]['images'][] = $img['id_image'];
|
|
|
174 |
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
return $obs;
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
/* $images = array(); */
|
|
|
182 |
/* foreach ($liaisons as $liaison) { */
|
|
|
183 |
|
|
|
184 |
/* if($liaison['ce_utilisateur'] == 0) { */
|
|
|
185 |
/* $liaison['prenom'] = $liaison['prenom_utilisateur']; */
|
|
|
186 |
/* $liaison['nom'] = $liaison['nom_utilisateur']; */
|
|
|
187 |
/* } */
|
|
|
188 |
|
|
|
189 |
/* $idObs = $liaison['id_observation']; */
|
|
|
190 |
/* $idImage = $liaison['id_image']; */
|
|
|
191 |
/* // On enregistre l'ID de l'obs pour n'effectuer qu'une seule requête par la suite */
|
|
|
192 |
/* $this->obsIds[] = $idObs; */
|
|
|
193 |
/* $index = $liaison['id_image'].'-'.$liaison['id_observation']; */
|
|
|
194 |
/* $this->indexImagesIds[$idImage] = $index; */
|
|
|
195 |
/* $images[$index] = array( */
|
|
|
196 |
/* 'id_image' => $idImage, */
|
|
|
197 |
/* 'id_observation' => $idObs, */
|
|
|
198 |
/* 'auteur.intitule' => $liaison['prenom'].' '.$liaison['nom'], */
|
|
|
199 |
/* 'binaire.href' => sprintf($this->conteneur->getParametre('url_images'), $idImage), */
|
|
|
200 |
/* 'determination.famille' => $liaison['famille'], */
|
|
|
201 |
/* 'determination.referentiel' => $liaison['nom_referentiel'], */
|
|
|
202 |
/* 'determination.ns' => $liaison['nom_ret'], */
|
|
|
203 |
/* 'determination.nn' => $liaison['nom_ret_nn'], */
|
|
|
204 |
/* 'determination.nt' => $liaison['nt'], */
|
|
|
205 |
/* 'date_observation' => $liaison['date_observation'], */
|
|
|
206 |
/* 'localite' => $this->formaterLieu($liaison), */
|
|
|
207 |
/* 'mots_cles_image_cel' => $this->formaterMotsClesCel($liaison['mots_cles_texte_image']), */
|
|
|
208 |
/* 'mots_cles_image_del' => "" */
|
|
|
209 |
/* ); */
|
|
|
210 |
/* } */
|
|
|
211 |
/* return $images; */
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Charger les votes pour chaque image
|
|
|
216 |
**/
|
|
|
217 |
private function chargerPropositionPlusProbable(&$images) {
|
|
|
218 |
|
|
|
219 |
$resultatsPropositions = $this->bdd->recupererTous(sprintf(
|
|
|
220 |
'SELECT * FROM del_commentaire WHERE ce_observation IN (%s) AND nom_sel IS NOT NULL',
|
|
|
221 |
implode(',', $this->obsIds)));
|
|
|
222 |
|
|
|
223 |
// !! attention la requete est reexecuté
|
|
|
224 |
$resultatsVotes = $this->bdd->recupererTous(sprintf(
|
|
|
225 |
'SELECT ce_proposition, valeur, ce_utilisateur FROM del_commentaire_vote WHERE ce_proposition IN'.
|
|
|
226 |
' ( SELECT id_commentaire FROM del_commentaire WHERE ce_observation IN (%s) AND nom_sel IS NOT NULL )'.
|
|
|
227 |
' ORDER BY ce_proposition',
|
|
|
228 |
implode(',', $this->obsIds)));
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
$votes = array(); // map ce_proposition -> score
|
|
|
232 |
|
|
|
233 |
// calcul des votes
|
|
|
234 |
// un vote identifié a un facteur de 3
|
|
|
235 |
foreach($resultatsVotes as $vote) {
|
|
|
236 |
if(!isset($votes[$vote['ce_proposition']])) {
|
|
|
237 |
$votes[$vote['ce_proposition']] = 0;
|
|
|
238 |
}
|
|
|
239 |
$valeur = ($vote['valeur'] == 1) ? 1 : -1;
|
|
|
240 |
$votes[$vote['ce_proposition']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
// calcul des propositions
|
|
|
244 |
$propositions = array();
|
|
|
245 |
|
|
|
246 |
foreach($resultatsPropositions as $proposition) {
|
|
|
247 |
$id_proposition = $proposition['id_commentaire'];
|
|
|
248 |
$id_obs = $proposition['ce_observation'];
|
|
|
249 |
|
|
|
250 |
// une proposition sans vote a un score de -1
|
|
|
251 |
|
|
|
252 |
if(isset($votes[$id_proposition])) {
|
|
|
253 |
$score = $votes[$id_proposition];
|
|
|
254 |
} else {
|
|
|
255 |
$score = -1;
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
// on garde la proposition avec le plus grand score
|
|
|
259 |
|
|
|
260 |
$proposition['valeur'] = $score;
|
|
|
261 |
if(!isset($propositions[$id_obs])) {
|
|
|
262 |
$propositions[$id_obs] = $proposition;
|
|
|
263 |
} else {
|
|
|
264 |
$score_ancienne_proposition = $propositions[$id_obs]['valeur'];
|
|
|
265 |
$propositions[$id_obs] = ($score >= $score_ancienne_proposition) ? $proposition : $propositions[$id_obs];
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
foreach ($images as $id => $image) {
|
|
|
271 |
|
|
|
272 |
if ($this->doitRemplacerObservationParProposition($propositions, $image)) {
|
|
|
273 |
|
|
|
274 |
$id_obs = $image['id_observation'];
|
|
|
275 |
$images[$id]['determination.famille'] = $propositions[$id_obs]['famille'];
|
|
|
276 |
$images[$id]['determination.ns'] = $propositions[$id_obs]['nom_sel'];
|
|
|
277 |
$images[$id]['determination.nn'] = $propositions[$id_obs]['nom_sel_nn'];
|
|
|
278 |
$images[$id]['determination.nt'] = $propositions[$id_obs]['nt'];
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
if(isset($images[$id]['determination.nn'])) {
|
|
|
282 |
$images[$id]['url_fiche_eflore'] = sprintf($this->conteneur->getParametre('url_fiche_eflore'), $images[$id]['determination.nn']); // formaterUrlFicheEflore
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
return $images;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
private function doitRemplacerObservationParProposition($propositions, $image) {
|
|
|
291 |
|
|
|
292 |
return ((isset($propositions[$image['id_observation']]) &&
|
|
|
293 |
$propositions[$image['id_observation']] != null &&
|
|
|
294 |
$propositions[$image['id_observation']]['nom_sel_nn'] != 0) &&
|
|
|
295 |
($propositions[$image['id_observation']]['valeur'] > 0 ||
|
|
|
296 |
$image['determination.nn'] == 0)
|
|
|
297 |
);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
/*-------------------------------------------------------------------------------
|
|
|
301 |
FORMATER ET METTRE EN FORME
|
|
|
302 |
--------------------------------------------------------------------------------*/
|
|
|
303 |
/**
|
|
|
304 |
* Formater les mots clés du cel en n'affichant que ceux faisant partie
|
|
|
305 |
* d'une liste définie dans le fichier de configuration
|
|
|
306 |
* @param $chaineMotCleCel la chaine de mots clés du cel
|
|
|
307 |
* @return string la chaine filtrée
|
|
|
308 |
* */
|
|
|
309 |
private function formaterMotsClesCel($chaineMotCleCel) {
|
|
|
310 |
return implode(',', array_intersect(
|
|
|
311 |
explode(',', $this->conteneur->getParametre('mots_cles_cel_affiches')), // $tabMotsClesAffiches
|
|
|
312 |
explode(',', $chaineMotCleCel))); // $tabMotsClesCel
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
private function formaterLieu($image) {
|
|
|
317 |
if(! $image['ce_zone_geo']) return '';
|
|
|
318 |
|
|
|
319 |
$lieu = $image['zone_geo'];
|
|
|
320 |
$id_zone_geo = $image['ce_zone_geo'];
|
|
|
321 |
if(strpos($image['ce_zone_geo'], 'INSEE-C:') === 0) {
|
|
|
322 |
$id_zone_geo = str_replace('INSEE-C:', '', $image['ce_zone_geo']);
|
|
|
323 |
$id_zone_geo = strlen($id_zone_geo) >= 5 ? substr($id_zone_geo, 0, 2) : $id_zone_geo;
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
return $lieu . ' ('.$id_zone_geo.')';
|
|
|
327 |
}
|
|
|
328 |
}
|