$PAGE context error when trying to send message

$PAGE context error when trying to send message

by Ben Laor -
Number of replies: 3

Hi everyone smile
I'm trying to send a message through the message api (as an email) but i get the following error:

"Coding problem: $PAGE->context was not set. You may have forgotten to call require_login() or $PAGE->set_context(). The page may not display correctly as a result

  • line 490 of /lib/pagelib.php: call to debugging()
  • line 817 of /lib/pagelib.php: call to moodle_page->magic_get_context()
  • line 1420 of /lib/weblib.php: call to moodle_page->__get()
  • line 5853 of /lib/moodlelib.php: call to format_string()
  • line 94 of /message/output/email/message_output_email.php: call to email_to_user()
  • line 142 of /lib/classes/message/manager.php: call to message_output_email->send_message()
  • line 104 of /lib/classes/message/manager.php: call to core\message\manager::send_message_to_processors()
  • line 251 of /lib/messagelib.php: call to core\message\manager::send_message()
  • line 35 of /blocks/myblock/cert_view.php: call to message_send()"

    here is my code:

    "require(__DIR__ . '/vendor/autoload.php');
    require_once('../../config.php');
    require_once(__DIR__ . '/config.php');

    global $BU_CFG;

    use mikehaertl\wkhtmlto\Pdf;

    // Check for all required variables.
    $course_id = required_param('course_id', PARAM_RAW);
    $user_id = required_param('user_id', PARAM_RAW);

    $url = isset($_GET['url']);

    require_login();

    if ($url) {

        // Build the message object
        $message = new \core\message\message();
        $message->name            = 'message_name';
        $message->component       = 'myblock';
        $message->userfrom        = $BU_CFG->BOT_ID;
        $message->userto          = 490;
        $message->subject         = 'message subject';
        $message->fullmessagehtml = 'my message html';
        $message->smallmessage    = 'my message small';

        $result = message_send($message);

        echo 'נשלחה בהצלחה!';
    } else {..."

    As you can see i have "require_login();" in my code so i don't really understand what is wrong...
    Also i made sure the "$message->name" is the same name that i defined in the "messages.php" file.

Moodle version: 3.2.1


Average of ratings: -
In reply to Ben Laor

Re: $PAGE context error when trying to send message

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Look at other places that use 'require_login()' and you may have to do a '$PAGE->set_context($context);' too.

In reply to Gareth J Barnard

Re: $PAGE context error when trying to send message

by Ben Laor -
Thank you! i did what you said and sure enough the error disappears, i added:

"$context = context_course::instance($course_id);
$PAGE->set_context($context);
"
following my "require_login();" call.

But... Now i get another error message:

"message_sent::create_from_ids() needs a $courseid to be passed, nothing was detected. Please, change the call to include it, using SITEID if the message is unrelated to any real course.
  • line 68 of /lib/classes/event/message_sent.php: call to debugging()
  • line 155 of /lib/classes/message/manager.php: call to core\event\message_sent::create_from_ids()
  • line 104 of /lib/classes/message/manager.php: call to core\message\manager::send_message_to_processors()
  • line 251 of /lib/messagelib.php: call to core\message\manager::send_message()
  • line 38 of /blocks/myblock/cert_view.php: call to message_send()"

    How would i even pass a courseid to that method? i'm not even using that class directly...

In reply to Ben Laor

Re: $PAGE context error when trying to send message

by Ben Laor -

Well i just set "$message->courseid = $course_id" and it seems to have solved the error.
Thank you for the help smile