Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Upgrade API: Difference between revisions

From MoodleDocs
(New page: ==Introduction== If you have done the right thing, Moodle will automatically create the database tables for your plugin when you visite the Admin notifications page (.../admin/index.php)....)
 
Line 28: Line 28:
==The files you need for the second release==
==The files you need for the second release==


;version.php
;version.php<pre>$plugin->version  = 2008080200;
$plugin->version  = 2008080200;
$plugin->requires = XXXXXXXXXX; // Copy the current value from the top-level version.php file.</pre>
$plugin->requires = XXXXXXXXXX; // Copy the current value from the top-level version.php file.
;db/install.xml : This file should now contain the updated definition for your mdl_question_myqtype table, with three columns col1, col2 and newcol.
;db/install.xml : This file should now contain the updated definition for your mdl_question_myqtype table, with three columns col1, col2 and newcol.
;db/upgrade.php
;db/upgrade.php

Revision as of 17:07, 31 July 2008

Introduction

If you have done the right thing, Moodle will automatically create the database tables for your plugin when you visite the Admin notifications page (.../admin/index.php). This process is controlled by three files within your plugin:

version.php
This records the version of the plugin code
db/install.xml
This is used when someone installs your plugin for the first time.
db/upgrade.php
This is used when someone who had an older version of your plugin installed upgrades to the latest version.

In addition, Moodle also stores in the database, the currently installed version of each plugin. This is normally stored in the mdl_config table, in a row with name something like plugintype_pluginname_version. For example qtype_myqtype_version. The exception to this rule are modules and blocks. Installed module version numbers are stored in the mdl_modules table. Block version numbers are in mdl_block.

A specific example

For the rest of this document, I will use a particular example, becuase it should make the explanation easier. You should be able to see how to generalise it.

We will suppose you that you are making a new question type myqtype. This is plugin type qtype, and the code will be in the question/type/myqtype folder. The currently installed version number will be stored in the qtype_myqtype_version row of the mdl_config table.

In addition, we will just consider the first two releases of the plugin. The first release will have version number 2008080100, and will just use one database table mdl_question_myqtype, with two columns col1 and col2. Then we will suppose that the second release is 2008080200, and that requires an extra column, newcol, to be added to the mdl_question_myqtype table.

The files you need for the first release

version.php
 $plugin->version  = 2008080100;

$plugin->requires = XXXXXXXXXX; // Copy the current value from the top-level version.php file.

db/install.xml
This file, which you should create with the XMLDB editor, should contain the definition for your mdl_question_myqtype table, with the two columns col1 and col2.

At this stage, you do not need a db/upgrade.php file.

The files you need for the second release

version.php
$plugin->version  = 2008080200;

$plugin->requires = XXXXXXXXXX; // Copy the current value from the top-level version.php file.

db/install.xml
This file should now contain the updated definition for your mdl_question_myqtype table, with three columns col1, col2 and newcol.
db/upgrade.php
This file should contain the code that people need to run to upgrade from version 2008080100 of your plugin. That is, the code to add a column newcol to the mdl_question_myqtype table. The upgrade.php file should contain a single function xmldb_qtype_myqtype_upgrade that looks a bit like:

function xmldb_qtype_myqtype_upgrade($oldversion = 0) { $result = true; /// Add a new column newcol to the mdl_question_myqtype if ($result && $oldversion < 2008080200) { // Code to add the column, generated by the 'View PHP Code' option of the XMLDB editor. } return $result; }