From f4620717c221d7c88ab953775ac65226ef72f10f Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 7 Jan 2018 19:06:01 +0100 Subject: [PATCH] Can create survey --- classes/components/posts.php | 4 +-- classes/components/survey.php | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/classes/components/posts.php b/classes/components/posts.php index a61479d..a5b309f 100644 --- a/classes/components/posts.php +++ b/classes/components/posts.php @@ -249,7 +249,7 @@ class Posts { * * @param string $kind_page The kind of target page * @param int $kind_page_id The ID of the target page - * @param int $userID The ID of the user creating for post + * @param int $userID The ID of the user creating the post * @param string $content The content of the post * @param int $visibility The visibility of the post * @param string $kind The kind of post @@ -338,7 +338,7 @@ class Posts { //Create the survey if required if($kind == $this::POST_KIND_SURVEY){ - + CS::get()->components->survey->create($postID, $userID, $survey_question, $survey_answers); } return $postID; diff --git a/classes/components/survey.php b/classes/components/survey.php index b955031..743171f 100644 --- a/classes/components/survey.php +++ b/classes/components/survey.php @@ -22,6 +22,54 @@ class Survey { */ const SURVEY_RESPONSE_TABLE = "sondage_reponse"; + /** + * Create a new survey + * + * @param int $postID The target post for the survey + * @param int $userID The ID of the user creating the survey + * @param string $question The question for the survey + * @param array $answers The answers for the survey + * @return bool True for a sucess / False for a failure + */ + public function create(int $postID, int $userID, string $question, array $answers) : bool { + + //Create the main survey informations + $main_infos = array( + "ID_utilisateurs" => $userID, + "ID_texte" => $postID, + "date_creation" => mysql_date(), + "question" => $question + ); + + //Try to create survey main informations table + if(!CS::get()->db->addLine($this::SURVEY_INFOS_TABLE, $main_infos)) + return false; + + //Get the ID of the survey + $surveyID = CS::get()->db->getLastInsertedID(); + + //Check for errors + if($surveyID < 1) + return false; + + //Process each answer + $answers_data = array(); + foreach($answers as $line){ + $answers_data[] = array( + "ID_sondage" => $surveyID, + "date_creation" => mysql_date(), + "Choix" => $line + ); + } + + //Insert all the answers + CS::get()->db->addLines($this::SURVEY_CHOICES_TABLE, $answers_data); + + //Success + return true; + + } + /** * Get informations about a survey *