Subversion Repositories eFlore/Applications.del

Rev

Rev 2174 | 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>
1990 samuel 16
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
1794 jpm 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();
2123 mathias 29
	private $ordre_defaut = 'asc';
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
 
2202 killian 56
		if (!isset($parametres['date.debut'])) {
57
			$this->parametres['date.debut'] = '1900-01-01';
1593 samuel 58
		}
1793 jpm 59
 
2202 killian 60
		if (!isset($parametres['date.fin'])) {
61
			$this->parametres['date.fin'] = date('Y-m-d');
62
		}
63
 
2124 mathias 64
		if (! isset($parametres['ordre'])) {
65
			$this->parametres['ordre'] = $this->ordre_defaut;
66
		} else {
67
 			$parametres['ordre'] = strtolower($parametres['ordre']);
68
 			if (! in_array($parametres['ordre'], array('asc', 'desc'))) {
69
 				$this->parametres['ordre'] = $this->ordre_defaut;
2123 mathias 70
			}
2122 mathias 71
		}
72
 
1593 samuel 73
		// Lancement du service
1881 jpm 74
		$this->idsObsImg = $this->getIdsObsImg();
75
		$infos = array();
1593 samuel 76
		$total = 0;
1881 jpm 77
		if ($this->idsObsImg) {
78
			$total = $this->getTotal();
1593 samuel 79
 
1881 jpm 80
			$this->infosObsImg = $this->recupererInfos();
81
			$infos = $this->formaterInfos();
82
			$infos = $this->chargerPropositionPlusProbable($infos);
83
			$infos = $this->orderArray($infos);
1593 samuel 84
		}
85
		$this->navigation->setTotal($total);
1793 jpm 86
 
1593 samuel 87
		// Mettre en forme le résultat et l'envoyer pour affichage
88
		$resultat = new ResultatService();
1881 jpm 89
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
1593 samuel 90
		return $resultat;
91
	}
1793 jpm 92
 
1593 samuel 93
	/*-------------------------------------------------------------------------------
1793 jpm 94
								CHARGEMENT DES IMAGES
1593 samuel 95
	--------------------------------------------------------------------------------*/
1794 jpm 96
 
1881 jpm 97
	private function getIdsObsImg() {
2202 killian 98
		$date_debut = "'{$this->parametres['date.debut']}'";
99
		$date_fin = "'{$this->parametres['date.fin']}'";
1794 jpm 100
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
2147 killian 101
		$limite = $limite ? $limite : 100; // 0 => 10
1794 jpm 102
		$depart = intval(@$this->parametres['navigation.depart']);
2122 mathias 103
		$ordre = $this->parametres['ordre'];
1593 samuel 104
 
1881 jpm 105
		$requete =
106
			'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
107
			'GROUP_CONCAT(iv.valeur) AS votes, '.
108
			'GROUP_CONCAT(DISTINCT tag) AS tags, '.
2147 killian 109
			'modif_date '.
1793 jpm 110
 
1881 jpm 111
			'FROM del_plantnet AS p '.
2147 killian 112
			'	JOIN del_observation_modif_date '.
113
			'		ON (p.id_observation = del_observation_modif_date.id_observation '.
2202 killian 114
			'		AND modif_date >= '.date('U', strtotime($date_debut)).') '.
115
			'		AND modif_date <= '.date('U', strtotime($date_fin)).') '.
1881 jpm 116
			'	LEFT JOIN del_image_vote AS iv '.
117
			'		ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
118
			'	LEFT JOIN del_image_tag AS it '.
119
			'		ON (id_image = it.ce_image AND it.actif = 1) '.
120
			'	LEFT JOIN del_commentaire AS c '.
2147 killian 121
			'		ON (p.id_observation = c.ce_observation) '.
1881 jpm 122
			'	LEFT JOIN del_commentaire_vote AS cv '.
123
			'		ON (c.id_commentaire = cv.ce_proposition) '.
2147 killian 124
			'GROUP BY id_image, p.id_observation '.
2122 mathias 125
			'ORDER BY modif_date ' . $ordre . ' '.
1881 jpm 126
			'LIMIT '.$depart.', '.$limite.
127
			' -- '.__FILE__.':'.__LINE__;
2147 killian 128
 
1881 jpm 129
		return $this->bdd->recupererTous($requete);
1794 jpm 130
	}
1593 samuel 131
 
