Totara allows developers to create custom notification templates programmatically, enabling automation of communications for organization-specific business processes not covered by standard notifications. A custom notification template can be developed within a local plugin or custom extension and triggered by specific events, scheduled tasks, or workflow actions.
As part of a recent customization, we implemented a notification workflow for suspended users. The requirement was to automatically remove a user from any event enrollments when their account status changed to suspended. Once the enrollment cancellation process was complete, a custom notification was sent to the user’s manager or supervisor. Now, with this example, we show you how to create a custom notification template through code. Follow these simple steps:
Step 1: Choose the Plugin
First, identify the plugin for which you want to create the notification. In our case, we are creating a notification template for the Sequence activity plugin.
Step 2: Open the Plugin Folder
Navigate to the plugin directory inside the mod folder:
mod/sequence
Step 3: Locate the Upgrade File
Inside the plugin folder, open the following file:
mod/sequence/db/upgrade.php
This file is used to run database and configuration changes whenever the plugin is upgraded.
Step 4: Add the Notification Template Code
Add the code that creates the notification template inside the appropriate upgrade step in upgrade.php.
Step 5: Update the Plugin Version
After adding the code, increase the plugin version number in the version.php file.
The version number used in the upgrade condition should match the new version number that you set in version.php.
Step 6: Upgrade the Plugin
Once the version is updated, run the Totara upgrade process. During the upgrade, the code in upgrade.php will execute automatically and create the new notification template in the database.
This means the notification template is installed automatically as part of the plugin upgrade, without requiring any manual database changes. The example below shows how we created a custom notification template during a plugin upgrade. This code runs during a plugin upgrade and checks whether the Suspension Cancellation notification template already exists. If it does not exist, a new template is created with its title, message content, and notification settings for managers and hotel staff.
| if ($oldversion < 2026061600) {
$SuspensionCancellationTemplate = $DB->get_record(‘sequence_notification_tpl’, array(‘reference’ => ‘suspension_cancellation’)); $transaction = $DB->start_delegated_transaction(); if(empty($SuspensionCancellationTemplate)){ $tpl_suspension_cancellation = new stdClass(); $tpl_suspension_cancellation->status = 1; $tpl_suspension_cancellation->reference = ‘suspension_cancellation’; $tpl_suspension_cancellation->title = get_string(‘setting:SuspensionCancellationTitle’,’sequence’); $tpl_suspension_cancellation->body = text_to_html(get_string(‘setting:suspension_cancellationmessage’, ‘sequence’)); $tpl_suspension_cancellation->ccmanager = 1; $tpl_suspension_cancellation->managerprefix = text_to_html(get_string(‘setting:suspension_cancellation_managermessage’, ‘sequence’)); $tpl_suspension_cancellation->hotelstaffcopy = 1; $tpl_suspension_cancellation->hotelstaffprefix = text_to_html(get_string(‘setting:suspension_cancellation_hotelstaffmessage’, ‘sequence’)); $DB->insert_record(‘sequence_notification_tpl’, $tpl_suspension_cancellation); } $transaction->allow_commit(); } |
Now, the next code, which we are going to mention, is used to make the template available in the Totara UI. Once this is done, the template will appear in the notification templates list, where administrators can easily manage it. From the UI, we can edit the notification content and configure recipients and other settings as well. This means future changes in the template can be made from the UI without modifying the code again. Please check the example code.
This change is made in the specified file. At the top of the file, alongside the existing constants, we create a new constant for our notification template. This constant acts as a unique identifier for the notification.
Later in the code, the same constant is used to determine when the notification should be sent. When the required condition is met, the system uses this identifier to locate and trigger the correct notification template.
mod/sequence/notification/lib.php
//constant
define(‘MDL_SQ_CONDITION_SUSPENSION_CANCELLATION’, 001);
| if (isset($templates[‘suspension_cancellation’])) {
$template = $templates[‘suspension_cancellation’]; $suspension_cancellation = new sequence_notification($defaults, false); $suspension_cancellation->title = $template->title; $suspension_cancellation->body = $template->body; $suspension_cancellation->managerprefix = $template->managerprefix; $suspension_cancellation->conditiontype = MDL_SQ_CONDITION_SUSPENSION_CANCELLATION; $suspension_cancellation->ccmanager = $template->ccmanager; $suspension_cancellation->status = $template->status; $suspension_cancellation->templateid = $template->id; $suspension_cancellation->hotelstaffcopy = $template->hotelstaffcopy; $suspension_cancellation->hotelstaffprefix = $template->hotelstaffprefix; $notifications[MDL_SQ_CONDITION_SUSPENSION_CANCELLATION] = $suspension_cancellation; } else { $missingtemplates[] = ‘suspensioncancellation’; } |
Now, from the UI, navigate to Site Administration and then choose Notifications, and in the list of all notifications, you can find your created notification template as well.



