MDL-53240 filetypes: Introduce the form element to specify plugin types

This is a stripped down version of the original Jonathon's element
without the actual JS selector. The options "allowall" and "onlytypes"
are not fully supported right now yet.
This commit is contained in:
Jonathon Fowler 2017-03-24 21:03:04 +01:00 committed by David Mudrák
parent 6c4a5fdf88
commit 97bb4f755e
4 changed files with 256 additions and 0 deletions

194
lib/form/filetypes.php Normal file
View File

@ -0,0 +1,194 @@
<?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/>.
/**
* Provides the {@link MoodleQuickForm_filetypes} class.
*
* @package core_form
* @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_form\filetypes_util;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->dirroot.'/lib/form/group.php');
/**
* File types and type groups selection form element.
*
* @package core_form
* @category form
* @copyright 2016 Jonathon Fowler <fowlerj@usq.edu.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_filetypes extends MoodleQuickForm_group {
/** @var array Allow selection from these file types only. */
protected $onlytypes = [];
/** @var bool Allow selection of 'All file types' (will be stored as '*'). */
protected $allowall = true;
/** @var core_form\filetypes_util instance to use as a helper. */
protected $util = null;
/**
* Constructor
*
* @param string $elementName Element's name
* @param string $elementLabel Label(s) for an element
* @param array $options element options:
* 'onlytypes': Allow selection from these file types only; for example ['onlytypes' => ['web_image']].
* 'allowall': Allow to select 'All file types', defaults to true. Does not apply with onlytypes are set.
* @param array|string $attributes Either a typical HTML attribute string or an associative array
*/
public function __construct($elementName = null, $elementLabel = null, $options = null, $attributes = null) {
parent::__construct($elementName, $elementLabel);
$this->_type = 'filetypes';
// Hard-frozen elements do not get the name populated automatically,
// which leads to PHP notice. Add it explicitly here.
$this->setAttributes(array('name' => $elementName));
$this->updateAttributes($attributes);
if (is_array($options) && $options) {
if (array_key_exists('onlytypes', $options) && is_array($options['onlytypes'])) {
$this->onlytypes = $options['onlytypes'];
}
if (!$this->onlytypes && array_key_exists('allowall', $options)) {
$this->allowall = (bool)$options['allowall'];
}
}
$this->util = new filetypes_util();
}
/**
* Assemble the elements of the form control.
*/
public function _createElements() {
$this->_generateId();
$this->setElements([
$this->createFormElement('text', 'filetypes', '', [
'id' => $this->getAttribute('id'),
]),
$this->createFormElement('static', 'browser', null,
'<span data-filetypesbrowser="'.$this->getAttribute('id').'"></span>'),
$this->createFormElement('static', 'descriptions'),
]);
}
/**
* Return the selected file types.
*
* @param array $submitValues submitted values
* @param bool $assoc if true the retured value is associated array
* @return array
*/
public function exportValue(&$submitValues, $assoc = false) {
$value = '';
$filetypeselement = null;
foreach ($this->_elements as $key => $element) {
if ($element->_attributes['name'] === 'filetypes') {
$filetypeselement = $this->_elements[$key];
}
}
if ($filetypeselement) {
$formval = $filetypeselement->exportValue($submitValues[$this->getName()], false);
if ($formval) {
$value = $this->util->normalize_file_types($formval);
if ($value === ['*'] && !$this->allowall) {
$value = [];
}
$value = implode(',', $value);
}
}
return $this->_prepareValue($value, $assoc);
}
/**
* Accepts a renderer (called shortly before the renderer's toHtml() method).
*
* @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
* @param bool $required Whether a group is required
* @param string $error An error message associated with a group
*/
public function accept(&$renderer, $required = false, $error = null) {
global $PAGE;
if ($this->isFrozen()) {
// Don't render the choose button if the control is frozen.
foreach ($this->_elements as $key => $element) {
if ($element->_attributes['name'] === 'browser') {
unset($this->_elements[$key]);
}
}
}
parent::accept($renderer, $required, $error);
}
/**
* Called by HTML_QuickForm whenever form event is made on this element
*
* @param string $event Name of event
* @param mixed $arg event arguments
* @param object $caller calling object
* @return bool
*/
public function onQuickFormEvent($event, $arg, &$caller) {
global $OUTPUT;
switch ($event) {
case 'updateValue':
$value = $this->_findValue($caller->_constantValues);
if (null === $value) {
if ($caller->isSubmitted()) {
$value = $this->_findValue($caller->_submitValues);
} else {
$value = (string)$this->_findValue($caller->_defaultValues);
}
}
if (!is_array($value)) {
$value = array('filetypes' => $value);
}
if ($value['filetypes'] !== null) {
$filetypes = $this->util->normalize_file_types($value['filetypes']);
if ($filetypes === ['*'] && !$this->allowall) {
$filetypes = [];
}
$value['descriptions'] = $OUTPUT->render_from_template('core_form/filetypes-descriptions',
$this->util->describe_file_types($filetypes));
}
$this->setValue($value);
return true;
}
return parent::onQuickFormEvent($event, $arg, $caller);
}
}

View File

@ -0,0 +1,60 @@
{{!
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_form/filetypes-descriptions
Template to describe chosen file types.
Classes required for JS:
* none
Data attributes required for JS:
* none
Context variables required for this template:
* hasdescriptions (bool)
* descriptions (array)
* description (string)
* extensions (string)
Example context (json):
{
"hasdescriptions": true,
"descriptions": [
{
"description": "Image (JPEG)",
"extensions": ".jpeg .jpe .jpg"
},
{
"description": "Image (GIF)",
"extensions": ".gif"
}
]
}
}}
<div class="form-filetypes-descriptions">
{{#hasdescriptions}}
<ul class="list-unstyled unstyled">
{{#descriptions}}
<li>{{description}} <small class="text-muted muted">{{extensions}}</small></li>
{{/descriptions}}
</ul>
{{/hasdescriptions}}
{{^hasdescriptions}}
<p>{{#str}}noselection, form{{/str}}</p>
{{/hasdescriptions}}
</div>

View File

@ -3105,6 +3105,7 @@ MoodleQuickForm::registerElementType('duration', "$CFG->libdir/form/duration.php
MoodleQuickForm::registerElementType('editor', "$CFG->libdir/form/editor.php", 'MoodleQuickForm_editor');
MoodleQuickForm::registerElementType('filemanager', "$CFG->libdir/form/filemanager.php", 'MoodleQuickForm_filemanager');
MoodleQuickForm::registerElementType('filepicker', "$CFG->libdir/form/filepicker.php", 'MoodleQuickForm_filepicker');
MoodleQuickForm::registerElementType('filetypes', "$CFG->libdir/form/filetypes.php", 'MoodleQuickForm_filetypes');
MoodleQuickForm::registerElementType('grading', "$CFG->libdir/form/grading.php", 'MoodleQuickForm_grading');
MoodleQuickForm::registerElementType('group', "$CFG->libdir/form/group.php", 'MoodleQuickForm_group');
MoodleQuickForm::registerElementType('header', "$CFG->libdir/form/header.php", 'MoodleQuickForm_header');

View File

@ -0,0 +1 @@
{{> core_form/element-group }}