【问题标题】:Moodle plugin : check if admin + add link to plugin in administrationMoodle插件:检查管理员是否在管理中添加插件链接
【发布时间】:2020-05-20 20:08:57
【问题描述】:

我是 moodle 插件开发的新手,我正在尝试创建一个插件,向管理员显示一个页面,我可以在其中添加我的 php 代码。

简而言之,我希望插件执行的操作已经在我上传到 Moodle 根目录的标准 php 文件中实现。从这里您可以调用文件,例如yourdomain.co.uk/moodlelocation/myfile.php 它将按预期运行。

这样做的问题是它不安全,因为任何人都可以加载 myfile.php 并反过来在页面上运行脚本。这也意味着使用此脚本的任何其他人(完成后将免费赠送)需要 FTP 到他们的主机并将两个 php 文件上传到他们的 Moodle 安装。

因此,我认为一个插件(一个非常非常基本的插件)可能是最好的解决方案。然后他们可以通过“站点管理”在管理员中加载页面。例如站点管理 > 开发 > MyPlugin。我假设我还可以将插件的主页限制为仅限管理员 (??)。

所以回顾一下,我可以创建一个 php 页面,让我的脚本全都摇摆不定,但我需要把它做成一个插件。

我做了一些阅读,我认为“本地”插件是最简单的方法(??)。

我已经设法在 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';

?>

这很好用,但有两个问题:

  1. 任何人都可以通过http://domain/local/webguides/index.php访问它
  2. 站点管理中没有指向它的链接(因此用户需要输入 URL)。

谁能阐明我将如何实现上述两个步骤?

提前致谢

附言理想情况下,我希望将插件保留在尽可能少的文件中,因此如果可以将所需的代码添加到 local/webguides/index.php 文件中,那将是首选。

【问题讨论】:

    标签: php moodle


    【解决方案1】:

    您需要创建一个功能,然后在显示页面之前需要该功能。

    首先,查看local/readme.txt - 它概述了本地插件所需的文件。

    或阅读https://docs.moodle.org/dev/Local_plugins的文档

    还可以查看现有的本地插件,以便了解它们是如何创建的 - https://moodle.org/plugins/?q=type:local

    至少,你需要

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

    加上你的索引文件

    local/webguides/index.php
    

    db/access.php 文件中有类似的东西

    defined('MOODLE_INTERNAL') || die();
    
    $capabilities = array(
    
        'local/webguides:view' => array(
            'captype' => 'read',
            'contextlevel' => CONTEXT_SYSTEM,
            'archetypes' => array(
            ),
        ),
    
    );
    

    您可能还需要'riskbitmask' =&gt; RISK_XXX,具体取决于您的代码中是否存在任何风险。如RISK_CONFIGRISK_PERSONAL

    lang/en/local_webguides.php 有类似的东西

    defined('MOODLE_INTERNAL') || die();
    
    $string['pluginname'] = 'Webguides';
    $string['webguides:view'] = 'Able to view webguids';
    

    version.php 有类似的东西

    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).
    

    2015051109 替换为您正在使用的 Moodle 版本 - 这将位于根文件夹的 version.php 中。

    然后在您的index.php 文件中在顶部附近使用它。

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

    因此,只有具有该功能的用户才能访问该页面。

    编辑:

    您可以通过settings.php 使用类似的方式添加链接

    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);
    }
    

    然后在你的索引页中广告这个

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

    并删除 require_login()require_capability() 并替换为

    admin_externalpage_setup('local_webguides');
    

    【讨论】:

    • 哇,非常感谢,我明天早上会再做这个,所以试试上面的。再次感谢您的帮助。
    • 嗨,我创建了上述内容(再次感谢)并通过moodle插件部分安装了它,安装得很好,但我无法在管理员中看到任何指向“站点管理”的链接。以上是否应该导致它出现?
    • 部分实现了这一点: add('localplugins', $settings); ?> 但现在我被困在如何将自定义代码添加到设置页面
    • 嘿,我已经更新了答案以添加指向站点管理菜单的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多