Calling plugin's lib.php functions from task worker

Calling plugin's lib.php functions from task worker

by Anze Pratnemer -
Number of replies: 7

Greetings,


I cannot get head around this problem: I'm developing local plugin that is gonna be run only by cron.


I have managed to get it installed, it shows up on Scheduled tasks list, all is fine and dandy. BUT!

What is the connection between \local\plugin_name\classes\tastk\worker.php and \local\plugin_name\lib.php?


My understanding is, that task worker should only trigger the function, that resides in lib.php or locallib.php, right? I've seen other plugins calling functions like enrol_get_plugin('ldap') or get_auth_plugin() or similiar. But there is no function that would work like local_get_plugin('plugin_name').


How do I create instance of plugin or access lib.php from task worker? All I want for now is to code in lib.php returns some string on the screen. Logic will follow later ofc.


My \local\plugin_name\classes\task\worker.php:

<?php

namespace local_plugin_name\task;

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

class worker extends \core\task\scheduled_task {                                                                     

    public function execute() {        

$this->plugin_name_get_data();

    }

public function get_name() {

        return get_string('plugin_name_task', 'local_plugin_name');

    }

}


My \local\plugin_name\lib.php:

<?php

defined('MOODLE_INTERNAL') || die;

function plugin_name_get_data(){

echo "echo Data gotten!";

mtrace("Mtrace here!");

}


return error:

Call to undefined method local_plugin_name\task\worker::plugin_name_get_data()

Average of ratings: -
In reply to Anze Pratnemer

Re: Calling plugin's lib.php functions from task worker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you want to access a function in local/plugin_name/lib.php, then you just need to write:

global $CFG;
require_once($CFG->dirroot.'/local/plugin_name/lib.php);
name_of_function();

enrol_get_plugin() gets you an instance of the enrolment plugin class - local plugins don't have a base class or instances, unless you define some classes for yourself.

Note you are far better to use automatic class loading + namespacing, instead of putting functions in lib.php or locallib.php - see https://docs.moodle.org/dev/Automatic_class_loading for more details.


Average of ratings: Useful (2)
In reply to Davo Smith

Re: Calling plugin's lib.php functions from task worker

by Anze Pratnemer -

Thank you for this solution.


Can we elaborate a bit more on your suggestion regarding automatic class loading?

That page and instructions are really really vague at least. For example when following that page:

I have a worker.php in \local\studissync\classes\task\:

<?php

namespace local_studissync\task;

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

class worker extends \core\task\scheduled_task {                   

    public function execute() {

if(class_exists('local_studissync_sync')){

\local_studissync\sync\local_studissync_sync::studissync_get_data();

}    }

public function get_name() {

        return get_string('studissynctask', 'local_studissync');

    }

}

I have sync.php in \local\studissync\classes:

<?php

namespace local_studissync\sync;

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

class local_studissync_sync{

public function studissync_get_data(){

}

}


and lib.php in \local\studissync\:

<?php

defined('MOODLE_INTERNAL') || die;

$instance =  new local_studissync_sync();


With this code I get error on installation:

Exception - Class 'local_studissync_sync' not found
Debug info: 
Error code: generalexceptionmessage

What am I missing?

In reply to Anze Pratnemer

Re: Calling plugin's lib.php functions from task worker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Did you bump the version number (and run an install) or purge caches?

There are a lot of classes in Moodle, so automatic class loading data is cached to make sure it runs quickly.

If the file name is: \local\studissync\classes\sync.php

Then the correct namespace is:

namespace local_studissync;

If you want a namespace of:

namespace local_studissync\sync;

Then you need to put the file in: \local\studissync\classes\sync\sync.php.


Personally, I'd organise the code like this (in \local\studissync\classes\sync.php)

namespace local_studissync;
defined('MOODLE_INTERNAL') || die();
class sync {
...
}

Then you can call:

$sync = new \local_studissync\sync();


Average of ratings: Useful (2)
In reply to Davo Smith

Re: Calling plugin's lib.php functions from task worker

by Anze Pratnemer -

Would rate it EXTRA usefull, but there is not such option.


Thank you very much, solution works now, mtrace is working.


One more question tho: where could I learn about how to start using already existing functionalities regarding creating courses and enroling users to said courses?


I know there are tons of plugin that do one or the other, but is there any tutorial or something that would guide me how to include functions like this and how to call them?


Is there a list of functions that are available to me in my plugin and how do I access them?

In reply to Anze Pratnemer

Re: Calling plugin's lib.php functions from task worker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There is some developer documentation at https://docs.moodle.org/dev/Main_Page but it doesn't cover everything.

Unfortunately, there are a huge number of functions available in Moodle (which have grown over many years), and they haven't been fully documented anywhere (other than PHPDocs in the code).

It's worth taking a look through some of the files in the lib/ directory - accesslib.php, coursecatlib.php, enrollib.php, formslib.php, moodlelib.php, outputrenderers.php, pagelib.php, setup.php and womenslib.php are all worth a look at. As is lib/dml/moodle_database.php.

After that, it is mostly a case of looking for some code that does something a bit like what you are wanting and reading the code to see how it works ...


In reply to Davo Smith

Re: Calling plugin's lib.php functions from task worker

by Garrett Boone -

Although I did not read this post before just now as I'm replying, about five minutes ago I happened upon that last file noted after reading through various files. 

Too funny. And you're right, the notes in the files are great! Much appreciated.