Note:

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

Blocks/Appendix C

From MoodleDocs
Revision as of 13:36, 14 July 2021 by David Mudrak (talk | contribs) (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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;
}