Subversion Repositories eFlore/Applications.del

Rev

Rev 1657 | Rev 1681 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1593 samuel 1
<?php
2
/**
1663 samuel 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
1663 samuel 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
 
76
                  $obs = $this->regrouperObs($liaisons);
1640 samuel 77
                  $obs = $this->chargerPropositionPlusProbable($obs);
1643 samuel 78
                  $obs = $this->orderArray($obs);
1593 samuel 79
		}
80
 
81
		$this->navigation->setTotal($total);
82
 
83
		// Mettre en forme le résultat et l'envoyer pour affichage
84
		$resultat = new ResultatService();
85
		//$resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $images);
86
                $resultat->corps = array('entete' => $this->conteneur->getEntete(), 'resultats' => $obs);
87
 
88
		return $resultat;
89
	}
90
 
1643 samuel 91
 
92
        private function orderArray(&$obs) {
93
 
94
          $ret = array();
95
 
96
          foreach ($obs as $o) {
97
            $ret[] = $o;
98
          }
99
 
100
          function cmp($a, $b) {
101
            return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
102
          }
103
 
104
          usort($ret, 'cmp');
105
          return $ret;
106
        }
1593 samuel 107
	/*-------------------------------------------------------------------------------
108
								CHARGEMENT DES IMAGES
109
	--------------------------------------------------------------------------------*/
110
	private function chargerLiaisons() {
111
 
112
          $date_debut = $this->parametres['date'];
1642 samuel 113
          $date_debut = '\'' . $date_debut . '\'';
1593 samuel 114
          $limite = @min(intval($this->parametres['navigation.limite']), 1000);
115
          $limite = $limite ? $limite : 10; // 0 => 10
116
          $depart = intval(@$this->parametres['navigation.depart']);
117
 
118
 
1642 samuel 119
 
1593 samuel 120
          $requete_sql =
121
            'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
1640 samuel 122
            'vdi.nom_sel, '.
1593 samuel 123
            'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
1640 samuel 124
            'vdi.zone_geo, vdi.latitude, vdi.longitude, '.
125
            'vdi.date_observation, vdi.date_creation, vdi.date_transmission, '.
1652 samuel 126
            'vdi.mots_cles_texte as mots_cles_texte, '.
127
            'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
1593 samuel 128
 
129
            'vdi.ce_utilisateur as ce_utilisateur, '.
130
            'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image, '.
131
 
132
            'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
133
 
134
            'GREATEST(vdi.date_creation, vdi.date_modification, MAX(del_image_tag.date), '.
135
            'MAX(del_image_tag.date_modification), MAX(del_image_vote.date), '.
136
            'IFNULL(MAX(del_commentaire.date),0), IFNULL(MAX(del_commentaire_vote.date),0)) as modif_date '.
137
 
138
            'from v_del_image as vdi '.
139
 
140
            'left join del_image_vote on del_image_vote.ce_image=id_image '.
141
            'left join del_image_tag on del_image_tag.ce_image=id_image '.
142
            'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
143
            'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
144
 
145
            'where ce_protocole=3 and actif=1 '.
146
 
147
            'group by id_image, id_observation '.
148
 
149
            'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
150
            '       MAX(vdi.date_modification) >= '.$date_debut.' or '.
151
            '       MAX(del_image_tag.date) >= '.$date_debut.' or '.
152
            '       MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
153
            '       MAX(del_image_vote.date) >= '.$date_debut.' or '.
154
            '       MAX(del_commentaire.date) >= '.$date_debut.' or '.
155
            '       MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
156
            'order by modif_date DESC '.
157
            'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
158
 
1643 samuel 159
          //echo $requete_sql; exit;
160
 
1593 samuel 161
          return $this->bdd->recupererTous($requete_sql);
162
 
163
 
164
 
1663 samuel 165
          // GROUP BY (très couteux) car multiples observations associées à une image
166
          // charlie est ici :-)
167
          // eg: 16150,16185,16245,16246,118995,129989
1593 samuel 168
 
169
	}
170
 
171
	/**
172
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
173
	* @param $liaisons les liaisons de la table del_obs_images
174
	* */
