user6796243
user6796243

Reputation: 43

Moodle plugin : check if admin + add link to plugin in administration

I'm new to moodle plugin development and am trying to create a plugin that displays a page to the admin where I can add my on php code.

In brief, what I want the plugin to do I have already achieved in a standard php file that I upload to the moodle root. From here you can call the file e.g. yourdomain.co.uk/moodlelocation/myfile.php and it will run as expected.

The problem with this is it isn't secure since anyone can load the myfile.php and in turn run the scripts on the page. It also means any one else using this script (it will be given away for free when complete) would need to FTP into their hosting and upload two php files to their moodle install.

Due to this, I thought a plugin (a very very basic plugin) may be the best solution. They could then load the page in the admin via the "site administration". e.g. site administration > Development > MyPlugin. I am assuming I could then also restrict the plugin's main page to admins only (??).

So to recap, I can create a php page that has my script all rocking and rolling BUT I need to make this into a plugin.

I did some reading and I think a "local" plugin was the easiest way to go (??).

I have managed to get the local plugin up and running using the below in local/webguides/inex.php :

<?php

// Standard config file and local library.

require_once(__DIR__ . '/../../config.php');

// Setting up the page.

$PAGE->set_context(context_system::instance());

$PAGE->set_pagelayout('standard');

$PAGE->set_title("webguides");

$PAGE->set_heading("webguides");

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

// Ouput the page header.

echo $OUTPUT->header();

echo 'MY php CODE here etc';

?>

This works fine but with two problems:

  1. Anyone can access it via http://domain/local/webguides/index.php
  2. There is no link to it in the site administration (so the user would need to type the URL in).

Can anyone shed any light how I would achieve the two steps above?

Thanks in advance

p.s. ideally I'd like to keep the plugin to as few files as possible so if the required code could be added to the local/webguides/index.php file it would be preferred.

Upvotes: 0

Views: 1467

Answers (1)

Russell England
Russell England

Reputation: 10241

You need to create a capability, then require that capability before displaying the page.

First, have a look at local/readme.txt - this gives an overview of the files needed for a local plugin.

Or read the documentation at https://docs.moodle.org/dev/Local_plugins

Also have a look at existing local plugins so you can see how they are created - https://moodle.org/plugins/?q=type:local

At a bare minimum, you need

local/webguides/db/access.php - this will have the capability
local/webguides/lang/en/local_webguides.php
local/webguides/version.php

Plus your index file

local/webguides/index.php

In the db/access.php file have something like

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

$capabilities = array(

    'local/webguides:view' => array(
        'captype' => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
        'archetypes' => array(
        ),
    ),

);

You might also need 'riskbitmask' => RISK_XXX depending on if there are any risks in you code. Such as RISK_CONFIG, RISK_PERSONAL, etc.

In lang/en/local_webguides.php have something like

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

$string['pluginname'] = 'Webguides';
$string['webguides:view'] = 'Able to view webguids';

In version.php have something like

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

$plugin->version   = 2020051901;        // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires  = 2015051109;        // Requires this Moodle version.
$plugin->component = 'local_webguides'; // Full name of the plugin (used for diagnostics).

Replace 2015051109 with the version of Moodle you are using - this will be in version.php in the root folder.

Then in your index.php file use this near the top.

require_capability('local/webguides:view', context_system::instance());

So only users with that capability will have access to the page.

EDIT:

You can add a link via settings.php using something like

defined('MOODLE_INTERNAL') || die;

if ($hassiteconfig) {
    $page = new admin_externalpage(
        'local_webguides',
        get_string('pluginname', 'local_webguides'),
        new moodle_url('/local/webguides/index.php'),
        'local/webguides:view'
    );

    $ADMIN->add('localplugins', $page);
}

Then in your index page ad this

require_once($CFG->libdir.'/adminlib.php');

and remove require_login() and require_capability() and replace with

admin_externalpage_setup('local_webguides');

Upvotes: 2

Related Questions