Skip to content

Commit

Permalink
Merge branch 'MDL-68797-master' of https://github.com/nguyenphuctien/…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 9, 2020
2 parents 4285ebb + ee5c5fb commit 44124e4
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 0 deletions.
206 changes: 206 additions & 0 deletions admin/classes/local/settings/autocomplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?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/>.

/**
* Auto complete admin setting.
*
* @package core_admin
* @copyright 2020 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_admin\local\settings;

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

require_once($CFG->libdir . '/adminlib.php');
/**
* Auto complete setting class.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2020 The Open University
*/
class autocomplete extends \admin_setting_configmultiselect {
/** @var boolean Should we allow typing new entries to the field? */
protected $tags = false;
/** @var string Name of an AMD module to send/process ajax requests. */
protected $ajax = '';
/** @var string Placeholder text for an empty list. */
protected $placeholder = '';
/** @var bool Whether the search has to be case-sensitive. */
protected $casesensitive = false;
/** @var bool Show suggestions by default - but this can be turned off. */
protected $showsuggestions = true;
/** @var string String that is shown when there are no selections. */
protected $noselectionstring = '';
/** @var string Delimiter to store values in database. */
protected $delimiter = ',';
/** @var string Should be multiple choices? */
protected $multiple = true;
/** @var string The link to manage choices. */
protected $manageurl = true;
/** @var string The text to display in manage link. */
protected $managetext = true;

/**
* Constructor
*
* @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting'
* for ones in config_plugins.
* @param string $visiblename localised
* @param string $description long localised info
* @param array $defaultsetting array of selected items
* @param array $choices options for autocomplete field
* @param array $attributes settings for autocomplete field
*/
public function __construct($name, $visiblename, $description, $defaultsetting, $choices, $attributes = null) {

if ($attributes === null) {
$attributes = [];
}

$this->placeholder = get_string('search');
$this->noselectionstring = get_string('noselection', 'form');
$defaultattributes = [
'tags',
'showsuggestions',
'placeholder',
'noselectionstring',
'ajax',
'casesensitive',
'delimiter',
'multiple',
'manageurl',
'managetext'
];

foreach ($defaultattributes as $attributename) {
if (isset($attributes[$attributename])) {
$this->$attributename = $attributes[$attributename];
}
}

parent::__construct($name, $visiblename, $description, $defaultsetting, $choices);
}

/**
* Returns the select setting(s)
*
* @return mixed null or array. Null if no settings else array of setting(s)
*/
public function get_setting() {
$result = $this->config_read($this->name);
if (is_null($result)) {
return null;
}
if ($result === '') {
return [];
}
return explode($this->delimiter, $result);
}

/**
* Saves setting(s) provided through $data
*
* @param array $data
*/
public function write_setting($data) {
if (!is_array($data)) {
return ''; // Ignore it.
}
if (!$this->load_choices() || empty($this->choices)) {
return '';
}

unset($data['xxxxx']);

$save = [];
foreach ($data as $value) {
if (!array_key_exists($value, $this->choices)) {
continue; // Ignore it.
}
$save[] = $value;
}

return ($this->config_write($this->name, implode($this->delimiter, $save)) ? '' : get_string('errorsetting', 'admin'));
}

/**
* Returns XHTML autocomplete field
*
* @param array $data Array of values to select by default
* @param string $query
* @return string XHTML autocomplete field
*/
public function output_html($data, $query = '') {
global $OUTPUT;

if (!$this->load_choices() or empty($this->choices)) {
return '';
}

$default = $this->get_defaultsetting();
if (empty($default)) {
$default = [];
}

if (is_null($data)) {
$data = [];
}

$context = [
'id' => $this->get_id(),
'name' => $this->get_full_name()
];

$defaults = [];
$options = [];
$template = 'core_admin/local/settings/autocomplete';

foreach ($this->choices as $value => $name) {
if (in_array($value, $default)) {
$defaults[] = $name;
}
$options[] = [
'value' => $value,
'text' => $name,
'selected' => in_array($value, $data),
'disabled' => false
];
}

$context['options'] = $options;
$context['tags'] = $this->tags;
$context['placeholder'] = $this->placeholder;
$context['casesensitive'] = $this->casesensitive;
$context['multiple'] = $this->multiple;
$context['showsuggestions'] = $this->showsuggestions;
$context['manageurl'] = $this->manageurl;
$context['managetext'] = $this->managetext;

if (is_null($default)) {
$defaultinfo = null;
} if (!empty($defaults)) {
$defaultinfo = implode(', ', $defaults);
} else {
$defaultinfo = get_string('none');
}

$element = $OUTPUT->render_from_template($template, $context);

return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query);
}
}
87 changes: 87 additions & 0 deletions admin/templates/local/settings/autocomplete.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{{!
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/>.
}}
{{!
@template core_admin\local\settings\autocomplete
Admin setting auto complete.
Context variables required for this template:
* name - element name
* id - element id
* options - choices
* multiple - is multiple choices?
* manageurl - url to manage choices
* managetext - string to display manage url
* tags - Should we allow typing new entries to the field?
* ajax - Name of an AMD module to send/process ajax requests
* placeholder - Placeholder text for an empty list
* casesensitive - Whether the search has to be case-sensitive
* showsuggestions - Show suggestions by default - but this can be turned off
* noselectionstring - String that is shown when there are no selections
Example context (json):
{
"name": "name0",
"id": "id0",
"options": [{
"value": "1",
"text": "option 1",
"selected": true,
"disabled": false
}],
"multiple": true,
"manageurl": "",
"managetext": "",
"tags": false,
"ajax": "",
"placeholder": "",
"casesensitive": false,
"showsuggestions": true,
"noselectionstring": ""
}
}}
{{!
Setting autocomplete.
}}

