Subversion Repositories eFlore/Applications.del

Rev

Rev 2126 | Rev 2171 | 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();
29
	private $date_defaut = '1900-01-01';
2123 mathias 30
	private $ordre_defaut = 'asc';
1881 jpm 31
	private $idsObsImg = array();
32
	private $infosObsImg = array();
1793 jpm 33
 
1881 jpm 34
 
1593 samuel 35
	public function __construct(Conteneur $conteneur = null) {
1794 jpm 36
		/* restore_exception_handler(); */
37
		/* restore_error_handler(); */
38
		/* ini_set("display_errors", "1"); */
1793 jpm 39
 
1593 samuel 40
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
41
		$this->navigation = $conteneur->getNavigation();
1793 jpm 42
		$this->bdd = $this->conteneur->getBdd();
1593 samuel 43
	}
1793 jpm 44
 
1593 samuel 45
	/**
46
	 * Méthode principale de la classe.
1793 jpm 47
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
1593 samuel 48
	 * pour l'afficher.
49
	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
50
	 * @param array $parametres les paramètres situés après le ? dans l'url
51
	 * */
52
	public function consulter($ressources, $parametres) {
53
		// initialiserRessourcesEtParametres()
54
		$this->ressources = $ressources;
55
		$this->parametres = $parametres;
1793 jpm 56
 
1794 jpm 57
		if (!isset($parametres['date'])) {
1593 samuel 58
			$this->parametres['date'] = $this->date_defaut;
59
		}
1793 jpm 60
 
2124 mathias 61
		if (! isset($parametres['ordre'])) {
62
			$this->parametres['ordre'] = $this->ordre_defaut;
63
		} else {
64
 			$parametres['ordre'] = strtolower($parametres['ordre']);
65
 			if (! in_array($parametres['ordre'], array('asc', 'desc'))) {
66
 				$this->parametres['ordre'] = $this->ordre_defaut;
2123 mathias 67
			}
2122 mathias 68
		}
69
 
1593 samuel 70
		// Lancement du service
1881 jpm 71
		$this->idsObsImg = $this->getIdsObsImg();
72
		$infos = array();
1593 samuel 73
		$total = 0;
1881 jpm 74
		if ($this->idsObsImg) {
75
			$total = $this->getTotal();
1593 samuel 76
 
1881 jpm 77
			$this->infosObsImg = $this->recupererInfos();
78
			$infos = $this->formaterInfos();
79
			$infos = $this->chargerPropositionPlusProbable($infos);
80
			$infos = $this->orderArray($infos);
1593 samuel 81
		}
82
		$this->navigation->setTotal($total);
1793 jpm 83
 
1593 samuel 84
		// Mettre en forme le résultat et l'envoyer pour affichage
85
		$resultat = new ResultatService();
1881 jpm 86
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
1593 samuel 87
		return $resultat;
88
	}
1793 jpm 89
 
1593 samuel 90
	/*-------------------------------------------------------------------------------
1793 jpm 91
								CHARGEMENT DES IMAGES
1593 samuel 92
	--------------------------------------------------------------------------------*/
1794 jpm 93
 
1881 jpm 94
	private function getIdsObsImg() {
95
		$date_debut = "'{$this->parametres['date']}'";
1794 jpm 96
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
2147 killian 97
		$limite = $limite ? $limite : 100; // 0 => 10
1794 jpm 98
		$depart = intval(@$this->parametres['navigation.depart']);
2122 mathias 99
		$ordre = $this->parametres['ordre'];
1593 samuel 100
 
1881 jpm 101
		$requete =
102
			'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
103
			'GROUP_CONCAT(iv.valeur) AS votes, '.
104
			'GROUP_CONCAT(DISTINCT tag) AS tags, '.
2147 killian 105
			'modif_date '.
1793 jpm 106
 
1881 jpm 107
			'FROM del_plantnet AS p '.
2147 killian 108
			'	JOIN del_observation_modif_date '.
109
			'		ON (p.id_observation = del_observation_modif_date.id_observation '.
110
			'		AND modif_date >= '.$date_debut.') '.
1881 jpm 111
			'	LEFT JOIN del_image_vote AS iv '.
112
			'		ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
113
			'	LEFT JOIN del_image_tag AS it '.
114
			'		ON (id_image = it.ce_image AND it.actif = 1) '.
115
			'	LEFT JOIN del_commentaire AS c '.
2147 killian 116
			'		ON (p.id_observation = c.ce_observation) '.
1881 jpm 117
			'	LEFT JOIN del_commentaire_vote AS cv '.
118
			'		ON (c.id_commentaire = cv.ce_proposition) '.
2147 killian 119
			'GROUP BY id_image, p.id_observation '.
2122 mathias 120
			'ORDER BY modif_date ' . $ordre . ' '.
1881 jpm 121
			'LIMIT '.$depart.', '.$limite.
122
			' -- '.__FILE__.':'.__LINE__;
2147 killian 123
 
1881 jpm 124
		return $this->bdd->recupererTous($requete);
1794 jpm 125
	}
