[ADD] survey_dropdown_simple_choice
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m43s
tests / test (pull_request) Successful in 1m19s

Display "Multiple choice: only one answer" (simple_choice) survey
questions as a dropdown list instead of radio buttons, per question.

The select is mirrored on hidden radios so that the native survey form
pipeline (validation, conditional questions, "Other" comment, AJAX
submit, correct answers display) keeps working. Native radio rendering
is kept when suggested answers contain images and in readonly mode.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-20 19:31:28 +02:00
parent 2835e2d2be
commit e6ec9a0ced
10 changed files with 306 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
# 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, la question est affichée
en boutons radio.
- Le raccourci clavier A-Z (mise en page « Une question par page ») ne
fonctionne pas sur ces questions.

View File

@@ -0,0 +1 @@
from . import models

View File

@@ -0,0 +1,27 @@
{
"name": "Survey Dropdown 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": "18.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,
}

View 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 18.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"

View File

@@ -0,0 +1 @@
from . import survey_question

View 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.",
)

View File

@@ -0,0 +1,83 @@
/** @odoo-module **/
import SurveyFormWidget from "@survey/js/survey_form";
// Keep native survey answer pipeline intact (validation, conditional
// questions, AJAX submit): the dropdown change is mapped onto the hidden
// mirror radios that survey_form.js knows about.
SurveyFormWidget.include({
start: function () {
const self = this;
return this._super.apply(this, arguments).then(function () {
if (!self.readonly) {
self.el.addEventListener('change', self._onDropdownChoiceChange.bind(self));
}
});
},
/**
* Arrow left/right on a focused dropdown must not navigate between pages.
*
* @override
*/
_onKeyDown: function (event) {
if (event.target.matches?.('select.o_survey_dropdown_choice')
&& ['ArrowLeft', 'ArrowRight'].includes(event.key)) {
return;
}
return this._super.apply(this, arguments);
},
/**
* Correct answers are colored on the mirror radios: show them instead of
* the dropdown so the participant sees the correction.
*
* @override
*/
_showQuestionAnswer: function (correctAnswers, questionId) {
this._super.apply(this, arguments);
const questionWrapper = this.el.querySelector(`.js_question-wrapper[id="${questionId}"]`);
const select = questionWrapper && questionWrapper.querySelector('select.o_survey_dropdown_choice');
if (select) {
select.disabled = true;
questionWrapper.querySelector('.o_survey_dropdown_select').classList.add('d-none');
questionWrapper.querySelector('.o_survey_dropdown_mirror').classList.remove('d-none');
}
},
_onDropdownChoiceChange: function (ev) {
const select = ev.target;
if (!select.classList || !select.classList.contains('o_survey_dropdown_choice')) {
return;
}
const group = select.closest('.o_survey_form_choice');
if (!group || this.readonly) {
return;
}
const previous = group.querySelector('input.o_survey_form_choice_item:checked');
group.querySelectorAll('input.o_survey_form_choice_item').forEach((input) => {
input.checked = false;
input.classList.remove('o_survey_form_choice_item_selected');
});
// Labels' 'o_survey_selected' class is left untouched: the native
// handler relies on it to find the previous answer (conditional
// questions) and moves it itself.
const value = select.value;
const target = value && group.querySelector(
`input.o_survey_form_choice_item[value="${value}"]`
);
if (target) {
target.checked = true;
target.classList.add('o_survey_form_choice_item_selected');
}
// Native change listener (jQuery delegated) handles: comment area
// visibility, conditional questions, and auto-submit when
// users_can_go_back on page_per_question layouts. On reset to the
// empty option, it is fired on the previous (now unchecked) radio,
// like the native untick of a radio.
const changed = target || previous;
if (changed) {
changed.dispatchEvent(new Event('change', { bubbles: true }));
}
},
});

View File

@@ -0,0 +1,9 @@
.o_survey_form {
select.o_survey_dropdown_choice {
min-height: 44px;
option {
color: initial;
}
}
}

View File

