[ADD] survey_dropdown_simple_choice
Display "Multiple choice: only one answer" (simple_choice) survey questions as a dropdown list instead of radio buttons, per question. The native radios are kept (hidden by CSS) and still carry the answer: the dropdown only clicks them, so validation, conditional questions, "Other" comment, auto-submit and AJAX submit are handled natively. In review / print mode, only the selected and correct answers are displayed. Native rendering is kept when suggested answers contain images. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
23
survey_dropdown_simple_choice/README.md
Normal file
23
survey_dropdown_simple_choice/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# survey_dropdown_simple_choice
|
||||
|
||||
Ajoute une option « Afficher en liste déroulante » aux questions
|
||||
« Choix multiple : une seule réponse » des sondages : les réponses sont
|
||||
proposées dans une liste déroulante au lieu de boutons radio.
|
||||
|
||||
Pratique pour les longues listes (pays, départements…).
|
||||
|
||||
## Utilisation
|
||||
|
||||
Dans le sondage, éditez la question → onglet **Options** → cochez
|
||||
**« Afficher en liste déroulante »**.
|
||||
|
||||
L'option « Autre » (commentaire) et les questions conditionnelles
|
||||
fonctionnent comme d'habitude.
|
||||
|
||||
## Limites
|
||||
|
||||
- Si une réponse proposée contient une image, la question reste affichée
|
||||
en boutons radio.
|
||||
- À l'impression et dans la revue des réponses (« Consulter vos réponses »),
|
||||
seule la réponse choisie est affichée, ainsi que la bonne réponse pour
|
||||
un sondage noté.
|
||||
1
survey_dropdown_simple_choice/__init__.py
Normal file
1
survey_dropdown_simple_choice/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import models
|
||||
27
survey_dropdown_simple_choice/__manifest__.py
Normal file
27
survey_dropdown_simple_choice/__manifest__.py
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "Survey Dropdown Simple Choice",
|
||||
"summary": "Display some single choice survey questions as a dropdown list",
|
||||
"description": "Adds a boolean on survey.question to display a "
|
||||
"\"Multiple choice: only one answer\" question as a dropdown list "
|
||||
"instead of radio buttons, with support for conditional questions "
|
||||
"and \"Other\" comments.",
|
||||
"author": "Elabore",
|
||||
"website": "https://www.elabore.coop",
|
||||
"category": "Marketing/Surveys",
|
||||
"license": "AGPL-3",
|
||||
"version": "16.0.1.0.0",
|
||||
"depends": ["survey"],
|
||||
"data": [
|
||||
"views/survey_question_views.xml",
|
||||
"views/survey_templates.xml",
|
||||
],
|
||||
"assets": {
|
||||
"survey.survey_assets": [
|
||||
"survey_dropdown_simple_choice/static/src/scss/survey_dropdown.scss",
|
||||
"survey_dropdown_simple_choice/static/src/js/survey_form_dropdown.js",
|
||||
],
|
||||
},
|
||||
"installable": True,
|
||||
"application": False,
|
||||
"auto_install": True,
|
||||
}
|
||||
41
survey_dropdown_simple_choice/i18n/fr.po
Normal file
41
survey_dropdown_simple_choice/i18n/fr.po
Normal file
@@ -0,0 +1,41 @@
|
||||
# Translation of Odoo Server.
|
||||
# This file contains the translation of the following modules:
|
||||
# * survey_dropdown_simple_choice
|
||||
#
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: Odoo Server 16.0\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-10-20 17:08+0000\n"
|
||||
"PO-Revision-Date: 2026-10-20 17:08+0000\n"
|
||||
"Last-Translator: \n"
|
||||
"Language-Team: \n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: \n"
|
||||
"Plural-Forms: \n"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,field_description:survey_dropdown_simple_choice.field_survey_question__display_dropdown
|
||||
msgid "Display as dropdown list"
|
||||
msgstr "Afficher en liste déroulante"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,help:survey_dropdown_simple_choice.field_survey_question__display_dropdown
|
||||
msgid ""
|
||||
"Display this single choice question as a dropdown list instead of radio "
|
||||
"buttons. Ignored if the suggested answers contain images."
|
||||
msgstr ""
|
||||
"Affiche cette question à choix unique sous la forme d'une liste déroulante "
|
||||
"au lieu de boutons radio. Ignoré si les réponses proposées contiennent des "
|
||||
"images."
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model.fields,field_description:survey_dropdown_simple_choice.field_survey_question__smart_search
|
||||
msgid "Smart Search"
|
||||
msgstr "Recherche intelligente"
|
||||
|
||||
#. module: survey_dropdown_simple_choice
|
||||
#: model:ir.model,name:survey_dropdown_simple_choice.model_survey_question
|
||||
msgid "Survey Question"
|
||||
msgstr "Question du sondage"
|
||||
1
survey_dropdown_simple_choice/models/__init__.py
Normal file
1
survey_dropdown_simple_choice/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import survey_question
|
||||
11
survey_dropdown_simple_choice/models/survey_question.py
Normal file
11
survey_dropdown_simple_choice/models/survey_question.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class SurveyQuestion(models.Model):
|
||||
_inherit = 'survey.question'
|
||||
|
||||
display_dropdown = fields.Boolean(
|
||||
string="Display as dropdown list",
|
||||
help="Display this single choice question as a dropdown list instead "
|
||||
"of radio buttons. Ignored if the suggested answers contain images.",
|
||||
)
|
||||
@@ -0,0 +1,55 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import SurveyFormWidget from "survey.form";
|
||||
|
||||
// The dropdown only drives the native radios (hidden by CSS): choosing an
|
||||
// option clicks the matching radio, so validation, conditional questions,
|
||||
// "Other" comment, auto-submit and AJAX submit are all handled natively.
|
||||
SurveyFormWidget.include({
|
||||
events: Object.assign({}, SurveyFormWidget.prototype.events, {
|
||||
'change select.o_survey_dropdown_choice': '_onDropdownChoiceChange',
|
||||
'change [data-survey-dropdown="edit"] .o_survey_form_choice_item': '_onDropdownRadioChange',
|
||||
}),
|
||||
|
||||
/**
|
||||
* A focused dropdown is handled like a text input: its keys (letters,
|
||||
* arrows, enter) must not trigger the survey shortcuts.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_onKeyDown: function (event) {
|
||||
if (event.target.matches?.('select.o_survey_dropdown_choice')
|
||||
&& !((event.ctrlKey || event.metaKey) && event.key === 'Enter')) {
|
||||
return;
|
||||
}
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* Click the radio of the chosen option, or the checked one to untick it
|
||||
* when the empty option is chosen (native untick behavior).
|
||||
*/
|
||||
_onDropdownChoiceChange: function (ev) {
|
||||
if (this.readonly) {
|
||||
return;
|
||||
}
|
||||
const select = ev.currentTarget;
|
||||
const group = select.closest('.o_survey_form_choice');
|
||||
const radio = select.value
|
||||
? group.querySelector(`.o_survey_form_choice_item[value="${select.value}"]`)
|
||||
: group.querySelector('.o_survey_form_choice_item:checked');
|
||||
if (radio && !(select.value && radio.checked)) {
|
||||
radio.click();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Keep the dropdown in sync when a radio changes by other means
|
||||
* (A-Z keyboard shortcuts).
|
||||
*/
|
||||
_onDropdownRadioChange: function (ev) {
|
||||
const group = ev.currentTarget.closest('.o_survey_form_choice');
|
||||
const checked = group.querySelector('.o_survey_form_choice_item:checked');
|
||||
group.querySelector('select.o_survey_dropdown_choice').value = checked ? checked.value : '';
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
.o_survey_form {
|
||||
select.o_survey_dropdown_choice {
|
||||
min-height: 44px;
|
||||
|
||||
option {
|
||||
color: initial;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Native radios still carry the answer but are replaced by the dropdown
|
||||
.o_survey_form_choice[data-survey-dropdown="edit"] .o_survey_choice_btn {
|
||||
display: none;
|
||||
}
|
||||
|
||||
// Review / print: only show the selected and correct answers
|
||||
.o_survey_form_choice[data-survey-dropdown="review"] .o_survey_choice_btn:not(.o_survey_selected):not(.bg-success) {
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="survey_question_form_dropdown" model="ir.ui.view">
|
||||
<field name="name">survey.question.form.dropdown</field>
|
||||
<field name="model">survey.question</field>
|
||||
<field name="inherit_id" ref="survey.survey_question_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='comments_allowed']" position="before">
|
||||
<field name="display_dropdown" attrs="{'invisible': [('question_type', '!=', 'simple_choice')]}"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
33
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
33
survey_dropdown_simple_choice/views/survey_templates.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<!-- Add a dropdown on top of the native radios when the display_dropdown
|
||||
flag is set. The radios are kept (hidden by CSS) and still carry the
|
||||
answer: the dropdown only clicks them (see survey_form_dropdown.js).
|
||||
In readonly mode (review / print), the radios are shown but only the
|
||||
selected and correct answers are kept visible (data-survey-dropdown
|
||||
"review"). The native rendering is kept when a suggestion carries an
|
||||
image. -->
|
||||
<template id="question_simple_choice" inherit_id="survey.question_simple_choice" name="Question: simple choice (dropdown)">
|
||||
<xpath expr="//t[@t-set='comment_line']" position="after">
|
||||
<t t-set="dropdown_enabled" t-value="question.display_dropdown and not any(a.value_image for a in question.suggested_answer_ids)"/>
|
||||
<t t-set="use_dropdown" t-value="dropdown_enabled and not survey_form_readonly"/>
|
||||
</xpath>
|
||||
<xpath expr="//div[hasclass('o_survey_form_choice')]" position="attributes">
|
||||
<attribute name="t-att-data-survey-dropdown">('review' if survey_form_readonly else 'edit') if dropdown_enabled else None</attribute>
|
||||
</xpath>
|
||||
<xpath expr="//div[hasclass('o_survey_form_choice')]/t[@t-set='item_idx']" position="after">
|
||||
<div t-if="use_dropdown" class="col-lg-12 mb-2 o_survey_dropdown_select">
|
||||
<select class="form-select o_survey_dropdown_choice" t-att-aria-label="question.title">
|
||||
<option value=""/>
|
||||
<option t-foreach="question.suggested_answer_ids" t-as="label"
|
||||
t-att-value="label.id"
|
||||
t-att-selected="'selected' if answer_line.suggested_answer_id == label else None"
|
||||
t-out="label.value"/>
|
||||
<option t-if="question.comments_allowed and question.comment_count_as_answer" value="-1"
|
||||
t-att-selected="'selected' if comment_line else None"
|
||||
t-out="question.comments_message or default_comments_message"/>
|
||||
</select>
|
||||
</div>
|
||||
</xpath>
|
||||
</template>
|
||||
</odoo>
|
||||
Reference in New Issue
Block a user