Subversion Repositories eFlore/Applications.del

Rev

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