- Moodle 3.x Developer's Guide
- Ian Wild
- 378字
- 2021-07-02 21:34:46
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.