Subversion Repositories eFlore/Applications.del

Rev

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