官术网_书友最值得收藏!

Storing configuration data

Having agreed with the client what the configuration screen is going to look like, we need to decide what information we need to store in the database to support these options. Our dual language plugin is a site administration plugin and so let's use the Admin settings API to store and retrieve the plugin's configuration data--see https://docs.moodle.org/dev/Admin_settings.

Because they are meant for functionality that doesn't quite fit the typical categories of a plugin, local plugins are a good place to gain an understanding of the interaction between frontend and backend APIs, as we are allowed to be flexible with the APIs we can experiment with.

In Eclipse, under /local/duallang, create a new file called settings.php:

And now copy in the following code:

<?php 
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local plugin "duallang" - Settings
*
* @package local_duallang
* @copyright 2016 Ian Wild
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

if ($hassiteconfig)
{
// Configuration settings to go here
}

Before moving on, let's take a brief look at the structure of this script.

It starts with the standard Moodle copyright heading, which contains a shortened version of Moodle's terms and conditions. Then comes a second set of comments bracketed with /** ... **/, which are DocBlock standard comments--if you have not encountered these before, then see https://en.wikipedia.org/wiki/PHPDoc#DocBlock for more details.

Then you will see a defined('MOODLE_INTERNAL') or die statement that ensures this script cannot be run outside of Moodle.

Finally, we have a check to determine if the current user has the capability of changing site configuration settings--as only site administrators should have the ability to alter the language display settings. In Eclipse, try highlighting the variable name, then right clicking, and finally selecting Open Declaration from the pop-up context menu. The variable $hassiteconfig is declared in index.php.

Now we need to add in some code to actually set the configuration data and for that we will use a set of functions that provide the means to manage the Site administration settings tree in the Administration block. The finished configuration screen, accessed through a new menu option under Language in the administration tree, is shown in the following screenshot:

We will learn how to code this page in the next section.

主站蜘蛛池模板: 清远市| 集贤县| 平邑县| 迁西县| 石台县| 当阳市| 泰兴市| 德清县| 定南县| 临邑县| 柳江县| 巢湖市| 龙海市| 绥化市| 体育| 潼关县| 泸水县| 靖西县| 江门市| 嘉荫县| 搜索| 罗田县| 望奎县| 东乌| 陕西省| 天峻县| 陆良县| 金门县| 渭源县| 南阳市| 寿阳县| 双流县| 望奎县| 宣威市| 鹿泉市| 康定县| 潜江市| 巍山| 大石桥市| 凤凰县| 旅游|