<div class="form-select defaultsnext">
<input type="hidden" name="{{name}}[xxxxx]" value="_qf__force_multiselect_submission">
<select class="custom-select" name="{{name}}[]"
id="{{id}}"
{{#multiple}}multiple{{/multiple}}>
{{#options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{text}}}</option>
{{/options}}
</select>
{{#manageurl}}
<a href="{{manageurl}}">{{managetext}}</a>
{{/manageurl}}
</div>
<br/>

{{#js}}
require(['core/form-autocomplete'], function(module) {
module.enhance({{#quote}}#{{id}}{{/quote}},
{{tags}},
{{#quote}}{{ajax}}{{/quote}},
{{#quote}}{{placeholder}}{{/quote}},
{{casesensitive}},
{{showsuggestions}},
{{#quote}}{{noselectionstring}}{{/quote}});
});
{{/js}}
1 change: 1 addition & 0 deletions admin/tool/mobile/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ public static function get_config($section) {
$settings->tool_mobile_forcelogout = get_config('tool_mobile', 'forcelogout');
$settings->tool_mobile_customlangstrings = get_config('tool_mobile', 'customlangstrings');
$settings->tool_mobile_disabledfeatures = get_config('tool_mobile', 'disabledfeatures');
$settings->tool_mobile_filetypeexclusionlist = get_config('tool_mobile', 'filetypeexclusionlist');
$settings->tool_mobile_custommenuitems = get_config('tool_mobile', 'custommenuitems');
$settings->tool_mobile_apppolicy = get_config('tool_mobile', 'apppolicy');
}
Expand Down
4 changes: 4 additions & 0 deletions admin/tool/mobile/lang/en/tool_mobile.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
$string['downloadcourses'] = 'Download courses';
$string['enablesmartappbanners'] = 'Enable App Banners';
$string['enablesmartappbanners_desc'] = 'If enabled, a banner promoting the mobile app will be displayed when accessing the site using a mobile browser.';
$string['filetypeexclusionlist'] = 'File type exclusion list';
$string['filetypeexclusionlist_desc'] = 'List of file types that we don\'t want users to try and open in the app. These files will still be listed on the app\'s course screen, but attempting to open them on iOS or Android would display a warning to the user indicating that this file type is not intended for use on a mobile device. They can then either cancel the open, or ignore the warning and open anyway.';
$string['filetypeexclusionlistplaceholder'] = 'Mobile file type exclusion list';
$string['forcedurlscheme'] = 'If you want to allow only your custom branded app to be opened via a browser window, then specify its URL scheme here. If you want to allow only the official app, then set the default value. Leave the field empty if you want to allow any app.';
$string['forcedurlscheme_key'] = 'URL scheme';
$string['forcelogout'] = 'Force log out';
Expand All @@ -81,6 +84,7 @@
$string['logininthebrowser'] = 'Via a browser window (for SSO plugins)';
$string['loginintheembeddedbrowser'] = 'Via an embedded browser (for SSO plugins)';
$string['mainmenu'] = 'Main menu';
$string['managefiletypes'] = 'Manage file types';
$string['minimumversion'] = 'If an app version is specified (3.8.0 or higher), any users using an older app version will be prompted to upgrade their app before being allowed access to the site.';
$string['minimumversion_key'] = 'Minimum app version required';
$string['mobileapp'] = 'Mobile app';
Expand Down
23 changes: 23 additions & 0 deletions admin/tool/mobile/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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

use core_admin\local\settings\autocomplete;

if ($hassiteconfig) {

$ADMIN->add('root', new admin_category('mobileapp', new lang_string('mobileapp', 'tool_mobile')), 'development');
Expand Down Expand Up @@ -188,6 +190,27 @@
new lang_string('custommenuitems', 'tool_mobile'),
new lang_string('custommenuitems_desc', 'tool_mobile'), '', PARAM_RAW, '50', '10'));

// File type exclusionlist.
$choices = [];
foreach (core_filetypes::get_types() as $key => $info) {
$text = '.' . $key;
if (!empty($info['type'])) {
$text .= ' (' . $info['type'] . ')';
}
$choices[$key] = $text;
}

$attributes = [
'manageurl' => new \moodle_url('/admin/tool/filetypes/index.php'),
'managetext' => get_string('managefiletypes', 'tool_mobile'),
'multiple' => true,
'delimiter' => ',',
'placeholder' => get_string('filetypeexclusionlistplaceholder', 'tool_mobile')
];
$temp->add(new autocomplete('tool_mobile/filetypeexclusionlist',
new lang_string('filetypeexclusionlist', 'tool_mobile'),
new lang_string('filetypeexclusionlist_desc', 'tool_mobile'), array(), $choices, $attributes));

$temp->add(new admin_setting_heading('tool_mobile/language',
new lang_string('language'), ''));

Expand Down
1 change: 1 addition & 0 deletions admin/tool/mobile/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function test_get_config() {
array('name' => 'tool_mobile_forcelogout', 'value' => 0),
array('name' => 'tool_mobile_customlangstrings', 'value' => ''),
array('name' => 'tool_mobile_disabledfeatures', 'value' => ''),
array('name' => 'tool_mobile_filetypeexclusionlist', 'value' => ''),
array('name' => 'tool_mobile_custommenuitems', 'value' => ''),
array('name' => 'tool_mobile_apppolicy', 'value' => ''),
array('name' => 'calendartype', 'value' => $CFG->calendartype),
Expand Down

0 comments on commit 44124e4

Please sign in to comment.