mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 15:59:29 +00:00
Upgraded surveys creation
This commit is contained in:
parent
ddfe2f4e8e
commit
ae7dd0bed3
@ -7,7 +7,7 @@
|
|||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class postsController {
|
class PostsController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visibility levels for the API
|
* Visibility levels for the API
|
||||||
@ -296,9 +296,14 @@ class postsController {
|
|||||||
if(count($answers) < 2)
|
if(count($answers) < 2)
|
||||||
Rest_fatal_error(400, "Please specify at least two valid answers for the survey !");
|
Rest_fatal_error(400, "Please specify at least two valid answers for the survey !");
|
||||||
|
|
||||||
//Save informations about the survey
|
//Save information about the survey
|
||||||
$survey_question = removeHTMLnodes($question);
|
$survey = new Survey();
|
||||||
$survey_answers = $answers;
|
$survey->set_question(removeHTMLnodes($question));
|
||||||
|
foreach($answers as $name){
|
||||||
|
$choice = new SurveyChoice();
|
||||||
|
$choice->set_name($name);
|
||||||
|
$survey->add_choice($choice);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//The content type is not supported
|
//The content type is not supported
|
||||||
@ -338,8 +343,7 @@ class postsController {
|
|||||||
isset($link_image) ? $link_image : null,
|
isset($link_image) ? $link_image : null,
|
||||||
|
|
||||||
//For survey posts
|
//For survey posts
|
||||||
isset($survey_question) ? $survey_question : null,
|
isset($survey) ? $survey : null
|
||||||
isset($survey_answers) ? $survey_answers : array()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
@ -25,24 +25,21 @@ class SurveyComponent {
|
|||||||
/**
|
/**
|
||||||
* Create a new survey
|
* Create a new survey
|
||||||
*
|
*
|
||||||
* @param int $postID The target post for the survey
|
* @param Survey $survey Information about the survey to create
|
||||||
* @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
|
* @return bool True for a sucess / False for a failure
|
||||||
*/
|
*/
|
||||||
public function create(int $postID, int $userID, string $question, array $answers) : bool {
|
public function create(Survey $survey) : bool {
|
||||||
|
|
||||||
//Create the main survey informations
|
//Create the main survey informations
|
||||||
$main_infos = array(
|
$main_info = array(
|
||||||
"ID_utilisateurs" => $userID,
|
"ID_utilisateurs" => $survey->get_userID(),
|
||||||
"ID_texte" => $postID,
|
"ID_texte" => $survey->get_postID(),
|
||||||
"date_creation" => mysql_date(),
|
"date_creation" => mysql_date(),
|
||||||
"question" => $question
|
"question" => $survey->get_question()
|
||||||
);
|
);
|
||||||
|
|
||||||
//Try to create survey main informations table
|
//Try to create survey main informations table
|
||||||
if(!CS::get()->db->addLine($this::SURVEY_INFOS_TABLE, $main_infos))
|
if(!CS::get()->db->addLine($this::SURVEY_INFOS_TABLE, $main_info))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//Get the ID of the survey
|
//Get the ID of the survey
|
||||||
@ -54,11 +51,11 @@ class SurveyComponent {
|
|||||||
|
|
||||||
//Process each answer
|
//Process each answer
|
||||||
$answers_data = array();
|
$answers_data = array();
|
||||||
foreach($answers as $line){
|
foreach($survey->get_choices() as $choice){
|
||||||
$answers_data[] = array(
|
$answers_data[] = array(
|
||||||
"ID_sondage" => $surveyID,
|
"ID_sondage" => $surveyID,
|
||||||
"date_creation" => mysql_date(),
|
"date_creation" => mysql_date(),
|
||||||
"Choix" => $line
|
"Choix" => $choice->get_name()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +76,7 @@ class SurveyComponent {
|
|||||||
public function get_infos(int $postID) : Survey {
|
public function get_infos(int $postID) : Survey {
|
||||||
|
|
||||||
//Get informations about the survey
|
//Get informations about the survey
|
||||||
$survey = $this->get_survey_infos($postID);
|
$survey = $this->get_survey_info($postID);
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(!$survey->isValid())
|
if(!$survey->isValid())
|
||||||
@ -214,7 +211,7 @@ class SurveyComponent {
|
|||||||
* @return Survey Information about the survey, or an invalid object 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) : Survey {
|
private function get_survey_info(int $postID) : Survey {
|
||||||
|
|
||||||
//Fetch the database
|
//Fetch the database
|
||||||
$conditions = "WHERE ID_texte = ?";
|
$conditions = "WHERE ID_texte = ?";
|
||||||
|
@ -348,16 +348,14 @@ class Posts {
|
|||||||
* @param string $link_title The title of the link associated with the post (if any)
|
* @param string $link_title The title of the link associated with the post (if any)
|
||||||
* @param string $link_description The description of the link associated with the post (if any)
|
* @param string $link_description The description of the link associated with the post (if any)
|
||||||
* @param string $link_image The link pointing on the image associated with the post (if any)
|
* @param string $link_image The link pointing on the image associated with the post (if any)
|
||||||
* @param string $survey_question The question of the survey associated with the post (if any)
|
* @param Survey $survey Information about a related survey (if any)
|
||||||
* @param array $survey_answers The answers of the survey associated with the post (if any)
|
|
||||||
* @return int The ID of the created post or -1 in case of failure
|
* @return int The ID of the created post or -1 in case of failure
|
||||||
*/
|
*/
|
||||||
public function create(string $kind_page, int $kind_page_id, int $userID, string $content,
|
public function create(string $kind_page, int $kind_page_id, int $userID, string $content,
|
||||||
int $visibility, string $kind, int $file_size = 0,
|
int $visibility, string $kind, int $file_size = 0,
|
||||||
string $file_type = null, string $file_path = null, int $videoID = 0,
|
string $file_type = null, string $file_path = null, int $videoID = 0,
|
||||||
int $time_end = 0, string $link_url = null, string $link_title = null,
|
int $time_end = 0, string $link_url = null, string $link_title = null,
|
||||||
string $link_description = null, string $link_image = null, string $survey_question = null,
|
string $link_description = null, string $link_image = null, Survey $survey = null) : int {
|
||||||
array $survey_answers = array()) : int {
|
|
||||||
|
|
||||||
//Generate new post informations
|
//Generate new post informations
|
||||||
//Get the corresponding kind of post for the database
|
//Get the corresponding kind of post for the database
|
||||||
@ -424,7 +422,10 @@ class Posts {
|
|||||||
|
|
||||||
//Create the survey if required
|
//Create the survey if required
|
||||||
if($kind == $this::POST_KIND_SURVEY){
|
if($kind == $this::POST_KIND_SURVEY){
|
||||||
CS::get()->components->survey->create($postID, $userID, $survey_question, $survey_answers);
|
//Complete the request
|
||||||
|
$survey->set_postID($postID);
|
||||||
|
$survey->set_userID($userID);
|
||||||
|
components()->survey->create($survey);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $postID;
|
return $postID;
|
||||||
|
Loading…
Reference in New Issue
Block a user