Subversion Repositories eFlore/Applications.del

Rev

Rev 1793 | Rev 1881 | 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
/**
1793 jpm 3
 * Le web service plantnet récupère toutes les image de la table v_del_image
1663 samuel 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
1593 samuel 7
 *
1794 jpm 8
 * @category DEL
9
 * @package Services
10
 * @subpackage Plantnet
11
 * @version 0.1
12
 * @author Mathias CHOUET <mathias@tela-botanica.org>
13
 * @author Samuel DUFOUR-KOWALSKI <samuel.dufour@cirad.fr>
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>
1593 samuel 19
 */
20
 
21
class Changements {
1793 jpm 22
 
1593 samuel 23
	private $indexImagesIds = array();
24
	private $conteneur;
25
	private $navigation;
26
	private $bdd;
27
	private $parametres = array();
28
	private $ressources = array();
29
	private $date_defaut = '1900-01-01';
1793 jpm 30
 
1593 samuel 31
	public function __construct(Conteneur $conteneur = null) {
1794 jpm 32
		/* restore_exception_handler(); */
33
		/* restore_error_handler(); */
34
		/* ini_set("display_errors", "1"); */
1793 jpm 35
 
1593 samuel 36
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
37
		$this->conteneur->chargerConfiguration('config_plantnet.ini');
38
		$this->navigation = $conteneur->getNavigation();
1793 jpm 39
		$this->bdd = $this->conteneur->getBdd();
1593 samuel 40
	}
1793 jpm 41
 
1593 samuel 42
	/**
43
	 * Méthode principale de la classe.
1793 jpm 44
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
1593 samuel 45
	 * pour l'afficher.
46
	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
47
	 * @param array $parametres les paramètres situés après le ? dans l'url
48
	 * */
49
	public function consulter($ressources, $parametres) {
50
		// initialiserRessourcesEtParametres()
51
		$this->ressources = $ressources;
52
		$this->parametres = $parametres;
1793 jpm 53
 
1794 jpm 54
		if (!isset($parametres['date'])) {
1593 samuel 55
			$this->parametres['date'] = $this->date_defaut;
56
		}
1793 jpm 57
 
1593 samuel 58
		// Lancement du service
59
		$liaisons = $this->chargerLiaisons();
1794 jpm 60
		$images = array();
1593 samuel 61
		$total = 0;
62
 
1794 jpm 63
		if ($liaisons) {
64
			$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
65
			$total = (int) $compte['nbre'];
1793 jpm 66
 
1794 jpm 67
			$imgdata = $this->recupererDonneeObs($liaisons);
68
			$obs = $this->regrouperObs($liaisons, $imgdata);
1681 samuel 69
 
1794 jpm 70
			$obs = $this->chargerPropositionPlusProbable($obs);
71
			$obs = $this->orderArray($obs);
1593 samuel 72
		}
1793 jpm 73
 
1593 samuel 74
		$this->navigation->setTotal($total);
1793 jpm 75
 
1593 samuel 76
		// Mettre en forme le résultat et l'envoyer pour affichage
77
		$resultat = new ResultatService();
1794 jpm 78
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $obs);
1793 jpm 79
 
1593 samuel 80
		return $resultat;
81
	}
1793 jpm 82
 
83
 
1794 jpm 84
	private function orderArray(&$obs) {
85
		$ret = array();
1643 samuel 86
 
1794 jpm 87
		foreach ($obs as $o) {
88
			$ret[] = $o;
89
		}
1643 samuel 90
 
1794 jpm 91
		function cmp($a, $b) {
92
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
93
		}
1643 samuel 94
 
1794 jpm 95
		usort($ret, 'cmp');
96
		return $ret;
97
	}
1793 jpm 98
 
1593 samuel 99
	/*-------------------------------------------------------------------------------
1793 jpm 100
								CHARGEMENT DES IMAGES
1593 samuel 101
	--------------------------------------------------------------------------------*/
1794 jpm 102
 
