x

Main chapters

  1. Get started
  2. Workspace
  3. Editor
  4. Question types
  5. Share
  6. Results
  7. Configuration
  8. Installation and updates
  9. Profile and personal settings
  10. Account and billing
  11. FAQs
  12. Development
 Actions

Notifications for developers

From LimeSurvey Manual

This page describes the notification system of LimeSurvey for plugin and core developers. For how administrators use notifications, see Notifications.

Usage

Notifications are created on the server side with PHP. They can be for a specific user or for a survey. You can also create a notification that is displayed until a certain action is taken, for example until the survey is activated. A page reload is always necessary to see new notifications.

A notification can either be important or of the default type:

  • Default (Notification::NORMAL_IMPORTANCE, 1): only shown in the list under the bell in the admin menu.
  • Nag once (Notification::NAG_ONCE_IMPORTANCE, 2): shown as a pop-up at page load and immediately marked as read, independent of user action.
  • Important (Notification::HIGH_IMPORTANCE, 3): shown as a pop-up at page load until it is read.

The bell lists the 50 latest notifications of the current user (and of the current survey, if a survey is open).

Database

Notifications are stored in the table notifications, so they persist between logins.

Fields:

  • id
  • entity, like survey, user, ...
  • entity_id, survey id, user id, ...
  • title, title of message (shown in dropdown and overview)
  • message, the actual message
  • display_class, success, danger, default
  • importance, 1 = normal, 2 = nag once, 3 = high/popup on page load
  • status, new, read
  • hash, a hashed value of title, message and entity/entity id; indexed; useful for unique notifications
  • created, datetime
  • first_read, datetime

Examples

Create a new notification for a survey

This notification will be visible for all users who visit the survey.

Note that it will only be marked as unread for the first user that reads it.

$not = new Notification(array(
  'survey_id' => 316222,
  'title' => 'My title',
  'message' => 'Hello, and welcome to LimeSurvey! Anyone who can see this survey can see this message.'
));
$not->save();


Send an important notification

Visible on page load as popup.

$not = new Notification(array(
  'survey_id' => 316222,
  'title' => 'My title',
  'importance' => Notification::HIGH_IMPORTANCE,
  'message' => 'This notification will be a pop-up modal automatically opened at load'
));
$not->save();


Notification to a specific user

Only this user can see this notification.

$user = ...
$not = new Notification(array(
  'user_id' => $user->uid,
  'title' => 'My title',
  'message' => 'This is a love letter only Marge can see.'
));
$not->save();


Broadcast

All users will get this notification.

Notification::broadcast(array(
  'title' => 'To all users',
  'message' => 'I\'m in love with Marge. I don\'t care if everyone knows.'
));


Broadcast to superadmin

This is how you broadcast a message to all superadmins. Also using localization.

$superadmins = User::model()->getSuperAdmins();
Notification::broadcast(array(
    'title' => gT('Database update'),
    'message' => gT('The database has been updated to version 259. New version includes notification system.')
), $superadmins);


Display class

You can change the modal class of the modal popup using display_class.

$not = new Notification(array(
  'survey_id' => 316222,
  'title' => 'Error',
  'message' => 'This modal will have a red panel header, because it\'s an error.',
  'display_class' => 'warning'
));
$not->save();


Unique notification

If you only want to create one copy of a message, but want to mark it as unread depending on program logic, use the UniqueNotification class:

$not = new UniqueNotification(array(
    'user_id' => 1,
    'title' => 'Unique title',
    'message' => 'Even if you save this multiple times, there will only be one copy'
));
$not->save();  // If this exact message already exists, save() will mark it as unread (status = new)


Unique notification with less nagging

The above unique message will pop up the first time, and then a second time be marked as unread. If you want a message to continue to be marked as unread, use UniqueNotification with 'markAsNew' => false:

(new UniqueNotification(array(
    'user_id' => 1,
    'markAsNew' => false,
    'title' => 'Unique title',
    'message' => 'Even if you save this multiple times, there will only be one copy.'
)))->save();

When an existing unique notification is saved again, its importance is set back to normal, so it pops up only once. To keep it popping up, add 'setNormalImportance' => false.


From the command line

The console command addnotification creates a notification for the user with ID 1:

php application/commands/console.php addnotification --title='Message title' --body='Hello, this is message body' --importance=1

Open issues

  • Merge with Yii flash system?
  • More plugin friendly? E.g. having a message history page, adding soft delete.