1617 |
mathias |
1 |
<?php
|
|
|
2 |
/**
|
1818 |
jpm |
3 |
* Accès aux sous-services de statistiques pour Identiplante / Pictoflora
|
1795 |
jpm |
4 |
*
|
1818 |
jpm |
5 |
* @see Documentation : <http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelStats>
|
1795 |
jpm |
6 |
*
|
|
|
7 |
* @category DEL
|
|
|
8 |
* @package Services
|
|
|
9 |
* @subpackage Statistiques
|
|
|
10 |
* @version 0.1
|
|
|
11 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
13 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
14 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
15 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
16 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
17 |
*/
|
1617 |
mathias |
18 |
class Statistiques extends RestService {
|
|
|
19 |
|
1818 |
jpm |
20 |
private $parametres = array();
|
|
|
21 |
private $ressources = array();
|
|
|
22 |
private $methode = null;
|
|
|
23 |
private $serviceNom = 'statistiques';
|
|
|
24 |
private $sousServiceNom = null;
|
|
|
25 |
private $cheminCourant = null;
|
1617 |
mathias |
26 |
|
1818 |
jpm |
27 |
private $conteneur;
|
|
|
28 |
|
|
|
29 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
30 |
protected $utilisationParametresBruts = true;
|
|
|
31 |
|
1617 |
mathias |
32 |
public function __construct() {
|
1818 |
jpm |
33 |
$this->cheminCourant = dirname(__FILE__).DS;
|
1617 |
mathias |
34 |
}
|
|
|
35 |
|
|
|
36 |
public function consulter($ressources, $parametres) {
|
1818 |
jpm |
37 |
$this->methode = 'consulter';
|
|
|
38 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
39 |
return $this->executerService();
|
|
|
40 |
}
|
1617 |
mathias |
41 |
|
1818 |
jpm |
42 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
43 |
$this->ressources = $ressources;
|
|
|
44 |
$this->parametres = $parametres;
|
|
|
45 |
}
|
1617 |
mathias |
46 |
|
1818 |
jpm |
47 |
private function executerService() {
|
|
|
48 |
$reponseHttp = new ReponseHttp();
|
|
|
49 |
try {
|
|
|
50 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
51 |
$resultat = $this->traiterRessources();
|
|
|
52 |
$reponseHttp->setResultatService($resultat);
|
|
|
53 |
} catch (Exception $e) {
|
|
|
54 |
$reponseHttp->ajouterErreur($e);
|
1617 |
mathias |
55 |
}
|
|
|
56 |
$reponseHttp->emettreLesEntetes();
|
|
|
57 |
$corps = $reponseHttp->getCorps();
|
|
|
58 |
return $corps;
|
|
|
59 |
}
|
|
|
60 |
|
1818 |
jpm |
61 |
private function traiterRessources() {
|
|
|
62 |
$this->analyserRessources();
|
|
|
63 |
$retour = $this->initialiserService();
|
|
|
64 |
return $retour;
|
1617 |
mathias |
65 |
}
|
|
|
66 |
|
1818 |
jpm |
67 |
private function analyserRessources() {
|
|
|
68 |
if ($this->methode == 'consulter') {
|
|
|
69 |
$this->sousServiceNom = 'statistiques-par-annee';
|
|
|
70 |
}
|
|
|
71 |
if ($this->sousServiceNom == null) {
|
|
|
72 |
$this->lancerMessageErreurRessource();
|
|
|
73 |
}
|
1617 |
mathias |
74 |
}
|
|
|
75 |
|
1818 |
jpm |
76 |
private function lancerMessageErreurRessource() {
|
|
|
77 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
78 |
$message = "La ressource demandée '$ressource' ".
|
|
|
79 |
"n'est pas disponible pour le service {$this->serviceNom} !\n".
|
|
|
80 |
self::getDoc();
|
|
|
81 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
82 |
throw new Exception($message, $code);
|
1788 |
mathias |
83 |
}
|
|
|
84 |
|
1818 |
jpm |
85 |
public static function getDoc() {
|
|
|
86 |
return "Les URLs disponibles pour ce service sont :\n".
|
|
|
87 |
" * en GET :\n".
|
|
|
88 |
" - statistiques/tout\n".
|
|
|
89 |
" - statistiques/moyenneObsSansNomParMois\n".
|
|
|
90 |
" - statistiques/moyenneObsIdentifieesParMois\n".
|
|
|
91 |
" - statistiques/pourcentageObsIdentifieesEnFinDAnnee\n".
|
|
|
92 |
" - statistiques/pourcentageObsIdentifieesEnFinDAnneePlusPlus\n".
|
|
|
93 |
" - statistiques/moyenneActionsParJour\n".
|
|
|
94 |
" - statistiques/personnesEnvoyantUnePropositionParMois\n";
|
1617 |
mathias |
95 |
}
|
|
|
96 |
|
1818 |
jpm |
97 |
private function initialiserService() {
|
|
|
98 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
99 |
$chemins = array();
|
|
|
100 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
|
|
101 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
102 |
$retour = '';
|
|
|
103 |
$service = null;
|
1788 |
mathias |
104 |
|
1818 |
jpm |
105 |
foreach ($chemins as $chemin) {
|
|
|
106 |
if (file_exists($chemin)) {
|
|
|
107 |
require_once $chemin;
|
|
|
108 |
$service = new $classe($this->conteneur);
|
|
|
109 |
if ($this->methode == 'consulter') {
|
|
|
110 |
$retour = $service->consulter();
|
|
|
111 |
} else {
|
|
|
112 |
$message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ".
|
|
|
113 |
"ne possède pas de méthode '{$this->methode}' !";
|
|
|
114 |
$code = RestServeur::HTTP_NON_IMPLEMENTE;
|
|
|
115 |
throw new Exception($message, $code);
|
|
|
116 |
}
|
1788 |
mathias |
117 |
}
|
|
|
118 |
}
|
1624 |
mathias |
119 |
|
1818 |
jpm |
120 |
if (is_null($service)) {
|
|
|
121 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
122 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
123 |
"est introuvable par le service '{$this->serviceNom}' !";
|
|
|
124 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
125 |
throw new Exception($message, $code);
|
1788 |
mathias |
126 |
}
|
1818 |
jpm |
127 |
return $retour;
|
1624 |
mathias |
128 |
}
|
|
|
129 |
|
1818 |
jpm |
130 |
private function obtenirNomClasseService($mot) {
|
|
|
131 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
132 |
return $classeNom;
|
1617 |
mathias |
133 |
}
|
1795 |
jpm |
134 |
}
|