Blocks/Appendix C

Jump to: navigation, search
Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.

Creating Database Tables for Blocks

Important Note:

The following describes what to do in Moodle <= 1.6. For Moodle >= 1.7, you need to use XMLDB, that is, create an install.xml file, instead of *.sql files.

(See also Category:XMLDB.)

For Moodle <= 1.6

You can easily create a database table for your block by adding two files mysql.sql and postgres7.sql to a subdirectory db/ of your block. The tables HAVE TO be named the same way as the block (e.g. block_rss2_feeds) otherwise Moodle will not be able to drop the table upon removal of the block from the system. Here is an example for mysql.sql:

CREATE TABLE prefix_rss2_feeds (
 `id` int(11) NOT NULL auto_increment,
 `userid` int(11) NOT NULL default '0',
 `title` text NOT NULL default '',
 `description` text NOT NULL default '',
 `url` varchar(255) NOT NULL default '',
 `pingbacktokenurl` varchar(255) NOT NULL default '',
 PRIMARY KEY  (`id`)
) TYPE=MyISAM  COMMENT='Remote news feed information.';

Pay attention that, furthermore, you might want to add a mysql.php and postgres7.php page that contains update routines for upgrading the database tables with evolving version numbers of the block. In case you use that be sure that you provide the necessary structure (again function name should be like the block name + "_upgrade"!).

Here is an example:

function rss_pingback_upgrade( $oldversion ) {
  global $CFG;
  if ($oldversion < 2003111500) {
    // Do something ...
  }
  return TRUE;
}