To achieve the desired functionality—triggering the action_user_question_answers action when a user clicks on the user’s name (user_id) in the tree view—you can modify the tree view to include a clickable button associated with each user. This button will execute the specified action with the appropriate context.
Here’s how you can implement this:
-
Add a Button to the Tree View:
Modify the existing tree view (view_user_questionnaire_statistics_tree) to include a new column with a button. This button will invoke the action_user_question_answers action when clicked.
-
Pass the Necessary Context:
Ensure that the button passes the relevant user_id and questionnaire_id to the action. This allows the action to filter the user answers based on the selected user and questionnaire.
-
Update the Action if Necessary:
Ensure that the action_user_question_answers is set up to handle the incoming context correctly, filtering the records based on the provided user_id and questionnaire_id.
Updated XML Code
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<!-- Tree View for User Questionnaire Statistics -->
<record id="view_user_questionnaire_statistics_tree" model="ir.ui.view">
<field name="name">user.questionnaire.statistics.tree</field>
<field name="model">user.questionnaire</field>
<field name="arch" type="xml">
<tree string="Questionnaire Statistics">
<field name="questionnaire_id"/>
<field name="user_id"/>
<field name="progress" widget="progressbar"/>
<field name="is_finished"/>
<!-- Add a button to trigger the action -->
<button name="%(action_user_question_answers)d"
string="View Answers"
type="action"
icon="fa-eye"
context="{
'default_user_questionnaire_id': id,
'questionnaire_id': questionnaire_id.id
}"/>
</tree>
</field>
</record>
<!-- Search View for User Questionnaire Statistics -->
<record id="view_user_questionnaire_statistics_search" model="ir.ui.view">
<field name="name">user.questionnaire.statistics.search</field>
<field name="model">user.questionnaire</field>
<field name="arch" type="xml">
<search string="Search Questionnaire Statistics">
<field name="questionnaire_id"/>
<field name="user_id"/>
<filter string="Completed" name="completed" domain="[('is_finished', '=', True)]"/>
<filter string="In Progress" name="in_progress" domain="[('is_finished', '=', False)]"/>
<group expand="0" string="Group By">
<filter string="Questionnaire" name="group_by_questionnaire" context="{'group_by': 'questionnaire_id'}"/>
<filter string="User" name="group_by_user" context="{'group_by': 'user_id'}"/>
<filter string="Status" name="group_by_status" context="{'group_by': 'is_finished'}"/>
</group>
</search>
</field>
</record>
<!-- Graph View for User Questionnaire Statistics -->
<record id="view_user_questionnaire_statistics_graph" model="ir.ui.view">
<field name="name">user.questionnaire.statistics.graph</field>
<field name="model">user.questionnaire</field>
<field name="arch" type="xml">
<graph string="Questionnaire Statistics" type="bar">
<field name="questionnaire_id"/>
<field name="progress" type="measure"/>
</graph>
</field>
</record>
<!-- Action for Viewing User Answers -->
<record id="action_user_question_answers" model="ir.actions.act_window">
<field name="name">User Answers</field>
<field name="res_model">user.question</field>
<field name="view_mode">tree,form</field>
<field name="domain">[
('user_questionnaire_id.user_id', '=', context.get('user_id')),
('user_questionnaire_id.questionnaire_id', '=', context.get('questionnaire_id'))
]</field>
<field name="context">{'default_user_questionnaire_id': context.get('default_user_questionnaire_id')}</field>
</record>
<!-- Action for Statistics -->
<record id="action_questionnaire_statistics" model="ir.actions.act_window">
<field name="name">Questionnaire Statistics</field>
<field name="res_model">user.questionnaire</field>
<field name="view_mode">tree,graph</field>
<field name="search_view_id" ref="view_user_questionnaire_statistics_search"/>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
No questionnaire statistics found
</p>
<p>
Statistics will appear here once users start answering questionnaires.
</p>
</field>
<field name="context">{'group_by': 'questionnaire_id'}</field>
</record>
<!-- Menu Item for Statistics -->
<menuitem id="menu_questionnaire_statistics"
name="Statistics"
parent="menu_questionnaire_root"
action="action_questionnaire_statistics"
sequence="20"/>
</odoo>
Explanation of Changes
-
Button Addition:
- A new
<button> element is added within the <tree> view.
name="%(action_user_question_answers)d" references the action_user_question_answers action by its XML ID.
string="View Answers" sets the label of the button.
type="action" specifies that this button will trigger an action.
icon="fa-eye" adds an eye icon to the button for better UI/UX (optional).
- The
context attribute passes necessary parameters to the action:
'default_user_questionnaire_id': id sets the default user_questionnaire_id based on the current record.
'questionnaire_id': questionnaire_id.id passes the questionnaire_id of the current record.
-
Action Configuration:
- The
action_user_question_answers is updated to use context.get('user_id') and context.get('questionnaire_id') to filter the user.question records appropriately.
- This ensures that when the action is triggered, it correctly filters the answers based on the selected user and questionnaire.
-
Domain Adjustment:
- The domain in
action_user_question_answers uses context.get('user_id') and context.get('questionnaire_id') to dynamically filter records based on the button clicked.
Additional Considerations
-
Permissions:
Ensure that the users have the necessary permissions to access the user.question model and the associated views.
-
UI Enhancements:
You can further enhance the UI by styling the button or adding additional information as needed.
-
Testing:
After making these changes, thoroughly test the functionality to ensure that clicking the "View Answers" button correctly opens the desired view with the appropriate filtered data.
By implementing these modifications, users will be able to click the "View Answers" button next to a user's name in the tree view, which will trigger the action_user_question_answers action and display the relevant questionnaire answers.