Calendar Grid Implementation

Technical reference for the calendar grid question type - schema, validation, frontend rendering, and XLSForm import/export

The Calendar Grid is an advanced question type that allows tracking data over time in a matrix format, optimized for calendar-based tracking (monthly or weekly periods).

Features

  • Dual Modes: Monthly or weekly tracking
  • Flexible Columns: select_one, select_multiple, integer, and text field types
  • Configurable Periods: 1-120 months or 1-520 weeks
  • Future Periods: Optional allowance for future months/weeks
  • Offline Support: Works in offline mode
  • XLSForm Compatible: Import/export with existing XLSForm tools

Question Schema

JSON Structure

{
  "type": "calendar_grid",
  "name": "reproductive_calendar",
  "label": "Reproductive History Calendar",
  "calendar_config": {
    "mode": "monthly",
    "months_back": 60,
    "start_from": "today",
    "allow_future": false
  },
  "columns": [
    {
      "key": "marital_status",
      "type": "select_one",
      "label": "Marital Status",
      "choices": [
        { "name": "1", "label": "Married" },
        { "name": "2", "label": "Unmarried" }
      ]
    },
    {
      "key": "fertility",
      "type": "integer",
      "label": "Number of Children"
    }
  ]
}

Calendar Config Fields

FieldTypeRequiredDescription
modestringYes"monthly" or "weekly"
months_backintegerYesNumber of past periods (1-120 monthly, 1-520 weekly)
start_fromstringNo"today" or "survey_start"
allow_futurebooleanNoWhether to show future periods

Column Fields

FieldTypeRequiredDescription
keystringYesVariable name for the column
typestringYes"select_one", "select_multiple", "integer", or "text"
labelstringYesDisplay label
choicesarrayFor select typesList of choices
relevantstringNoXPath condition for column visibility

Backend Implementation

Files Modified

  1. survey-service/api/common/dtos/survey.py - Added CALENDAR_GRID enum, CalendarColumnSchema, CalendarConfigSchema DTOs
  2. survey-service/api/domain/validators/schema_validator.py - Calendar grid field validation
  3. survey-service/api/usecase/xlsform_parser.py - Type mapping for calendar_grid
  4. survey-service/api/usecase/xlsform_writer.py - Export conversion to begin_repeat with field-list

Frontend Implementation

Survey Designer Components

  • survey-designer-types.ts - QuestionType includes calendar_grid, CalendarColumn, CalendarConfig interfaces
  • survey-designer.tsx - "Calendar Grid" option in Add Question menu, editor panel with mode selector, periods back input, columns builder
  • survey-designer-utils.ts - Schema transformations for calendar_grid

Form Filler

  • calendar-grid-renderer.tsx - NEW: renders calendar as table matrix, handles monthly/weekly modes, ISO week date algorithm
  • form-filler.tsx - Added calendar_grid to page processing

Validation Rules

  1. Required Fields: calendar_config must be present, at least one column required
  2. Column Validation: Each column must have a unique key. Types must be: select_one, select_multiple, integer, or text. Select types must have choices
  3. Range Validation: months_back: 1-120 (monthly) or 1-520 (weekly)

XLSForm Import/Export

Export (MQUAD -> XLSForm)

Calendar grid converts to a repeat group with field-list appearance:

type                name                    label                   appearance    repeat_count
begin_repeat        reproductive_calendar    Reproductive Calendar   field-list   60
select_one          marital_status          Marital Status
select_one          fertility              # of Children
end_repeat

Import (XLSForm -> MQUAD)

The importer detects calendar patterns: repeat with field-list appearance, contains 2+ select questions, repeat_count uses months calculation or label contains "calendar".

Calendar Rules Engine

The Calendar Rules Engine enables automatic prefilling of calendar grid columns based on answers to previous questions. See the Calendar Rules guide for details.