From 22f8ab582c0e57dde4a24be9201205aa1caf3f61 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 22 Apr 2018 08:31:34 +0200 Subject: [PATCH] Implemented SurveyChoice object --- RestControllers/SurveysController.php | 25 ++++++++++++++++++++++++- classes/components/SurveyComponent.php | 18 +++++++++--------- classes/models/Survey.php | 2 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/RestControllers/SurveysController.php b/RestControllers/SurveysController.php index a6aee29..e1f35f8 100644 --- a/RestControllers/SurveysController.php +++ b/RestControllers/SurveysController.php @@ -103,9 +103,32 @@ class SurveysController { $data["creation_time"] = $survey->get_time_sent(); $data["question"] = $survey->get_question(); $data["user_choice"] = $survey->get_user_choice(); - $data["choices"] = $survey->get_choices(); + + //Process survey choices + $data["choices"] = array(); + + foreach($survey->get_choices() as $choice) + $data["choices"][$choice->get_id()] = self::SurveyChoiceToAPI($choice); return $data; } + /** + * Turn a SurveyChoice object into an API array + * + * @param SurveyChoice $choice The choice to convert + * @return array Generated array + */ + public static function SurveyChoiceToAPI(SurveyChoice $choice) : array { + + $data = array(); + + $data["choiceID"] = $choice->get_id(); + $data["name"] = $choice->get_name(); + $data["responses"] = $choice->get_responses(); + + return $data; + + } + } \ No newline at end of file diff --git a/classes/components/SurveyComponent.php b/classes/components/SurveyComponent.php index 23531ec..dcc7096 100644 --- a/classes/components/SurveyComponent.php +++ b/classes/components/SurveyComponent.php @@ -252,7 +252,7 @@ class SurveyComponent { * Get survey choices * * @param int $surveyID The ID of the target survey - * @return array Informations about the choices of the survey + * @return array Informations about the choices of the survey (as Survey objects) */ private function get_survey_choices(int $surveyID) : array { @@ -266,8 +266,8 @@ class SurveyComponent { //Parse results $choices = array(); foreach($result as $row){ - $choice_infos = $this->parse_survey_choices($row); - $choices[$choice_infos["choiceID"]] = $choice_infos; + $choice_infos = $this->dbToChoice($row); + $choices[$choice_infos->get_id()] = $choice_infos; } return $choices; @@ -278,15 +278,15 @@ class SurveyComponent { * Parse survey choices from database * * @param array $db_infos Informations about the choice - * @return array Usable informations about the surveay + * @return SurveyChoice Usable informations about the survey */ - private function parse_survey_choices(array $db_infos) : array { + private function dbToChoice(array $db_infos) : SurveyChoice { - $infos = array(); + $infos = new SurveyChoice(); - $infos['choiceID'] = $db_infos['ID']; - $infos['name'] = $db_infos['Choix']; - $infos['responses'] = $this->count_choices_responses($infos['choiceID']); + $infos->set_id($db_infos['ID']); + $infos->set_name($db_infos['Choix']); + $infos->set_responses($this->count_choices_responses($infos->get_id())); return $infos; diff --git a/classes/models/Survey.php b/classes/models/Survey.php index 5855967..56fec7d 100644 --- a/classes/models/Survey.php +++ b/classes/models/Survey.php @@ -67,7 +67,7 @@ class Survey extends BaseUniqueObjectFromUser { $this->choices = $choices; } - public function add_choice(array $choice){ + public function add_choice(SurveyChoice $choice){ $this->choices[] = $choice; }