1142 |
aurelien |
1 |
<?php
|
1815 |
jpm |
2 |
// declare(encoding='UTF-8');
|
1142 |
aurelien |
3 |
/**
|
1813 |
jpm |
4 |
* Web service récupèrant toutes les données de la table del_obs_images
|
|
|
5 |
* pour retourner une liste d'images associées à la détermination la plus probable.
|
1142 |
aurelien |
6 |
*
|
1813 |
jpm |
7 |
* Possibilité de ne renvoyer que les images les mieux notées pour un protocole donné.
|
|
|
8 |
*
|
|
|
9 |
* @category DEL
|
|
|
10 |
* @package Services
|
|
|
11 |
* @subpackage Determinations
|
|
|
12 |
* @version 0.1
|
|
|
13 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
14 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
15 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
16 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
17 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
18 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
1142 |
aurelien |
19 |
*/
|
|
|
20 |
|
|
|
21 |
class ListeImagesDeterminationsProbables {
|
1700 |
jpm |
22 |
|
1168 |
aurelien |
23 |
private $indexImagesIds = array();
|
1142 |
aurelien |
24 |
private $conteneur;
|
|
|
25 |
private $navigation;
|
|
|
26 |
private $bdd;
|
1813 |
jpm |
27 |
|
|
|
28 |
private $erreurs = array();
|
1142 |
aurelien |
29 |
private $parametres = array();
|
1813 |
jpm |
30 |
private $protocoles = array();
|
1700 |
jpm |
31 |
|
1874 |
jpm |
32 |
private $idsImagesOrdonnees = array();
|
1813 |
jpm |
33 |
private $resultats = array();
|
|
|
34 |
private $propositions = array();
|
|
|
35 |
private $votes = array();
|
|
|
36 |
|
1142 |
aurelien |
37 |
public function __construct(Conteneur $conteneur = null) {
|
|
|
38 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
39 |
$this->navigation = $conteneur->getNavigation();
|
1793 |
jpm |
40 |
$this->bdd = $this->conteneur->getBdd();
|
1813 |
jpm |
41 |
$this->chargerProtocoles();
|
1142 |
aurelien |
42 |
}
|
1700 |
jpm |
43 |
|
1813 |
jpm |
44 |
private function chargerProtocoles() {
|
|
|
45 |
$requete = 'SELECT id_protocole FROM del_image_protocole -- '.__FILE__.' : '.__LINE__;
|
|
|
46 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
47 |
if ($resultats) {
|
|
|
48 |
foreach ($resultats as $infos) {
|
|
|
49 |
$this->protocoles[] = $infos['id_protocole'];
|
|
|
50 |
}
|
|
|
51 |
sort($this->protocoles);
|
1468 |
raphael |
52 |
}
|
1813 |
jpm |
53 |
}
|
1700 |
jpm |
54 |
|
1813 |
jpm |
55 |
public function consulter($parametres) {
|
|
|
56 |
$this->parametres = $parametres;
|
1143 |
aurelien |
57 |
$this->verifierParametres();
|
1700 |
jpm |
58 |
|
1142 |
aurelien |
59 |
// Lancement du service
|
1874 |
jpm |
60 |
$this->idsImagesOrdonnees = $this->getIdsImages();
|
1922 |
jpm |
61 |
$this->modifierEnteteTotal();
|
1813 |
jpm |
62 |
$infos = $this->chargerInfos();
|
|
|
63 |
if ($infos) {
|
|
|
64 |
$this->traiterResultats($infos);
|
|
|
65 |
$this->completerResutlats();
|
1144 |
aurelien |
66 |
}
|
1700 |
jpm |
67 |
|
1142 |
aurelien |
68 |
// Mettre en forme le résultat et l'envoyer pour affichage
|
|
|
69 |
$resultat = new ResultatService();
|
1813 |
jpm |
70 |
$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $this->resultats);
|
1142 |
aurelien |
71 |
return $resultat;
|
|
|
72 |
}
|
1700 |
jpm |
73 |
|
|
|
74 |
private function verifierParametres() {
|
1813 |
jpm |
75 |
$this->verifierParamProtocole();
|
|
|
76 |
$this->verifierParamVote();
|
1144 |
aurelien |
77 |
|
1813 |
jpm |
78 |
if (!empty($this->erreurs)) {
|
|
|
79 |
$msg = "Erreur de configuration :\n".implode("\n\n", $this->erreurs);
|
|
|
80 |
throw new Exception($msg, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
1142 |
aurelien |
81 |
}
|
1813 |
jpm |
82 |
}
|
1700 |
jpm |
83 |
|
1813 |
jpm |
84 |
private function verifierParamProtocole() {
|
|
|
85 |
if (isset($this->parametres['masque.protocole'])) {
|
|
|
86 |
$protocoleExistant = in_array($this->parametres['masque.protocole'], $this->protocoles);
|
|
|
87 |
|
|
|
88 |
if (!is_numeric($this->parametres['masque.protocole']) || $protocoleExistant === false) {
|
|
|
89 |
$protocolesListe = implode(', ', $this->protocoles);
|
|
|
90 |
$msg = "La valeur pour le protocole doit être un entier compris dans les numéros de protocole ".
|
|
|
91 |
"existants : $protocolesListe";
|
|
|
92 |
$this->erreurs[] = $msg;
|
|
|
93 |
}
|
1142 |
aurelien |
94 |
}
|
1813 |
jpm |
95 |
}
|
1700 |
jpm |
96 |
|
1813 |
jpm |
97 |
private function verifierParamVote() {
|
|
|
98 |
if (isset($this->parametres['masque.valeur_min_vote'])) {
|
|
|
99 |
$minVote = $this->parametres['masque.valeur_min_vote'];
|
|
|
100 |
if (!is_numeric($minVote) || ($minVote < 0 && $minVote > 5)) {
|
|
|
101 |
$this->erreurs[] = "La valeur minimum de valeur des votes doit être un entier compris entre 0 et 5.";
|
|
|
102 |
}
|
1142 |
aurelien |
103 |
}
|
|
|
104 |
}
|
1700 |
jpm |
105 |
|
1874 |
jpm |
106 |
private function getIdsImages() {
|
1813 |
jpm |
107 |
$whereClause = $this->getClauseWhere();
|
|
|
108 |
$depart = $this->navigation->getDepart();
|
|
|
109 |
$limite = $this->navigation->getLimite();
|
1464 |
raphael |
110 |
|
1924 |
jpm |
111 |
$requete = 'SELECT DISTINCT SQL_CALC_FOUND_ROWS di.id_image '.
|
1874 |
jpm |
112 |
'FROM del_image AS di '.
|
1813 |
jpm |
113 |
' LEFT JOIN del_image_stat AS ds ON (di.id_image = ds.ce_image) '.
|
|
|
114 |
"$whereClause ".
|
1922 |
jpm |
115 |
'ORDER BY moyenne DESC, id_image DESC '.
|
1813 |
jpm |
116 |
"LIMIT $depart, $limite ".
|
|
|
117 |
' -- '.__FILE__.' : '.__LINE__;
|
1874 |
jpm |
118 |
//Debug::printr($requete);
|
|
|
119 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
120 |
$idImgs = array();
|
|
|
121 |
if ($resultats !== false ) {
|
|
|
122 |
foreach ($resultats as $resultat) {
|
|
|
123 |
$idImgs[] = $resultat['id_image'];
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
return $idImgs;
|
1813 |
jpm |
127 |
}
|
|
|
128 |
|
|
|
129 |
private function getClauseWhere() {
|
1700 |
jpm |
130 |
$where = array();
|
1813 |
jpm |
131 |
if (isset($this->parametres['masque.protocole'])) {
|
|
|
132 |
$protocoleIdP = $this->bdd->proteger($this->parametres['masque.protocole']);
|
|
|
133 |
$where[] = "ds.ce_protocole = $protocoleIdP ";
|
|
|
134 |
}
|
|
|
135 |
if (isset($this->parametres['masque.valeur_vote_min'])) {
|
|
|
136 |
$voteP = $this->bdd->proteger($this->parametres['masque.valeur_vote_min']);
|
|
|
137 |
$where[] = "moyenne >= $voteP";
|
|
|
138 |
}
|
|
|
139 |
return (count($where) > 0) ? 'WHERE '.implode(' AND ', $where).' ' : '';
|
|
|
140 |
}
|
1464 |
raphael |
141 |
|
1874 |
jpm |
142 |
private function chargerInfos() {
|
|
|
143 |
$idImgsConcat = implode(',', $this->idsImagesOrdonnees);
|
|
|
144 |
|
1924 |
jpm |
145 |
$requete = 'SELECT DISTINCT SQL_CALC_FOUND_ROWS '.
|
1874 |
jpm |
146 |
'di.id_image, di.mots_cles_texte AS mots_cles_texte_image, '.
|
|
|
147 |
'do.id_observation, nom_referentiel, nom_ret, '.
|
|
|
148 |
'nom_ret_nn, nt, famille, ce_zone_geo, zone_geo, date_observation, '.
|
|
|
149 |
'do.ce_utilisateur, do.nom_utilisateur, do.prenom_utilisateur, '.
|
2156 |
mathias |
150 |
'di.prenom_utilisateur as prenom, di.nom_utilisateur as nom '. // retrocompatibilité redondance cheloute
|
1874 |
jpm |
151 |
'FROM del_image AS di '.
|
|
|
152 |
' INNER JOIN del_observation AS do ON (di.ce_observation = do.id_observation) '.
|
|
|
153 |
' LEFT JOIN del_image_stat AS ds ON (di.id_image = ds.ce_image) '.
|
|
|
154 |
"WHERE di.id_image IN ($idImgsConcat) ".
|
1922 |
jpm |
155 |
'ORDER BY moyenne DESC, id_image DESC '.
|
1874 |
jpm |
156 |
' -- '.__FILE__.' : '.__LINE__;
|
1922 |
jpm |
157 |
//Debug::printr($requete);
|
1874 |
jpm |
158 |
return $this->bdd->recupererTous($requete);
|
|
|
159 |
}
|
|
|
160 |
|
1813 |
jpm |
161 |
private function modifierEnteteTotal() {
|
|
|
162 |
$requete = 'SELECT FOUND_ROWS() AS nbre -- '.__FILE__.' : '.__LINE__;
|
|
|
163 |
$compte = $this->bdd->recuperer($requete);
|
|
|
164 |
$total = ($compte !== false) ? (int) $compte['nbre'] : 0;
|
|
|
165 |
$this->navigation->setTotal($total);
|
1464 |
raphael |
166 |
}
|
1700 |
jpm |
167 |
|
1142 |
aurelien |
168 |
/**
|
|
|
169 |
* Retourner un tableau d'images formaté en fonction des liaisons trouvées
|
1813 |
jpm |
170 |
* @param $infos les infos sur les images et observations
|
1142 |
aurelien |
171 |
* */
|
1813 |
jpm |
172 |
private function traiterResultats($infos) {
|
1874 |
jpm |
173 |
//Debug::printr($infos);
|
1813 |
jpm |
174 |
foreach ($infos as $info) {
|
|
|
175 |
$idImage = $info['id_image'];
|
|
|
176 |
$index = $this->formaterIndexResultat($info);
|
1700 |
jpm |
177 |
|
1813 |
jpm |
178 |
$this->obsIds[] = $info['id_observation'];
|
1168 |
aurelien |
179 |
$this->indexImagesIds[$idImage] = $index;
|
1813 |
jpm |
180 |
$this->resultats[$index] = array(
|
1700 |
jpm |
181 |
'id_image' => $idImage,
|
1813 |
jpm |
182 |
'id_observation' => $info['id_observation'],
|
|
|
183 |
'auteur.intitule' => $this->formaterIntituleAuteur($info),
|
|
|
184 |
'binaire.href' => $this->formaterBinaireHref($info),
|
|
|
185 |
'determination.famille' => $info['famille'],
|
|
|
186 |
'determination.referentiel' => $info['nom_referentiel'],
|
|
|
187 |
'determination.ns' => $info['nom_ret'],
|
|
|
188 |
'determination.nn' => $info['nom_ret_nn'],
|
|
|
189 |
'determination.nt' => $info['nt'],
|
|
|
190 |
'date_observation' => $info['date_observation'],
|
|
|
191 |
'localite' => $this->formaterLieu($info),
|
|
|
192 |
'mots_cles_image_cel' => $this->formaterMotsClesCel($info),
|
|
|
193 |
'mots_cles_image_del' => ''
|
1142 |
aurelien |
194 |
);
|
|
|
195 |
}
|
|
|
196 |
}
|
1700 |
jpm |
197 |
|
1813 |
jpm |
198 |
private function formaterIndexResultat($infos) {
|
|
|
199 |
return $infos['id_image'].'-'.$infos['id_observation'];
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
private function formaterIntituleAuteur($infos) {
|
|
|
203 |
if ($infos['ce_utilisateur'] == 0) {
|
|
|
204 |
$infos['prenom'] = $infos['prenom_utilisateur'];
|
|
|
205 |
$infos['nom'] = $infos['nom_utilisateur'];
|
|
|
206 |
}
|
|
|
207 |
$intitule = $infos['prenom'].' '.$infos['nom'];
|
|
|
208 |
return $intitule;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
private function formaterBinaireHref($infos) {
|
1907 |
mathias |
212 |
return sprintf(
|
|
|
213 |
$this->conteneur->getParametre('cel_img_url_tpl'),
|
|
|
214 |
$infos['id_image'],
|
|
|
215 |
$this->conteneur->getParametre('determinations.format_image')
|
|
|
216 |
);
|
1813 |
jpm |
217 |
}
|
|
|
218 |
|
|
|
219 |
private function formaterLieu($infos) {
|
|
|
220 |
$lieuFormate = '';
|
|
|
221 |
if ($infos['ce_zone_geo']) {
|
|
|
222 |
$lieu = $infos['zone_geo'];
|
|
|
223 |
$id_zone_geo = $infos['ce_zone_geo'];
|
|
|
224 |
if (strpos($infos['ce_zone_geo'], 'INSEE-C:') === 0) {
|
|
|
225 |
$id_zone_geo = str_replace('INSEE-C:', '', $infos['ce_zone_geo']);
|
|
|
226 |
$id_zone_geo = strlen($id_zone_geo) >= 5 ? substr($id_zone_geo, 0, 2) : $id_zone_geo;
|
|
|
227 |
}
|
|
|
228 |
$lieuFormate = "$lieu ($id_zone_geo)";
|
|
|
229 |
}
|
|
|
230 |
return $lieuFormate;
|
|
|
231 |
}
|
|
|
232 |
|
1142 |
aurelien |
233 |
/**
|
1813 |
jpm |
234 |
* Formater les mots clés du cel en n'affichant que ceux faisant partie d'une liste définie dans le
|
|
|
235 |
* fichier de configuration.
|
|
|
236 |
*
|
|
|
237 |
* @param $infos le tableau contenant les infos sur une image.
|
|
|
238 |
* @return string la chaine filtrée
|
1700 |
jpm |
239 |
*/
|
1813 |
jpm |
240 |
private function formaterMotsClesCel($infos) {
|
|
|
241 |
$motsClesAffiches = $this->conteneur->getParametreTableau('determinations.mots_cles_cel_affiches');
|
|
|
242 |
$motsClesCel = explode(',', $infos['mots_cles_texte_image']);
|
|
|
243 |
$motsCles = array_intersect($motsClesAffiches, $motsClesCel);
|
|
|
244 |
return implode(',', $motsCles);
|
|
|
245 |
}
|
1468 |
raphael |
246 |
|
1813 |
jpm |
247 |
private function completerResutlats() {
|
|
|
248 |
$this->chargerVotes();
|
|
|
249 |
$this->chargerPropositions();
|
1468 |
raphael |
250 |
|
1813 |
jpm |
251 |
$this->completerMotsCles();
|
1468 |
raphael |
252 |
|
1813 |
jpm |
253 |
foreach ($this->resultats as $index => $infos) {
|
|
|
254 |
if ($this->doitRemplacerObservationParProposition($index)) {
|
|
|
255 |
$id_obs = $infos['id_observation'];
|
|
|
256 |
$this->resultats[$index]['determination.famille'] = $this->propositions[$id_obs]['famille'];
|
|
|
257 |
$this->resultats[$index]['determination.ns'] = $this->propositions[$id_obs]['nom_sel'];
|
|
|
258 |
$this->resultats[$index]['determination.nn'] = $this->propositions[$id_obs]['nom_sel_nn'];
|
|
|
259 |
$this->resultats[$index]['determination.nt'] = $this->propositions[$id_obs]['nt'];
|
1922 |
jpm |
260 |
$this->resultats[$index]['determination.referentiel'] = $this->propositions[$id_obs]['nom_referentiel'];
|
1813 |
jpm |
261 |
}
|
|
|
262 |
$this->completerUrlFicheEflore($index);
|
|
|
263 |
}
|
|
|
264 |
}
|
1700 |
jpm |
265 |
|
1813 |
jpm |
266 |
private function chargerVotes() {
|
|
|
267 |
$idsObs = implode(',', $this->obsIds);
|
|
|
268 |
$requete = 'SELECT ce_proposition, valeur, ce_utilisateur '.
|
|
|
269 |
'FROM del_commentaire_vote '.
|
|
|
270 |
'WHERE ce_proposition IN '.
|
|
|
271 |
'( SELECT id_commentaire '.
|
|
|
272 |
' FROM del_commentaire '.
|
|
|
273 |
" WHERE ce_observation IN ($idsObs) AND nom_sel IS NOT NULL ) ".
|
|
|
274 |
'ORDER BY ce_proposition '.
|
|
|
275 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
276 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
277 |
if ($resultats !== false) {
|
|
|
278 |
foreach ($resultats as $vote) {
|
|
|
279 |
if (!isset($this->votes[$vote['ce_proposition']])) {
|
|
|
280 |
$this->votes[$vote['ce_proposition']] = 0;
|
|
|
281 |
}
|
|
|
282 |
$valeur = ($vote['valeur'] == 1) ? 1 : -1;
|
|
|
283 |
$this->votes[$vote['ce_proposition']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
|
1142 |
aurelien |
284 |
}
|
1700 |
jpm |
285 |
}
|
1813 |
jpm |
286 |
}
|
1700 |
jpm |
287 |
|
1813 |
jpm |
288 |
private function chargerPropositions() {
|
|
|
289 |
$idsObs = implode(',', $this->obsIds);
|
|
|
290 |
$requete = 'SELECT * '.
|
|
|
291 |
'FROM del_commentaire '.
|
|
|
292 |
"WHERE ce_observation IN ($idsObs) ".
|
|
|
293 |
'AND nom_sel IS NOT NULL '.
|
|
|
294 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
295 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
296 |
|
|
|
297 |
foreach($resultats as $proposition) {
|
1700 |
jpm |
298 |
$id_proposition = $proposition['id_commentaire'];
|
|
|
299 |
$id_obs = $proposition['ce_observation'];
|
1813 |
jpm |
300 |
$proposition['valeur'] = (isset($this->votes[$id_proposition])) ? $this->votes[$id_proposition] : -1;
|
1700 |
jpm |
301 |
|
1813 |
jpm |
302 |
if (!isset($this->propositions[$id_obs])) {
|
|
|
303 |
$this->propositions[$id_obs] = $proposition;
|
1700 |
jpm |
304 |
} else {
|
1813 |
jpm |
305 |
$score_actuel = $proposition['valeur'];
|
|
|
306 |
$score_precedent = $this->propositions[$id_obs]['valeur'];
|
|
|
307 |
if ($score_actuel >= $score_precedent) {
|
|
|
308 |
$this->propositions[$id_obs] = $proposition;
|
|
|
309 |
}
|
1142 |
aurelien |
310 |
}
|
1700 |
jpm |
311 |
}
|
1813 |
jpm |
312 |
}
|
1168 |
aurelien |
313 |
|
1813 |
jpm |
314 |
private function completerMotsCles() {
|
|
|
315 |
$idsImages = implode(',', array_keys($this->indexImagesIds));
|
|
|
316 |
$requete = 'SELECT tag, ce_image '.
|
|
|
317 |
'FROM del_image_tag '.
|
|
|
318 |
"WHERE ce_image IN ($idsImages) ".
|
|
|
319 |
'AND actif = 1 '.
|
|
|
320 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
321 |
$resultats = $this->bdd->recupererTous($requete);
|
1700 |
jpm |
322 |
|
1813 |
jpm |
323 |
foreach ($resultats as $info) {
|
|
|
324 |
$index = $this->indexImagesIds[$info['ce_image']];
|
|
|
325 |
$tag = ($this->resultats[$index]['mots_cles_image_del'] != '') ? ','.$info['tag'] : $info['tag'];
|
|
|
326 |
$this->resultats[$index]['mots_cles_image_del'] .= $tag ;
|
1700 |
jpm |
327 |
}
|
1142 |
aurelien |
328 |
}
|
1700 |
jpm |
329 |
|
1813 |
jpm |
330 |
private function doitRemplacerObservationParProposition($index) {
|
|
|
331 |
$idObs = $this->resultats[$index]['id_observation'];
|
|
|
332 |
return ((isset($this->propositions[$idObs])
|
|
|
333 |
&& $this->propositions[$idObs] != null
|
|
|
334 |
&& $this->propositions[$idObs]['nom_sel_nn'] != 0)
|
|
|
335 |
&& ($this->propositions[$idObs]['valeur'] > 0 || $this->resultats[$index]['determination.nn'] == 0)
|
1142 |
aurelien |
336 |
);
|
|
|
337 |
}
|
1700 |
jpm |
338 |
|
1813 |
jpm |
339 |
private function completerUrlFicheEflore($index) {
|
|
|
340 |
if (isset($this->resultats[$index]['determination.nn'])) {
|
|
|
341 |
$urlTpl = $this->conteneur->getParametre('determinations.url_fiche_eflore_tpl');
|
1922 |
jpm |
342 |
$nn = (int) $this->resultats[$index]['determination.nn'];
|
|
|
343 |
$ref = $this->resultats[$index]['determination.referentiel'];
|
|
|
344 |
$code_ref = $this->getCodeReferentiel($ref);
|
|
|
345 |
if (is_int($nn) && ! isset($code_ref)) {
|
|
|
346 |
$code_ref = 'bdtfx';
|
|
|
347 |
}
|
|
|
348 |
$this->resultats[$index]['url_fiche_eflore'] = sprintf($urlTpl, $code_ref, $nn);
|
|
|
349 |
}
|
|
|
350 |
}
|
1142 |
aurelien |
351 |
|
1922 |
jpm |
352 |
private function getCodeReferentiel($ref) {
|
|
|
353 |
$code = $ref;
|
|
|
354 |
if ($position = strpos($ref, '_')) {
|
|
|
355 |
$code = substr($ref, 0, $position);
|
|
|
356 |
} else if ($position = strpos($ref, ':')) {
|
|
|
357 |
$code = substr($ref, 0, $position);
|
1700 |
jpm |
358 |
}
|
1922 |
jpm |
359 |
return $code;
|
1144 |
aurelien |
360 |
}
|
1813 |
jpm |
361 |
}
|