1881 jpm 132
	private function getTotal() {
133
		$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
134
		$total = (int) $compte['nbre'];
135
		return $total;
136
	}
137
 
1794 jpm 138
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
1881 jpm 139
	private function recupererInfos() {
1794 jpm 140
		// recuperer les ids
1881 jpm 141
		$idsImg = array();
142
		foreach ($this->idsObsImg as $ids) {
143
			$id = $ids['id_image'];
144
			$idsImg[] = $id;
1794 jpm 145
		}
1881 jpm 146
		$idsImgConcat = implode(',', $idsImg);
1643 samuel 147
 
1881 jpm 148
		$requete = 'SELECT '.
2174 killian 149
			'p.id_observation, p.id_image, '.
150
			'cp.id_plantnet, ' .
151
			'p.nom_sel, '.
152
			'p.nom_referentiel, p.nom_ret, p.famille, '.
153
			'p.zone_geo, p.latitude, p.longitude, '.
154
			'p.date_observation, p.date_creation, p.date_transmission, '.
155
			'p.mots_cles_texte, '.
156
			'p.ce_utilisateur, p.prenom_utilisateur, p.nom_utilisateur, p.courriel_utilisateur, '.
157
			'p.i_mots_cles_texte AS mots_cles_texte_image, p.nom_original AS nom_image '.
1881 jpm 158
			'FROM del_plantnet AS p '.
2174 killian 159
			'LEFT JOIN tb_cel.cel_plantnet AS cp ON p.id_observation = cp.id_observation '.
1881 jpm 160
			"WHERE id_image IN ($idsImgConcat) ".
161
			' -- '.__FILE__.':'.__LINE__;
1794 jpm 162
		// recuperer les donnees
1881 jpm 163
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 164
 
1794 jpm 165
		// regroupe les données par id_image
166
		$img_data = array();
1881 jpm 167
		foreach ($resultats as $infos) {
168
			$idImg = $infos['id_image'];
169
			$img_data[$idImg] = $infos;
1794 jpm 170
		}
171
		return $img_data;
1593 samuel 172
	}
1681 samuel 173
 
1593 samuel 174
	/**
175
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
176
	* @param $liaisons les liaisons de la table del_obs_images
1794 jpm 177
	*/
1881 jpm 178
	private function formaterInfos() {
1794 jpm 179
		// regroupe les observations
180
		$obs = array();
1922 jpm 181
		$imgCelTpl = $this->conteneur->getParametre('cel_img_url_tpl');
1881 jpm 182
		foreach ($this->idsObsImg as $ids) {
183
			$idobs = $ids['id_observation'];
184
			$idimg = $ids['id_image'];
1593 samuel 185
 
1881 jpm 186
			$imgdata = $this->infosObsImg[$idimg];
1593 samuel 187
 
1794 jpm 188
			if (!isset($obs[$idobs])) {
189
				$obs[$idobs] = array();
190
			}
1793 jpm 191
 
1794 jpm 192
			$obs[$idobs]['id_observation'] = $idobs;
2174 killian 193
			$obs[$idobs]['id_plantnet'] = $imgdata['id_plantnet'];
1794 jpm 194
			$obs[$idobs]['auteur_id'] = $imgdata['ce_utilisateur'];
195
			$obs[$idobs]['auteur_prenom'] = $imgdata['prenom_utilisateur'];
196
			$obs[$idobs]['auteur_nom'] = $imgdata['nom_utilisateur'];
197
			$obs[$idobs]['auteur_courriel'] = $imgdata['courriel_utilisateur'];
1593 samuel 198
 
1794 jpm 199
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
1593 samuel 200
 
1794 jpm 201
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
202
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
203
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
1881 jpm 204
			$obs[$idobs]['date_changement'] = $ids['modif_date'];
1793 jpm 205
 
1794 jpm 206
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
207
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
208
			$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
2126 mathias 209
			//$obs[$idobs]['nn'] = $imgdata['nom_ret_nn'];
210
			//$obs[$idobs]['nt'] = $imgdata['nt'];
1794 jpm 211
			$obs[$idobs]['famille'] = $imgdata['famille'];
1640 samuel 212
 
1794 jpm 213
			$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
214
			$obs[$idobs]['latitude'] = floatval($imgdata['latitude']);
215
			$obs[$idobs]['longitude'] = floatval($imgdata['longitude']);
1640 samuel 216
 
1794 jpm 217
			if (!isset($obs[$idobs]['images'])) {
218
				$obs[$idobs]['images'] = array();
219
			}
1640 samuel 220
 
1794 jpm 221
			$img_obj = array(
1881 jpm 222
				'id_image' => $idimg,
1794 jpm 223
				'nom_image' => $imgdata['nom_image'],
1922 jpm 224
				'url' => sprintf($imgCelTpl, $idimg, 'O'),
1881 jpm 225
				'votes' => array_map('intval', explode(',', $ids['votes'])),
226
				'tags' => explode(',', $ids['tags']),
1794 jpm 227
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
228
				);
229
			// push
230
			$obs[$idobs]['images'][] = $img_obj;
231
		}
232
		return $obs;
1593 samuel 233
	}
