Files

81 lines
3.4 KiB
Python
Raw Permalink Normal View History

2026-07-01 14:41:49 +07:00
from odoo import models, fields, api
class SurveyDashboardView(models.Model):
_name = 'survey.dashboard.view'
_description = 'Survey Dashboard View'
_auto = False
_order = 'create_date desc'
_rec_name = 'question_id'
survey_id = fields.Many2one('survey.survey', string='Survey', readonly=True)
user_input_id = fields.Many2one('survey.user_input', string='User Input', readonly=True)
partner_id = fields.Many2one('res.partner', string='Respondent', readonly=True)
question_id = fields.Many2one('survey.question', string='Question', readonly=True)
answer_type = fields.Selection([
('simple_choice', 'Multiple choice: only one answer'),
('multiple_choice', 'Multiple choice: multiple answers allowed'),
('text_box', 'Multiple Lines Text Box'),
('char_box', 'Single Line Text Box'),
('numerical_box', 'Numerical Value'),
('scale', 'Scale'),
('date', 'Date'),
('datetime', 'Datetime'),
('matrix', 'Matrix')
], string='Question Type', readonly=True)
answer_value = fields.Char(string='Answer Value', readonly=True)
response_count = fields.Integer(string='Count', readonly=True)
create_date = fields.Datetime(string='Response Date', readonly=True)
is_done = fields.Boolean(string='Completed', readonly=True)
def init(self):
# Drop view first to allow structural changes
self.env.cr.execute("DROP VIEW IF EXISTS survey_dashboard_view")
self.env.cr.execute("""
CREATE VIEW survey_dashboard_view AS (
SELECT
ROW_NUMBER() OVER () as id,
sui.id as user_input_id,
sui.survey_id as survey_id,
sui.partner_id as partner_id,
sq.id as question_id,
sq.question_type as answer_type,
-- ✅ FIX: Explicitly cast ALL branches to TEXT
CASE
WHEN sq.question_type = 'matrix' THEN
CONCAT(sqa_row.value::text, ': ', sqa_col.value::text)
WHEN sq.question_type IN ('simple_choice', 'multiple_choice') THEN
sqa_col.value::text
ELSE COALESCE(
uls.value_text_box::text,
uls.value_char_box::text,
uls.value_date::text,
uls.value_datetime::text,
uls.value_numerical_box::text,
uls.value_scale::text,
''
)
END as answer_value,
1 as response_count,
sui.create_date as create_date,
(sui.state = 'done') as is_done
FROM survey_user_input sui
LEFT JOIN survey_user_input_line uls ON uls.user_input_id = sui.id
LEFT JOIN survey_question sq ON sq.id = uls.question_id
-- Joins for Multiple Choice and Matrix answers
LEFT JOIN survey_question_answer sqa_col ON uls.suggested_answer_id = sqa_col.id
LEFT JOIN survey_question_answer sqa_row ON uls.matrix_row_id = sqa_row.id
WHERE sui.state IN ('done')
AND sq.is_page = False
AND sq.question_type IS NOT NULL
)
""")