Note:

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

Notifications: Difference between revisions

From MoodleDocs
(Documentation on notifications)
 
(Removed \core\notification::problem($message); because it doesn't work - the function doesn't exist in current moodle)
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Notifications
= Notifications =
 
{{Moodle 3.1}}
Moodle 3.1


New APIs were created around notifications in 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.
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.
'''NOTE:''' Not to be confused with sending an arbitrary user either a 1-1 message, a group message, or a system message which are also called "Notifications", please see the [[Message API]].


== Adding notifications ==
== Adding notifications ==
Line 20: Line 21:


     // Add a notification of some kind.
     // Add a notification of some kind.
     \core\notification:add($message, $type);
     \core\notification::add($message, $type);


     // Or use the following helper functions:
     // Or use the following helper functions:
     \core\notification:error($message);
     \core\notification::error($message);
     \core\notification:warning($message);
     \core\notification::warning($message);
     \core\notification:info($message);
     \core\notification::info($message);
     \core\notification:problem($message);
     \core\notification::success($message);


=== Displaying in-page notifications ===
=== Displaying in-page notifications ===
Line 35: Line 36:
Notifications added in this way are ''not'' added to the session. They are only displayed immediately on the page.
Notifications added in this way are ''not'' added to the session. They are only displayed immediately on the page.
     require(['core/notification'], function(notification) {
     require(['core/notification'], function(notification) {
       notificiation.addNotification({
       notification.addNotification({
         message: "Your message here",
         message: "Your message here",
         type: "info"
         type: "info"
       });
       });


       notificiation.addNotifications([
       notification.addNotifications([
         {
         {
           message: "Your notification here",
           message: "Your notification here",

Latest revision as of 12:31, 14 October 2020

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.

NOTE: Not to be confused with sending an arbitrary user either a 1-1 message, a group message, or a system message which are also called "Notifications", please see the Message API.

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::success($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) {
     notification.addNotification({
       message: "Your message here",
       type: "info"
     });
     notification.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.