May 7, 2025 3 min read

Creating a Moodle Plugin for Moodle 5.0

Creating a Moodle Plugin for Moodle 5.0

Creating a plugin for Moodle 5.0 can be an exciting way to extend the platform's functionality to suit your educational needs. In this guide, we’ll walk through the essential steps of building a Moodle plugin, focusing on setting up a development environment, creating the plugin structure, adding core features, testing, and deployment.


1. Introduction: Why Create a Moodle Plugin?

Moodle is a highly customizable learning management system (LMS) widely used for online education. While Moodle offers a rich set of features, there are times when you need to develop custom functionality. Creating a plugin allows you to add specific features tailored to your institution’s requirements, such as displaying personalized data or integrating external systems.

2. Getting Started: Setting Up Your Development Environment

To create a Moodle plugin, follow these steps to prepare your environment:

  • Install Moodle 5.0: Set up a local Moodle instance using a web server like Apache, PHP (at least 8.0), and a database like MySQL.

  • Development Tools: Use a code editor like Visual Studio Code, Git for version control, and Composer for PHP dependency management.

  • Moodle Developer Mode: Enable debugging to catch errors during development by adding the following to config.php:

    $CFG->debug = (E_ALL | E_STRICT);
    $CFG->debugdisplay = 1;
    
  • Directory Structure: Moodle plugins reside in subdirectories of the /moodle/local/ or /moodle/mod/ folder, depending on the plugin type.

In Moodle, the directory structure for plugins is as follows:

  • Blocks: /blocks/
  • Modules (Activities): /mod/
  • Local Plugins: /local/
  • Reports: /report/
  • Auth Plugins: /auth/
  • Themes: /theme/
  • Course Formats: /course/format/

3. Creating the Basic Plugin Structure

Start by creating your plugin directory under /moodle/local/:

mkdir -p moodle/local/hashplugin
cd moodle/local/hashplugin

Inside this directory, create the following essential files:

  • version.php - Defines plugin metadata.
  • lang/en/local_hashplugin.php - Holds language strings.
  • db/access.php - Defines capabilities.
  • settings.php - Adds plugin configuration.
  • lib.php - Main logic.
  • index.php - Front-end page.
  • renderer.php - Defines how the plugin outputs content.

Example: version.php

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

$plugin->component = 'local_hashplugin';
$plugin->version = 2025050700;
$plugin->requires = 2024040100; // Moodle 5.0
$plugin->maturity = MATURITY_STABLE;
$plugin->release = 'v1.0';

4. Implementing Core Features

a. Displaying Student Hash

In lib.php, add a function to generate and display a hash:

function local_hashplugin_display_user_hash($userid) {
    global $DB;
    $user = $DB->get_record('user', array('id' => $userid));
    if ($user) {
        $hash = hash('sha256', $user->username);
        return "User Hash: " . $hash;
    }
    return "User not found.";
}

b. Flag Generation and Validation

In lib.php, add the flag generation logic:

function local_hashplugin_generate_flag($userid, $coursekey) {
    global $DB;
    $user = $DB->get_record('user', array('id' => $userid));
    if ($user) {
        $userhash = hash('sha256', $user->username);
        $flag = hash('sha256', $userhash . '-' . $coursekey);
        return "Flag: " . $flag;
    }
    return "Error generating flag.";
}

Display Example (index.php):

require('../../config.php');
require_once($CFG->dirroot . '/local/hashplugin/lib.php');

$userid = required_param('id', PARAM_INT);
echo local_hashplugin_display_user_hash($userid);

5. Testing the Plugin

  • Unit Testing: Write unit tests using Moodle’s built-in testing framework located in /tests.
  • Functional Testing:
    • Navigate to Site administration -> Plugins -> Local plugins.
    • Ensure the plugin appears and runs without errors.
  • Debugging: Check logs in moodledata for any warnings or issues.

Example Test Case:

public function test_display_user_hash() {
    $userid = 2;
    $output = local_hashplugin_display_user_hash($userid);
    $this->assertStringContainsString("User Hash:", $output);
}

6. Packaging and Deploying

  • Code Check: Run Moodle’s code checker tool to ensure compliance.
  • Package the Plugin:
zip -r hashplugin.zip local/hashplugin
  • Install the Plugin:

    • Upload the ZIP via Site administration -> Plugins -> Install plugins.
    • Follow the on-screen instructions to complete installation.
  • Upgrade Moodle Database:

php admin/cli/upgrade.php

7. Conclusion: Best Practices and Tips

  • Follow Moodle Coding Guidelines: This ensures compatibility and maintainability.
  • Use Moodle APIs: Always leverage built-in functions to avoid breaking changes in future Moodle updates.
  • Version Control: Maintain your plugin with version numbers and changelogs for each release.
  • Community Feedback: Share your plugin on Moodle's plugin directory to gather feedback and suggestions.

By following these steps, you can create a functional and robust Moodle plugin tailored to your specific requirements. Happy coding!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Nimbus Code.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.