1593 samuel 103
	private function chargerLiaisons() {
1794 jpm 104
		$date_debut = $this->parametres['date'];
105
		$date_debut = '\'' . $date_debut . '\'';
106
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
107
		$limite = $limite ? $limite : 10; // 0 => 10
108
		$depart = intval(@$this->parametres['navigation.depart']);
1593 samuel 109
 
1794 jpm 110
		$requete_sql =
111
			'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
1593 samuel 112
 
1794 jpm 113
			'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
1642 samuel 114
 
1794 jpm 115
			'GREATEST('.
116
			'IFNULL(vdi.date_creation, \''.$this->date_defaut.'\'), '.
117
			'IFNULL(vdi.date_modification, \''.$this->date_defaut.'\'), '.
118
			'IFNULL(MAX(del_image_tag.date), \''.$this->date_defaut.'\'), '.
119
			'IFNULL(MAX(del_image_tag.date_modification), \''.$this->date_defaut.'\'), '.
120
			'IFNULL(MAX(del_image_vote.date), \''.$this->date_defaut.'\'), '.
121
			'IFNULL(MAX(del_commentaire.date), \''.$this->date_defaut.'\'), '.
122
			'IFNULL(MAX(del_commentaire_vote.date), \''.$this->date_defaut.'\')) as modif_date '.
1793 jpm 123
 
1794 jpm 124
			'from v_del_image as vdi '.
1593 samuel 125
 
1794 jpm 126
			'left join del_image_vote on del_image_vote.ce_image=id_image and del_image_vote.ce_protocole=3 '.
127
			'left join del_image_tag on del_image_tag.ce_image=id_image and del_image_tag.actif=1 '.
128
			'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
129
			'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
1593 samuel 130
 
1794 jpm 131
			'group by id_image, id_observation '.
1593 samuel 132
 
1794 jpm 133
			'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
134
			'       MAX(vdi.date_modification) >= '.$date_debut.' or '.
135
			'       MAX(del_image_tag.date) >= '.$date_debut.' or '.
136
			'       MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
137
			'       MAX(del_image_vote.date) >= '.$date_debut.' or '.
138
			'       MAX(del_commentaire.date) >= '.$date_debut.' or '.
139
			'       MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
140
			'order by modif_date DESC '.
141
			'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
1593 samuel 142
 
1794 jpm 143
		//echo $requete_sql; exit;
1593 samuel 144
 
1794 jpm 145
		return $this->bdd->recupererTous($requete_sql);
1593 samuel 146
 
1794 jpm 147
		// GROUP BY (très couteux) car multiples observations associées à une image
148
		// charlie est ici :-)
149
		// eg: 16150,16185,16245,16246,118995,129989
150
	}
1593 samuel 151
 
1794 jpm 152
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
153
	private function recupererDonneeObs(&$liaisons) {
154
		// recuperer les ids
155
		$ids = array();
156
		foreach ($liaisons as $img) {
157
			$id = $img['id_image'];
158
			$ids[] = $id;
159
		}
1643 samuel 160
 
1794 jpm 161
		// recuperer les donnees
162
		$resultats = $this->bdd->recupererTous(sprintf(
163
			'SELECT  '.
164
			'vdi.id_observation, vdi.id_image, '.
165
			'vdi.nom_sel, '.
166
			'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
167
			'vdi.zone_geo, vdi.latitude, vdi.longitude, '.
168
			'vdi.date_observation, vdi.date_creation, vdi.date_transmission, '.
169
			'vdi.mots_cles_texte as mots_cles_texte, '.
170
			'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
1593 samuel 171
 
1794 jpm 172
			'vdi.ce_utilisateur as ce_utilisateur, '.
173
			'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image '.
1593 samuel 174
 
1794 jpm 175
			'FROM v_del_image as vdi '.
176
			'WHERE vdi.id_image IN (%s) '.
177
			'', implode(',', $ids)));
1793 jpm 178
 
1794 jpm 179
		// regroupe les données par id_image
180
		$img_data = array();
181
		foreach ($resultats as $img) {
182
			$id = $img['id_image'];
183
			$img_data[$id] = $img;
184
		}
185
		return $img_data;
1593 samuel 186
	}
1681 samuel 187
 
1593 samuel 188
	/**
189
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
190
	* @param $liaisons les liaisons de la table del_obs_images
1794 jpm 191
	*/
