Merge branch 'MDL-69772-master-allcountrycodes' of git://github.com/mudrd8mz/moodle into master

This commit is contained in:
Eloy Lafuente (stronk7)
2020-10-06 00:03:54 +02:00
6 changed files with 196 additions and 15 deletions
+60
View File
@@ -4947,6 +4947,66 @@ class admin_setting_langlist extends admin_setting_configtext {
}
/**
* Allows to specify comma separated list of known country codes.
*
* This is a simple subclass of the plain input text field with added validation so that all the codes are actually
* known codes.
*
* @package core
* @category admin
* @copyright 2020 David Mudrák <david@moodle.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_countrycodes extends admin_setting_configtext {
/**
* Construct the instance of the setting.
*
* @param string $name Name of the admin setting such as 'allcountrycodes' or 'myplugin/countries'.
* @param lang_string|string $visiblename Language string with the field label text.
* @param lang_string|string $description Language string with the field description text.
* @param string $defaultsetting Default value of the setting.
* @param int $size Input text field size.
*/
public function __construct($name, $visiblename, $description, $defaultsetting = '', $size = null) {
parent::__construct($name, $visiblename, $description, $defaultsetting, '/^(?:\w+(?:,\w+)*)?$/', $size);
}
/**
* Validate the setting value before storing it.
*
* The value is first validated through custom regex so that it is a word consisting of letters, numbers or underscore; or
* a comma separated list of such words.
*
* @param string $data Value inserted into the setting field.
* @return bool|string True if the value is OK, error string otherwise.
*/
public function validate($data) {
$parentcheck = parent::validate($data);
if ($parentcheck !== true) {
return $parentcheck;
}
if ($data === '') {
return true;
}
$allcountries = get_string_manager()->get_list_of_countries(true);
foreach (explode(',', $data) as $code) {
if (!isset($allcountries[$code])) {
return get_string('invalidcountrycode', 'core_error', $code);
}
}
return true;
}
}
/**
* Selection of one of the recognised countries using the list
* returned by {@link get_list_of_countries()}.