Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 1880 → Rev 1881

/trunk/services/modules/0.1/plantnet/Changements.php
1,9 → 1,9
<?php
/**
* Le web service plantnet récupère toutes les image de la table v_del_image
* ordonées par date de modification
* Les images sont regroupées en observations
* Les tags, les votes et les propositions de determinations sont intégrés à l'observation
* Le web service plantnet récupère toutes les infos de la vue del_plantnet.
* Ordonées par date de modification.
* Les images sont regroupées en observations.
* Les tags, les votes et les propositions de determinations sont intégrés à l'observation.
*
* @category DEL
* @package Services
20,14 → 20,17
 
class Changements {
 
private $indexImagesIds = array();
private $conteneur;
private $navigation;
private $bdd;
 
private $parametres = array();
private $ressources = array();
private $date_defaut = '1900-01-01';
private $idsObsImg = array();
private $infosObsImg = array();
 
 
public function __construct(Conteneur $conteneur = null) {
/* restore_exception_handler(); */
/* restore_error_handler(); */
34,7 → 37,6
/* ini_set("display_errors", "1"); */
 
$this->conteneur = $conteneur == null ? new Conteneur() : $conteneur;
$this->conteneur->chargerConfiguration('config_plantnet.ini');
$this->navigation = $conteneur->getNavigation();
$this->bdd = $this->conteneur->getBdd();
}
56,131 → 58,111
}
 
// Lancement du service
$liaisons = $this->chargerLiaisons();
$images = array();
$this->idsObsImg = $this->getIdsObsImg();
$infos = array();
$total = 0;
if ($this->idsObsImg) {
$total = $this->getTotal();
 
if ($liaisons) {
$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
$total = (int) $compte['nbre'];
 
$imgdata = $this->recupererDonneeObs($liaisons);
$obs = $this->regrouperObs($liaisons, $imgdata);
 
$obs = $this->chargerPropositionPlusProbable($obs);
$obs = $this->orderArray($obs);
$this->infosObsImg = $this->recupererInfos();
$infos = $this->formaterInfos();
$infos = $this->chargerPropositionPlusProbable($infos);
$infos = $this->orderArray($infos);
}
 
$this->navigation->setTotal($total);
 
// Mettre en forme le résultat et l'envoyer pour affichage
$resultat = new ResultatService();
$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $obs);
 
$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $infos);
return $resultat;
}
 
 
private function orderArray(&$obs) {
$ret = array();
 
foreach ($obs as $o) {
$ret[] = $o;
}
 
function cmp($a, $b) {
return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
}
 
usort($ret, 'cmp');
return $ret;
}
 
/*-------------------------------------------------------------------------------
CHARGEMENT DES IMAGES
--------------------------------------------------------------------------------*/
 
