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 |
|
1813 |
jpm |
32 |
private $resultats = array();
|
|
|
33 |
private $propositions = array();
|
|
|
34 |
private $votes = array();
|
|
|
35 |
|
1142 |
aurelien |
36 |
public function __construct(Conteneur $conteneur = null) {
|
|
|
37 |
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
|
|
|
38 |
$this->navigation = $conteneur->getNavigation();
|
1793 |
jpm |
39 |
$this->bdd = $this->conteneur->getBdd();
|
1813 |
jpm |
40 |
$this->chargerProtocoles();
|
1142 |
aurelien |
41 |
}
|
1700 |
jpm |
42 |
|
1813 |
jpm |
43 |
private function chargerProtocoles() {
|
|
|
44 |
$requete = 'SELECT id_protocole FROM del_image_protocole -- '.__FILE__.' : '.__LINE__;
|
|
|
45 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
46 |
if ($resultats) {
|
|
|
47 |
foreach ($resultats as $infos) {
|
|
|
48 |
$this->protocoles[] = $infos['id_protocole'];
|
|
|
49 |
}
|
|
|
50 |
sort($this->protocoles);
|
1468 |
raphael |
51 |
}
|
1813 |
jpm |
52 |
}
|
1700 |
jpm |
53 |
|
1813 |
jpm |
54 |
public function consulter($parametres) {
|
|
|
55 |
$this->parametres = $parametres;
|
1143 |
aurelien |
56 |
$this->verifierParametres();
|
1700 |
jpm |
57 |
|
1142 |
aurelien |
58 |
// Lancement du service
|
1813 |
jpm |
59 |
$infos = $this->chargerInfos();
|
|
|
60 |
$this->modifierEnteteTotal();
|
|
|
61 |
if ($infos) {
|
|
|
62 |
$this->traiterResultats($infos);
|
|
|
63 |
$this->completerResutlats();
|
1144 |
aurelien |
64 |
}
|
1700 |
jpm |
65 |
|
1142 |
aurelien |
66 |
// Mettre en forme le résultat et l'envoyer pour affichage
|
|
|
67 |
$resultat = new ResultatService();
|
1813 |
jpm |
68 |
$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $this->resultats);
|
1142 |
aurelien |
69 |
return $resultat;
|
|
|
70 |
}
|
1700 |
jpm |
71 |
|
|
|
72 |
private function verifierParametres() {
|
1813 |
jpm |
73 |
$this->verifierParamProtocole();
|
|
|
74 |
$this->verifierParamVote();
|
1144 |
aurelien |
75 |
|
1813 |
jpm |
76 |
if (!empty($this->erreurs)) {
|
|
|
77 |
$msg = "Erreur de configuration :\n".implode("\n\n", $this->erreurs);
|
|
|
78 |
throw new Exception($msg, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
1142 |
aurelien |
79 |
}
|
1813 |
jpm |
80 |
}
|
1700 |
jpm |
81 |
|
1813 |
jpm |
82 |
private function verifierParamProtocole() {
|
|
|
83 |
if (isset($this->parametres['masque.protocole'])) {
|
|
|
84 |
$protocoleExistant = in_array($this->parametres['masque.protocole'], $this->protocoles);
|
|
|
85 |
|
|
|
86 |
if (!is_numeric($this->parametres['masque.protocole']) || $protocoleExistant === false) {
|
|
|
87 |
$protocolesListe = implode(', ', $this->protocoles);
|
|
|
88 |
$msg = "La valeur pour le protocole doit être un entier compris dans les numéros de protocole ".
|
|
|
89 |
"existants : $protocolesListe";
|
|
|
90 |
$this->erreurs[] = $msg;
|
|
|
91 |
}
|
1142 |
aurelien |
92 |
}
|
1813 |
jpm |
93 |
}
|
1700 |
jpm |
94 |
|
1813 |
jpm |
95 |
private function verifierParamVote() {
|
|
|
96 |
if (isset($this->parametres['masque.valeur_min_vote'])) {
|
|
|
97 |
$minVote = $this->parametres['masque.valeur_min_vote'];
|
|
|
98 |
if (!is_numeric($minVote) || ($minVote < 0 && $minVote > 5)) {
|
|
|
99 |
$this->erreurs[] = "La valeur minimum de valeur des votes doit être un entier compris entre 0 et 5.";
|
|
|
100 |
}
|
1142 |
aurelien |
101 |
}
|
|
|
102 |
}
|
1700 |
jpm |
103 |
|
1813 |
jpm |
104 |
private function chargerInfos() {
|
|
|
105 |
$whereClause = $this->getClauseWhere();
|
|
|
106 |
$depart = $this->navigation->getDepart();
|
|
|
107 |
$limite = $this->navigation->getLimite();
|
1464 |
raphael |
108 |
|
1813 |
jpm |
109 |
$requete = 'SELECT SQL_CALC_FOUND_ROWS di.id_observation AS id_observation, nom_referentiel, nom_ret, '.
|
|
|
110 |
'nom_ret_nn, nt, famille, ce_zone_geo, zone_geo, lieudit, station, milieu, date_observation, '.
|
|
|
111 |
'di.mots_cles_texte AS mots_cles_texte, di.commentaire AS commentaire, '.
|
|
|
112 |
'di.i_mots_cles_texte AS mots_cles_texte_image, date_transmission, di.id_image AS id_image, '.
|
|
|
113 |
'di.ce_utilisateur AS ce_utilisateur, prenom, nom, courriel, di.nom_utilisateur, di.prenom_utilisateur, '.
|
|
|
114 |
'di.courriel_utilisateur, nom_original, ce_protocole, moyenne, nb_votes, nb_tags '.
|
|
|
115 |
'FROM v_del_image AS di '.
|
|
|
116 |
' LEFT JOIN del_utilisateur AS du '.
|
|
|
117 |
' ON du.id_utilisateur = IF(LENGTH(di.ce_utilisateur) > 5, 0, di.ce_utilisateur) '.
|
|
|
118 |
' LEFT JOIN del_image_stat AS ds ON (di.id_image = ds.ce_image) '.
|
|
|
119 |
"$whereClause ".
|
|
|
120 |
'GROUP BY id_image '.
|
|
|
121 |
'ORDER BY moyenne '.
|
|
|
122 |
"LIMIT $depart, $limite ".
|
|
|
123 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
124 |
// TODO : optimisation voir http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelOptmisationVues
|
|
|
125 |
return $this->bdd->recupererTous($requete);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
private function getClauseWhere() {
|
1700 |
jpm |
129 |
$where = array();
|
1813 |
jpm |
130 |
if (isset($this->parametres['masque.protocole'])) {
|
|
|
131 |
$protocoleIdP = $this->bdd->proteger($this->parametres['masque.protocole']);
|
|
|
132 |
$where[] = "ds.ce_protocole = $protocoleIdP ";
|
|
|
133 |
}
|
|
|
134 |
if (isset($this->parametres['masque.valeur_vote_min'])) {
|
|
|
135 |
$voteP = $this->bdd->proteger($this->parametres['masque.valeur_vote_min']);
|
|
|
136 |
$where[] = "moyenne >= $voteP";
|
|
|
137 |
}
|
|
|
138 |
return (count($where) > 0) ? 'WHERE '.implode(' AND ', $where).' ' : '';
|
|
|
139 |
}
|
1464 |
raphael |
140 |
|
1813 |
jpm |
141 |
private function modifierEnteteTotal() {
|
|
|
142 |
$requete = 'SELECT FOUND_ROWS() AS nbre -- '.__FILE__.' : '.__LINE__;
|
|
|
143 |
$compte = $this->bdd->recuperer($requete);
|
|
|
144 |
$total = ($compte !== false) ? (int) $compte['nbre'] : 0;
|
|
|
145 |
$this->navigation->setTotal($total);
|
1464 |
raphael |
146 |
}
|
1700 |
jpm |
147 |
|
1142 |
aurelien |
148 |
/**
|
|
|
149 |
* Retourner un tableau d'images formaté en fonction des liaisons trouvées
|
1813 |
jpm |
150 |
* @param $infos les infos sur les images et observations
|
1142 |
aurelien |
151 |
* */
|
1813 |
jpm |
152 |
private function traiterResultats($infos) {
|
|
|
153 |
foreach ($infos as $info) {
|
|
|
154 |
$idImage = $info['id_image'];
|
|
|
155 |
$index = $this->formaterIndexResultat($info);
|
1700 |
jpm |
156 |
|
1813 |
jpm |
157 |
$this->obsIds[] = $info['id_observation'];
|
1168 |
aurelien |
158 |
$this->indexImagesIds[$idImage] = $index;
|
1813 |
jpm |
159 |
$this->resultats[$index] = array(
|
1700 |
jpm |
160 |
'id_image' => $idImage,
|
1813 |
jpm |
161 |
'id_observation' => $info['id_observation'],
|
|
|
162 |
'auteur.intitule' => $this->formaterIntituleAuteur($info),
|
|
|
163 |
'binaire.href' => $this->formaterBinaireHref($info),
|
|
|
164 |
'determination.famille' => $info['famille'],
|
|
|
165 |
'determination.referentiel' => $info['nom_referentiel'],
|
|
|
166 |
'determination.ns' => $info['nom_ret'],
|
|
|
167 |
'determination.nn' => $info['nom_ret_nn'],
|
|
|
168 |
'determination.nt' => $info['nt'],
|
|
|
169 |
'date_observation' => $info['date_observation'],
|
|
|
170 |
'localite' => $this->formaterLieu($info),
|
|
|
171 |
'mots_cles_image_cel' => $this->formaterMotsClesCel($info),
|
|
|
172 |
'mots_cles_image_del' => ''
|
1142 |
aurelien |
173 |
);
|
|
|
174 |
}
|
|
|
175 |
}
|
1700 |
jpm |
176 |
|
1813 |
jpm |
177 |
private function formaterIndexResultat($infos) {
|
|
|
178 |
return $infos['id_image'].'-'.$infos['id_observation'];
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
private function formaterIntituleAuteur($infos) {
|
|
|
182 |
if ($infos['ce_utilisateur'] == 0) {
|
|
|
183 |
$infos['prenom'] = $infos['prenom_utilisateur'];
|
|
|
184 |
$infos['nom'] = $infos['nom_utilisateur'];
|
|
|
185 |
}
|
|
|
186 |
$intitule = $infos['prenom'].' '.$infos['nom'];
|
|
|
187 |
return $intitule;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
private function formaterBinaireHref($infos) {
|
|
|
191 |
return sprintf($this->conteneur->getParametre('determinations.url_image_tpl'), $infos['id_image']);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
private function formaterLieu($infos) {
|
|
|
195 |
$lieuFormate = '';
|
|
|
196 |
if ($infos['ce_zone_geo']) {
|
|
|
197 |
$lieu = $infos['zone_geo'];
|
|
|
198 |
$id_zone_geo = $infos['ce_zone_geo'];
|
|
|
199 |
if (strpos($infos['ce_zone_geo'], 'INSEE-C:') === 0) {
|
|
|
200 |
$id_zone_geo = str_replace('INSEE-C:', '', $infos['ce_zone_geo']);
|
|
|
201 |
$id_zone_geo = strlen($id_zone_geo) >= 5 ? substr($id_zone_geo, 0, 2) : $id_zone_geo;
|
|
|
202 |
}
|
|
|
203 |
$lieuFormate = "$lieu ($id_zone_geo)";
|
|
|
204 |
}
|
|
|
205 |
return $lieuFormate;
|
|
|
206 |
}
|
|
|
207 |
|
1142 |
aurelien |
208 |
/**
|
1813 |
jpm |
209 |
* Formater les mots clés du cel en n'affichant que ceux faisant partie d'une liste définie dans le
|
|
|
210 |
* fichier de configuration.
|
|
|
211 |
*
|
|
|
212 |
* @param $infos le tableau contenant les infos sur une image.
|
|
|
213 |
* @return string la chaine filtrée
|
1700 |
jpm |
214 |
*/
|
1813 |
jpm |
215 |
private function formaterMotsClesCel($infos) {
|
|
|
216 |
$motsClesAffiches = $this->conteneur->getParametreTableau('determinations.mots_cles_cel_affiches');
|
|
|
217 |
$motsClesCel = explode(',', $infos['mots_cles_texte_image']);
|
|
|
218 |
$motsCles = array_intersect($motsClesAffiches, $motsClesCel);
|
|
|
219 |
return implode(',', $motsCles);
|
|
|
220 |
}
|
1468 |
raphael |
221 |
|
1813 |
jpm |
222 |
private function completerResutlats() {
|
|
|
223 |
$this->chargerVotes();
|
|
|
224 |
$this->chargerPropositions();
|
1468 |
raphael |
225 |
|
1813 |
jpm |
226 |
$this->completerMotsCles();
|
1468 |
raphael |
227 |
|
1813 |
jpm |
228 |
foreach ($this->resultats as $index => $infos) {
|
|
|
229 |
if ($this->doitRemplacerObservationParProposition($index)) {
|
|
|
230 |
$id_obs = $infos['id_observation'];
|
|
|
231 |
$this->resultats[$index]['determination.famille'] = $this->propositions[$id_obs]['famille'];
|
|
|
232 |
$this->resultats[$index]['determination.ns'] = $this->propositions[$id_obs]['nom_sel'];
|
|
|
233 |
$this->resultats[$index]['determination.nn'] = $this->propositions[$id_obs]['nom_sel_nn'];
|
|
|
234 |
$this->resultats[$index]['determination.nt'] = $this->propositions[$id_obs]['nt'];
|
|
|
235 |
}
|
|
|
236 |
$this->completerUrlFicheEflore($index);
|
|
|
237 |
}
|
|
|
238 |
}
|
1700 |
jpm |
239 |
|
1813 |
jpm |
240 |
private function chargerVotes() {
|
|
|
241 |
$idsObs = implode(',', $this->obsIds);
|
|
|
242 |
$requete = 'SELECT ce_proposition, valeur, ce_utilisateur '.
|
|
|
243 |
'FROM del_commentaire_vote '.
|
|
|
244 |
'WHERE ce_proposition IN '.
|
|
|
245 |
'( SELECT id_commentaire '.
|
|
|
246 |
' FROM del_commentaire '.
|
|
|
247 |
" WHERE ce_observation IN ($idsObs) AND nom_sel IS NOT NULL ) ".
|
|
|
248 |
'ORDER BY ce_proposition '.
|
|
|
249 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
250 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
251 |
if ($resultats !== false) {
|
|
|
252 |
foreach ($resultats as $vote) {
|
|
|
253 |
if (!isset($this->votes[$vote['ce_proposition']])) {
|
|
|
254 |
$this->votes[$vote['ce_proposition']] = 0;
|
|
|
255 |
}
|
|
|
256 |
$valeur = ($vote['valeur'] == 1) ? 1 : -1;
|
|
|
257 |
$this->votes[$vote['ce_proposition']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
|
1142 |
aurelien |
258 |
}
|
1700 |
jpm |
259 |
}
|
1813 |
jpm |
260 |
}
|
1700 |
jpm |
261 |
|
1813 |
jpm |
262 |
private function chargerPropositions() {
|
|
|
263 |
$idsObs = implode(',', $this->obsIds);
|
|
|
264 |
$requete = 'SELECT * '.
|
|
|
265 |
'FROM del_commentaire '.
|
|
|
266 |
"WHERE ce_observation IN ($idsObs) ".
|
|
|
267 |
'AND nom_sel IS NOT NULL '.
|
|
|
268 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
269 |
$resultats = $this->bdd->recupererTous($requete);
|
|
|
270 |
|
|
|
271 |
foreach($resultats as $proposition) {
|
1700 |
jpm |
272 |
$id_proposition = $proposition['id_commentaire'];
|
|
|
273 |
$id_obs = $proposition['ce_observation'];
|
1813 |
jpm |
274 |
$proposition['valeur'] = (isset($this->votes[$id_proposition])) ? $this->votes[$id_proposition] : -1;
|
1700 |
jpm |
275 |
|
1813 |
jpm |
276 |
if (!isset($this->propositions[$id_obs])) {
|
|
|
277 |
$this->propositions[$id_obs] = $proposition;
|
1700 |
jpm |
278 |
} else {
|
1813 |
jpm |
279 |
$score_actuel = $proposition['valeur'];
|
|
|
280 |
$score_precedent = $this->propositions[$id_obs]['valeur'];
|
|
|
281 |
if ($score_actuel >= $score_precedent) {
|
|
|
282 |
$this->propositions[$id_obs] = $proposition;
|
|
|
283 |
}
|
1142 |
aurelien |
284 |
}
|
1700 |
jpm |
285 |
}
|
1813 |
jpm |
286 |
}
|
1168 |
aurelien |
287 |
|
1813 |
jpm |
288 |
private function completerMotsCles() {
|
|
|
289 |
$idsImages = implode(',', array_keys($this->indexImagesIds));
|
|
|
290 |
$requete = 'SELECT tag, ce_image '.
|
|
|
291 |
'FROM del_image_tag '.
|
|
|
292 |
"WHERE ce_image IN ($idsImages) ".
|
|
|
293 |
'AND actif = 1 '.
|
|
|
294 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
295 |
$resultats = $this->bdd->recupererTous($requete);
|
1700 |
jpm |
296 |
|
1813 |
jpm |
297 |
foreach ($resultats as $info) {
|
|
|
298 |
$index = $this->indexImagesIds[$info['ce_image']];
|
|
|
299 |
$tag = ($this->resultats[$index]['mots_cles_image_del'] != '') ? ','.$info['tag'] : $info['tag'];
|
|
|
300 |
$this->resultats[$index]['mots_cles_image_del'] .= $tag ;
|
1700 |
jpm |
301 |
}
|
1142 |
aurelien |
302 |
}
|
1700 |
jpm |
303 |
|
1813 |
jpm |
304 |
private function doitRemplacerObservationParProposition($index) {
|
|
|
305 |
$idObs = $this->resultats[$index]['id_observation'];
|
|
|
306 |
return ((isset($this->propositions[$idObs])
|
|
|
307 |
&& $this->propositions[$idObs] != null
|
|
|
308 |
&& $this->propositions[$idObs]['nom_sel_nn'] != 0)
|
|
|
309 |
&& ($this->propositions[$idObs]['valeur'] > 0 || $this->resultats[$index]['determination.nn'] == 0)
|
1142 |
aurelien |
310 |
);
|
|
|
311 |
}
|
1700 |
jpm |
312 |
|
1813 |
jpm |
313 |
private function completerUrlFicheEflore($index) {
|
|
|
314 |
if (isset($this->resultats[$index]['determination.nn'])) {
|
|
|
315 |
$urlTpl = $this->conteneur->getParametre('determinations.url_fiche_eflore_tpl');
|
|
|
316 |
$nn = $this->resultats[$index]['determination.nn'];
|
1142 |
aurelien |
317 |
|
1813 |
jpm |
318 |
$this->resultats[$index]['url_fiche_eflore'] = sprintf($urlTpl, $nn);
|
1700 |
jpm |
319 |
}
|
1144 |
aurelien |
320 |
}
|
1813 |
jpm |
321 |
}
|