1793 jpm 234
 
1593 samuel 235
	/**
1794 jpm 236
	 * Charger les votes pour chaque image
237
	 */
1640 samuel 238
	private function chargerPropositionPlusProbable(&$obs) {
1794 jpm 239
		$obsIds = array_keys($obs);
1881 jpm 240
		$idsObsConcat = implode(',', $obsIds);
1593 samuel 241
 
2126 mathias 242
		$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, cv.ce_utilisateur '.
1881 jpm 243
			'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
244
			"WHERE ce_observation IN ($idsObsConcat) ".
1794 jpm 245
			'AND nom_sel IS NOT NULL '.
1881 jpm 246
			'AND c.id_commentaire = cv.ce_proposition '.
247
			' -- '.__FILE__.':'.__LINE__;
248
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 249
 
1794 jpm 250
		// calcul des votes
251
		// un vote identifié a un facteur de 3
252
		// additionne tous les vote par ce_proposition
1881 jpm 253
		$votes = array(); // map ce_proposition -> score
1794 jpm 254
		foreach ($resultats as $vote) {
255
			if (!isset($votes[$vote['id_commentaire']])) {
256
				$votes[$vote['id_commentaire']] = 0;
257
			}
258
			$valeur = ($vote['valeur'] == 1) ? 1 : -1;
259
			$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
260
		}
1793 jpm 261
 
1794 jpm 262
		foreach ($resultats as $vote) {
263
			$idobs = $vote['ce_observation'];
1793 jpm 264
 
1794 jpm 265
			if (!isset($obs[$idobs]['determinations'])) {
266
				$obs[$idobs]['determinations'] = array();
267
			}
1793 jpm 268
 
1794 jpm 269
			$obs[$idobs]['determinations'][$vote['id_commentaire']] =
270
			array('nom_sel' => $vote['nom_sel'],
271
				'nom_ret' => $vote['nom_ret'],
272
				'score' => $votes[$vote['id_commentaire']],
273
				'nn' => $vote['nom_sel_nn']);
274
		}
275
		return $obs;
1593 samuel 276
	}
1793 jpm 277
 
1881 jpm 278
	private function orderArray(&$obs) {
279
		$ret = array();
280
		foreach ($obs as $o) {
281
			$ret[] = $o;
282
		}
283
 
2124 mathias 284
		function cmpDesc($a, $b) {
1881 jpm 285
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
286
		}
2124 mathias 287
		function cmpAsc($a, $b) {
288
			return cmpDesc($b, $a);
2123 mathias 289
		}
1881 jpm 290
 
2123 mathias 291
		if ($this->parametres['ordre'] == 'desc') {
292
			usort($ret, 'cmpDesc');
293
		} else {
294
			usort($ret, 'cmpAsc');
295
		}
1881 jpm 296
		return $ret;
297
	}
298
 
1593 samuel 299
	/**
1794 jpm 300
	 * Formater les mots clés du cel en n'affichant que ceux faisant partie
301
	 * d'une liste définie dans le fichier de configuration
302
	 * @param $chaineMotCleCel la chaine de mots clés du cel
303
	 * @return string la chaine filtrée
304
	 */
1593 samuel 305
	private function formaterMotsClesCel($chaineMotCleCel) {
1989 mathias 306
		$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new,plantnet-mobile";
1652 samuel 307
 
1794 jpm 308
		$result = array_intersect(
309
			explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
310
			explode(',', $chaineMotCleCel)); // $tabMotsClesCel
1652 samuel 311
 
1794 jpm 312
		if (count($result) === 0) {
313
		  return array();
314
		}
315
		$ret = explode(',', implode(',', $result));
316
		return $ret;
1593 samuel 317
	}
1793 jpm 318
 
1990 samuel 319
}