private function chargerLiaisons() {
$date_debut = $this->parametres['date'];
$date_debut = '\'' . $date_debut . '\'';
private function getIdsObsImg() {
$date_debut = "'{$this->parametres['date']}'";
$limite = @min(intval($this->parametres['navigation.limite']), 1000);
$limite = $limite ? $limite : 10; // 0 => 10
$depart = intval(@$this->parametres['navigation.depart']);
 
$requete_sql =
'select SQL_CALC_FOUND_ROWS vdi.id_observation, vdi.id_image, '.
 
'GROUP_CONCAT(del_image_vote.valeur) as votes, GROUP_CONCAT(DISTINCT tag) as tags, '.
 
$requete =
'SELECT SQL_CALC_FOUND_ROWS p.id_observation, p.id_image, '.
'GROUP_CONCAT(iv.valeur) AS votes, '.
'GROUP_CONCAT(DISTINCT tag) AS tags, '.
'GREATEST('.
'IFNULL(vdi.date_creation, \''.$this->date_defaut.'\'), '.
'IFNULL(vdi.date_modification, \''.$this->date_defaut.'\'), '.
'IFNULL(MAX(del_image_tag.date), \''.$this->date_defaut.'\'), '.
'IFNULL(MAX(del_image_tag.date_modification), \''.$this->date_defaut.'\'), '.
'IFNULL(MAX(del_image_vote.date), \''.$this->date_defaut.'\'), '.
'IFNULL(MAX(del_commentaire.date), \''.$this->date_defaut.'\'), '.
'IFNULL(MAX(del_commentaire_vote.date), \''.$this->date_defaut.'\')) as modif_date '.
' IFNULL(p.date_creation, \''.$this->date_defaut.'\'), '.
' IFNULL(p.date_modification, \''.$this->date_defaut.'\'), '.
' IFNULL(MAX(it.date), \''.$this->date_defaut.'\'), '.
' IFNULL(MAX(it.date_modification), \''.$this->date_defaut.'\'), '.
' IFNULL(MAX(iv.date), \''.$this->date_defaut.'\'), '.
' IFNULL(MAX(c.date), \''.$this->date_defaut.'\'), '.
' IFNULL(MAX(cv.date), \''.$this->date_defaut.'\')) AS modif_date '.
 
'from v_del_image as vdi '.
 
'left join del_image_vote on del_image_vote.ce_image=id_image and del_image_vote.ce_protocole=3 '.
'left join del_image_tag on del_image_tag.ce_image=id_image and del_image_tag.actif=1 '.
'left join del_commentaire on del_commentaire.ce_observation=id_observation '.
'left join del_commentaire_vote on del_commentaire_vote.ce_proposition=del_commentaire.id_commentaire '.
 
'group by id_image, id_observation '.
 
'having MAX(vdi.date_creation) >= '.$date_debut.' or '.
' MAX(vdi.date_modification) >= '.$date_debut.' or '.
' MAX(del_image_tag.date) >= '.$date_debut.' or '.
' MAX(del_image_tag.date_modification) >= '.$date_debut.' or '.
' MAX(del_image_vote.date) >= '.$date_debut.' or '.
' MAX(del_commentaire.date) >= '.$date_debut.' or '.
' MAX(del_commentaire_vote.date) >= '.$date_debut.' '.
'order by modif_date DESC '.
'limit '.$depart.', '.$limite.' -- '.__FILE__.':'.__LINE__;
 
//echo $requete_sql; exit;
 
return $this->bdd->recupererTous($requete_sql);
 
'FROM del_plantnet AS p '.
' LEFT JOIN del_image_vote AS iv '.
' ON (id_image = iv.ce_image AND iv.ce_protocole = 3) '.
' LEFT JOIN del_image_tag AS it '.
' ON (id_image = it.ce_image AND it.actif = 1) '.
' LEFT JOIN del_commentaire AS c '.
' ON (id_observation = c.ce_observation) '.
' LEFT JOIN del_commentaire_vote AS cv '.
' ON (c.id_commentaire = cv.ce_proposition) '.
'GROUP BY id_image, id_observation '.
'HAVING MAX(p.date_creation) >= '.$date_debut.' '.
' OR MAX(p.date_modification) >= '.$date_debut.' '.
' OR MAX(it.date) >= '.$date_debut.' '.
' OR MAX(it.date_modification) >= '.$date_debut.' '.
' OR MAX(iv.date) >= '.$date_debut.' '.
' OR MAX(c.date) >= '.$date_debut.' '.
' OR MAX(cv.date) >= '.$date_debut.' '.
'ORDER BY modif_date DESC '.
'LIMIT '.$depart.', '.$limite.
' -- '.__FILE__.':'.__LINE__;
//echo $requete; exit;
// GROUP BY (très couteux) car multiples observations associées à une image
// charlie est ici :-)
// eg: 16150,16185,16245,16246,118995,129989
return $this->bdd->recupererTous($requete);
}
 
private function getTotal() {
$compte = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
$total = (int) $compte['nbre'];
return $total;
}
 
// recupere les donnée associées (fait en 2 requetes pour optimiser)
private function recupererDonneeObs(&$liaisons) {
private function recupererInfos() {
// recuperer les ids
$ids = array();
foreach ($liaisons as $img) {
$id = $img['id_image'];
$ids[] = $id;
$idsImg = array();
foreach ($this->idsObsImg as $ids) {
$id = $ids['id_image'];
$idsImg[] = $id;
}
$idsImgConcat = implode(',', $idsImg);
 
$requete = 'SELECT '.
'id_observation, id_image, '.
'nom_sel, '.
'nom_referentiel, nom_ret, nom_ret_nn, nt, famille, '.
'zone_geo, latitude, longitude, '.
'date_observation, date_creation, date_transmission, '.
'mots_cles_texte, '.
'ce_utilisateur, prenom_utilisateur, nom_utilisateur, courriel_utilisateur, '.
'i_mots_cles_texte AS mots_cles_texte_image, nom_original AS nom_image '.
'FROM del_plantnet AS p '.
"WHERE id_image IN ($idsImgConcat) ".
' -- '.__FILE__.':'.__LINE__;
// recuperer les donnees
$resultats = $this->bdd->recupererTous(sprintf(
'SELECT '.
'vdi.id_observation, vdi.id_image, '.
'vdi.nom_sel, '.
'vdi.nom_referentiel, vdi.nom_ret, vdi.nom_ret_nn, vdi.nt, vdi.famille, '.
'vdi.zone_geo, vdi.latitude, vdi.longitude, '.
'vdi.date_observation, vdi.date_creation, vdi.date_transmission, '.
'vdi.mots_cles_texte as mots_cles_texte, '.
'vdi.i_mots_cles_texte as mots_cles_texte_image, '.
$resultats = $this->bdd->recupererTous($requete);
 
'vdi.ce_utilisateur as ce_utilisateur, '.
'vdi.prenom_utilisateur, vdi.courriel_utilisateur, vdi.nom_utilisateur, vdi.nom_original as nom_image '.
 
'FROM v_del_image as vdi '.
'WHERE vdi.id_image IN (%s) '.
'', implode(',', $ids)));
 
// regroupe les données par id_image
$img_data = array();
foreach ($resultats as $img) {
$id = $img['id_image'];
$img_data[$id] = $img;
foreach ($resultats as $infos) {
$idImg = $infos['id_image'];
$img_data[$idImg] = $infos;
}
return $img_data;
}
189,14 → 171,14
* Retourner un tableau d'images formaté en fonction des liaisons trouvées
* @param $liaisons les liaisons de la table del_obs_images
*/
private function regrouperObs(&$liaisons, &$imgdatas) {
private function formaterInfos() {
// regroupe les observations
$obs = array();
foreach ($liaisons as $img) {
$idobs = $img['id_observation'];
$idimg = $img['id_image'];
foreach ($this->idsObsImg as $ids) {
$idobs = $ids['id_observation'];
$idimg = $ids['id_image'];
 
$imgdata = $imgdatas[$idimg];
$imgdata = $this->infosObsImg[$idimg];
 
if (!isset($obs[$idobs])) {
$obs[$idobs] = array();
213,7 → 195,7
$obs[$idobs]['date_observation'] = $imgdata['date_observation'];
$obs[$idobs]['date_publication'] = $imgdata['date_transmission'];
$obs[$idobs]['date_creation'] = $imgdata['date_creation'];
$obs[$idobs]['date_changement'] = $img['modif_date'];
$obs[$idobs]['date_changement'] = $ids['modif_date'];
 
$obs[$idobs]['nom_sel'] = $imgdata['nom_sel'];
$obs[$idobs]['nom_referentiel'] = $imgdata['nom_referentiel'];
231,11 → 213,11
}
 
$img_obj = array(
'id_image' => $img['id_image'],
'id_image' => $idimg,
'nom_image' => $imgdata['nom_image'],
'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $img['id_image']),
'votes' => array_map('intval', explode(',', $img['votes'])),
'tags' => explode(',', $img['tags']),
'url' => sprintf('http://api.tela-botanica.org/img:%09dO.jpg', $idimg),
'votes' => array_map('intval', explode(',', $ids['votes'])),
'tags' => explode(',', $ids['tags']),
'mots_cles_img_cel' => $this->formaterMotsClesCel($imgdata['mots_cles_texte_image'])
);
// push
249,20 → 231,20
*/
private function chargerPropositionPlusProbable(&$obs) {
$obsIds = array_keys($obs);
$idsObsConcat = implode(',', $obsIds);
 
$resultats = $this->bdd->recupererTous(sprintf(
'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, del_commentaire_vote.ce_utilisateur '.
'FROM del_commentaire_vote, del_commentaire '.
'WHERE ce_observation IN (%s) '.
$requete = 'SELECT ce_observation, id_commentaire, valeur, nom_sel, nom_sel_nn, nom_ret, nom_ret_nn, cv.ce_utilisateur '.
'FROM del_commentaire_vote AS cv, del_commentaire AS c '.
"WHERE ce_observation IN ($idsObsConcat) ".
'AND nom_sel IS NOT NULL '.
'AND del_commentaire.id_commentaire=del_commentaire_vote.ce_proposition '.
'', implode(',', $obsIds)));
'AND c.id_commentaire = cv.ce_proposition '.
' -- '.__FILE__.':'.__LINE__;
$resultats = $this->bdd->recupererTous($requete);
 
$votes = array(); // map ce_proposition -> score
 
// calcul des votes
// un vote identifié a un facteur de 3
// additionne tous les vote par ce_proposition
$votes = array(); // map ce_proposition -> score
foreach ($resultats as $vote) {
if (!isset($votes[$vote['id_commentaire']])) {
$votes[$vote['id_commentaire']] = 0;
287,6 → 269,21
return $obs;
}
 
private function orderArray(&$obs) {
$ret = array();
foreach ($obs as $o) {
$ret[] = $o;
}
 
function cmp($a, $b) {
return ($a['date_changement'] < $b['date_changement']) ? 1 : -1;
}
 
usort($ret, 'cmp');
return $ret;
}
 
 
/*-------------------------------------------------------------------------------
FORMATER ET METTRE EN FORME
--------------------------------------------------------------------------------*/