diff --git a/assets/js/components/posts/interface.js b/assets/js/components/posts/interface.js index c2028f34..a3a2b1a8 100644 --- a/assets/js/components/posts/interface.js +++ b/assets/js/components/posts/interface.js @@ -127,6 +127,26 @@ ComunicWeb.components.posts.interface = { }, + /** + * Send a response to a survey + * + * @param {int} postID The ID of the target post + * @param {int} reponseID The ID of the selected response + * @param {function} callback This function is called when we got a response + */ + survey_send_response: function(postID, responseID, callback){ + + //Prepare an API request + apiURI = "surveys/send_response"; + params = { + postID: postID, + responseID: responseID + }; + + //Perform the request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + }, + /** * Cancel a response to a survey * diff --git a/assets/js/components/posts/ui.js b/assets/js/components/posts/ui.js index d627fe04..8d8d10b6 100644 --- a/assets/js/components/posts/ui.js +++ b/assets/js/components/posts/ui.js @@ -629,6 +629,77 @@ ComunicWeb.components.posts.ui = { } } + else { + + //Offer the user the possibility to respond the survey + var surveyResponseForm = createElem2({ + appendTo: surveyResponse, + type: "div", + class: "input-group" + }); + + //Create response chooser + var surveyResponseChooser = createElem2({ + appendTo: surveyResponseForm, + type: "select", + class: "form-control" + }); + + //Display options + for(j in survey_choices){ + + //Create an element for the choice + option = createElem2({ + appendTo: surveyResponseChooser, + type: "option", + innerHTML: survey_choices[j].name, + value: survey_choices[j].choiceID, + }); + + } + + //Add confirm button + var chooseButtonSpan = createElem2({ + appendTo: surveyResponseForm, + type: "span", + class: "input-group-btn" + }); + + var chooseButton = createElem2({ + appendTo: chooseButtonSpan, + type: "button", + class: "btn btn-default", + innerHTML: "Send" + }); + + //Make confirm button lives + chooseButton.onclick = function(){ + + //Get selected answer ID + var response_id = surveyResponseChooser.value; + + //Lock send button + chooseButton.disabled = true; + + //Perform a request on the server + ComunicWeb.components.posts.interface.survey_send_response(infos.ID, response_id, function(response){ + + //Unlock button + chooseButton.disabled = false; + + //Check for errors + if(response.error){ + ComunicWeb.common.notificationSystem.showNotification("Could send response to survey !", "danger"); + return; + } + + //Reload post + ComunicWeb.components.posts.actions.reload_post(infos.ID, postRoot); + + + }); + } + } } }