mirror of
				https://github.com/pierre42100/ComunicWeb
				synced 2025-11-03 19:54:14 +00:00 
			
		
		
		
	Can create a new choice for a survey
This commit is contained in:
		@@ -305,6 +305,25 @@ async function showConfirmDialog(msg) {
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Ask the user to enter a string
 | 
			
		||||
 * 
 | 
			
		||||
 * @param {String} title The dialog of the dialog to show
 | 
			
		||||
 * @param {String} message Helper message to show to the user
 | 
			
		||||
 * @param {String} defaultValue The default value of the message
 | 
			
		||||
 */
 | 
			
		||||
async function showInputTextDialog(title, message, defaultValue = "") {
 | 
			
		||||
    return new Promise((res, rej) => 
 | 
			
		||||
        ComunicWeb.common.messages.inputString(
 | 
			
		||||
            title,
 | 
			
		||||
            message,
 | 
			
		||||
            defaultValue,
 | 
			
		||||
            (msg) => msg === false ? rej : res(msg)
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Prepare for potential future translation system
 | 
			
		||||
 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -229,6 +229,19 @@ const PostsInterface = {
 | 
			
		||||
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a new choice for this survey
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param {number} postID The ID of the associated post
 | 
			
		||||
	 * @param {String} choice The value of the choice
 | 
			
		||||
	 */
 | 
			
		||||
	createSurveyChoice: async function(postID, choice) {
 | 
			
		||||
		await api("survey/create_new_choice", {
 | 
			
		||||
			postID: postID,
 | 
			
		||||
			choice: choice
 | 
			
		||||
		}, true);
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Prevent new choices from being created
 | 
			
		||||
	 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -881,6 +881,38 @@ ComunicWeb.components.posts.ui = {
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
				// Offer the user to create a new response, if possible
 | 
			
		||||
				if(info.data_survey.user_choice == 0 && info.data_survey.allowNewChoices) {
 | 
			
		||||
 | 
			
		||||
					const link = createElem2({
 | 
			
		||||
						appendTo: surveyResponse,
 | 
			
		||||
						type: "div",
 | 
			
		||||
						class: "a txt-center",
 | 
			
		||||
						innerHTML: tr("Add a new choice to this survey")
 | 
			
		||||
					});
 | 
			
		||||
 | 
			
		||||
					link.addEventListener("click", async () => {
 | 
			
		||||
 | 
			
		||||
						try {
 | 
			
		||||
							const choice = await showInputTextDialog(
 | 
			
		||||
								tr("Create a choice"),
 | 
			
		||||
								tr("Please specify the new choice you would like to create for this survey:"),
 | 
			
		||||
								""
 | 
			
		||||
							);
 | 
			
		||||
 | 
			
		||||
							await PostsInterface.createSurveyChoice(info.ID, choice);
 | 
			
		||||
 | 
			
		||||
							//Reload post
 | 
			
		||||
							ComunicWeb.components.posts.actions.reload_post(info.ID, postRoot);
 | 
			
		||||
 | 
			
		||||
						} catch(e) {
 | 
			
		||||
							console.error(e);
 | 
			
		||||
							notify(tr("Could not create a new choice for this survey!"), "danger");
 | 
			
		||||
						}
 | 
			
		||||
					});
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
				// If the user is the owner of the survey, offer him to close it
 | 
			
		||||
				if(info.data_survey.userID == userID() && info.data_survey.allowNewChoices) {
 | 
			
		||||
 | 
			
		||||
@@ -903,6 +935,7 @@ ComunicWeb.components.posts.ui = {
 | 
			
		||||
							ComunicWeb.components.posts.actions.reload_post(info.ID, postRoot);
 | 
			
		||||
 | 
			
		||||
						} catch(e) {
 | 
			
		||||
							console.error(e);
 | 
			
		||||
							notify(tr("Could not block new choices from being created!"), "danger");
 | 
			
		||||
						}
 | 
			
		||||
					});
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user