0

I am fairly new to Moodle. I am working on a custom plugin in which I wanted to make a pop up that shows after the user has logged in for the first time. Exploring the forum, I came up with the idea of catching event and triggering something. Basically I tried to catch the user_loggedin event and in the observer function I tried to trigger a redirection which has my pop up but it didn't seems to work properly. I am not getting any kind of error (debugging is on) so it is hard for me to troubleshoot the actual problem. My db/events.php file:

<?php
/*
 * @package     local_message
 * @author      Kristian
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

$observer = array(
    array(
        'eventname' => '\core\event\user_loggedin',
        'callback' => '\local_custom_signup\local_first_signup_observer::first_signup',
    ),
);
//var_dump($observer);

My classes/observer.php file:

<?php
/*
 * @package     local_message
 * @author      Kristian
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace local_custom_signup;


defined('MOODLE_INTERNAL') || die();

 class local_first_signup_observer{

    public static function first_signup(\core\event\user_loggedin $event){
        global $CFG;
        redirect($CFG->wwwroot . '/local/custom_signup/signupform.php', get_string('cancelled_form', 'local_message'));
        
    }
 }
Shihab
  • 17
  • 6

1 Answers1

1

You cannot do that - event handlers cannot redirect to a new page, because Moodle is usually in the middle of making other changes (and there may well be other event handlers that are still wanting to have a go at handling that event).

That said, the most likely reason why you're not seeing this do anything, is that you've called your file "classes/observers.php", but called the class "local_first_signup_observer". Moodle has no way of guessing that it needs to look in a "observers.php" to find this class.

If you rename "observers.php" to "local_first_signup_observer.php" (and purge all caches), then it should find it (but it still won't do anything useful, as redirects aren't allowed here).

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • Hi @davosmith, Thank you so much for your answer. Your information will definitely help me throughout my Moodle journey. With that being said, I have another question. If Moodle cannot allow me to redirect somewhere, can I make a pop up window instead ( like when the login event captured it will show a pop up window before redirecting me to the dashboard ) on the observer function. – Shihab Sep 12 '22 at 16:55
  • You could possibly do that by using the before_footer hook to insert some javascript onto a page to create a popup. Alternatively, in your event handler code, you could do something a bit hacky, like saving $SESSION->wantsurl somewhere (maybe in another $SESSION variable), then replacing it with the URL you want to go to. No idea if that would actually work - I've not tried it - but maybe worth a go. In most cases where I've needed something like this, I've ended up making some small core code changes to handle it (but that's not usually the recommended route, if you can avoid it). – davosmith Sep 12 '22 at 20:04
  • Hi @davosmith. Thanks again. I did what you said. I renamed my observer.php to local_first_signup_observer.php and inside `first_signup()` function instead of redirecting I add `error_log('logged in')` just to check if that is working now. But unfortunately after I log in it shows nothing. What am I doing wrong. Is there any other way to check whether that function is working or not? – Shihab Sep 13 '22 at 06:24
  • I would suggest you investigate using xdebug with your IDE (I recommend PHPStorm, as that works very well with debugging), then simply stick a breakpoint in the login script and step through until you find out why your plugin isn't called. You may also want to increase your plugin's version number and run and upgrade, as that often clears out problems of things not getting picked up by Moodle. – davosmith Sep 13 '22 at 08:30
  • Thank you @davosmith. I will try the $SESSION thing you suggested and also the xdebug. I will get back to you soon :) – Shihab Sep 13 '22 at 12:23
  • Hi @davosmith the hacky process you suggested works exactly how I wanted. I stored the session in a variable and and replace it with a different `$CFG->url` to where I wanted to be. I had to create a different `$CFG` into config.php file. Anyway Thanks a lot. – Shihab Sep 19 '22 at 12:05