Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Notifications

From MoodleDocs
Revision as of 07:35, 3 March 2016 by Andrew Nicols (talk | contribs) (Documentation on notifications)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Notifications

Moodle 3.1

New APIs were created around notifications in Moodle 3.1. These allow for notifications to be added to the session and subsequently retrieved, either during the current page load, from within the page, or on a later page.

Adding notifications

The API allows for creation of four types of notification: error, warning, info, and success.

Redirect notifications

The typical way of using these notifications is when calling the redirect() function. This takes arguments for the target URL, as well as the notification message, and the notification type.

This will likely cover a majority of uses in Moodle.

   redirect('/index.php', 'Message on redirect', null, \core\output\notification::NOTIFY_SUCCESS);

Adding other notifications

For those wishing to add notifications outside of a redirect, then the following methods exist for adding notifications:

   // Add a notification of some kind.
   \core\notification:add($message, $type);
   // Or use the following helper functions:
   \core\notification:error($message);
   \core\notification:warning($message);
   \core\notification:info($message);
   \core\notification:problem($message);

Displaying in-page notifications

It is also possible to display standard notifications from JavaScript after the page has loaded.

This can be achieved using the core/notification AMD module's addNotificiation(), or addNotifications functions.

Notifications added in this way are not added to the session. They are only displayed immediately on the page.

   require(['core/notification'], function(notification) {
     notificiation.addNotification({
       message: "Your message here",
       type: "info"
     });
     notificiation.addNotifications([
       {
         message: "Your notification here",
         type: "info"
       },
       {
         message: "Your second notification here",
         type: "success"
       }
     ]);
   });

Rendering notifications

Notifications are usually rendered automatically at the following times:

  • If notifications are added after the page heading, but before the footer, these are immediately output;
  • If notifications are added after the page footer, these are stored in the session until fetched at a later time; and
  • If notifications are added before the page heading starts, they are automatically fetched and rendered during render of the page heading (e.g. $OUTPUT->heading()). This includes those stored in the session from a previous page load.

Notifications can also be fetched using the core/notification AMD module's fetchNotifications() function.

This is called as standard by a web service shortly after page load.