install.php (what is the purpose of install.php file in activity)

install.php (what is the purpose of install.php file in activity)

by jeff hardy -
Number of replies: 5

what is the purpose of install.php file in activity

inside db folder

Average of ratings: -
In reply to jeff hardy

Re: install.php (what is the purpose of install.php file in activity)

by Randy Thornton -
Picture of Documentation writers
It calls the function that uses the install.xml file to create the relevant database table(s) for that activity during install.
In reply to jeff hardy

Re: install.php (what is the purpose of install.php file in activity)

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
You don't have to have an install.php file as part of your plugin. You do need to have install.xml for an activity to create the main activity table and any additional tables you might need for your plugin.

install.php is used if you have any processes or data perhaps that need to be added at install time. I've had a quick glance through the default mod plugins that ship with Moodle, and only 2 have anything in install.php beyond a placeholder function. mod_lti and mod_survey. mod_lti checks that some key has been defined somewhere else, and mod_survey loads up its tables with some data.

If you do need to use install.php, you create a function named: xmldb_yourpluginname_install().

Of more use is the upgrade.php file. So it's well worth having a dig around existing plugins to see what they're doing there.
Average of ratings: Useful (1)
In reply to Mark Sharp

Re: install.php (what is the purpose of install.php file in activity)

by Randy Thornton -
Picture of Documentation writers
Mark,

Thanks for the full explanation. I should have linked to here before: https://docs.moodle.org/dev/Plugin_files#db.2Finstall.php

I noticed also that some default mods don't have this file. Some third party plugins don't either. Upgrade.php seems to be just that, upgrades to the tables, if any. For example, the upgrade file for mod chat basically does nothing.

So for mods without an install.php, where does the process to make the tables with install.xml get called the first time?

Randy
In reply to Randy Thornton

Re: install.php (what is the purpose of install.php file in activity)

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Moodle will just detect that this is a new plugin and look for the install.xml file, you don't need to do anything.

With the upgrade.php file, the important thing to remember is to update the install.xml file with new fields and tables you might introduce in the upgrade.
In reply to Mark Sharp

Re: install.php (what is the purpose of install.php file in activity)

by Randy Thornton -
Picture of Documentation writers
Mark,

Ah, good. I have looked at several plugins to see how they do it and what you say makes perfect sense. Upgrade.php is often checking versions to decide what to do, and new fields are added in the install.xml file just as you say. I think I understand it now.

Thanks for your help with this.

Randy