Subversion Repositories eFlore/Applications.del

Rev

Rev 2213 | Rev 2223 | 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
 *
2222 julien 8
 * Paramètres: date.debut et date.fin (unixtime en secondes), navigation.limite (10 par défaut), navigation.depart, ordre (DESC par défaut)
9
 * Ne pas Oublier l'api key dans le header $_SERVER['HTTP_API_KEY']
10
 *
1794 jpm 11
 * @category DEL
12
 * @package Services
13
 * @subpackage Plantnet
14
 * @version 0.1
15
 * @author Mathias CHOUET <mathias@tela-botanica.org>
16
 * @author Samuel DUFOUR-KOWALSKI <samuel.dufour@cirad.fr>
17
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @author Aurelien PERONNET <aurelien@tela-botanica.org>
1990 samuel 19
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
1794 jpm 20
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
21
 * @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
1593 samuel 22
 */
23
 
24
class Changements {
1793 jpm 25
 
1593 samuel 26
	private $conteneur;
27
	private $navigation;
28
	private $bdd;
1881 jpm 29
 
1593 samuel 30
	private $parametres = array();
31
	private $ressources = array();
2222 julien 32
	private $ordre_defaut = 'DESC';
1881 jpm 33
	private $idsObsImg = array();
34
	private $infosObsImg = array();
1793 jpm 35
 
1881 jpm 36
 
1593 samuel 37
	public function __construct(Conteneur $conteneur = null) {
1794 jpm 38
		/* restore_exception_handler(); */
39
		/* restore_error_handler(); */
40
		/* ini_set("display_errors", "1"); */
1793 jpm 41
 
1593 samuel 42
		$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
43
		$this->navigation = $conteneur->getNavigation();
1793 jpm 44
		$this->bdd = $this->conteneur->getBdd();
1593 samuel 45
	}
1793 jpm 46
 
1593 samuel 47
	/**
48
	 * Méthode principale de la classe.
1793 jpm 49
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
1593 samuel 50
	 * pour l'afficher.
51
	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
52
	 * @param array $parametres les paramètres situés après le ? dans l'url
53
	 * */
54
	public function consulter($ressources, $parametres) {
55
		// initialiserRessourcesEtParametres()
56
		$this->ressources = $ressources;
57
		$this->parametres = $parametres;
1793 jpm 58
 
2213 arthur 59
		if (!isset($parametres['date.debut'])) {
2222 julien 60
			$this->parametres['date.debut'] = '0';
1593 samuel 61
		}
1793 jpm 62
 
2213 arthur 63
		if (!isset($parametres['date.fin'])) {
2222 julien 64
			$this->parametres['date.fin'] = time();
2213 arthur 65
		}
66
 
2124 mathias 67
		if (! isset($parametres['ordre'])) {
68
			$this->parametres['ordre'] = $this->ordre_defaut;
69
		} else {
70
 			$parametres['ordre'] = strtolower($parametres['ordre']);
71
 			if (! in_array($parametres['ordre'], array('asc', 'desc'))) {
72
 				$this->parametres['ordre'] = $this->ordre_defaut;
2123 mathias 73
			}
2122 mathias 74
		}
75
 
1593 samuel 76
		// Lancement du service
1881 jpm 77
		$this->idsObsImg = $this->getIdsObsImg();
78
		$infos = array();
1593 samuel 79
		$total = 0;
1881 jpm 80
		if ($this->idsObsImg) {
81
			$total = $this->getTotal();
1593 samuel 82
 
1881 jpm 83
			$this->infosObsImg = $this->recupererInfos();
84
			$infos = $this->formaterInfos();
85
			$infos = $this->chargerPropositionPlusProbable($infos);
86
			$infos = $this->orderArray($infos);
1593 samuel 87
		}
88
		$this->navigation->setTotal($total);
1793 jpm 89
 
1593 samuel 90
		// Mettre en forme le résultat et l'envoyer pour affichage
91
		$resultat = new ResultatService();
1881 jpm 92
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
1593 samuel 93
		return $resultat;
94
	}
1793 jpm 95
 
1593 samuel 96
	/*-------------------------------------------------------------------------------
1793 jpm 97
								CHARGEMENT DES IMAGES
1593 samuel 98
	--------------------------------------------------------------------------------*/
1794 jpm 99
 
1881 jpm 100
	private function getIdsObsImg() {
2222 julien 101
        $date_debut = date('Y-m-d H:i:s', $this->parametres['date.debut']);
102
        $date_fin = date('Y-m-d H:i:s', $this->parametres['date.fin']);
1794 jpm 103
		$limite = @min(intval($this->parametres['navigation.limite']), 1000);
2222 julien 104
		$limite = $limite ?: 100; // 0 => 10
1794 jpm 105
		$depart = intval(@$this->parametres['navigation.depart']);
2122 mathias 106
		$ordre = $this->parametres['ordre'];
1593 samuel 107
 
2222 julien 108
        $date_debut = "'{$date_debut}'";
109
        $date_fin = "'{$date_fin}'";
110
 
1881 jpm 111
		$requete =
112
			'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
113
			'GROUP_CONCAT(iv.valeur) AS votes, '.
2222 julien 114
			'GROUP_CONCAT(DISTINCT it.tag) AS del_image_tags, '.
2147 killian 115
			'modif_date '.
1881 jpm 116
			'FROM del_plantnet AS p '.
2147 killian 117
			'	JOIN del_observation_modif_date '.
118
			'		ON (p.id_observation = del_observation_modif_date.id_observation '.
2222 julien 119
			'		AND modif_date >= '.$date_debut.' '.
120
			'		AND modif_date <= '.$date_fin.') '.
1881 jpm 121
			'	LEFT JOIN del_image_vote AS iv '.
122
			'		ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
123
			'	LEFT JOIN del_image_tag AS it '.
124
			'		ON (id_image = it.ce_image AND it.actif = 1) '.
125
			'	LEFT JOIN del_commentaire AS c '.
2147 killian 126
			'		ON (p.id_observation = c.ce_observation) '.
1881 jpm 127
			'	LEFT JOIN del_commentaire_vote AS cv '.
128
			'		ON (c.id_commentaire = cv.ce_proposition) '.
2147 killian 129
			'GROUP BY id_image, p.id_observation '.
2122 mathias 130
			'ORDER BY modif_date ' . $ordre . ' '.
1881 jpm 131
			'LIMIT '.$depart.', '.$limite.
132
			' -- '.__FILE__.':'.__LINE__;
2147 killian 133
 
1881 jpm 134
		return $this->bdd->recupererTous($requete);
2222 julien 135
        // On récupère id_obs, id_image, votes, tags, modif_date
1794 jpm 136
	}
1593 samuel 137
 
1881 jpm 138
	private function getTotal() {
139
		$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
140
		$total = (int) $compte['nbre'];
141
		return $total;
142
	}
143
 
1794 jpm 144
	// recupere les donnée associées (fait en 2 requetes pour optimiser)
1881 jpm 145
	private function recupererInfos() {
1794 jpm 146
		// recuperer les ids
1881 jpm 147
		$idsImg = array();
148
		foreach ($this->idsObsImg as $ids) {
149
			$id = $ids['id_image'];
150
			$idsImg[] = $id;
1794 jpm 151
		}
1881 jpm 152
		$idsImgConcat = implode(',', $idsImg);
1643 samuel 153
 
1881 jpm 154
		$requete = 'SELECT '.
2222 julien 155
			'p.id_observation, p.plantnet_id, p.id_image, '.
156
			'cp.plant_net_occurrence_id, ' .
157
			'p.nom_sel, p.nom_sel_nn, p.nom_ret, p.nom_ret_nn,'.
158
			'p.nom_referentiel, p.famille, '.
159
			'p.zone_geo, p.altitude,'.
160
			'p.date_observation, p.date_created, p.date_published, '.
161
			'p.is_wild, p.identiplante_score, p.is_identiplante_validated, p.cel_photo_tag, '.
162
			'p.ce_utilisateur, p.courriel_utilisateur, '.
163
			'p.original_name AS nom_image, p.i_date_updated AS image_updated '.
1881 jpm 164
			'FROM del_plantnet AS p '.
2222 julien 165
			'LEFT JOIN tb_new_cel.pn_tb_pair AS cp ON p.id_observation = cp.occurrence_id '.
1881 jpm 166
			"WHERE id_image IN ($idsImgConcat) ".
167
			' -- '.__FILE__.':'.__LINE__;
1794 jpm 168
		// recuperer les donnees
1881 jpm 169
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 170
 
1794 jpm 171
		// regroupe les données par id_image
172
		$img_data = array();
1881 jpm 173
		foreach ($resultats as $infos) {
174
			$idImg = $infos['id_image'];
175
			$img_data[$idImg] = $infos;
1794 jpm 176
		}
177
		return $img_data;
1593 samuel 178
	}
1681 samuel 179
 
1593 samuel 180
	/**
181
	* Retourner un tableau d'images formaté en fonction des liaisons trouvées
182
	* @param $liaisons les liaisons de la table del_obs_images
1794 jpm 183
	*/
1881 jpm 184
	private function formaterInfos() {
1794 jpm 185
		// regroupe les observations
186
		$obs = array();
1922 jpm 187
		$imgCelTpl = $this->conteneur->getParametre('cel_img_url_tpl');
1881 jpm 188
		foreach ($this->idsObsImg as $ids) {
189
			$idobs = $ids['id_observation'];
190
			$idimg = $ids['id_image'];
1593 samuel 191
 
1881 jpm 192
			$imgdata = $this->infosObsImg[$idimg];
1593 samuel 193
 
1794 jpm 194
			if (!isset($obs[$idobs])) {
195
				$obs[$idobs] = array();
196
			}
1793 jpm 197
 
1794 jpm 198
			$obs[$idobs]['id_observation'] = $idobs;
2222 julien 199
			$obs[$idobs]['plantnet_id'] = $imgdata['plantnet_id'];
2213 arthur 200
			$obs[$idobs]['auteur_courriel'] = $imgdata['courriel_utilisateur'];
1593 samuel 201
 
2222 julien 202
            $obs[$idobs]['mots_cles_obs_cel'] = $imgdata['cel_photo_tag'];
1593 samuel 203
 
1794 jpm 204
			$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
2222 julien 205
            $obs[$idobs]['date_creation'] = $imgdata['date_created'];
206
            $obs[$idobs]['date_changement'] = $ids['modif_date'];
207
			$obs[$idobs]['date_publication'] = $imgdata['date_published'];
208
			$obs[$idobs]['is_wild'] = $imgdata['is_wild'];
209
			$obs[$idobs]['identiplante_score'] = $imgdata['identiplante_score'];
210
			$obs[$idobs]['is_identiplante_validated'] = $imgdata['is_identiplante_validated'];
1793 jpm 211
 
1794 jpm 212
			$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
2222 julien 213
			$obs[$idobs]['nom_sel_nn'] = $imgdata['nom_sel_nn'];
1794 jpm 214
			$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
215
			$obs[$idobs]['nom_ret'] = $imgdata['nom_ret'];
2222 julien 216
			$obs[$idobs]['nom_ret_nn'] = $imgdata['nom_ret_nn'];
1794 jpm 217
			$obs[$idobs]['famille'] = $imgdata['famille'];
1640 samuel 218
 
1794 jpm 219
			$obs[$idobs]['zone_geo'] = $imgdata['zone_geo'];
2222 julien 220
			$obs[$idobs]['altitude'] = $imgdata['altitude'];
1640 samuel 221
 
1794 jpm 222
			if (!isset($obs[$idobs]['images'])) {
223
				$obs[$idobs]['images'] = array();
224
			}
1640 samuel 225
 
1794 jpm 226
			$img_obj = array(
1881 jpm 227
				'id_image' => $idimg,
1794 jpm 228
				'nom_image' => $imgdata['nom_image'],
2222 julien 229
                'image_updated' => $imgdata['image_updated'],
1922 jpm 230
				'url' => sprintf($imgCelTpl, $idimg, 'O'),
1881 jpm 231
				'votes' => array_map('intval', explode(',', $ids['votes'])),
2222 julien 232
				'tags' => explode(',', $ids['del_image_tags']),
233
//				'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
1794 jpm 234
				);
235
			// push
236
			$obs[$idobs]['images'][] = $img_obj;
237
		}
238
		return $obs;
1593 samuel 239
	}
1793 jpm 240
 
1593 samuel 241
	/**
1794 jpm 242
	 * Charger les votes pour chaque image
243
	 */
1640 samuel 244
	private function chargerPropositionPlusProbable(&$obs) {
1794 jpm 245
		$obsIds = array_keys($obs);
1881 jpm 246
		$idsObsConcat = implode(',', $obsIds);
1593 samuel 247
 
2126 mathias 248
		$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, cv.ce_utilisateur '.
1881 jpm 249
			'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
250
			"WHERE ce_observation IN ($idsObsConcat) ".
1794 jpm 251
			'AND nom_sel IS NOT NULL '.
1881 jpm 252
			'AND c.id_commentaire = cv.ce_proposition '.
253
			' -- '.__FILE__.':'.__LINE__;
254
		$resultats = $this->bdd->recupererTous($requete);
1593 samuel 255
 
1794 jpm 256
		// calcul des votes
257
		// un vote identifié a un facteur de 3
258
		// additionne tous les vote par ce_proposition
1881 jpm 259
		$votes = array(); // map ce_proposition -> score
1794 jpm 260
		foreach ($resultats as $vote) {
261
			if (!isset($votes[$vote['id_commentaire']])) {
262
				$votes[$vote['id_commentaire']] = 0;
263
			}
264
			$valeur = ($vote['valeur'] == 1) ? 1 : -1;
265
			$votes[$vote['id_commentaire']] += is_numeric($vote['ce_utilisateur']) ? 3 * $valeur : $valeur;
266
		}
1793 jpm 267
 
1794 jpm 268
		foreach ($resultats as $vote) {
269
			$idobs = $vote['ce_observation'];
1793 jpm 270
 
1794 jpm 271
			if (!isset($obs[$idobs]['determinations'])) {
272
				$obs[$idobs]['determinations'] = array();
273
			}
1793 jpm 274
 
1794 jpm 275
			$obs[$idobs]['determinations'][$vote['id_commentaire']] =
276
			array('nom_sel' => $vote['nom_sel'],
277
				'nom_ret' => $vote['nom_ret'],
278
				'score' => $votes[$vote['id_commentaire']],
279
				'nn' => $vote['nom_sel_nn']);
280
		}
281
		return $obs;
1593 samuel 282
	}
1793 jpm 283
 
1881 jpm 284
	private function orderArray(&$obs) {
285
		$ret = array();
286
		foreach ($obs as $o) {
287
			$ret[] = $o;
288
		}
289
 
2124 mathias 290
		function cmpDesc($a, $b) {
1881 jpm 291
			return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
292
		}
2124 mathias 293
		function cmpAsc($a, $b) {
294
			return cmpDesc($b, $a);
2123 mathias 295
		}
1881 jpm 296
 
2123 mathias 297
		if ($this->parametres['ordre'] == 'desc') {
298
			usort($ret, 'cmpDesc');
299
		} else {
300
			usort($ret, 'cmpAsc');
301
		}
1881 jpm 302
		return $ret;
303
	}
304
 
1593 samuel 305
	/**
1794 jpm 306
	 * Formater les mots clés du cel en n'affichant que ceux faisant partie
307
	 * d'une liste définie dans le fichier de configuration
308
	 * @param $chaineMotCleCel la chaine de mots clés du cel
309
	 * @return string la chaine filtrée
310
	 */
1593 samuel 311
	private function formaterMotsClesCel($chaineMotCleCel) {
1989 mathias 312
		$mots_cles_cel_affiches = "fleur,fleurs,feuille,feuilles,ecorce,fruit,fruits,port,plantnet,plantscan_new,plantnet-mobile";
1652 samuel 313
 
1794 jpm 314
		$result = array_intersect(
315
			explode(',', $mots_cles_cel_affiches), // $tabMotsClesAffiches
316
			explode(',', $chaineMotCleCel)); // $tabMotsClesCel
1652 samuel 317
 
1794 jpm 318
		if (count($result) === 0) {
319
		  return array();
320
		}
321
		$ret = explode(',', implode(',', $result));
322
		return $ret;
1593 samuel 323
	}
1793 jpm 324
 
1990 samuel 325
}