Moodle plug-in development series--XMLDB Editor

Source: Internet
Author: User

Moodle plug-in development series--XMLDB Editor

Location: Site Administration > Development > XML Editor

The XML Editor is a tool for making install.xml files, and Install.xml is a canonical file that specifies Moodle to establish database tables.

It makes editing of tables/fields/keys/indexes simple, allowing developers to use time for coding and improvement rather than errors caused by XML files and manual edits.

L Moodle, all install.xml files are in the DB directory of each plugin, which can be edited with a few clicks and keystrokes (we recommend this). Those install.xml will contain all the information needed to generate a specific object to support each RDBMS.

Note: To handle files Well, the Web server requires write access to all DB directories (the Install.xml file itself). If you cannot click or load or create a link, this means that you do not have the Create/DB directory, or the directory is not writable.

Premise: Plug-in has been installed normally, sample plug-in is simpledatabase, the plug-in type is block (section).

    1. Quick Start

Use the XML editor to create a table for the new plug-in:

The DB directory of the plugin, such as blocks/simpledatabase/db, ensures that the network server has write access to the directory. If not, refer to modify permissions:

L Moodle, navigate to Web site management, development, XMLDB editor

L You should now see a list of the locations of the XML databases in/blocks/simpledatabase/db, and the Create button should be clickable (blue font). (If you see the plugin directory, but the Create button is not point, make sure the Web server has write permissions for this directory!) )

l Click [Create]

This will create a new Install.xml file under the/blocks/simpledatabase/db directory. Click [XML] to browse install.xml content, as follows:

Content Note: ID is the default table primary key, type int, length 10, and the default table name is ' Block_simpledatabase ', while the table name in the actual database asks "Mdl_block_simpledatabase" (the default table prefix is Mdl_, automatically added).

l Click [Load]. (This loads the contents of the Install.xml file into memory)

l Click [Edit]

Under this page, you can create a new table and manipulate the current table.

L Click on the table name, i.e. Block_simpledatabase

You can edit information such as table fields, keys, indexes, and so on.

L Now you can use the XML editor to create and edit your plugin tables.

By prompting to edit the table, each operation can view the appropriate SQL or PHP code.

If modified, the page will prompt "modified to save" similar hints, such as.

Demo Action: Add a field

1) Click "New Field"

2) Fill in the new field information, then click "Change"

This is the page has been prompted to change, display the new field information below, while the [Save] button also shows processing, click [Save], the new field information will be saved in the earlier Install.xml file.

3) View Install.xml content

Go back to the XMLDB editor list and open the corresponding XML file with the following:

Special note: After saving, you need to uninstall the plugin, reinstall, only then, the plug-in database table will be created.

Install.xml It's just the structure of the database table that records the plug-in, not the actual database table operation.

Plug-in upgrade, you need to manually create a upgrade.php file in the DB directory. This upgrade.php file should look like this:

<?php

function Xmldb_mymodule_upgrade ($oldversion) {
Global$CFG;

$result = TRUE;

Insert PHP code from XMLDB Editor here

Return$result;
}
?>

To get the "//insert PHP code here" code (that is, PHP that implements database table operations, the editor provides), open the XML editor, load the associated install.xml file, and edit the file.

Select the "View PHP" option,

Then choose the action "add Field" (Add fields), and then copy and paste the generated code.

For example, add the Flag field above, the PHP code for the operation is below the gray box content, Copy the code into upgrade.php, and the Flag field is added during the plug-in upgrade, you need to adjust the version number of the plug-in (version.php file), larger than the old version number, and replace the xxxxxxxxxx in the code with the new version number (recommended) to avoid unknown errors.

For example, the old version number is 2016022400, then the new version number can be 2016022401, larger than the old version.

During the upgrade process, the field is verified to exist in the plug-in table, that is:

if (! $dbman->field_exists ($table, $field)) {

$dbman->add_field ($table, $field);

}

Note: The XMLDB editor simply stores the database table structure in XML format and does not change the database. and upgrade.php content is the actual implementation of database operations, the completion of database additions and deletions and other changes. and the corresponding PHP code can be provided through the XMLDB editor. You can refer to the new Field Operation above.

At this point, because the plug-in version number has not changed, the upgrade process will not be performed, you need to change the version number of the plug-in version number, so that the system will detect the need to upgrade.

For example: version.php in version 2015110604, then need to increase (how much does not matter), to 2015110605, while the upgrade.php file version number to be the same as the modified.

    1. Use

L XMLDB Editor is easier to use. It is highly recommended to actually operate for a period of time to see how it works and how to modify the Install.xml file.

L This is a top-down organizational structure, starting with loading (or creating) a new Xmldb file. You can edit the file, and the file base structure is displayed for processing. There are two types of elements in the structure: tables and statements, and the XMLDB Editor allows you to add, edit, delete, and move.

When you edit a table, you can see and easily work with the fields, keywords, and indexes of the table. Note that some fields are not editable because they may be used as part of a key or index.

L fields can be edited, specifying their name, type, length, decimal, empty, default, and so on. The keys and indexes are the same.

All the Xmldb editor pages allow you to annotate changes to input items (tables, indexes, key areas, declarations). Using it, you hope that it can help other developers understand a little about the database model.

L If you want to define a field of an enumerated type, you should provide a comma-delimited list, with each option surrounded by single quotation marks. For example: "Options", ' option2 ', ' Option3 '. However, enumerations are obsolete in Moodle 2, so try to avoid them.

    1. Convention (Reference)

In addition to the guidelines for database structure, you should follow some more prescriptive rules:

3.1 About Name:

A) All lowercase names (tables, indexes, keys, and fields).

b) Table and field names must use a-Z, 0-9, and _ characters only. Maximum of 28 characters.

c) The key and index names in the Xmldb file must be formed with the "-" (minus) character to concatenate the field names.

D) The primary key must be named "PRIMARY".

e) It is strongly recommended to avoid the retention of names.

3.2 About null values

A) Avoid using the default value "" (empty string) to create all the fields.

3.3 About foreign keys

A) under the table for each XML file, you must define the existing foreign key (FK) correctly. This will allow everyone to know a better structure that allows development to a better constrained system in the future and will provide the necessary information to create the appropriate indicator of the underlying code.

b) Note that if you define any combination of fields as FK you will not create any indexed fields, and the code will do it automatically!

c) Respect for the Convention 3.1-c)

3.4 About unique Keys

A) Declare any domain unique key (UK) if they intend to target as a FK (foreign key). Create a unique index.

b) Respect for the Convention 3.1-c)

    1. See

L XMDB defines an XML structure

Installing and upgrading the Plugin database table

L Use Moodle xmldb Editor Forum discussion

Appendix B-From an XML editor that introduces Moodle programming courses

Moodle plug-in development series--XMLDB Editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.