Event Listener on Forum - notification query

Event Listener on Forum - notification query

by Paul Vincent -
Number of replies: 2

Hi,

I'm putting together a local plugin that is successfully observing forum events (\mod_forum\event\discussion_created and \mod_forum\event\post_created), but the action I'm wanting to perform involves displaying a notice or dialog box to the user containing some of the forum object's data. I've tried using core notifications, jquery dialogs and plain js with no success, but I believe the issue is that none of these will work as the page refreshes as part of the action? Or am I doing something else incorrectly?

E.g.:

class local_verifycheck_observer {
    public static function newPost(\mod_forum\event\post_created $event) {
      echo '<script>confirm("Event Triggered and code executed."); </script>';
       sleep(2);
    }
}

The alert or dialog works for a more passive event such as Discussion viewed, so I'm guessing it's the page reload that's causing theh problem, but is there any way of addressing this?


Many thanks,

Paul


Average of ratings: -
In reply to Paul Vincent

Re: Event Listener on Forum - notification query

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Event handlers shouldn't ever be trying to output something onto the page - they will usually be triggered before the page output starts or in a script that processes something, then redirects to a new page. Extra output in either case would break the page.

Even if they happened to be send during the page drawing (which would be unusual), you would have no control about where on the page the output would occur (again, potentially breaking the layout).

You may have some mileage by using the \core\notification::add('This is my message', \core\output\notification::NOTIFY_INFO) function to add a message to show at the top of the next page that is drawn.
In reply to Davo Smith

Re: Event Listener on Forum - notification query

by Paul Vincent -
Many thanks Davo, that confirms my fear but the notification is helping for now; not sure why I couldn't get that working before but the function as you've provided outputs perfectly. Many thanks again!