6.3 KiB
Manual Survey Sending Implementation - Complete
Overview
Successfully implemented the ability to manually send surveys to both patients and staff members. This feature allows administrators to select a survey template and choose whether to send it to a patient or a staff member.
Implementation Details
1. Database Migration
File: apps/surveys/migrations/0008_add_staff_field_to_survey_instance.py
- Added
stafffield toSurveyInstancemodel - Field is nullable to support both patient and staff recipients
- Allows survey instances to be linked to either a patient or a staff member (not both)
2. Model Updates
File: apps/surveys/models.py
- Updated
SurveyInstancemodel withstafffield - Field links to
Usermodel (staff member) - Maintains relationship with existing
patientfield - Both fields are optional (null=True)
3. Service Layer Updates
File: apps/surveys/services.py
- Updated
SurveyDeliveryServiceclass - Modified
send_survey()method to handle staff recipients - Added logic to detect recipient type (patient vs staff)
- Sends notifications based on recipient preferences:
- Email notifications for staff
- SMS notifications for patients
- Generates survey links appropriate for each recipient type
4. Form Implementation
File: apps/surveys/forms.py
- Created
ManualSurveySendFormclass - Includes recipient type selection (patient/staff)
- Dynamic field rendering:
- Shows patient dropdown when patient selected
- Shows staff dropdown when staff selected
- Validates that appropriate recipient is selected
- Filters patients and staff by organization context
5. View Implementation
File: apps/surveys/ui_views.py
- Created
manual_survey_sendview - Handles GET request: displays form with survey templates
- Handles POST request: processes form and sends survey
- Creates
SurveyInstancewith appropriate recipient - Uses
SurveyDeliveryServicefor delivery - Provides success/error messages
- Redirects to survey instance list on success
6. Template Implementation
File: templates/surveys/manual_send.html
- Clean, modern interface using Bootstrap 5
- Two-column layout for better UX
- Left column: Survey template selection
- Right column: Recipient selection
- Dynamic recipient fields based on type selection
- Form validation feedback
- Professional styling with icons
7. URL Configuration
File: apps/surveys/urls.py
- Added URL pattern:
/surveys/send-manual/ - Named URL:
surveys:manual_send
8. Navigation Updates
File: templates/layouts/partials/sidebar.html
- Added "Send Survey" link under Surveys section
- Positioned before "Survey Instances" for logical flow
- Uses icon for visual clarity
- Proper i18n support with trans tags
Key Features
Recipient Type Selection
- Radio buttons to choose between Patient and Staff
- Dynamic form fields based on selection
- Automatic filtering of available recipients
Survey Template Selection
- Dropdown list of active survey templates
- Only shows templates for the user's organization
- Displays template names in English/Arabic
Organization Context
- Filters patients by user's hospital
- Filters staff by user's organization
- Ensures data isolation and security
Email Delivery
- For staff: sends email with survey link
- For patients: sends SMS with survey link
- Uses existing notification infrastructure
- Respects user notification preferences
User Experience
- Clean, intuitive interface
- Real-time form validation
- Success/error feedback messages
- Responsive design for mobile devices
Database Changes
Migration 0008_add_staff_field_to_survey_instance
operations = [
migrations.AddField(
model_name='surveyinstance',
name='staff',
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name='survey_instances',
to='accounts.user'
),
),
]
Testing Performed
- Database Migration: Successfully applied without errors
- Form Validation: Tested both patient and staff selection paths
- Survey Sending: Verified survey instances created correctly
- Notification Delivery: Confirmed emails sent to staff
- UI/UX: Validated interface works as expected
- Navigation: Confirmed sidebar link accessible
- Permissions: Verified access control works correctly
Usage
For Administrators
- Navigate to Surveys section in sidebar
- Click "Send Survey" link
- Select survey template from dropdown
- Choose recipient type (Patient or Staff)
- Select specific recipient from filtered list
- Click "Send Survey" button
- View success message and survey instance
API Access
The manual sending feature is also available via the API:
POST /api/surveys/manual-send/
Future Enhancements (Optional)
- Bulk Sending: Add ability to send survey to multiple recipients at once
- Scheduling: Add option to schedule survey sending for future date/time
- Survey Preview: Add preview of survey before sending
- Custom Message: Add ability to include custom message with survey invitation
- Delivery Tracking: Add detailed tracking of survey delivery status
- Reminder System: Add automatic reminder for uncompleted surveys
Dependencies
- Django Models
- Django Forms
- Bootstrap 5 (frontend)
- Existing notification services
- User authentication system
Security Considerations
- Access Control: Only authenticated users can send surveys
- Organization Isolation: Users can only send to recipients in their organization
- Permission Checks: Validates user has appropriate permissions
- Data Validation: Form validates all inputs before processing
Performance Impact
- Minimal database impact (one new field)
- Efficient queries with proper indexing
- Uses existing notification infrastructure
- No additional server load expected
Conclusion
The manual survey sending feature has been successfully implemented and tested. It provides administrators with a flexible tool to send surveys to both patients and staff members, with proper organization filtering and notification delivery. The implementation follows Django best practices and integrates seamlessly with the existing PX360 system.