1593 samuel 126
 
1881 jpm 127
	private function getTotal() {
128
		$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
129
		$total = (int) $compte['nbre'];
130
		return $total;
131
	}
132
 
1794 jpm 133
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
1881 jpm 134
	private function recupererInfos() {
1794 jpm 135
		// recuperer les ids
1881 jpm 136
		$idsImg = array();
137
		foreach ($this->idsObsImg as $ids) {
138
			$id = $ids['id_image'];
139
			$idsImg[] = $id;
1794 jpm 140
		}
1881 jpm 141
		$idsImgConcat = implode(',', $idsImg);
1643 samuel 142
 
1881 jpm 143
		$requete = 'SELECT '.
144
			'id_observation, id_image, '.
145
			'nom_sel, '.
2126 mathias 146
			'nom_referentiel, nom_ret, famille, '.
1881 jpm 147
			'zone_geo, latitude, longitude, '.
148
			'date_observation, date_creation, date_transmission, '.
149
			'mots_cles_texte, '.
150
			'ce_utilisateur, prenom_utilisateur, nom_utilisateur, courriel_utilisateur, '.
151
			'i_mots_cles_texte AS mots_cles_texte_image, nom_original AS nom_image '.
152
			'FROM del_plantnet AS p '.
153
			"WHERE id_image IN ($idsImgConcat) ".
154
			' -- '.__FILE__.':'.__LINE__;
1794 jpm 155
		// recuperer les donnees
1881 jpm 156
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 157
 
1794 jpm 158
		// regroupe les données par id_image
159
		$img_data = array();
1881 jpm 160
		foreach ($resultats as $infos) {
161
			$idImg = $infos['id_image'];
162
			$img_data[$idImg] = $infos;
1794 jpm 163
		}
164
		return $img_data;
1593 samuel 165
	}
1681 samuel 166
 
1593 samuel 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
1794 jpm 170
	*/
1881 jpm 171
	private function formaterInfos() {
1794 jpm 172
		// regroupe les observations
173
		$obs = array();
1922 jpm 174
		$imgCelTpl = $this->conteneur->getParametre('cel_img_url_tpl');
1881 jpm 175
		foreach ($this->idsObsImg as $ids) {
176
			$idobs = $ids['id_observation'];
177
			$idimg = $ids['id_image'];
1593 samuel 178
 
1881 jpm 179
			$imgdata = $this->infosObsImg[$idimg];
1593 samuel 180
 
1794 jpm 181
			if (!isset($obs[$idobs])) {
182
				$obs[$idobs] = array();
183
			}
1793 jpm 184
 
1794 jpm 185
			$obs[$idobs]['id_observation'] = $idobs;
186
			$obs[$idobs]['auteur_id'] = $imgdata['ce_utilisateur'];
187
			$obs[$idobs]['auteur_prenom'] = $imgdata['prenom_utilisateur'];
188
			$obs[$idobs]['auteur_nom'] = $imgdata['nom_utilisateur'];
189
			$obs[$idobs]['auteur_courriel'] = $imgdata['courriel_utilisateur'];
1593 samuel 190
 
1794 jpm 191
			$obs[$idobs]['mots_cles_obs_cel'] = $this->formaterMotsClesCel($imgdata['mots_cles_texte']);
1593 samuel 192
 
1794 jpm 193
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
194
			$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
195
			$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
1881 jpm 196
			$obs[$idobs]['date_changement'] = $ids['modif_date'];
1793 jpm 197
 
1794 jpm 198
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
199
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
200
			$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
2126 mathias 201
			//$obs[$idobs]['nn'] = $imgdata['nom_ret_nn'];
202
			//$obs[$idobs]['nt'] = $imgdata['nt'];
1794 jpm 203
			$obs[$idobs]['famille'] = $imgdata['famille'];
1640 samuel 204
 
1794 jpm 205
			$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
206
			$obs[$idobs]['latitude'] = floatval($imgdata['latitude']);
207
			$obs[$idobs]['longitude'] = floatval($imgdata['longitude']);
1640 samuel 208
 
1794 jpm 209
			if (!isset($obs[$idobs]['images'])) {
210
				$obs[$idobs]['images'] = array();
211
			}
1640 samuel 212
 
1794 jpm 213
			$img_obj = array(
1881 jpm 214
				'id_image' => $idimg,
1794 jpm 215
				'nom_image' => $imgdata['nom_image'],
1922 jpm 216
				'url' => sprintf($imgCelTpl, $idimg, 'O'),
1881 jpm 217
				'votes' => array_map('intval', explode(',', $ids['votes'])),
218
				'tags' => explode(',', $ids['tags']),
1794 jpm 219
				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
220
				);
221
			// push
222
			$obs[$idobs]['images'][] = $img_obj;
223
		}
224
		return $obs;
1593 samuel 225
	}
