MDL-64554 core_customfield: use modal forms for fields

This commit is contained in:
Marina Glancy
2021-02-17 18:06:53 +01:00
parent 72be49c473
commit aa33a22733
27 changed files with 282 additions and 259 deletions
@@ -238,20 +238,4 @@ class course_handler extends \core_customfield\handler {
}
}
}
/**
* Set up page customfield/edit.php
*
* @param field_controller $field
* @return string page heading
*/
public function setup_edit_page(field_controller $field) : string {
global $CFG, $PAGE;
require_once($CFG->libdir.'/adminlib.php');
$title = parent::setup_edit_page($field);
admin_externalpage_setup('course_customfield');
$PAGE->navbar->add($title);
return $title;
}
}
+34 -6
View File
@@ -1,5 +1,5 @@
@core @core_course @core_customfield
Feature: Fields locked control where they are displayed
@core @core_course @core_customfield @javascript
Feature: Fields locked control who is able to edit it
In order to display custom fields on course listing
As a manager
I can change the visibility of the fields
@@ -19,12 +19,40 @@ Feature: Fields locked control where they are displayed
| user | course | role |
| teacher1 | C1 | editingteacher |
Scenario: Display course custom fields on homepage
Scenario: Editing locked and not locked custom fields
When I log in as "admin"
And I navigate to "Courses > Course custom fields" in site administration
And I click on "Add a new custom field" "link"
And I click on "Short text" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
| Locked | No |
| Name | Test field1 |
| Short name | testfield1 |
| Locked | No |
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I click on "Add a new custom field" "link"
And I click on "Short text" "link"
And I set the following fields to these values:
| Name | Test field2 |
| Short name | testfield2 |
| Locked | Yes |
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration
And I set the following fields to these values:
| Test field1 | testcontent1 |
| Test field2 | testcontent2 |
And I press "Save and display"
And I am on site homepage
Then I should see "Test field1: testcontent1"
And I should see "Test field2: testcontent2"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
And the field "Test field1" matches value "testcontent1"
And I should not see "Test field2"
And I press "Save and display"
And I am on site homepage
And I should see "Test field1: testcontent1"
And I should see "Test field2: testcontent2"
@@ -1,4 +1,4 @@
@core @core_course @core_customfield
@core @core_course @core_customfield @javascript
Feature: The visibility of fields control where they are displayed
In order to display custom fields on course listing
As a manager
@@ -28,7 +28,7 @@ Feature: The visibility of fields control where they are displayed
| Name | Test field |
| Short name | testfield |
| Visible to | Everyone |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
Then I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -48,7 +48,7 @@ Feature: The visibility of fields control where they are displayed
| Name | Test field |
| Short name | testfield |
| Visible to | Nobody |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -68,7 +68,7 @@ Feature: The visibility of fields control where they are displayed
| Name | Test field |
| Short name | testfield |
| Visible to | Teachers |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core_customfield/form",["jquery","core/str","core/notification","core/ajax","core/templates","core/sortable_list","core/inplace_editable"],function(a,b,c,d,e,f){var g=function(f,g,h,i,j){b.get_strings([{key:"confirm"},{key:"confirmdelete"+g,component:"core_customfield"},{key:"yes"},{key:"no"}]).done(function(b){c.confirm(b[0],b[1],b[2],b[3],function(){var b="field"===g?"core_customfield_delete_field":"core_customfield_delete_category";d.call([{methodname:b,args:{id:f}},{methodname:"core_customfield_reload_template",args:{component:h,area:i,itemid:j}}])[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);return null}).fail(c.exception)})}).fail(c.exception)},h=function(b,f,g){var h=d.call([{methodname:"core_customfield_create_category",args:{component:b,area:f,itemid:g}},{methodname:"core_customfield_reload_template",args:{component:b,area:f,itemid:g}}]),i;h[0].then(function(a){i=a;return null}).fail(c.exception);h[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);window.location.href="#category-"+i;return null}).fail(c.exception)};return{init:function init(){var e=a("#customfield_catlist"),i=e.attr("data-component"),j=e.attr("data-area"),k=e.attr("data-itemid");a("[data-role=deletefield]").on("click",function(b){g(a(this).attr("data-id"),"field",i,j,k);b.preventDefault()});a("[data-role=deletecategory]").on("click",function(b){g(a(this).attr("data-id"),"category",i,j,k);b.preventDefault()});a("[data-role=addnewcategory]").on("click",function(){h(i,j,k)});var l=function(a){return a.closest("[data-category-id]").find("[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]").attr("data-value")},m=new f(a("#customfield_catlist .categorieslist"),{moveHandlerSelector:".movecategory [data-drag-type=move]"});m.getElementName=function(b){return a.Deferred().resolve(l(b))};a("[data-category-id]").on("sortablelist-drop",function(a,b){if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_category",args:{id:b.element.data("category-id"),beforeid:b.targetNextElement.data("category-id")}}]);e[0].fail(c.exception)}a.stopPropagation()});var n=new f(a("#customfield_catlist .fieldslist tbody"),{moveHandlerSelector:".movefield [data-drag-type=move]"});n.getDestinationName=function(c,d){if(!d.length){return b.get_string("totopofcategory","customfield",l(c))}else if(d.attr("data-field-name")){return b.get_string("afterfield","customfield",d.attr("data-field-name"))}else{return a.Deferred().resolve("")}};a("[data-field-name]").on("sortablelist-drop",function(a,b){a.stopPropagation();if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_field",args:{id:b.element.data("field-id"),beforeid:b.targetNextElement.data("field-id"),categoryid:+b.targetList.closest("[data-category-id]").attr("data-category-id")}}]);e[0].fail(c.exception)}}).on("sortablelist-drag",function(d){d.stopPropagation();b.get_string("therearenofields","core_customfield").then(function(b){a("#customfield_catlist .categorieslist").children().each(function(){var c=a(this).find(a(".field")),d=a(this).find(a(".nofields"));if(!c.length&&!d.length){a(this).find("tbody").append("<tr class=\"nofields\"><td colspan=\"5\">"+b+"</td></tr>")}if(c.length&&d.length){d.remove()}});return null}).fail(c.exception)});a("[data-category-id], [data-field-name]").on("sortablelist-dragstart",function(b,c){setTimeout(function(){a(".sortable-list-is-dragged").width(c.element.width())},501)})}}});
define ("core_customfield/form",["jquery","core/str","core/notification","core/ajax","core/templates","core/sortable_list","core_form/modalform","core/inplace_editable"],function(a,b,c,d,e,f,g){var h=function(f,g,h,i,j){b.get_strings([{key:"confirm"},{key:"confirmdelete"+g,component:"core_customfield"},{key:"yes"},{key:"no"}]).done(function(b){c.confirm(b[0],b[1],b[2],b[3],function(){var b="field"===g?"core_customfield_delete_field":"core_customfield_delete_category";d.call([{methodname:b,args:{id:f}},{methodname:"core_customfield_reload_template",args:{component:h,area:i,itemid:j}}])[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);return null}).fail(c.exception)})}).fail(c.exception)},i=function(b,f,g){var h=d.call([{methodname:"core_customfield_create_category",args:{component:b,area:f,itemid:g}},{methodname:"core_customfield_reload_template",args:{component:b,area:f,itemid:g}}]),i;h[0].then(function(a){i=a;return null}).fail(c.exception);h[1].then(function(a){return e.render("core_customfield/list",a)}).then(function(b,c){e.replaceNode(a("[data-region=\"list-page\"]"),b,c);window.location.href="#category-"+i;return null}).fail(c.exception)},j=function(a){var c=a.closest(".action-menu").querySelector(".dropdown-toggle"),d=new g({formClass:"core_customfield\\field_config_form",args:{categoryid:a.getAttribute("data-categoryid"),type:a.getAttribute("data-type")},modalConfig:{title:b.get_string("addingnewcustomfield","core_customfield",a.getAttribute("data-typename"))},returnFocus:c});d.addEventListener(d.events.FORM_SUBMITTED,function(){return window.location.reload()});d.show()},k=function(a){var c=new g({formClass:"core_customfield\\field_config_form",args:{id:a.getAttribute("data-id")},modalConfig:{title:b.get_string("editingfield","core_customfield",a.getAttribute("data-name"))},returnFocus:a});c.addEventListener(c.events.FORM_SUBMITTED,function(){return window.location.reload()});c.show()};return{init:function init(){var e=a("#customfield_catlist"),g=e.attr("data-component"),l=e.attr("data-area"),m=e.attr("data-itemid");a("[data-role=deletefield]").on("click",function(b){h(a(this).attr("data-id"),"field",g,l,m);b.preventDefault()});a("[data-role=deletecategory]").on("click",function(b){h(a(this).attr("data-id"),"category",g,l,m);b.preventDefault()});a("[data-role=addnewcategory]").on("click",function(){i(g,l,m)});a("[data-role=addfield]").on("click",function(a){j(a.currentTarget);a.preventDefault()});a("[data-role=editfield]").on("click",function(a){k(a.currentTarget);a.preventDefault()});var n=function(a){return a.closest("[data-category-id]").find("[data-inplaceeditable][data-itemtype=category][data-component=core_customfield]").attr("data-value")},o=new f(a("#customfield_catlist .categorieslist"),{moveHandlerSelector:".movecategory [data-drag-type=move]"});o.getElementName=function(b){return a.Deferred().resolve(n(b))};a("[data-category-id]").on("sortablelist-drop",function(a,b){if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_category",args:{id:b.element.data("category-id"),beforeid:b.targetNextElement.data("category-id")}}]);e[0].fail(c.exception)}a.stopPropagation()});var p=new f(a("#customfield_catlist .fieldslist tbody"),{moveHandlerSelector:".movefield [data-drag-type=move]"});p.getDestinationName=function(c,d){if(!d.length){return b.get_string("totopofcategory","customfield",n(c))}else if(d.attr("data-field-name")){return b.get_string("afterfield","customfield",d.attr("data-field-name"))}else{return a.Deferred().resolve("")}};a("[data-field-name]").on("sortablelist-drop",function(a,b){a.stopPropagation();if(b.positionChanged){var e=d.call([{methodname:"core_customfield_move_field",args:{id:b.element.data("field-id"),beforeid:b.targetNextElement.data("field-id"),categoryid:+b.targetList.closest("[data-category-id]").attr("data-category-id")}}]);e[0].fail(c.exception)}}).on("sortablelist-drag",function(d){d.stopPropagation();b.get_string("therearenofields","core_customfield").then(function(b){a("#customfield_catlist .categorieslist").children().each(function(){var c=a(this).find(a(".field")),d=a(this).find(a(".nofields"));if(!c.length&&!d.length){a(this).find("tbody").append("<tr class=\"nofields\"><td colspan=\"5\">"+b+"</td></tr>")}if(c.length&&d.length){d.remove()}});return null}).fail(c.exception)});a("[data-category-id], [data-field-name]").on("sortablelist-dragstart",function(b,c){setTimeout(function(){a(".sortable-list-is-dragged").width(c.element.width())},501)})}}});
//# sourceMappingURL=form.min.js.map
File diff suppressed because one or more lines are too long
+53 -2
View File
@@ -21,9 +21,10 @@
* @copyright 2018 Toni Barbera
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates', 'core/sortable_list', 'core/inplace_editable'],
define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates', 'core/sortable_list',
'core_form/modalform', 'core/inplace_editable'],
function(
$, Str, Notification, Ajax, Templates, SortableList) {
$, Str, Notification, Ajax, Templates, SortableList, ModalForm) {
/**
* Display confirmation dialogue
@@ -84,6 +85,48 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates'
}).fail(Notification.exception);
};
/**
* Create new custom field
*
* @param {HTMLElement} element
*/
var createNewField = function(element) {
const returnFocus = element.closest(".action-menu").querySelector(".dropdown-toggle");
const form = new ModalForm({
formClass: "core_customfield\\field_config_form",
args: {
categoryid: element.getAttribute('data-categoryid'),
type: element.getAttribute('data-type'),
},
modalConfig: {
title: Str.get_string('addingnewcustomfield', 'core_customfield', element.getAttribute('data-typename')),
},
returnFocus,
});
form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());
form.show();
};
/**
* Edit custom field
*
* @param {HTMLElement} element
*/
var editField = function(element) {
const form = new ModalForm({
formClass: "core_customfield\\field_config_form",
args: {
id: element.getAttribute('data-id'),
},
modalConfig: {
title: Str.get_string('editingfield', 'core_customfield', element.getAttribute('data-name')),
},
returnFocus: element,
});
form.addEventListener(form.events.FORM_SUBMITTED, () => window.location.reload());
form.show();
};
return {
/**
* Initialise the custom fields manager
@@ -104,6 +147,14 @@ define(['jquery', 'core/str', 'core/notification', 'core/ajax', 'core/templates'
$('[data-role=addnewcategory]').on('click', function() {
createNewCategory(component, area, itemid);
});
$('[data-role=addfield]').on('click', function(e) {
createNewField(e.currentTarget);
e.preventDefault();
});
$('[data-role=editfield]').on('click', function(e) {
editField(e.currentTarget);
e.preventDefault();
});
var categoryName = function(element) {
return element
+92 -12
View File
@@ -26,9 +26,6 @@ namespace core_customfield;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/formslib.php');
/**
* Class field_config_form
*
@@ -36,7 +33,10 @@ require_once($CFG->libdir . '/formslib.php');
* @copyright 2018 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class field_config_form extends \moodleform {
class field_config_form extends \core_form\dynamic_form {
/** @var field_controller */
protected $field;
/**
* Class definition
@@ -44,13 +44,9 @@ class field_config_form extends \moodleform {
* @throws \coding_exception
*/
public function definition() {
global $PAGE;
$mform = $this->_form;
$field = $this->_customdata['field'];
if (!($field && $field instanceof field_controller)) {
throw new \coding_exception('Field must be passed in customdata');
}
$field = $this->get_field();
$handler = $field->get_handler();
$mform->addElement('header', '_commonsettings', get_string('commonsettings', 'core_customfield'));
@@ -97,7 +93,7 @@ class field_config_form extends \moodleform {
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$this->add_action_buttons(true);
// This form is only used inside modal dialogues and never needs action buttons.
}
/**
@@ -111,8 +107,7 @@ class field_config_form extends \moodleform {
global $DB;
$errors = array();
/** @var field_controller $field */
$field = $this->_customdata['field'];
$field = $this->get_field();
$handler = $field->get_handler();
// Check the shortname is specified and is unique for this component-area-itemid combination.
@@ -131,4 +126,89 @@ class field_config_form extends \moodleform {
return $errors;
}
/**
* Get field
*
* @return field_controller
* @throws \moodle_exception
*/
protected function get_field(): field_controller {
if ($this->field === null) {
if (!empty($this->_ajaxformdata['id'])) {
$this->field = \core_customfield\field_controller::create((int)$this->_ajaxformdata['id']);
} else if (!empty($this->_ajaxformdata['categoryid']) && !empty($this->_ajaxformdata['type'])) {
$category = \core_customfield\category_controller::create((int)$this->_ajaxformdata['categoryid']);
$type = clean_param($this->_ajaxformdata['type'], PARAM_PLUGIN);
$this->field = \core_customfield\field_controller::create(0, (object)['type' => $type], $category);
} else {
throw new \moodle_exception('fieldnotfound', 'core_customfield');
}
}
return $this->field;
}
/**
* Check if current user has access to this form, otherwise throw exception
*
* Sometimes permission check may depend on the action and/or id of the entity.
* If necessary, form data is available in $this->_ajaxformdata
*/
protected function check_access_for_dynamic_submission(): void {
$field = $this->get_field();
$handler = $field->get_handler();
if (!$handler->can_configure()) {
print_error('nopermissionconfigure', 'core_customfield');
}
}
/**
* Load in existing data as form defaults
*
* Can be overridden to retrieve existing values from db by entity id and also
* to preprocess editor and filemanager elements
*
* Example:
* $this->set_data(get_entity($this->_ajaxformdata['id']));
*/
public function set_data_for_dynamic_submission(): void {
$this->set_data(api::prepare_field_for_config_form($this->get_field()));
}
/**
* Process the form submission
*
* This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS.
*
* @return mixed
*/
public function process_dynamic_submission() {
$data = $this->get_data();
$field = $this->get_field();
$handler = $field->get_handler();
$handler->save_field_configuration($field, $data);
return null;
}
/**
* Form context
* @return \context
*/
protected function get_context_for_dynamic_submission(): \context {
return $this->get_field()->get_handler()->get_configuration_context();
}
/**
* Page url
* @return \moodle_url
*/
protected function get_page_url_for_dynamic_submission(): \moodle_url {
$field = $this->get_field();
if ($field->get('id')) {
$params = ['action' => 'editfield', 'id' => $field->get('id')];
} else {
$params = ['action' => 'addfield', 'categoryid' => $field->get('categoryid'), 'type' => $field->get('type')];
}
return new \moodle_url($field->get_handler()->get_configuration_url(), $params);
}
}
-60
View File
@@ -178,18 +178,6 @@ abstract class handler {
return true;
}
/**
* The form to create or edit a field
*
* @param field_controller $field
* @return field_config_form
*/
public function get_field_config_form(field_controller $field) : field_config_form {
$form = new field_config_form(null, ['field' => $field]);
$form->set_data(api::prepare_field_for_config_form($field));
return $form;
}
/**
* Generates a name for the new category
*
@@ -798,52 +786,4 @@ abstract class handler {
$data->delete();
}
}
/**
* Set up page customfield/edit.php
*
* Handler should override this method and set page context
*
* @param field_controller $field
* @return string page heading
*/
public function setup_edit_page(field_controller $field) : string {
global $PAGE;
// Page context.
$context = $this->get_configuration_context();
if ($context->contextlevel == CONTEXT_MODULE) {
list($course, $cm) = get_course_and_cm_from_cmid($context->instanceid, '', $context->get_course_context()->instanceid);
require_login($course, false, $cm);
} else if ($context->contextlevel == CONTEXT_COURSE) {
require_login($context->instanceid, false);
} else {
$PAGE->set_context(null); // This will set to system context only if the context was not set before.
if ($PAGE->context->id != $context->id) {
// In case of user or block context level this method must be overridden.
debugging('Handler must override setup_edit_page() and set the page context before calling parent method.',
DEBUG_DEVELOPER);
}
}
// Set up url and title.
if ($field->get('id')) {
$field = $this->validate_field($field);
} else {
$this->validate_category($field->get_category());
}
$url = new \moodle_url('/customfield/edit.php',
['id' => $field->get('id'), 'type' => $field->get('type'), 'categoryid' => $field->get('categoryid')]);
$PAGE->set_url($url);
$typestr = get_string('pluginname', 'customfield_' . $field->get('type'));
if ($field->get('id')) {
$title = get_string('editingfield', 'core_customfield',
$field->get_formatted_name());
} else {
$title = get_string('addingnewcustomfield', 'core_customfield', $typestr);
}
$PAGE->set_title($title);
return $title;
}
}
+3 -8
View File
@@ -96,10 +96,6 @@ class management implements renderable, templatable {
$fieldarray['shortname'] = $field->get('shortname');
$fieldarray['movetitle'] = get_string('movefield', 'core_customfield', $fieldname);
$fieldarray['editfieldurl'] = (new \moodle_url('/customfield/edit.php', [
'id' => $fieldarray['id'],
]))->out(false);
$categoryarray['fields'][] = $fieldarray;
}
@@ -107,11 +103,10 @@ class management implements renderable, templatable {
$menu->set_alignment(\action_menu::BL, \action_menu::BL);
$menu->set_menu_trigger(get_string('createnewcustomfield', 'core_customfield'));
$baseaddfieldurl = new \moodle_url('/customfield/edit.php',
array('action' => 'editfield', 'categoryid' => $category->get('id')));
foreach ($fieldtypes as $type => $fieldname) {
$addfieldurl = new \moodle_url($baseaddfieldurl, array('type' => $type));
$action = new \action_menu_link_secondary($addfieldurl, null, $fieldname);
$action = new \action_menu_link_secondary(new \moodle_url('#'), null, $fieldname,
['data-role' => 'addfield', 'data-categoryid' => $category->get('id'), 'data-type' => $type,
'data-typename' => $fieldname]);
$menu->add($action);
}
$menu->attributes['class'] .= ' float-left mr-1';
-61
View File
@@ -1,61 +0,0 @@
<?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/>.
/**
* Edit configuration of a custom field
*
* @package core_customfield
* @copyright 2018 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../config.php');
require_once($CFG->libdir . '/adminlib.php');
$id = optional_param('id', 0, PARAM_INT);
$categoryid = optional_param('categoryid', 0, PARAM_INT);
$type = optional_param('type', null, PARAM_COMPONENT);
if ($id) {
$field = \core_customfield\field_controller::create($id);
} else if ($categoryid && $type) {
$category = \core_customfield\category_controller::create($categoryid);
$field = \core_customfield\field_controller::create(0, (object)['type' => $type], $category);
} else {
print_error('fieldnotfound', 'core_customfield');
}
$handler = $field->get_handler();
require_login();
if (!$handler->can_configure()) {
print_error('nopermissionconfigure', 'core_customfield');
}
$title = $handler->setup_edit_page($field);
$mform = $handler->get_field_config_form($field);
if ($mform->is_cancelled()) {
redirect($handler->get_configuration_url());
} else if ($data = $mform->get_data()) {
$handler->save_field_configuration($field, $data);
redirect($handler->get_configuration_url());
}
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$mform->display();
echo $OUTPUT->footer();
-1
View File
@@ -131,7 +131,6 @@ class core_customfield_external extends external_api {
'name' => new external_value(PARAM_NOTAGS, 'name'),
'shortname' => new external_value(PARAM_NOTAGS, 'shortname'),
'type' => new external_value(PARAM_NOTAGS, 'type'),
'editfieldurl' => new external_value(PARAM_URL, 'edit field url'),
'id' => new external_value(PARAM_INT, 'id'),
)
)
@@ -1,4 +1,4 @@
@customfield @customfield_checkbox
@customfield @customfield_checkbox @javascript
Feature: Managers can manage course custom fields checkbox
In order to have additional data on the course
As a manager
@@ -17,7 +17,7 @@ Feature: Managers can manage course custom fields checkbox
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Checkbox" "dialogue"
Then I should see "Test field"
And I log out
@@ -27,29 +27,26 @@ Feature: Managers can manage course custom fields checkbox
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Checkbox" "dialogue"
And I click on "Edit" "link" in the "Test field" "table_row"
And I set the following fields to these values:
| Name | Edited field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I should not see "Test field"
And I log out
@javascript
Scenario: Delete a custom course checkbox field
When I click on "Add a new custom field" "link"
And I click on "Checkbox" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Checkbox" "dialogue"
And I click on "Delete" "link" in the "Test field" "table_row"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
And I log out
@javascript
Scenario: A checkbox checked by default must be shown on listing but allow uncheck that will keep showing
Given the following "users" exist:
| username | firstname | lastname | email |
@@ -66,7 +63,7 @@ Feature: Managers can manage course custom fields checkbox
| Name | Test field |
| Short name | testfield |
| Checked by default | Yes |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Checkbox" "dialogue"
And I log out
And I log in as "teacher1"
And I am on site homepage
@@ -103,21 +103,24 @@ class customfield_checkbox_plugin_testcase extends advanced_testcase {
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$this->setAdminUser();
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[1], $data);
$form->process_dynamic_submission();
// Try submitting with 'unique values' checked.
$submitdata['configdata']['uniquevalues'] = 1;
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertFalse($form->is_validated());
}
@@ -1,4 +1,4 @@
@customfield @customfield_date
@customfield @customfield_date @javascript
Feature: Managers can manage course custom fields date
In order to have additional data on the course
As a manager
@@ -17,7 +17,7 @@ Feature: Managers can manage course custom fields date
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
Then I should see "Test field"
And I log out
@@ -27,28 +27,26 @@ Feature: Managers can manage course custom fields date
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I click on "[data-role='editfield']" "css_element"
And I set the following fields to these values:
| Name | Edited field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I log out
@javascript
Scenario: Delete a custom course date field
When I click on "Add a new custom field" "link"
And I click on "Date and time" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I click on "[data-role='deletefield']" "css_element"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
And I log out
@javascript
Scenario: A date field makerd to include time must show those fields on course form
Given the following "users" exist:
| username | firstname | lastname | email |
@@ -65,7 +63,7 @@ Feature: Managers can manage course custom fields date
| Name | Test field |
| Short name | testfield |
| Include time | 1 |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I log out
Then I log in as "teacher1"
When I am on site homepage
@@ -76,7 +74,6 @@ Feature: Managers can manage course custom fields date
Then "#id_customfield_testfield_minute" "css_element" should be visible
And I log out
@javascript
Scenario: A date field makerd to not include time must not show those fields on course form
Given the following "users" exist:
| username | firstname | lastname | email |
@@ -93,7 +90,7 @@ Feature: Managers can manage course custom fields date
| Name | Test field |
| Short name | testfield |
| Include time | |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Date and time" "dialogue"
And I log out
Then I log in as "teacher1"
When I am on site homepage
+6 -5
View File
@@ -100,15 +100,16 @@ class customfield_date_plugin_testcase extends advanced_testcase {
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$this->setAdminUser();
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[1], $data);
$form->process_dynamic_submission();
}
/**
@@ -1,4 +1,4 @@
@customfield @customfield_select
@customfield @customfield_select @javascript
Feature: Managers can manage course custom fields select
In order to have additional data on the course
As a manager
@@ -22,7 +22,7 @@ Feature: Managers can manage course custom fields select
a
b
"""
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
Then I should see "Test field"
And I log out
@@ -37,16 +37,15 @@ Feature: Managers can manage course custom fields select
a
b
"""
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I click on "Edit" "link" in the "Test field" "table_row"
And I set the following fields to these values:
| Name | Edited field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I should not see "Test field"
And I log out
@javascript
Scenario: Delete a custom course select field
When I click on "Add a new custom field" "link"
And I click on "Dropdown menu" "link"
@@ -58,7 +57,7 @@ Feature: Managers can manage course custom fields select
a
b
"""
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I click on "Delete" "link" in the "Test field" "table_row"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
@@ -70,7 +69,7 @@ Feature: Managers can manage course custom fields select
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I should see "Please provide at least two options, with each on a new line." in the "Menu options (one per line)" "form_row"
And I set the field "Menu options (one per line)" to multiline:
"""
@@ -78,9 +77,9 @@ Feature: Managers can manage course custom fields select
b
"""
And I set the field "Default value" to "c"
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And I should see "The default value must be one of the options from the list above" in the "Default value" "form_row"
And I set the field "Default value" to "b"
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Dropdown menu" "dialogue"
And "testfield" "text" should exist in the "Test field" "table_row"
And I log out
@@ -104,15 +104,16 @@ class customfield_select_plugin_testcase extends advanced_testcase {
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$this->setAdminUser();
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[1], $data);
$form->process_dynamic_submission();
}
/**
@@ -1,4 +1,4 @@
@customfield @customfield_text
@customfield @customfield_text @javascript
Feature: Managers can manage course custom fields text
In order to have additional data on the course
As a manager
@@ -17,7 +17,7 @@ Feature: Managers can manage course custom fields text
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
Then I should see "Test field"
And I log out
@@ -27,24 +27,23 @@ Feature: Managers can manage course custom fields text
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I click on "Edit" "link" in the "Test field" "table_row"
And I set the following fields to these values:
| Name | Edited field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I navigate to "Reports > Logs" in site administration
And I press "Get these logs"
And I log out
@javascript
Scenario: Delete a custom course text field
When I click on "Add a new custom field" "link"
And I click on "Short text" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I click on "Delete" "link" in the "Test field" "table_row"
And I click on "Yes" "button" in the "Confirm" "dialogue"
And I wait until the page is ready
@@ -70,7 +69,7 @@ Feature: Managers can manage course custom fields text
| Short name | testfield |
| Visible to | Everyone |
| Link | https://www.moodle.org/$$ |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
Then I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -99,7 +98,7 @@ Feature: Managers can manage course custom fields text
| Name | Test field |
| Short name | testfield |
| Maximum number of characters | 3 |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
Then I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -126,7 +125,7 @@ Feature: Managers can manage course custom fields text
| Name | Test field |
| Short name | testfield |
| Default value | testdefault |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
Then I log in as "teacher1"
When I am on site homepage
+6 -5
View File
@@ -109,15 +109,16 @@ class customfield_text_plugin_testcase extends advanced_testcase {
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$this->setAdminUser();
$submitdata = (array)$this->cfields[1]->to_record();
$submitdata['configdata'] = $this->cfields[1]->get('configdata');
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[1]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[1], $data);
$form->process_dynamic_submission();
}
/**
@@ -35,14 +35,14 @@ Feature: Default value for the textarea custom field can contain images
| Default value | v |
# Embed the image into Default value.
And I select the text in the "Default value" Atto editor
And I click on "Insert or edit image" "button" in the "//*[@data-fieldtype='editor']/*[descendant::*[@id='id_configdata_defaultvalue_editoreditable']]" "xpath_element"
And I click on "Insert or edit image" "button" in the "Default value" "form_row"
And I click on "Browse repositories..." "button"
And I click on "Private files" "link" in the ".fp-repo-area" "css_element"
And I click on "gd-logo.png" "link"
And I click on "Select this file" "button"
And I set the field "Describe this image for someone who cannot see it" to "Example"
And I click on "Save image" "button"
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Text area" "dialogue"
And I log out
Scenario: For the courses that existed before the custom field was created the default value is displayed
@@ -55,7 +55,7 @@ Feature: Default value for the textarea custom field can contain images
And I am on "Course 1" course homepage
And I navigate to "Edit settings" in current page administration
And I expand all fieldsets
Then "//*[@id='id_customfield_testfield_editoreditable']//img[contains(@src, 'draftfile.php') and contains(@src, '/gd-logo.png') and @alt='Example']" "xpath_element" should exist
Then "//img[contains(@src, 'draftfile.php') and contains(@src, '/gd-logo.png') and @alt='Example']" "xpath_element" should exist in the "Test field" "form_row"
# Save the course without changing the default value.
And I press "Save and display"
And I log out
@@ -72,7 +72,7 @@ Feature: Default value for the textarea custom field can contain images
| Course full name | Course 2 |
| Course short name | C2 |
And I expand all fieldsets
Then "//*[@id='id_customfield_testfield_editoreditable']//img[contains(@src, 'draftfile.php') and contains(@src, '/gd-logo.png') and @alt='Example']" "xpath_element" should exist
Then "//img[contains(@src, 'draftfile.php') and contains(@src, '/gd-logo.png') and @alt='Example']" "xpath_element" should exist in the "Test field" "form_row"
And I press "Save and display"
And I log out
# Now the same image is displayed as "value" and not as "defaultvalue".
@@ -1,4 +1,4 @@
@customfield @customfield_textarea
@customfield @customfield_textarea @javascript
Feature: Managers can manage course custom fields textarea
In order to have additional data on the course
As a manager
@@ -17,7 +17,7 @@ Feature: Managers can manage course custom fields textarea
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Text area" "dialogue"
Then I should see "Test field"
And I log out
@@ -27,23 +27,22 @@ Feature: Managers can manage course custom fields textarea
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Text area" "dialogue"
And I click on "Edit" "link" in the "Test field" "table_row"
And I set the following fields to these values:
| Name | Edited field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Test field" "dialogue"
Then I should see "Edited field"
And I should not see "Test field"
And I log out
@javascript
Scenario: Delete a custom course textarea field
When I click on "Add a new custom field" "link"
And I click on "Text area" "link"
And I set the following fields to these values:
| Name | Test field |
| Short name | testfield |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Text area" "dialogue"
And I click on "Delete" "link" in the "Test field" "table_row"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Test field"
@@ -105,15 +105,16 @@ class customfield_textarea_plugin_testcase extends advanced_testcase {
* Create a configuration form and submit it with the same values as in the field
*/
public function test_config_form() {
$this->setAdminUser();
$submitdata = (array)$this->cfields[3]->to_record();
$submitdata['configdata'] = $this->cfields[3]->get('configdata');
\core_customfield\field_config_form::mock_submit($submitdata, []);
$handler = $this->cfcat->get_handler();
$form = $handler->get_field_config_form($this->cfields[3]);
$submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
$form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
$submitdata, true);
$form->set_data_for_dynamic_submission();
$this->assertTrue($form->is_validated());
$data = $form->get_data();
$handler->save_field_configuration($this->cfields[3], $data);
$form->process_dynamic_submission();
}
/**
+1 -1
View File
@@ -106,7 +106,7 @@
<td class="col-3">{{{shortname}}}</td>
<td class="col-2">{{{type}}}</td>
<td class="col-2 text-right">
<a href="{{editfieldurl}}" data-role="editfield">{{#pix}}
<a href="#" data-role="editfield" data-name="{{name}}" data-id="{{id}}">{{#pix}}
t/edit, core, {{#str}} edit, moodle {{/str}} {{/pix}}</a>
<a href="#" data-id="{{id}}" data-role="deletefield">{{#pix}}
t/delete, core, {{#str}} delete, moodle {{/str}} {{/pix}}</a>
@@ -1,4 +1,4 @@
@core @core_course @core_customfield
@core @core_course @core_customfield @javascript
Feature: Teachers can edit course custom fields
In order to have additional data on the course
As a teacher
@@ -80,14 +80,14 @@ Feature: Teachers can edit course custom fields
And I navigate to "Courses > Course custom fields" in site administration
And I click on "Edit" "link" in the "Field 1" "table_row"
And I select the text in the "Description" Atto editor
And I click on "Insert or edit image" "button" in the "//*[@data-fieldtype='editor']/*[descendant::*[@id='id_description_editoreditable']]" "xpath_element"
And I click on "Insert or edit image" "button" in the "Description" "form_row"
And I click on "Browse repositories..." "button"
And I click on "Private files" "link" in the ".fp-repo-area" "css_element"
And I click on "gd-logo.png" "link"
And I click on "Select this file" "button"
And I set the field "Describe this image for someone who cannot see it" to "Example"
And I click on "Save image" "button"
And I press "Save changes"
And I click on "Save changes" "button" in the "Updating Field 1" "dialogue"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -104,12 +104,13 @@ Feature: Teachers can edit course custom fields
And I click on "Short text" "link"
And I set the following fields to these values:
| Name | Test field |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
Then I should see "You must supply a value here" in the "Short name" "form_row"
And I set the field "Short name" to "short name"
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I should see "The short name can only contain alphanumeric lowercase characters and underscores (_)." in the "Short name" "form_row"
And I set the field "Short name" to "f1"
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I should see "Short name already exists" in the "Short name" "form_row"
And I click on "Cancel" "button" in the "Adding a new Short text" "dialogue"
And I log out
@@ -1,4 +1,4 @@
@core @core_course @core_customfield
@core @core_course @core_customfield @javascript
Feature: Requiredness The course custom fields can be mandatory or not
In order to make users required to fill a custom field
As a manager
@@ -27,7 +27,7 @@ Feature: Requiredness The course custom fields can be mandatory or not
| Name | Test field |
| Short name | testfield |
| Required | Yes |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
@@ -48,7 +48,7 @@ Feature: Requiredness The course custom fields can be mandatory or not
| Name | Test field |
| Short name | testfield |
| Required | No |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
+2 -2
View File
@@ -1,4 +1,4 @@
@core @core_course @core_customfield
@core @core_course @core_customfield @javascript
Feature: Uniqueness The course custom fields can be mandatory or not
In order to make users required to fill a custom field
As a manager
@@ -27,7 +27,7 @@ Feature: Uniqueness The course custom fields can be mandatory or not
| Name | Test field |
| Short name | testfield |
| Unique data | Yes |
And I press "Save changes"
And I click on "Save changes" "button" in the "Adding a new Short text" "dialogue"
And I log out
Scenario: A course custom field with unique data must not allow same data in same field in different courses
+8
View File
@@ -0,0 +1,8 @@
This files describes API changes in /customfield/*,
Information provided here is intended especially for developers.
=== 3.11 ===
* Methods \core_customfield\handler::get_field_config_form() and \core_customfield\handler::setup_edit_page() are no
longer used. Components that define custom fields areas do not need to implement them. Field edit form opens in
the modal now.