Line 13... |
Line 13... |
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
15 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
15 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
16 |
*/
|
16 |
*/
|
Line 17... |
Line 17... |
17 |
|
17 |
|
18 |
class VoteObservation extends RestService {
|
18 |
class VoteObservation {
|
19 |
private $conteneur;
|
19 |
private $conteneur;
|
20 |
private $bdd;
|
20 |
private $bdd;
|
21 |
private $navigation;
|
21 |
private $navigation;
|
22 |
private $mapping;
|
22 |
private $mapping;
|
Line 87... |
Line 87... |
87 |
|
87 |
|
88 |
public function ajouter($ressources, $parametres) {
|
88 |
public function ajouter($ressources, $parametres) {
|
89 |
$this->ressources = $ressources;
|
89 |
$this->ressources = $ressources;
|
90 |
$this->parametres = $parametres;
|
90 |
$this->parametres = $parametres;
|
91 |
$this->verifierParametresAjoutModif();
|
- |
|
92 |
|
- |
|
93 |
// Vérifie si la proposition existe, et la crée sinon
|
- |
|
94 |
// (cas du vote sur la proposition fabriquée à partir de l'observation originale)
|
- |
|
95 |
$idProposition = $this->ressources[1];
|
- |
|
96 |
if ($idProposition == 0) {
|
- |
|
97 |
$idProposition = $this->creerPropositionDeterminationInitiale($idProposition);
|
- |
|
98 |
}
|
91 |
$this->verifierParametresAjoutModif();
|
99 |
$idVote = $this->ajouterVote();
|
92 |
$idVote = $this->ajouterVote();
|
100 |
if ($idVote) {
|
93 |
if ($idVote) {
|
101 |
$resultat = new ResultatService();
|
94 |
$resultat = new ResultatService();
|
102 |
$resultat->corps = array('id_vote' => $idVote);
|
95 |
$resultat->corps = array('id_vote' => $idVote);
|
103 |
return $resultat;
|
96 |
return $resultat;
|
104 |
}
|
97 |
}
|
105 |
return false;
|
98 |
return false;
|
Line 106... |
Line 99... |
106 |
}
|
99 |
}
|
- |
|
100 |
|
- |
|
101 |
private function ajouterVote() {
|
107 |
|
102 |
$idProposition = $this->creerPropositionDeterminationInitiale();
|
108 |
private function ajouterVote() {
|
103 |
|
109 |
$idObsP = $this->bdd->proteger($this->ressources[0]);
|
104 |
$idObsP = $this->bdd->proteger($this->ressources[0]);
|
110 |
$idPropositionP = $this->bdd->proteger($this->ressources[1]);
|
105 |
$idPropositionP = $this->bdd->proteger($idProposition);
|
111 |
$idUtilisateurP = $this->bdd->proteger($this->parametres['utilisateur']);
|
106 |
$idUtilisateurP = $this->bdd->proteger($this->parametres['utilisateur']);
|
112 |
$valeurP = $this->bdd->proteger($this->parametres['valeur']);
|
107 |
$valeurP = $this->bdd->proteger($this->parametres['valeur']);
|
113 |
$requete = 'INSERT INTO del_commentaire_vote (ce_proposition , ce_utilisateur , valeur , date) '.
|
108 |
$requete = 'INSERT INTO del_commentaire_vote (ce_proposition , ce_utilisateur , valeur , date) '.
|
Line 126... |
Line 121... |
126 |
// ATTENTION : idVote doit être récupéré avant toute nouvelle requete !
|
121 |
// ATTENTION : idVote doit être récupéré avant toute nouvelle requete !
|
127 |
$idVote = $this->bdd->recupererIdDernierAjout();
|
122 |
$idVote = $this->bdd->recupererIdDernierAjout();
|
128 |
return $idVote;
|
123 |
return $idVote;
|
129 |
}
|
124 |
}
|
Line 130... |
Line 125... |
130 |
|
125 |
|
131 |
private function creerPropositionDeterminationInitiale($id_observation) {
|
126 |
/** Si l'identifiant de proposition vaut 0, c'est un vote sur une proposition
|
132 |
$proposition_existe = $this->propositionInitialeExiste($id_observation);
|
127 |
* fabriquée à partir de l'observation originale, dont on doit obtenir l'id
|
133 |
if ($proposition_existe === false) {
|
128 |
* (cas où l'on vient de voter pour celle et cela a créé la proposition, puis
|
134 |
$id_proposition = $this->creerPropositionAPartirObservation($id_observation);
|
129 |
* on revote pour celle ci en changeant d'avis sans recharger la page)
|
135 |
} else {
|
130 |
*/
|
136 |
$requete = 'SELECT id_commentaire '.
|
131 |
private function creerPropositionDeterminationInitiale() {
|
137 |
'FROM del_commentaire '.
|
132 |
$idProposition = $this->ressources[1];
|
138 |
"WHERE ce_observation = $id_observation ".
|
133 |
if ($idProposition === 0) {
|
139 |
'AND proposition_initiale = 1 '.
|
134 |
$propositionExiste = $this->verifierExistencePropositionInitiale();
|
140 |
' -- '.__FILE__.' : '.__LINE__;
|
135 |
if ($propositionExiste === false) {
|
- |
|
136 |
$idProposition = $this->creerPropositionInitiale();
|
141 |
$resultat = $this->bdd->recuperer($requete);
|
137 |
} else {
|
- |
|
138 |
$idProposition = $this->getIdPropositionInitiale();
|
142 |
$id_proposition = $resultat['id_commentaire'];
|
139 |
}
|
143 |
}
|
140 |
}
|
144 |
return $id_proposition;
|
141 |
return $idProposition;
|
Line 145... |
Line 142... |
145 |
}
|
142 |
}
|
- |
|
143 |
|
146 |
|
144 |
private function verifierExistencePropositionInitiale() {
|
147 |
private function propositionInitialeExiste($id_observation) {
|
145 |
$idObservationP = $this->bdd->proteger($this->ressources[0]);
|
148 |
$requete = 'SELECT COUNT(*) >= 1 AS existe '.
|
146 |
$requete = 'SELECT COUNT(*) >= 1 AS existe '.
|
149 |
'FROM del_commentaire '.
|
147 |
'FROM del_commentaire '.
|
150 |
"WHERE ce_observation = $id_observation ".
|
148 |
"WHERE ce_observation = $idObservationP ".
|
151 |
'AND proposition_initiale = 1 '.
|
149 |
'AND proposition_initiale = 1 '.
|
152 |
' -- '.__FILE__.' : '.__LINE__;
|
150 |
' -- '.__FILE__.' : '.__LINE__;
|
153 |
$resultat = $this->bdd->recuperer($requete);
|
151 |
$resultat = $this->bdd->recuperer($requete);
|
Line 154... |
Line 152... |
154 |
return $resultat['existe'] == 1;
|
152 |
return $resultat['existe'] == 1;
|
155 |
}
|
153 |
}
|
156 |
|
154 |
|
157 |
private function creerPropositionAPartirObservation($id_observation) {
|
155 |
private function creerPropositionInitiale() {
|
158 |
$idObservationP = $this->bdd->proteger($id_observation);
|
156 |
$idObservationP = $this->bdd->proteger($this->ressources[0]);
|
159 |
$requete = 'INSERT IGNORE INTO del_commentaire '.
|
157 |
$requete = 'INSERT IGNORE INTO del_commentaire '.
|
160 |
'(ce_observation, ce_utilisateur, utilisateur_prenom, utilisateur_nom, utilisateur_courriel, '.
|
158 |
'(ce_observation, ce_utilisateur, utilisateur_prenom, utilisateur_nom, utilisateur_courriel, '.
|
Line 166... |
Line 164... |
166 |
' LEFT JOIN del_utilisateur du ON (do.ce_utilisateur = du.id_utilisateur) '.
|
164 |
' LEFT JOIN del_utilisateur du ON (do.ce_utilisateur = du.id_utilisateur) '.
|
167 |
"WHERE id_observation = $idObservationP ".
|
165 |
"WHERE id_observation = $idObservationP ".
|
168 |
' -- '.__FILE__.' : '.__LINE__;
|
166 |
' -- '.__FILE__.' : '.__LINE__;
|
Line 169... |
Line 167... |
169 |
|
167 |
|
170 |
$this->bdd->executer($requete);
|
- |
|
171 |
|
- |
|
172 |
// Attention à l'abstraction utilisée, récupérer le dernier id en mysql ou msqli ça marche bien, mais pour d'autres moins
|
168 |
$this->bdd->executer($requete);
|
173 |
$id = $this->bdd->recupererIdDernierAjout();
|
169 |
$id = $this->bdd->recupererIdDernierAjout();
|
174 |
return $id;
|
170 |
return $id;
|
Line -... |
Line 171... |
- |
|
171 |
}
|
- |
|
172 |
|
- |
|
173 |
private function getIdPropositionInitiale() {
|
- |
|
174 |
$idObservationP = $this->bdd->proteger($this->ressources[0]);
|
- |
|
175 |
$requete = 'SELECT id_commentaire '.
|
- |
|
176 |
'FROM del_commentaire '.
|
- |
|
177 |
"WHERE ce_observation = $idObservationP ".
|
- |
|
178 |
'AND proposition_initiale = 1 '.
|
- |
|
179 |
' -- '.__FILE__.' : '.__LINE__;
|
- |
|
180 |
$resultat = $this->bdd->recuperer($requete);
|
- |
|
181 |
return $resultat['id_commentaire'];
|
175 |
}
|
182 |
}
|
176 |
|
183 |
|
177 |
public function modifier($ressources, $parametres) {
|
184 |
public function modifier($ressources, $parametres) {
|
178 |
$this->ressources = $ressources;
|
185 |
$this->ressources = $ressources;
|
- |
|
186 |
$this->parametres = $parametres;
|
- |
|
187 |
$this->verifierParametresAjoutModif();
|
- |
|
188 |
$resultat = $this->modifierVote();
|
- |
|
189 |
if ($resultat > 0) {
|
- |
|
190 |
return 'ok';
|
Line 179... |
Line -... |
179 |
$this->parametres = $parametres;
|
- |
|
180 |
$this->verifierParametresAjoutModif();
|
- |
|
181 |
|
- |
|
182 |
// Si l'identifiant de proposition vaut 0, c'est un vote sur une proposition
|
- |
|
183 |
// fabriquée à partir de l'observation originale, dont on doit obtenir l'id
|
- |
|
184 |
// (cas où l'on vient de voter pour celle et cela a créé la proposition, puis
|
191 |
}
|
185 |
// on revote pour celle ci en changeant d'avis sans recharger la page)
|
192 |
}
|
186 |
$idProposition = $this->ressources[1];
|
193 |
|
187 |
if ($idProposition == 0) {
|
194 |
private function modifierVote() {
|
188 |
$idProposition = $this->renvoyerIdOuCreerPropositionDeterminationInitiale($idProposition);
|
195 |
$idProposition = $this->creerPropositionDeterminationInitiale();
|
189 |
if ($idProposition == null) {
|
- |
|
190 |
$msg = "Aucun identifiant de proposition n'est lié à cette observation.";
|
196 |
if ($idProposition == null) {
|
- |
|
197 |
$msg = "Aucun identifiant de proposition n'est lié à cette observation.";
|
- |
|
198 |
throw new Exception($msg, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
191 |
throw new Exception($msg, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
199 |
}
|
192 |
}
|
200 |
|
193 |
}
|
201 |
$idObsP = $this->bdd->proteger($this->ressources[0]);
|
194 |
$idPropositionP = $this->bdd->proteger($idProposition);
|
202 |
$idPropositionP = $this->bdd->proteger($idProposition);
|
195 |
$idUtilisateurP = $this->bdd->proteger($parametres['utilisateur']);
|
203 |
$idUtilisateurP = $this->bdd->proteger($this->parametres['utilisateur']);
|
196 |
$valeurP = $this->bdd->proteger($parametres['valeur']);
|
204 |
$valeurP = $this->bdd->proteger($this->parametres['valeur']);
|
197 |
$requete = 'UPDATE del_commentaire_vote '.
|
205 |
$requete = 'UPDATE del_commentaire_vote '.
|
Line 198... |
Line 206... |
198 |
"SET valeur = $valeurP, date = NOW() ".
|
206 |
"SET valeur = $valeurP, date = NOW() ".
|
199 |
"WHERE ce_proposition = $idPropositionP AND ce_utilisateur = $idUtilisateurP ".
|
207 |
"WHERE ce_proposition = $idPropositionP AND ce_utilisateur = $idUtilisateurP ".
|
- |
|
208 |
' -- '.__FILE__.' : '.__LINE__;
|
200 |
' -- '.__FILE__.' : '.__LINE__;
|
209 |
|
201 |
|
210 |
$resultat = $this->bdd->executer($requete);
|
- |
|
211 |
if ($resultat === false) {
|
- |
|
212 |
$msg = "Un erreur est survenu lors de la tentative de modification du vote.";
|
202 |
$resultats = $this->bdd->requeter($requete);
|
213 |
throw new Exception($msg, RestServeur::HTTP_CODE_ERREUR);
|
203 |
if ($resultats == false) {
|
214 |
} else if ($resultat === 0) {
|
- |
|
215 |
$msg = "Aucun vote ne correspond au critères fournis : ".
|
204 |
RestServeur::envoyerEnteteStatutHttp(RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
|
216 |
"idObs -> $idObsP, idProposition -> $idPropositionP et id_utilisateur -> $idUtilisateurP.";
|
Line 205... |
Line 217... |
205 |
} else {
|
217 |
throw new Exception($msg, RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE);
|
206 |
RestServeur::envoyerEnteteStatutHttp(RestServeur::HTTP_CODE_OK);
|
218 |
}
|