1793 jpm 226
 
1593 samuel 227
	/**
1794 jpm 228
	 * Charger les votes pour chaque image
229
	 */
1640 samuel 230
	private function chargerPropositionPlusProbable(&$obs) {
1794 jpm 231
		$obsIds = array_keys($obs);
1881 jpm 232
		$idsObsConcat = implode(',', $obsIds);
1593 samuel 233
 
2126 mathias 234
		$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, cv.ce_utilisateur '.
1881 jpm 235
			'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
236
			"WHERE ce_observation IN ($idsObsConcat) ".
1794 jpm 237
			'AND nom_sel IS NOT NULL '.
1881 jpm 238
			'AND c.id_commentaire = cv.ce_proposition '.
239
			' -- '.__FILE__.':'.__LINE__;
240
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 241
 
1794 jpm 242
		// calcul des votes
243
		// un vote identifié a un facteur de 3
244
		// additionne tous les vote par ce_proposition
1881 jpm 245
		$votes = array(); // map ce_proposition -> score
1794 jpm 246
		foreach ($resultats as $vote) {
247
			if (!isset($votes[$vote['id_commentaire']])) {
248
				$votes[$vote['id_commentaire']] = 0;
249
			}
250
			$valeur = ($vote['valeur'] == 1) ? 1 : -1;
251
			$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
252
		}
1793 jpm 253
 
1794 jpm 254
		foreach ($resultats as $vote) {
255
			$idobs = $vote['ce_observation'];
1793 jpm 256
 
1794 jpm 257
			if (!isset($obs[$idobs]['determinations'])) {
258
				$obs[$idobs]['determinations'] = array();
259
			}
1793 jpm 260
 
1794 jpm 261
			$obs[$idobs]['determinations'][$vote['id_commentaire']] =
262
			array('nom_sel' => $vote['nom_sel'],
263
				'nom_ret' => $vote['nom_ret'],
264
				'score' => $votes[$vote['id_commentaire']],
265
				'nn' => $vote['nom_sel_nn']);
266
		}
267
		return $obs;
1593 samuel 268
	}
1793 jpm 269
 
1881 jpm 270
	private function orderArray(&$obs) {
271
		$ret = array();
272
		foreach ($obs as $o) {
273
			$ret[] = $o;
274
		}
275
 
2124 mathias 276
		function cmpDesc($a, $b) {
1881 jpm 277
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
278
		}
2124 mathias 279
		function cmpAsc($a, $b) {
280
			return cmpDesc($b, $a);
2123 mathias 281
		}
1881 jpm 282
 
2123 mathias 283
		if ($this->parametres['ordre'] == 'desc') {
284
			usort($ret, 'cmpDesc');
285
		} else {
286
			usort($ret, 'cmpAsc');
287
		}
1881 jpm 288
		return $ret;
289
	}
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) {
1989 mathias 298
		$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new,plantnet-mobile";
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
 
1990 samuel 311
}