From 512f252c6743d8d88ad87440737fb2bd904fc078 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 13 May 2018 19:16:10 +0200 Subject: [PATCH] Can get all the response of a user to a survey into a SurveyResponse object --- RestControllers/SurveysController.php | 20 ++++++++++++ classes/components/SurveyComponent.php | 42 ++++++++++++++++++++++++++ classes/models/SurveyResponse.php | 40 ++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 classes/models/SurveyResponse.php diff --git a/RestControllers/SurveysController.php b/RestControllers/SurveysController.php index e1f35f8..7cbded6 100644 --- a/RestControllers/SurveysController.php +++ b/RestControllers/SurveysController.php @@ -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; + + } + } \ No newline at end of file diff --git a/classes/components/SurveyComponent.php b/classes/components/SurveyComponent.php index dd37240..7eaceea 100644 --- a/classes/components/SurveyComponent.php +++ b/classes/components/SurveyComponent.php @@ -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 diff --git a/classes/models/SurveyResponse.php b/classes/models/SurveyResponse.php new file mode 100644 index 0000000..ff97ee5 --- /dev/null +++ b/classes/models/SurveyResponse.php @@ -0,0 +1,40 @@ +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; + } + +} \ No newline at end of file