custom module not passing event observer info to lib.php function

custom module not passing event observer info to lib.php function

by Tom Tom -
Number of replies: 4

Maybe one of you can see what I am obviously overlooking. I created a module. It is an admin/tool. It has an observer that captures course completion events. If it is a specific course I invoke a function of the module to do x,y,x. If it is another specific course that has been completed I do the same--send that info to a function of the module (its lib.php file) to trigger a,b,c. The problem I'm having is I cannot get the function to be acted upon. Yes I have updated the version of my module after each change. Yes I have cleared out the cache. I have tried this on another moodle and have the same problem. The cron is running. There are not debug messages. I've tried namespaces and just calling the function. Same issue. My IDE recognizes the function with and without namespace. Permissions on the file on the server are correct as well as the owner. So I'm obviously missing something that is starring at me. I do not have an issue with capture the event or triggering it. That all works just fine. I am using tracing/logging to ensure that the code works just fine up until it gets to the lib.php function . . . just stops and doesn't go any further.

The structure of my module looks like this:

admin/tool/mytool

admin/tool/mytool/classes/

admin/tool/mytool/classes/form.php

admin/tool/mytool/classes/tool_mytool_observer.php (where I capture the course_completed event and try to invoke a fuction)

admin/tool/mytool/db

admin/tool/mytool/db/events.php

admin/tool/mytool/db/install.xml

admin/tool/mytool/upgrade.php

admin/tool/mytool/lang

admin/tool/mytool/index.php

admin/tool/mytool/lib.php (where the function is I want to use)

admin/tool/mytool/settings.php

admin/tool/mytool/version.php


My code:


admin/tool/classes/tool_mytool_observer.php:


<?php

namespace tool_mytool;

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

class tool_mytool_observer {

    public static function ccChanged(\core\event\course_completed $event) {
        global $DB;

        $courseID = $event->courseid;
        $studentID = $event->relateduserid;
        
        if($courseID==6){
      

           //this is the function that I cannot get the code to execute (or any

          //other lib.php function for that matter

           \tool_mytool\sendNotifications($studentID,'student');

      }

}


admin/tool/lib.php:


<?php

namespace tool_mytool;

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

require_once(dirname(__DIR__,3).'/lib/moodlelib.php');
require_once(dirname(__DIR__,3).'/lib/completionlib.php');

function sendNotifications($userID,$userRole){

  ....

}



So what could be preventing the code from using the functions? I've moved the functions around (I know that sounds stupid) in the lib.php file. I've tried all I know. I was going to try the "includefile" in the events callback file in the db dir but I have written other similar modules using events and had no issues with using functions in their lib.php files.


So I'm at a loss as to what is going on here. Do you see anything?





Average of ratings: -
In reply to Tom Tom

Re: custom module not passing event observer info to lib.php function

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
The two lines containing require_once(dirname(__DIR__,3) could be the problem. Why aren't you using the global $CFG variable?
But the error could also be hidden in the db/events.php file.
In reply to Renaat Debleu

Re: custom module not passing event observer info to lib.php function

by Tom Tom -
I just copied another tool module I created for role change event capturing . . . didn't have any problems with those two lines. But I will look at it though. Also again I can capture the course completion event just fine . . . I just cannot invoke the lib.php function I'm passing the data to . . . but here is the event.php . . . pretty simple I thought . . .
<?php

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

$observers = array (

array (
'eventname' => '\core\event\course_completed',
'callback' => '\tool_mytool\tool_mytool_observer::ccChanged',
),

);
In reply to Tom Tom

Re: custom module not passing event observer info to lib.php function

by Tom Tom -
Following up here for historical purposes sake . . . and anyone who has this problem . . . commenting out the two lines in the lib.php file didn't change anything. So at last resort (I've been trying since Thursday morning to figure out the problem here . . . ) I just decided to abandon the lib.php file and move the code (in several places . . . so much for avoiding duplicate code) into the observer . . . and it worked. Why is it that I can use the other events and the lib.php file with the above set up and not the \core\event\course_completed . . . well I will have to defer to someone else or maybe figure it out down the road . . . definitely a weird one.
In reply to Tom Tom

Re: custom module not passing event observer info to lib.php function

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Out of interest - at any point did you try writing:

require_once($CFG->dirroot.'/admin/tool/mytool/lib.php');

Plugin's lib.php files are not loaded automatically on demand, but you can sometimes (by chance) find that they've already been loaded by some other code.

However, putting functions in lib.php is generally a bad idea (except for ones, such as the 'pluginfile' function, which _must_ be there, in order for Moodle core code to find them). You are much better off putting your supporting functions in a suitably-named class, within the classes/ folder of your plugin and letting the automatic class loading code include it when required.
Average of ratings: Useful (1)