Advertisement

Create Your First Moodle Plugin (moodle plugins chapter 2)

Moodle has 3 types of plugins viz

mod = for activity module plugins

blocks = to be pasted on a page

local = a plugin that becomes the new page
What I will explain now is the plugin type "local". The first thing you should know is the plugin structure like this.

myplugin/
├─ db/
│  ├─ install.xml
├─ lang/
│  ├─ en/
│  │  ├─ local_myplugin.php
├─ index.php
├─ lib.php
├─ version.php
has two default folders ie
db = to save the database installation
lang = to save language configuration, 
index.php = as the initial appearance of the plugin page
lib.php = custom help configuration
version = required moodle version configuration

1. Create install.xml in folder "db"
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/myplugin/db" VERSION="20130407" COMMENT="XMLDB file for local message plugin"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
 >
 <TABLES>
    <TABLE NAME="myplugin" COMMENT="each record is a message">
        <FIELDS>
            <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
            <FIELD NAME="name" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
            <FIELD NAME="address" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
            <FIELD NAME="email" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
            <FIELD NAME="phone" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
        </FIELDS>
        <KEYS>
            <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
        </KEYS>
    </TABLE>
</TABLES>
</XMLDB>


2. Next we make language settings. Inside the "lang" folder, create a language code folder, for example
"en" for english, "es" for spain, "id" for Indonesia. I will try to give an example setting for English.  create a file named local_myplugin.php in the "en" folder.
<?php

/**
 * Version information for the local_myplugin plugin.
 *
 * @package    local_myplugin
 * @copyright  2022 derillab
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

$string['pluginname'] = 'My Plugin';
$string['name'] = 'Name';
$string['email'] = 'Email';
$string['address'] = 'Address';
$string['phone'] = 'Phone';

3. Create this "lib.php" file for custom settings, but this time we won't include many settings.

<?php

/**
 * @package     local_myplugin
 * @author      Deril Lab
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function local_myplugin_before_footer()
{
	global $DB, $USER;
	echo "";
	
}


4. Create "version.php" for setting the supported moodle version
You can see the version code on the official website https://docs.moodle.org/dev/Releases
<?php

/**
 * @package     local_myplugin
 * @author      Deril Lab
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @var stdClass $plugin
 */

defined('MOODLE_INTERNAL') || die();

$plugin->component = 'local_myplugin';
$plugin->version = 2020071900;
$plugin->requires = 2016052300; // Moodle version

5. Lastly, make "index.php" for the initial initial appearance of the plugin.

<?php

/**
 * @package     local_myplugin
 * @author      Deril Lab
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

//CALL CONFIG.PHP
require_once(__DIR__ . '/../../config.php');

$PAGE->set_url(new moodle_url('/local/myplugin/index.php'));

//SET TITLE PAGE
$PAGE->set_title(get_string("pluginname", "local_myplugin"));
$PAGE->set_heading(get_string("pluginname", "local_myplugin"));

GLOBAL $DB;
GLOBAL $CFG;
GLOBAL $USER;

//PRIVATE PAGE FOR PUBLIC
require_login();

//SET BREADCRHUM
$PAGE->navbar->add(get_string("pluginname", "local_myplugin"));

//GET HEADER
echo $OUTPUT->header();

//CONTENT
$data = $DB->get_records_sql("SELECT * FROM {myplugin} LIMIT 5");

$content = '<table class="table">
<tr>
<td>'.get_string("name", "local_myplugin").'</td>
<td>'.get_string("address", "local_myplugin").'</td>
</tr>';

foreach ($data as $key => $value) {
	

	$content .= '

	<tr>
	<td>'.$value->name.'</td>
	<td>'.$value->address.'</td>
	</tr>';

}

$content .='

</table>';

echo $content;


//GET FOOTER
echo $OUTPUT->footer();


?>

6. Compress your plugin folder "zip", then install it into Moodle.





7. Look for the "mdl_myplugin" table in your Moodle database then fill in the data for testing.



8. To access your plugin i.e. "localhost/local/myplugin".



Source Example Moodle Plugin

Post a Comment

0 Comments