Timetable scheduling and lesson selection
Timetable scheduling lets a student build a semester plan from module lesson choices. It keeps the saved choices separate from the lesson data used for display, then turns those choices into an interactive weekly timetable with clear rules for changing classes, adding TA sessions, and showing clashes.
Scheduling model
The timetable is a semester-scoped plan. Each semester maps module codes to lesson choices, while related maps hold display colors, hidden modules, and modules treated as TA modules. The wider timetable experience reads these maps as the saved plan and uses module data to recover the actual lesson times.
The main concepts are:
| Concept | Purpose |
|---|---|
| Module lesson config | Records the selected lesson for each lesson type in a module. |
| Class number | Represents one complete class choice for a normal module. |
| Lesson ID | Identifies one exact lesson time. TA modules use these IDs so several matching sessions can be selected. |
| Semester lesson map | Supplies the available lesson records for a module in one semester. |
| Display state | Holds module colors, hidden modules, and TA status alongside the lesson choices. |
The timetable keeps normal and TA selection models distinct. A normal module has one class number for each lesson type. A TA module can hold several exact lesson IDs, which supports selecting individual teaching sessions rather than one complete class.
References: website/…/timetables.ts• L13-16 website/…/timetables.ts• L35-37 website/…/modules.ts• L40-43 website/…/modules.ts• L183-200 website/…/reducers.ts• L117-134
Lesson data hydration
Lesson data hydration turns saved identifiers into the full lesson records that the timetable UI needs. It combines the semester lesson map from the module data with the saved module config, then adds the module code and title so each displayed lesson can stand alone.
The data moves through these stages:
flowchart LR
A[Saved semester config] --> B[Selected class numbers or lesson IDs]
C[Module semester lesson data] --> D[Available lesson map]
B --> E[Hydrated lesson records]
D --> E
E --> F[Interaction flags and colors]
F --> G[Weekly timetable data]
A missing module produces no usable lesson record for that module. This makes module loading a prerequisite for a complete timetable view and explains why shared timetables load their modules before they are shown.
References: website/…/lessonHydration.ts• L27-65 website/…/modules.ts• L192-200 website/…/TimetableContainer.tsx• L197-246
Timetable actions and state changes
Timetable actions describe user intent. The timetable reducer applies those actions to the semester plan and its related display state, while the app reducer owns the temporary active lesson used during a lesson change.
The main action groups are:
- Module actions: add, remove, or reset a semester timetable.
- Lesson actions: start a change, replace a normal lesson choice, add or remove a TA lesson, and cancel the change.
- Module display actions: choose a color or hide and show a module.
- TA actions: enable or disable TA mode for a module.
- Import actions: replace a semester plan and restore imported hidden and TA module lists.
Adding a module first loads its module data. If the load succeeds, the system creates an initial choice from the available lesson groups. If it fails, the user gets a retry notification and the timetable is not changed. Loading a set of timetable modules also filters out module codes that are not valid in the module bank.
References: website/…/timetables.ts• L41-107 website/…/timetables.ts• L120-228 website/…/timetables.ts• L291-305 website/…/timetables.ts• L308-379 website/…/TimetableContent.tsx• L254-276
Reducer state transitions
The reducer treats each semester as an independent scheduling scope. Module changes update the lesson config and color map together, while hide/show and TA actions update their matching lists. Removing a module also removes its color, hidden flag, and TA flag.
Lesson changes have different effects by intent:
| Action | Result |
|---|---|
| Replace lesson | Replaces the selected lesson type with the new choice. |
| Add lesson | Adds exact lesson IDs without duplicates. |
| Remove lesson | Removes the requested exact lesson IDs. |
| Set lesson config | Replaces the complete config for one module. |
| Cancel or complete a lesson change | Clears the temporary active lesson. |
Saved timetable state is kept across sessions. When the academic year changes, the current plan is cleared for the new year and the old plan is placed in the archive instead of being treated as the new plan.
References: website/…/timetables.ts• L86-155 website/…/timetables.ts• L159-225 website/…/timetables.ts• L228-318 website/…/app.ts• L33-53
Lesson-choice rules
Lesson selection uses a two-step interaction. The first selectable lesson becomes the active lesson. The next selection either replaces that lesson choice or changes a TA selection, based on the module and lesson type of the active lesson.
The interaction layer adds flags to each visible lesson. These flags tell the UI whether a lesson is active, whether it can start a choice, and whether it can be added to the saved config. The flags keep the selection rules in one place so the cell display and click behavior agree.
Normal module choices
For a normal module, the system treats a class number as the unit of choice. It shows alternatives for the active lesson type when the same module is being changed. Selecting an available alternative replaces the class number for that lesson type. A lesson with the same class number as the active lesson is not offered as an additional choice.
When no module or lesson is active, the visible set is limited to the saved choices. This prevents a click on an unrelated module from changing the wrong lesson group and makes the first click a clear start to a new choice.
References: website/…/interactabilityHydration.ts• L54-92 website/…/interactabilityHydration.ts• L143-164 website/…/TimetableContent.tsx• L202-237
TA lesson choices
TA mode changes the choice unit from a class number to an exact lesson. The UI can show all lesson types for the active TA module, and each selected lesson ID can be added or removed independently. The last selected TA lesson cannot be removed because a TA module must keep at least one lesson.
Enabling TA mode converts the module's current class-number choices into the matching exact lesson IDs. If semester lesson data is unavailable, it still marks the module as a TA module with an empty config so the state can be repaired later. Disabling TA mode converts the selected exact lessons back to the closest class number for each lesson type when semester data is available. If that data is missing, it keeps the exact lesson IDs and then runs timetable validation. This keeps the existing plan as stable as possible while changing the selection model.
References: website/…/interactabilityHydration.ts• L73-92 website/…/TimetableContent.tsx• L179-200 website/…/timetables.ts• L362-469 website/…/timetables.test.ts• L463-641
Choice recovery
Choice recovery protects saved plans when module data changes. Validation first converts older timetable formats into the current semester config shape, then removes unknown modules and repairs invalid lesson choices against the current semester lesson map before the timetable relies on them.
The recovery policy differs by module type:
- A normal module receives one valid class number for each available lesson type.
- A TA module keeps valid exact lesson IDs. If none remain, it recovers lessons by matching the old class number or by choosing an available lesson.
- A missing lesson type is removed from the config.
This repair runs when a timetable is set or when the semester plan is validated. It lets old saved or imported data degrade to a usable plan instead of leaving broken identifiers in the UI. Missing modules and malformed lesson entries are ignored without stopping validation of the rest of the plan.
References: website/…/timetables.ts• L208-289 website/…/validation.ts• L39-57 website/…/validation.ts• L59-131 website/…/validation.ts• L134-271 website/…/timetables.test.ts• L132-402
Conflict handling
The timetable does not reject overlapping lesson choices. It keeps all selected lessons and presents time conflicts in separate rows on the same day, so a student can inspect the full plan while deciding which choices to change.
Two lessons conflict when they are on the same day and their time ranges overlap. A lesson that ends exactly when another starts does not conflict. The weekly arrangement groups lessons by day and assigns each overlapping set to another row for display.
This diagram shows how selected lessons become rows without hiding a conflict.
flowchart LR
A[Selected lessons] --> B[Group by day]
B --> C[Compare time ranges]
C -->|No overlap| D[Keep in current row]
C -->|Overlap| E[Place in another row]
D --> F[Weekly timetable arrangement]
E --> F
Exam conflicts use a separate check. The module list marks exam clashes for examinable modules, but hidden modules and TA modules are excluded from that check. The timetable still keeps those modules in the saved plan; only the warning and grouping behavior changes.
References: website/…/lessonsArrangement.ts• L16-26 website/…/lessonsArrangement.ts• L29-81 website/…/TimetableContent.tsx• L307-350
Scheduling data consumed by the timetable UI
The timetable UI consumes a prepared view of the schedule rather than the raw saved config. It reads the selected semester, hydrates lesson records, applies visibility and TA status, adds colors, marks interaction choices, and arranges the result by day.
The data contract is:
| Data | Consumer purpose |
|---|---|
| Semester timetable config | Identifies the modules and saved choices. |
| Module semester data | Supplies lesson time, day, venue, type, and teaching weeks. |
| Hidden module list | Removes a module's lessons from the visible timetable while keeping the module in the plan. |
| TA module list | Selects TA rules for visibility and lesson changes. |
| Color mapping | Gives each module a stable visual identity. |
| Active lesson | Marks the current change target and drives available alternatives. |
| Arranged lesson rows | Gives the timetable renderer day groups with non-overlapping rows. |
The timetable page first removes hidden modules from the rendered lesson set. It then hydrates the remaining lessons with interaction flags, flattens them, and arranges them for the weekly view. The module table still receives the full added-module list, which is why hiding a module does not remove it from the plan.
References: website/…/TimetableContent.tsx• L371-394 website/…/timetables.ts• L24-57 website/…/timetables.ts• L67-93 website/…/timetables.ts• L114-125
Week and time semantics
Each lesson carries a day, start time, end time, venue, and teaching weeks. The UI uses the week data to decide whether a lesson occurs on a given date and to describe regular, odd, even, or date-range teaching patterns.
The schedule therefore represents more than a weekly time slot. A lesson can appear only on selected instructional weeks or on a date range with a repeat interval. The timetable keeps this information on the lesson so views such as Today and lesson details can apply the same availability rule.
References: website/…/modules.ts• L16-27 website/…/modules.ts• L169-181 website/…/index.ts• L103-119 website/…/index.ts• L138-188
Related timetable pages
The timetable experience gives the wider product view. The timetable rendering and interaction page explains how the prepared schedule becomes visible cells and user feedback.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep normal-module class-number rules aligned across config migration, validation, hydration, and lesson-cell selection.
- Keep TA conversion rules aligned with the saved
tamodule list so a mode switch does not reinterpret lesson choices. - Keep missing-module behavior consistent across module loading, timetable validation, and lesson hydration.
- Keep hidden-module handling aligned between the rendered lesson set and the module table so hidden modules remain recoverable.
- Keep lesson identifier changes compatible with saved plans and imported timetable data.
- Keep week-range handling aligned between lesson availability checks and the text shown for a lesson.
- Keep semester-level state updates aligned so colors, hidden modules, and TA status follow timetable imports and resets.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A module cannot be loaded while adding it | The module is not added and the user sees a retry notification. | Load the module before creating its initial lesson config. |
| A saved timetable uses an older config format | Lesson and TA choices can use outdated identifiers or shapes. | Migrate the semester config before validating its lesson choices. |
| A saved lesson choice is no longer in the semester data | The old identifier can no longer hydrate into a displayed lesson. | Run timetable and module validation against the current lesson map. |
| A normal module stores more than one class number for a lesson type | The config no longer represents one complete class choice. | Recover normal configs to one valid class number per lesson type. |
| TA mode changes while semester data is missing | The mode switch cannot find equivalent class or lesson choices. | Preserve the available config and run validation when module data returns. |
| Selected lessons overlap in day and time | The schedule contains a real timetable conflict. | Keep the lessons and place them on separate day rows so the conflict is visible. |
| The academic year changes | The old plan must not become the new year's active plan. | Archive the old plan and start the new year with the current default state. |
Key files
| File | Why to read it |
|---|---|
| website/…/TimetableContent.tsx | Shows how state, lesson hydration, interaction flags, and weekly arrangement connect to the timetable page. |
| website/…/timetables.ts | Defines the user-facing timetable operations, config recovery, module loading, and rules for switching TA mode. |
| website/…/timetables.test.ts | Shows the action contract for module loading, old-config migration, malformed data, and TA conversion edge cases. |
| website/…/timetables.ts | Shows how semester plans and related display state change after each action. |
| website/…/interactabilityHydration.ts | Defines which lessons are visible and selectable during a lesson change. |
| website/…/validation.ts | Defines how invalid or old lesson choices become usable current choices. |
| website/…/lessonHydration.ts | Shows how saved identifiers become full lesson records for the UI. |
| website/…/lessonsArrangement.ts | Defines day grouping and the row model used to show overlapping lessons. |
| website/…/timetables.ts | Defines the timetable config, lesson, interaction, and arrangement data contracts. |
| website/…/modules.ts | Defines the module semester data and the lesson fields consumed by scheduling. |