Can get all the response of a user to a survey into a SurveyResponse object

This commit is contained in:
Pierre 2018-05-13 19:16:10 +02:00
parent 8c2ba7fd31
commit 512f252c67
3 changed files with 102 additions and 0 deletions

View File

@ -131,4 +131,24 @@ class SurveysController {
}
/**
* Turn a SurveyResponse object into API array
*
* @param SurveyResponse $response The response to convert
* @return array Generated API entry
*/
public static function SurveyResponseToAPI(SurveyResponse $response) : array {
$data = array();
$data["id"] = $response->get_id();
$data["time_sent"] = $response->get_time_sent();
$data["userID"] = $response->get_userID();
$data["surveyID"] = $response->get_surveyID();
$data["choiceID"] = $response->get_choiceID();
return $data;
}
}

View File

@ -189,6 +189,28 @@ class SurveyComponent {
array($userID));
}
/**
* Get all the responses of the user, as SurveyResponse objects
*
* @param int $userID Target user ID
* @return array The list of responses
*/
public function get_all_responses(int $userID) : array {
//Perform the query over the database
$tableName = self::SURVEY_RESPONSE_TABLE;
$conditions = "WHERE ID_utilisateurs = ?";
$values = array($userID);
$entries = cs()->db->select($tableName, $conditions, $values);
//Process each entry
$responses = array();
foreach($entries as $entry)
$responses[] = $this::dbToSurveyResponse($entry);
return $responses;
}
/**
* Delete the survey associated to a post
*
@ -339,6 +361,26 @@ class SurveyComponent {
return $result[0]["ID_sondage_choix"];
}
/**
* Turn a database entry into SurveyResponse object
*
* @param array $entry The entry in the database
* @return SurveyResponse Generated object
*/
private static function dbToSurveyResponse(array $entry) : SurveyResponse {
$response = new SurveyResponse();
$response->set_id($entry["ID"]);
$response->set_time_sent(strtotime($entry["date_envoi"]));
$response->set_userID($entry["ID_utilisateurs"]);
$response->set_surveyID($entry["ID_sondage"]);
$response->set_choiceID($entry["ID_sondage_choix"]);
return $response;
}
}
//Register component

View File

@ -0,0 +1,40 @@
<?php
/**
* Survey response object
*
* @author Pierre HUBERT
*/
class SurveyResponse extends BaseUniqueObjectFromUser {
//Private fields
private $surveyID;
private $choiceID;
//Set and get survey ID
public function set_surveyID(int $surveyID){
$this->surveyID = $surveyID;
}
public function has_surveyID() : bool {
return $this->surveyID > 0;
}
public function get_surveyID() : int {
return $this->surveyID;
}
//Set and get choice ID
public function set_choiceID(int $choiceID){
$this->choiceID = $choiceID;
}
public function has_choiceID() : bool {
return $this->choiceID > 0;
}
public function get_choiceID() : int {
return $this->choiceID;
}
}