Subversion Repositories eFlore/Applications.del

Rev

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