mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-11-04 12:14:12 +00:00 
			
		
		
		
	Response to a survey can be saved.
This commit is contained in:
		@@ -30,6 +30,39 @@ class surveysController {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send a response to a survey
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @url POST /surveys/send_response
 | 
			
		||||
	 */
 | 
			
		||||
	public function send_response(){
 | 
			
		||||
 | 
			
		||||
		user_login_required();
 | 
			
		||||
 | 
			
		||||
		//Get the ID of the survey
 | 
			
		||||
		$surveyID = $this->getSurveyIDFromPostID("postID");
 | 
			
		||||
 | 
			
		||||
		//Get the ID of the response of the user
 | 
			
		||||
		if(!isset($_POST['choiceID']))
 | 
			
		||||
			Rest_fatal_error(401, "Please specify the ID of your choice in your request!");
 | 
			
		||||
		$choiceID = (int) $_POST['choiceID'];
 | 
			
		||||
 | 
			
		||||
		//Try to cancel the user's previous response to the survey
 | 
			
		||||
		if(!components()->survey->cancel_response($surveyID, userID))
 | 
			
		||||
			Rest_fatal_error(500, "Couldn't cancel user previous response to the survey !");
 | 
			
		||||
		
 | 
			
		||||
		//Check if the user choice exists or not
 | 
			
		||||
		if(!components()->survey->choice_exists($surveyID, $choiceID))
 | 
			
		||||
			Rest_fatal_error(404, "Specified response does not exists!");
 | 
			
		||||
		
 | 
			
		||||
		//Save the answer
 | 
			
		||||
		if(!components()->survey->send_response(userID, $surveyID, $choiceID))
 | 
			
		||||
			Rest_fatal_error(500, "An error occured while trying to save response to the survey!");
 | 
			
		||||
		
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("success" => "User response has been saved!");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the ID of a survey from a $_POST ID
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -106,6 +106,20 @@ class Survey {
 | 
			
		||||
		return CS::get()->db->count($this::SURVEY_INFOS_TABLE, "WHERE ID_texte = ?", array($postID)) > 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Check wether a choice exists and is attached to a survey
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param int $surveyID the ID of the survey
 | 
			
		||||
	 * @param int $choiceID The ID of the choice to check
 | 
			
		||||
	 * @return bool TRUE if a choice exists and is valid / FALSE else
 | 
			
		||||
	 */
 | 
			
		||||
	public function choice_exists(int $surveyID, int $choiceID) : bool {
 | 
			
		||||
		return CS::get()->db->count(
 | 
			
		||||
			$this::SURVEY_CHOICES_TABLE, 
 | 
			
		||||
			"WHERE ID_sondage = ? AND ID = ?", 
 | 
			
		||||
			array($surveyID, $choiceID)) > 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the ID of a survey associated to a post
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -128,6 +142,28 @@ class Survey {
 | 
			
		||||
		return $results[0]["ID"];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send a response to a survey
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param int $userID The ID of the user giving a response to the survey
 | 
			
		||||
	 * @param int $surveyID The ID of the target survey
 | 
			
		||||
	 * @param int $choiceID The ID of the selected choice
 | 
			
		||||
	 * @return bool TRUE for a success / FALSE for a failure
 | 
			
		||||
	 */
 | 
			
		||||
	public function send_response(int $userID, int $surveyID, int $choiceID){
 | 
			
		||||
		
 | 
			
		||||
		//Generate the new data line
 | 
			
		||||
		$data = array(
 | 
			
		||||
			"ID_utilisateurs" => $userID,
 | 
			
		||||
			"ID_sondage" => $surveyID,
 | 
			
		||||
			"ID_sondage_choix" => $choiceID,
 | 
			
		||||
			"date_envoi" => mysql_date()
 | 
			
		||||
		);
 | 
			
		||||
 | 
			
		||||
		//Try to save response
 | 
			
		||||
		return CS::get()->db->addLine($this::SURVEY_RESPONSE_TABLE, $data);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Cancel the response of a user to a survey
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user