mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 22:09:29 +00:00
Users can cancel responses to a survey.
This commit is contained in:
parent
e4861d562d
commit
5637be6d61
57
RestControllers/surveysController.php
Normal file
57
RestControllers/surveysController.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* Restserver controller
|
||||
*
|
||||
* Surveys controller
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
class surveysController {
|
||||
|
||||
/**
|
||||
* Cancel a response to a survey
|
||||
*
|
||||
* @url POST /surveys/cancel_response
|
||||
*/
|
||||
public function cancel_response(){
|
||||
|
||||
user_login_required();
|
||||
|
||||
//Get the ID of the survey
|
||||
$surveyID = $this->getSurveyIDFromPostID("postID");
|
||||
|
||||
//Try to cancel the user's response to the survey
|
||||
if(!components()->survey->cancel_response($surveyID, userID))
|
||||
Rest_fatal_error(500, "Couldn't cancel user response to the survey !");
|
||||
|
||||
//Success
|
||||
return array("success" => "The response to the survey was cancelled!");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of a survey from a $_POST ID
|
||||
*
|
||||
* @param string $name The name of the post field that contains
|
||||
* associated POST ID
|
||||
* @return int The ID of the target survey
|
||||
*/
|
||||
private function getSurveyIDFromPostID(string $name) : int {
|
||||
|
||||
//Get the ID of the target post
|
||||
$postID = getPostPostIDWithAccess("postID");
|
||||
|
||||
//Check if a survey is associated with the post
|
||||
$surveyID = components()->survey->get_id($postID);
|
||||
|
||||
//Check for errors
|
||||
if($surveyID == 0)
|
||||
Rest_fatal_error(401, "No survey was found with the specified post !");
|
||||
|
||||
//Return survey ID
|
||||
return $surveyID;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -128,6 +128,22 @@ class Survey {
|
||||
return $results[0]["ID"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the response of a user to a survey
|
||||
*
|
||||
* @param int $surveyID The ID of the target survey
|
||||
* @param int $userID The ID of the user removing his response
|
||||
* @return bool FALSE in case of failure / TRUE in case of success
|
||||
*/
|
||||
public function cancel_response(int $surveyID, int $userID) : bool {
|
||||
|
||||
//Perform a request on the database
|
||||
return CS::get()->db->deleteEntry(
|
||||
$this::SURVEY_RESPONSE_TABLE,
|
||||
"ID_sondage = ? AND ID_utilisateurs = ?",
|
||||
array($surveyID, $userID));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the survey associated to a post
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user