1681 samuel 192
	private function regrouperObs(&$liaisons, &$imgdatas) {
1794 jpm 193
		// regroupe les observations
194
		$obs = array();
195
		foreach ($liaisons as $img) {
196
			$idobs = $img['id_observation'];
197
			$idimg = $img['id_image'];
1593 samuel 198
 
1794 jpm 199
			$imgdata = $imgdatas[$idimg];
1593 samuel 200
 
1794 jpm 201
			if (!isset($obs[$idobs])) {
202
				$obs[$idobs] = array();
203
			}
1793 jpm 204
 
1794 jpm 205
			$obs[$idobs]['id_observation'] = $idobs;
206
			$obs[$idobs]['auteur_id'] = $imgdata['ce_utilisateur'];
207
			$obs[$idobs]['auteur_prenom'] = $imgdata['prenom_utilisateur'];
208
			$obs[$idobs]['auteur_nom'] = $imgdata['nom_utilisateur'];
209
			$obs[$idobs]['auteur_courriel'] = $imgdata['courriel_utilisateur'];
1593 samuel 210
 
1794 jpm 211
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
1593 samuel 212
 
1794 jpm 213
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
214
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
215
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
216
			$obs[$idobs]['date_changement'] = $img['modif_date'];
1793 jpm 217
 
1794 jpm 218
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
219
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
220
			$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
221
			$obs[$idobs]['nn'] = $imgdata['nom_ret_nn'];
222
			$obs[$idobs]['nt'] = $imgdata['nt'];
223
			$obs[$idobs]['famille'] = $imgdata['famille'];
1640 samuel 224
 
1794 jpm 225
			$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
226
			$obs[$idobs]['latitude'] = floatval($imgdata['latitude']);
227
			$obs[$idobs]['longitude'] = floatval($imgdata['longitude']);
1640 samuel 228
 
1794 jpm 229
			if (!isset($obs[$idobs]['images'])) {
230
				$obs[$idobs]['images'] = array();
231
			}
1640 samuel 232
 
1794 jpm 233
			$img_obj = array(
234
				'id_image' => $img['id_image'],
235
				'nom_image' => $imgdata['nom_image'],
236
				'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $img['id_image']),
237
				'votes' => array_map('intval', explode(',', $img['votes'])),
238
				'tags' => explode(',', $img['tags']),
239
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
240
				);
241
			// push
242
			$obs[$idobs]['images'][] = $img_obj;
243
		}
244
		return $obs;
1593 samuel 245
	}
1793 jpm 246
 
1593 samuel 247
	/**
1794 jpm 248
	 * Charger les votes pour chaque image
249
	 */
1640 samuel 250
	private function chargerPropositionPlusProbable(&$obs) {
1794 jpm 251
		$obsIds = array_keys($obs);
1593 samuel 252
 
1794 jpm 253
		$resultats = $this->bdd->recupererTous(sprintf(
254
			'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, del_commentaire_vote.ce_utilisateur '.
255
			'FROM del_commentaire_vote, del_commentaire '.
256
			'WHERE ce_observation IN (%s) '.
257
			'AND nom_sel IS NOT NULL '.
258
			'AND del_commentaire.id_commentaire=del_commentaire_vote.ce_proposition '.
259
			'', implode(',', $obsIds)));
1593 samuel 260
 
1794 jpm 261
		$votes = array(); // map ce_proposition -> score
1593 samuel 262
 
1794 jpm 263
		// calcul des votes
264
		// un vote identifié a un facteur de 3
265
		// additionne tous les vote par ce_proposition
266
		foreach ($resultats as $vote) {
267
			if (!isset($votes[$vote['id_commentaire']])) {
268
				$votes[$vote['id_commentaire']] = 0;
269
			}
270
			$valeur = ($vote['valeur'] == 1) ? 1 : -1;
271
			$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
272
		}
1793 jpm 273
 
1794 jpm 274
		foreach ($resultats as $vote) {
275
			$idobs = $vote['ce_observation'];
1793 jpm 276
 
1794 jpm 277
			if (!isset($obs[$idobs]['determinations'])) {
278
				$obs[$idobs]['determinations'] = array();
279
			}
1793 jpm 280
 
1794 jpm 281
			$obs[$idobs]['determinations'][$vote['id_commentaire']] =
282
			array('nom_sel' => $vote['nom_sel'],
283
				'nom_ret' => $vote['nom_ret'],
284
				'score' => $votes[$vote['id_commentaire']],
285
				'nn' => $vote['nom_sel_nn']);
286
		}
287
		return $obs;
1593 samuel 288
	}
1793 jpm 289
 
1593 samuel 290
	/*-------------------------------------------------------------------------------
291
								FORMATER ET METTRE EN FORME
292
	--------------------------------------------------------------------------------*/
1794 jpm 293
 
1593 samuel 294
	/**
1794 jpm 295
	 * Formater les mots clés du cel en n'affichant que ceux faisant partie
296
	 * d'une liste définie dans le fichier de configuration
297
	 * @param $chaineMotCleCel la chaine de mots clés du cel
298
	 * @return string la chaine filtrée
299
	 */
1593 samuel 300
	private function formaterMotsClesCel($chaineMotCleCel) {
1794 jpm 301
		$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new";
1652 samuel 302
 
1794 jpm 303
		$result = array_intersect(
304
			explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
305
			explode(',', $chaineMotCleCel)); // $tabMotsClesCel
1652 samuel 306
 
1794 jpm 307
		if (count($result) === 0) {
308
		  return array();
309
		}
310
		$ret = explode(',', implode(',', $result));
311
		return $ret;
1593 samuel 312
	}
1793 jpm 313
 
1794 jpm 314
}