Skip to content

Skeleton

Creating a plugin skeleton for a Moodle plugin can be done in two primary ways. One involves using the Moodle Plugin Skeleton Generator tool, which can be installed as a plugin in Moodle, and the other is manually creating the plugin skeleton, which commonly includes copying code from an existing plugin.

Using the Moodle Plugin Skeleton Generator Tool

The Moodle Plugin Skeleton Generator tool automates the creation of the initial plugin structure, ensuring that the generated skeleton adheres to Moodle's coding standards and best practices. This tool can be installed directly as a plugin in your Moodle site.

  1. Install the Plugin Skeleton Generator Plugin:

    git clone https://github.com/mudrd8mz/moodle-tool_pluginskel MOODLE_ROOT_DIRECTORY/admin/tool/pluginskel
    

  2. Access the Plugin Skeleton Generator: Once installed, go to

    Site administration > Development > Moodle Plugin Skeleton Generator.

  3. Generate the Plugin Skeleton:

    • Open the Plugin Skeleton Generator page.
    • Fill in the required details such as the plugin type (e.g., mod for activity modules), plugin name (e.g., mymodule), and any other configuration options.
    • Click the "Generate" button to create the plugin skeleton.
  4. Review the Generated Files:

    • The tool will create a new directory with the name of your plugin (e.g., mymodule) inside the appropriate plugin directory (mod for activity modules).
    • This directory will contain all the necessary files and folders, such as version.php, lib.php, view.php, and more.

2. Manually Creating the Plugin Skeleton

If you prefer or need more control over the initial setup, you can manually create the plugin skeleton. This involves setting up the directory structure and creating the essential files yourself. It is also common to copy the code from an existing plugin to serve as a starting point.

Create the Plugin Directory: - Navigate to the appropriate directory (e.g, mod for activity modules) in your Moodle installation and create a new directory for your plugin.

```bash
cd /path/to/moodle/mod
mkdir mymodule
cd mymodule
```

Create Essential Files and Directories: - Set up the initial structure by creating the necessary files and subdirectories.

```bash
mkdir backup classes db lang pix
touch version.php index.php view.php lib.php mod_form.php settings.php
```

OR

Copy Code from an Existing Plugin: - Find an existing plugin similar to the one you want to create. Copy its directory structure and files into your new plugin directory. - Rename the files and update their content to match the new plugin’s name and functionality.

For example, if copying from an existing module called `mod_example`, you would:

```bash
cp -r /path/to/moodle/mod/example/* /path/to/moodle/mod/mymodule/
cd /path/to/moodle/mod/mymodule/
```

Update File Contents: - Edit the copied files to replace instances of the old plugin's name with the new plugin's name (example to mymodule). - Update the metadata in version.php, database schema in db/install.xml, language strings in lang/en/mymodule.php, and other essential configurations.

Conclusion

Using the Moodle Plugin Skeleton Generator:

  • Advantages: Faster, ensures adherence to standards, less error-prone.
  • Steps: Install the generator as a plugin, fill in details, generate skeleton.

Manually Creating the Plugin Skeleton:

  • Advantages: More control and flexibility, better understanding of structure.
  • Steps: Create directories and files or copy code from an existing plugin, update and implement basic functionality.

Both methods are valid and can be chosen based on your specific needs and preferences. The Moodle Plugin Skeleton Generator tool, when installed as a plugin, simplifies the process significantly and is recommended for beginners or those looking to save time. Manually creating the skeleton, especially by copying from an existing plugin, offers greater control and is beneficial for understanding the underlying structure and code.