Implemented Survey Object

This commit is contained in:
Pierre 2018-04-21 20:24:05 +02:00
parent b00e401ada
commit 23d1761bef
3 changed files with 47 additions and 25 deletions

View File

@ -7,7 +7,7 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
class surveysController { class SurveysController {
/** /**
* Cancel a response to a survey * Cancel a response to a survey
@ -87,4 +87,25 @@ class surveysController {
} }
/**
* Parse a survey object into a valid API entry
*
* @param Survey $survey The survey object to convert
* @return array Generated API entry
*/
public static function SurveyToAPI(Survey $survey) : array {
$data = array();
$data["ID"] = $survey->get_id();
$data["userID"] = $survey->get_userID();
$data["postID"] = $survey->get_postID();
$data["creation_time"] = $survey->get_time_sent();
$data["question"] = $survey->get_question();
$data["user_choice"] = $survey->get_user_choice();
$data["choices"] = $survey->get_choices();
return $data;
}
} }

View File

@ -528,6 +528,10 @@ class postsController {
//Update visibility level //Update visibility level
$infos['visibility_level'] = $this::VISIBILITY_LEVELS_API[$infos['visibility_level']]; $infos['visibility_level'] = $this::VISIBILITY_LEVELS_API[$infos['visibility_level']];
//Turn survey into API entry (if any)
if($infos["data_survey"] != null)
$infos["data_survey"] = SurveysController::SurveyToAPI($infos["data_survey"]);
//Parse comments if required //Parse comments if required
if(isset($infos['comments'])){ if(isset($infos['comments'])){
if($infos['comments'] != null){ if($infos['comments'] != null){

View File

@ -74,24 +74,22 @@ class SurveyComponent {
* Get informations about a survey * Get informations about a survey
* *
* @param int $postID The ID of the post related to the survey * @param int $postID The ID of the post related to the survey
* @return array Informations about the survey / empty array in case of failure * @return Survey Informations about the survey / invalid Survey in case of failure
*/ */
public function get_infos(int $postID) : array { public function get_infos(int $postID) : Survey {
$survey = array();
//Get informations about the survey //Get informations about the survey
$survey = $this->get_survey_infos($postID); $survey = $this->get_survey_infos($postID);
//Check for errors //Check for errors
if(count($survey) == 0) if(!$survey->isValid())
return array(); return new Survey();
//Get the choice of the user //Get the choice of the user
$survey['user_choice'] = user_signed_in() ? $this->get_user_choice($survey['ID'], userID) : 0; $survey->set_user_choice(user_signed_in() ? $this->get_user_choice($survey->get_id(), userID) : 0);
//Get the choices of the survey //Get the choices of the survey
$survey['choices'] = $this->get_survey_choices($survey['ID']); $survey->set_choices($this->get_survey_choices($survey->get_id()));
return $survey; return $survey;
} }
@ -213,10 +211,10 @@ class SurveyComponent {
* Get survey informations by post ID * Get survey informations by post ID
* *
* @param int $postID The DI of the related post * @param int $postID The DI of the related post
* @return array Informations about the survey, or an empty array in case * @return Survey Information about the survey, or an invalid object in case
* of failure * of failure
*/ */
private function get_survey_infos(int $postID) : array { private function get_survey_infos(int $postID) : Survey {
//Fetch the database //Fetch the database
$conditions = "WHERE ID_texte = ?"; $conditions = "WHERE ID_texte = ?";
@ -225,30 +223,29 @@ class SurveyComponent {
$result = CS::get()->db->select($this::SURVEY_INFOS_TABLE, $conditions, $condVals); $result = CS::get()->db->select($this::SURVEY_INFOS_TABLE, $conditions, $condVals);
if(count($result) == 0) if(count($result) == 0)
return array(); return new Survey();
else else
return $this->parse_survey_infos($result[0]); return $this->dbToSurvey($result[0]);
} }
/** /**
* Parse survey informations from database * Turn survey entry from the database into Survey object
* *
* @param array $db_infos Informations from the database * @param array $data Data from the database
* @return array Informations about the post * @return Survey Generated survey object
*/ */
private function parse_survey_infos(array $db_infos) : array { private function dbToSurvey(array $data) : Survey {
$infos = array(); $survey = new Survey();
//Get informations $survey->set_id($data['ID']);
$infos['ID'] = $db_infos['ID']; $survey->set_userID($data["ID_utilisateurs"]);
$infos['userID'] = $db_infos['ID_utilisateurs']; $survey->set_postID($data["ID_texte"]);
$infos['postID'] = $db_infos['ID_texte']; $survey->set_time_sent(strtotime($data['date_creation']));
$infos['creation_time'] = strtotime($db_infos['date_creation']); $survey->set_question($data['question']);
$infos['question'] = $db_infos['question'];
return $infos; return $survey;
} }
/** /**