@@ -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" invisible="question_type != 'simple_choice'"/>
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Redirect the render of simple_choice questions to the dropdown
widget when the display_dropdown flag is set. The native radio
rendering is kept when a suggestion carries an image, and in
readonly mode (print / review) so that corrections are displayed. -->
<template id="question_container" inherit_id="survey.question_container" name="Survey question container: dropdown dispatch">
<xpath expr="//t[@t-if=&quot;question.question_type == 'simple_choice'&quot;]" position="replace">
<t t-if="question.question_type == 'simple_choice'">
<t t-if="question.display_dropdown and not answers_contain_image and not survey_form_readonly"
t-call="survey_dropdown_simple_choice.question_simple_choice_dropdown"/>
<t t-else="" t-call="survey.question_simple_choice"/>
</t>
</xpath>
</template>
<!-- Dropdown rendering for simple_choice questions.
The native JS (survey_form.js) still reads its answers from the
checked radios inside the .o_survey_form_choice wrapper:
the select and a hidden mirror group of radios coexist, a JS patch
keeps them in sync, so conditional questions, score display,
auto-submit and the 'other' comment keep working. -->
<template id="question_simple_choice_dropdown" name="Question: simple choice (dropdown)">
<t t-set="answer_line" t-value="answer_lines.filtered(lambda line: line.suggested_answer_id)"/>
<t t-set="comment_line" t-value="answer_lines.filtered(lambda line: line.value_char_box)"/>
<t t-set="other_selected" t-value="comment_line and not answer_line"/>
<div class="o_survey_answer_wrapper o_survey_form_choice"
t-att-data-name="question.id"
t-att-data-is-skipped-question="is_skipped_question or None"
data-question-type="simple_choice_radio">
<div class="row g-2 o_survey_dropdown_select">
<div class="col-sm-12">
<select class="form-select o_survey_dropdown_choice"
t-att-name="question.id">
<option value="" t-att-selected="'selected' if not answer_line and not other_selected else None"/>
<t t-foreach="question.suggested_answer_ids" t-as="label">
<t t-set="answer_selected" t-value="answer_line and answer_line.suggested_answer_id.id == label.id"/>
<option t-att-value="label.id" t-att-selected="'selected' if answer_selected else None"
t-out="label.value"/>
</t>
<t t-if="question.comments_allowed and question.comment_count_as_answer">
<option value="-1" class="o_survey_dropdown_other_option"
t-att-selected="'selected' if other_selected else None"
t-out="question.comments_message or default_comments_message"/>
</t>
</select>
</div>
</div>
<!-- Hidden mirror radios: no 'name' attribute on purpose, they must
not flow into the submitted form data (the select carries the
answer for validation); only their checked state is read by
_prepareSubmitChoices. They are shown in place of the select
when correct answers are displayed after the page. -->
<div class="row g-2 d-none o_survey_dropdown_mirror">
<t t-foreach="question.suggested_answer_ids" t-as="label">
<t t-set="answer_selected" t-value="answer_line and answer_line.suggested_answer_id.id == label.id"/>
<div class="col-sm-12">
<label t-attf-class="o_survey_choice_btn py-1 px-3 w-100 h-100 rounded #{'o_survey_selected' if answer_selected else ''}">
<i class="fa fa-check-circle float-end mt-1 ms-1 position-relative"/>
<i class="fa fa-circle-thin float-end mt-1 ms-1 position-relative"/>
<input type="radio" t-att-value="label.id"
t-att-id="str(question.id) + '_' + str(label.id)"
t-attf-class="o_survey_form_choice_item invisible position-absolute #{'o_survey_form_choice_item_selected' if answer_selected else ''}"
t-att-checked="'checked' if answer_selected else None"/>
<span class="ms-2 text-break" t-field="label.value"/>
</label>
</div>
</t>
<t t-if="question.comments_allowed and question.comment_count_as_answer">
<div class="col-sm-12">
<label t-attf-class="o_survey_choice_btn py-1 px-3 h-100 w-100 rounded #{'o_survey_selected' if other_selected else ''}">
<i class="fa fa-check-circle float-end mt-1 ms-1 position-relative"/>
<i class="fa fa-circle-thin float-end mt-1 ms-1 position-relative"/>
<input type="radio" value="-1"
t-attf-class="o_survey_form_choice_item o_survey_js_form_other_comment invisible position-absolute #{'o_survey_form_choice_item_selected' if other_selected else ''}"
t-att-checked="'checked' if other_selected else None"/>
<span class="ms-2" t-out="question.comments_message or default_comments_message"/>
</label>
</div>
</t>
</div>
<!-- Comment areas: exact copy of the native structure so that
_applyCommentAreaVisibility and _prepareSubmitComment work. -->
<t t-if="question.comments_allowed and question.comment_count_as_answer">
<div t-attf-class="o_survey_comment_container mt-3 py-0 px-1 h-auto #{'d-none' if not comment_line else ''}">
<textarea type="text" class="form-control o_survey_question_text_box bg-transparent rounded-0 p-0"
t-att-disabled="None if comment_line else 'disabled'"><t t-esc="comment_line.value_char_box if comment_line else ''"/></textarea>
</div>
</t>
<div t-if="question.comments_allowed and not question.comment_count_as_answer" class="mb-2 o_survey_comment_container mt-3">
<textarea type="text" class="col form-control o_survey_comment o_survey_question_text_box bg-transparent rounded-0 p-0"
t-att-placeholder="question.comments_message or default_comments_message if not survey_form_readonly else ''"><t t-esc="comment_line.value_char_box if comment_line else ''"/></textarea>
</div>
</div>
</template>
</odoo>