175
	private function regrouperObs(&$liaisons) {
176
 
177
          // regroupe les observations
178
          $obs = array();
179
          foreach ($liaisons as $img) {
180
            $idobs = $img['id_observation'];
181
 
182
            if (!isset($obs[$idobs])) {
183
              $obs[$idobs] = array();
184
            }
185
 
186
            $obs[$idobs]['id_observation'] = $idobs;
1640 samuel 187
            $obs[$idobs]['auteur_id'] = $img['ce_utilisateur'];
188
            $obs[$idobs]['auteur_prenom'] = $img['prenom_utilisateur'];
189
            $obs[$idobs]['auteur_nom'] = $img['nom_utilisateur'];
190
            $obs[$idobs]['auteur_courriel'] = $img['courriel_utilisateur'];
1593 samuel 191
 
1654 samuel 192
            $obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($img['mots_cles_texte']);
1640 samuel 193
 
194
            $obs[$idobs]['date_observation'] = $img['date_observation'];
195
            $obs[$idobs]['date_publication'] = $img['date_transmission'];
196
            $obs[$idobs]['date_creation'] = $img['date_creation'];
197
            $obs[$idobs]['date_changement'] = $img['modif_date'];
198
 
199
            $obs[$idobs]['nom_sel'] = $img['nom_sel'];
200
            $obs[$idobs]['nom_referentiel'] = $img['nom_referentiel'];
201
            $obs[$idobs]['nom_ret'] = $img['nom_ret'];
202
            $obs[$idobs]['nn'] = $img['nom_ret_nn'];
203
            $obs[$idobs]['nt'] = $img['nt'];
204
            $obs[$idobs]['famille'] = $img['famille'];
205
 
206
            $obs[$idobs]['zone_geo'] = $img['zone_geo'];
1653 samuel 207
            $obs[$idobs]['latitude'] = floatval($img['latitude']);
208
            $obs[$idobs]['longitude'] = floatval($img['longitude']);
1640 samuel 209
 
210
 
1593 samuel 211
            if (!isset($obs[$idobs]['images'])) {
212
              $obs[$idobs]['images'] = array();
213
            }
214
 
1640 samuel 215
            $img_obj = array(
216
                             'id_image' => $img['id_image'],
217
                             'nom_image' => $img['nom_image'],
1652 samuel 218
                             'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $img['id_image']),
1640 samuel 219
                             'votes' => array_map('intval', explode(',', $img['votes'])),
220
                             'tags' => explode(',', $img['tags']),
1654 samuel 221
                             'mots_cles_img_cel' => $this->formaterMotsClesCel($img['mots_cles_texte_image'])
1640 samuel 222
                             );
223
            // push
224
            $obs[$idobs]['images'][] = $img_obj;
1593 samuel 225
 
226
          }
227
 
228
          return $obs;
229
 
230
	}
231
 
232
	/**
1640 samuel 233
         * Charger les votes pour chaque image
234
         **/
235
	private function chargerPropositionPlusProbable(&$obs) {
1593 samuel 236
 
1640 samuel 237
          $obsIds = array_keys($obs);
1593 samuel 238
 
1640 samuel 239
          $resultats = $this->bdd->recupererTous(sprintf(
1657 samuel 240
                  'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, del_commentaire_vote.ce_utilisateur '.
1640 samuel 241
                  'FROM del_commentaire_vote, del_commentaire '.
242
                  'WHERE ce_observation IN (%s) '.
243
                  'AND nom_sel IS NOT NULL '.
244
                  'AND del_commentaire.id_commentaire=del_commentaire_vote.ce_proposition '.
245
                  '', implode(',', $obsIds)));
1593 samuel 246
 
247
 
248
          $votes = array(); // map ce_proposition -> score
249
 
250
          // calcul des votes
251
          // un vote identifié a un facteur de 3
1640 samuel 252
          // additionne tous les vote par ce_proposition
253
          foreach($resultats as $vote) {
254
            if(!isset($votes[$vote['id_commentaire']])) {
255
              $votes[$vote['id_commentaire']] = 0;
1593 samuel 256
            }
257
            $valeur = ($vote['valeur'] == 1) ? 1 : -1;
1640 samuel 258
            $votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
1593 samuel 259
          }
1640 samuel 260
 
261
          foreach($resultats as $vote) {
262
            $idobs = $vote['ce_observation'];
1593 samuel 263
 
1640 samuel 264
            if(!isset($obs[$idobs]['determinations'])) {
265
              $obs[$idobs]['determinations'] = array();
1593 samuel 266
            }
267
 
1640 samuel 268
            $obs[$idobs]['determinations'][$vote['id_commentaire']] =
269
              array('nom_sel' => $vote['nom_sel'],
1657 samuel 270
                    'nom_ret' => $vote['nom_ret'],
1640 samuel 271
                    'score' => $votes[$vote['id_commentaire']],
272
                    'nn' => $vote['nom_sel_nn']
273
                    );
1593 samuel 274
          }
275
 
276
 
1640 samuel 277
          return $obs;
1593 samuel 278
	}
279
 
1640 samuel 280
 
1593 samuel 281
	/*-------------------------------------------------------------------------------
282
								FORMATER ET METTRE EN FORME
283
	--------------------------------------------------------------------------------*/
284
	/**
285
	*  Formater les mots clés du cel en n'affichant que ceux faisant partie
286
	*  d'une liste définie dans le fichier de configuration
287
	*  @param $chaineMotCleCel la chaine de mots clés du cel
288
	*  @return string la chaine filtrée
289
	* */
290
	private function formaterMotsClesCel($chaineMotCleCel) {
1652 samuel 291
 
292
 
293
          $mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new";
294
 
295
 
296
          $result = array_intersect(
297
                                 explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
298
                                 explode(',', $chaineMotCleCel)); // $tabMotsClesCel
299
 
300
          return explode(',', implode(',', $result));
1593 samuel 301
	}
302
 
303
}