The best way to ensure a certain plugin is installed in Moodle

The best way to ensure a certain plugin is installed in Moodle

by Vitaly Potenko -
Number of replies: 3
Picture of Core developers Picture of Plugin developers

Hi! What's the best way to check in code that some plugin (say, by its shortname - the plugin directory name) is installed in system? What functions or classes should I use to do it?

Average of ratings: -
In reply to Vitaly Potenko

Re: The best way to ensure a certain plugin is installed in Moodle

by Rex Lorenzo -

Try look at the lib/moodlelib.php function moodle_needs_upgrading. It calls useful functions like get_plugin_list().

Average of ratings: Useful (1)
In reply to Rex Lorenzo

Re: The best way to ensure a certain plugin is installed in Moodle

by Vitaly Potenko -
Picture of Core developers Picture of Plugin developers

Thanks Rex, I'm aware of this function and some other functions which Moodle uses for installation and upgrading. I just hoped that Moodle has a brilliant function, something like plugin_is_installed($pluginname, ...) so I just write a line of code and that's it. A good reason to make it myself.

In reply to Vitaly Potenko

Re: The best way to ensure a certain plugin is installed in Moodle

by Jitendra Gaur -

Hi Vitaly,

You can use moodle(M2.3) default plugin manager code.

require_once('config.php');
require_once($CFG->libdir . '/pluginlib.php');


$pluginman = plugin_manager::instance();
$plugin_info = $pluginman->get_plugin_info('qtype_calculated');

print_object($plugin_info);

plugininfo_qtype Object
(
    [type] => qtype
    [typerootdir] => /Users/jitendra/Sites/moodle_23/question/type
    [name] => calculated
    [displayname] => Calculated
    [source] => std
    [rootdir] => /Users/jitendra/Sites/moodle_23/question/type/calculated
    [versiondisk] => 2012061700
    [versiondb] => 2012061700
    [versionrequires] => 2012061700
    [dependencies] => 
    [instances] => 
    [sortorder] => 
    [availableupdates] => 
)

You can pass the plugin name is the get_plugin_info() code and It will tell you about the plugin.But the problem with this code is that you have to specify the plugin type.

Hope it will help you.