How do I correctly require a dependency plugin in Moodle 3.5

How do I correctly require a dependency plugin in Moodle 3.5

by Dan Baker -
Number of replies: 10

I have an activity plugin that requires another plugin to work correctly. I followed the recommendation 

of requesting this plugin as a dependency in the version.php file like this :

$plugin->dependencies = array('local_aws' => 2017071501 ); //AWS php SDK  dependency

The problem is when I install my plugin I receive a Warning :

Warning: require_once(/var/www/moodle/local/aws/sdk/aws-autoloader.php): failed to open stream: 
No such file or directory in /var/www/moodle/mod/tutorship/lib.php on line 37
Fatal error: require_once(): Failed opening required '/var/www/moodle/local/aws/sdk/aws-autoloader.php'
(include_path='/var/www/moodle/lib/pear:.:/usr/share/php') in /var/www/moodle/mod/mymodulep/lib.php on line 37

Line 37 uses this code:


// For AWS API Part
require_once($CFG->dirroot . '/local/aws/sdk/aws-autoloader.php');

So it seems to me during the install of my plugin the require_ once is not being satisfied as the AWS plugin has yet to be installed. Well I followed the recommendation of making it a dependency in version.php but it isn't being installed before the require_ once is checked ?

How can I resolve this conundrum ?






Average of ratings: -
In reply to Dan Baker

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Ken Task -
Picture of Particularly helpful Moodlers

Think I'd make a backup of DB - sql dump and a backup of code before going further ... see way down below question ...

Looks like plugin zip doesn't include the dependencies ... AWS php SDK Amazons PHP SDK (software development kit).

Manual ...

Your plugin requires it be in
moodlecode/local/local/aws/sdk/

See if a 'aws' directory exist.  If not create it manually.
inside aws mkdir sdk if sdk doesn't exist

Then acquire via zip

wget https://github.com/aws/aws-sdk-php/releases/download/3.112.0/aws.zip

unzip ...
unzip aws.zip
see if the 'aws-autoloader.php script is here after unzipping.

Change ownerships/permissions for web service user.

Plugin work?

There are other releases ... older .. for aws php SDK
https://github.com/aws/aws-sdk-php/releases
above is latest.

** Are you sure this plugin is compat with core version of Moodle you are running?

'SoS', Ken

Average of ratings: Useful (1)
In reply to Ken Task

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Ken Task -
Picture of Particularly helpful Moodlers
Opps ... didn't notice the forum was developers. Please dis-regard. smile
Ken
In reply to Ken Task

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Dan Baker -
My plugin installs without warning or issues if the aws dependency plugin is installed first.
In reply to Dan Baker

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Dan,

maybe you could use a locallib.php which can do the require_once. In all your files where you require the lib.php you have to change to require the locallib.php.
While the lib.php is required during the installation the locallib.php will not be loaded at this time.

Best regards
Andreas

Average of ratings: Useful (2)
In reply to Dan Baker

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Your upgrade or install procedure uses a function from mod/tutership/lib.php or mod/mymodulep/lib.php, but during installation or upgrading you cannot be sure that these libraries are working (yet). Move the complete install and upgrade procedure of the tutorship and/or mymodulep module into  db/upgrade.php files, without calling functions or making referrals to these libraries.

 

 

Average of ratings: Useful (1)
In reply to Dan Baker

Re: How do I correctly require a dependency plugin in Moodle 3.5

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

As far as I am aware, dependency declarations stop a plugin from installing if the dependent plugin isn't present, but it does not automatically cause the dependent plugin to be installed. (I may be wrong, as I always work with the code directly and have never used the automatic Moodle plugin installation process).

Generally you should avoid loading a large library with a require_once at the top of your plugin's lib.php, as that file is accessed from a lot of other places throughout Moodle. Putting the require within a function, or at the top of one of your library classes within the classes/ directory would be a much better way of handling it (but do avoid using the locallib.php recommended by one of the other posters - use of locallib.php is now discouraged in favour of autoloading classes within the classes/ directory).
Average of ratings: Useful (1)
In reply to Davo Smith

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators
Hi Davo,
in general you are right in what you say. You should not abuse the locallib.php. But Dan tries to load an autoloader from an external library and he needs this library essentially like the config.php. I don't know if that is really good placed in an additional class that only is created to load an external autoloader. I'd say there is no advantage of this.
On the other way all core modules (/mod) have a locallib.php to load the external libs like "filelib.php" and so on. Why should that be wrong?
Just my opinion.
Average of ratings: Useful (1)
In reply to Andreas Grabs

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Dan Baker -
Thanks for these tips. 

 Is there also another possibility worth exploring  -  Could I  bundle the aws plugin within mine and then I can just change the path to :

require_once($CFG->dirroot.'/mod/tutorship/aws/sdk/aws-autoloader.php


This  way  it  would  get  installed  with  my  plugin  without  a  need for an external  plugin.

I am  looking for the simpler  coding  solution. 
In reply to Dan Baker

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Yes, you can put required third-party library inside your plugin. It is not necessarily the best way (what if two different plugins need the same library?) but it is the simplest way.

And, it is normally worth getting things working the simplest possible way before you try to make them work better.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: How do I correctly require a dependency plugin in Moodle 3.5

by Dan Baker -
It works but I needed to increase max_execution time in the php.ini file as it is now quite a big plugin and  needs more  time  to install properly. 
 
I wonder if I can reduce the size by using only the APIs I need in the SDK folder.Ill put that question to the aws plugin developers.