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