MDL-51260 tags: Update the tags form element to use the autocomplete field

AMOS BEGIN
    REM [interestslist_help,moodle] Commas are no longer required
    REM [entertags,tag] Commas are no longer required
    REM [othertags,tag] Commas are no longer required
AMOS END
This commit is contained in:
Damyon Wiese
2015-10-15 12:03:35 +08:00
parent 25de6f3ec0
commit bb6ca6f360
7 changed files with 96 additions and 273 deletions
@@ -35,53 +35,33 @@ Feature: Verify that all form fields values can be get and set
And I log in as "admin"
And I expand "Site administration" node
And I expand "Appearance" node
And I follow "Manage tags"
# Select (multi-select) - We will check "I set the field...".
And I set the field "otagsadd" to "OT1, OT2, OT3, OT4, OT5"
And I press "Add official tags"
And I am on site homepage
And I follow "Course 1"
And I follow "Reset"
# Select (multi-select) - Checking "the select box should contain".
And I expand all fieldsets
And the "Unenrol users" select box should contain "No roles"
And the "Unenrol users" select box should contain "Student"
And the "Unenrol users" select box should contain "Non-editing teacher"
And the "Unenrol users" select box should contain "Teacher"
And the "Unenrol users" select box should contain "Manager"
And the "Unenrol users" select box should contain "No roles, Student, Non-editing teacher, Teacher, Manager"
And the "Unenrol users" select box should contain "Manager, Teacher, Non-editing teacher, Student, No roles"
And the "Unenrol users" select box should not contain "President"
And the "Unenrol users" select box should not contain "Baker"
And the "Unenrol users" select box should not contain "President, Baker"
And I am on site homepage
And I follow "Course 1"
And I turn editing mode on
And I follow "Test this one"
And I press "Create page"
# Select (multi-select) - Checking "the select box should contain".
And the "tags[officialtags][]" select box should contain "OT1"
And the "tags[officialtags][]" select box should contain "OT2"
And the "tags[officialtags][]" select box should contain "OT3"
And the "tags[officialtags][]" select box should contain "OT4"
And the "tags[officialtags][]" select box should contain "OT5"
And the "tags[officialtags][]" select box should contain "OT1, OT2, OT3, OT4, OT5"
And the "tags[officialtags][]" select box should contain "OT5, OT4, OT3, OT2, OT1"
And the "tags[officialtags][]" select box should not contain "OT6"
And the "tags[officialtags][]" select box should not contain "OT7"
And the "tags[officialtags][]" select box should not contain "OT6, OT7"
# Text (textarea & editor) & Select (multi-select) - Checking "I set the following fields to these values".
When I set the following fields to these values:
| HTML format | Student page contents to be tagged |
| tags[officialtags][] | OT1, OT3, OT5 |
| HTML format | Student page contents |
And I press "Save"
Then I should see "Student page contents to be tagged" in the "region-main" "region"
And I should see "OT1" in the ".wiki-tags" "css_element"
And I should see "OT3" in the ".wiki-tags" "css_element"
And I should see "OT5" in the ".wiki-tags" "css_element"
And I should not see "OT2" in the ".wiki-tags" "css_element"
And I should not see "OT4" in the ".wiki-tags" "css_element"
Then I should see "Student page contents" in the "region-main" "region"
And I follow "Edit"
# Select (multi-select) - Checking "I set the field".
And I set the field "tags[officialtags][]" to "OT2, OT4"
And I press "Save"
And I should see "OT2" in the ".wiki-tags" "css_element"
And I should see "OT4" in the ".wiki-tags" "css_element"
And I should not see "OT1" in the ".wiki-tags" "css_element"
And I should not see "OT3" in the ".wiki-tags" "css_element"
And I should not see "OT5" in the ".wiki-tags" "css_element"
And I follow "Edit"
# Select (multi-select) - Checking "the field matches value" and "the field does not match value".
And the field "tags[officialtags][]" matches value "OT2, OT4"
And the field "tags[officialtags][]" does not match value "OT4"
And the field "tags[officialtags][]" does not match value "OT2"
And the field "tags[officialtags][]" does not match value "OT1, OT3, OT5"
And I press "Cancel"
And I follow "Edit settings"
And I expand all fieldsets
# Checkbox - Checking "I set the field".
+3 -2
View File
@@ -4,6 +4,7 @@ Feature: Adding blog tag block
As a user
I need to be able to use block blog tag
@javascript
Scenario: Adding block blog tag to the course
Given the following "users" exist:
| username | firstname | lastname | email |
@@ -29,7 +30,7 @@ Feature: Adding blog tag block
And I set the following fields to these values:
| Entry title | Blog post from teacher |
| Blog entry body | Teacher blog post content |
| Other tags (enter tags separated by commas) | Cats, dogs |
| Tags | Cats, dogs |
And I press "Save changes"
And I log out
And I log in as "student1"
@@ -39,7 +40,7 @@ Feature: Adding blog tag block
And I set the following fields to these values:
| Entry title | Blog post from student |
| Blog entry body | Student blog post content |
| Other tags (enter tags separated by commas) | DOGS, mice |
| Tags | dogs, mice |
And I press "Save changes"
And I follow "c1"
Then I should see "Cats" in the "Blog tags" "block"
+9 -8
View File
@@ -1,4 +1,4 @@
@core @core_course @core_tag
@core @core_course @core_tag @javascript
Feature: Tagging courses
In order to search courses
As a teacher
@@ -27,7 +27,7 @@ Feature: Tagging courses
And I follow "Course 1"
And I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Other tags (enter tags separated by commas) | Mathematics |
| Tags | Mathematics |
And I press "Save and display"
And I log out
@@ -36,15 +36,16 @@ Feature: Tagging courses
And I follow "Course 1"
And "Course tags" "link" should not exist in the "Administration" "block"
And I click on "Edit settings" "link" in the "Administration" "block"
And the field "Other tags (enter tags separated by commas)" matches value "Mathematics"
And I expand all fieldsets
Then I should see "Mathematics" in the ".form-autocomplete-selection" "css_element"
And I set the following fields to these values:
| Other tags (enter tags separated by commas) | Mathematics, Algebra |
| Tags | Algebra |
And I press "Save and display"
And I click on "Dashboard" "link" in the "Navigation" "block"
And I follow "Course 2"
And I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Other tags (enter tags separated by commas) | Mathematics, Geometry |
| Tags | Mathematics, Geometry |
And I press "Save and display"
And I log out
And I log in as "user1"
@@ -71,15 +72,15 @@ Feature: Tagging courses
And I follow "Course 1"
And "Edit settings" "link" should not exist in the "Administration" "block"
And I click on "Course tags" "link" in the "Administration" "block"
And the field "Other tags (enter tags separated by commas)" matches value "Mathematics"
Then I should see "Mathematics" in the ".form-autocomplete-selection" "css_element"
And I set the following fields to these values:
| Other tags (enter tags separated by commas) | Mathematics, Algebra |
| Tags | Algebra |
And I press "Save changes"
And I click on "Dashboard" "link" in the "Navigation" "block"
And I follow "Course 2"
And I click on "Course tags" "link" in the "Administration" "block"
And I set the following fields to these values:
| Other tags (enter tags separated by commas) | Mathematics, Geometry |
| Tags | Mathematics, Geometry |
And I press "Save changes"
And I log out
And I log in as "user1"
+1 -1
View File
@@ -1002,7 +1002,7 @@ $string['institution'] = 'Institution';
$string['instudentview'] = 'in student view';
$string['interests'] = 'Interests';
$string['interestslist'] = 'List of interests';
$string['interestslist_help'] = 'Enter your interests separated by commas. Your interests will be displayed on your profile page as tags.';
$string['interestslist_help'] = 'Enter your interests. Your interests will be displayed on your profile page as tags.';
$string['invalidemail'] = 'Invalid email address';
$string['invalidlogin'] = 'Invalid login, please try again';
$string['invalidusername'] = 'The username can only contain alphanumeric lowercase characters, underscore (_), hyphen (-), period (.) or at symbol (@)';
+2 -2
View File
@@ -37,7 +37,7 @@ $string['deletedcoursetags'] = 'Deleted - Course tags';
$string['description'] = 'Description';
$string['editname'] = 'Edit tag name';
$string['edittag'] = 'Edit this tag';
$string['entertags'] = 'Enter tags separated by commas';
$string['entertags'] = 'Enter tags...';
$string['errortagfrontpage'] = 'Tagging the site main page is not allowed';
$string['errorupdatingrecord'] = 'Error updating tag record';
$string['eventtagadded'] = 'Tag added to an item';
@@ -63,7 +63,7 @@ $string['noresultsfor'] = 'No results for "{$a}"';
$string['nothingtoupdate'] = 'Nothing to update';
$string['officialtag'] = 'Official';
$string['otags'] = 'Official tags';
$string['othertags'] = 'Other tags (enter tags separated by commas)';
$string['othertags'] = 'Other tags';
$string['owner'] = 'Owner';
$string['ptags'] = 'User defined tags (Comma separated)';
$string['relatedblogs'] = 'Most recent blog entries';
+51 -214
View File
@@ -16,9 +16,9 @@
/**
* Drop down for question categories.
* Tag autocomplete field.
*
* Contains HTML class for editing tags, both official and peronal.
* Contains HTML class for editing tags, both official and personal.
*
* @package core_form
* @copyright 2009 Tim Hunt
@@ -26,19 +26,18 @@
*/
global $CFG;
require_once($CFG->libdir . '/form/group.php');
require_once($CFG->libdir . '/form/autocomplete.php');
/**
* Form field type for editing tags.
*
* HTML class for editing tags, both official and peronal.
* HTML class for editing tags, both official and personal.
*
* @package core_form
* @category form
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_tags extends MoodleQuickForm_group {
class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
/**
* Inidcates that the user should be the usual interface, with the official
* tags listed seprately, and a text box where they can type anything.
@@ -60,16 +59,9 @@ class MoodleQuickForm_tags extends MoodleQuickForm_group {
const NOOFFICIAL = 'noofficial';
/**
* Control the fieldnames for form elements display => int, one of the constants above.
* @var array
* @var boolean $showingofficial Official tags shown? (if not, then don't show link to manage official tags).
*/
protected $_options = array('display' => MoodleQuickForm_tags::DEFAULTUI);
/**
* Caches the list of official tags, to save repeat DB queries.
* @var array
*/
protected $_officialtags = null;
protected $showingofficial = false;
/**
* Constructor
@@ -80,26 +72,47 @@ class MoodleQuickForm_tags extends MoodleQuickForm_group {
* @param mixed $attributes Either a typical HTML attribute string or an associative array.
*/
function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
$this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
$this->_appendName = true;
$this->_type = 'tags';
// set the options, do not bother setting bogus ones
if (is_array($options)) {
foreach ($options as $name => $value) {
if (isset($this->_options[$name])) {
if (is_array($value) && is_array($this->_options[$name])) {
$this->_options[$name] = array_merge($this->_options[$name], $value);
} else {
$this->_options[$name] = $value;
}
}
}
if (!isset($options['display'])) {
$options['display'] = self::DEFAULTUI;
}
global $CFG;
$this->showingofficial = $options['display'] != MoodleQuickForm_tags::NOOFFICIAL;
$validoptions = array();
if ($this->showingofficial) {
$validoptions = $this->load_official_tags();
}
// 'tags' option allows us to type new tags.
if ($options['display'] == MoodleQuickForm_tags::ONLYOFFICIAL) {
$attributes['tags'] = false;
} else {
$attributes['tags'] = true;
}
$attributes['multiple'] = 'multiple';
$attributes['placeholder'] = get_string('entertags', 'tag');
parent::MoodleQuickForm_autocomplete($elementName, $elementLabel, $validoptions, $attributes);
}
/**
* Returns HTML for select form element.
*
* @return string
*/
function toHtml(){
global $CFG, $OUTPUT;
if (empty($CFG->usetags)) {
debugging('A tags formslib field has been created even thought $CFG->usetags is false.', DEBUG_DEVELOPER);
}
$managelink = '';
if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showingofficial) {
$url = $CFG->wwwroot .'/tag/manage.php';
$managelink = ' ' . $OUTPUT->action_link($url, get_string('manageofficialtags', 'tag'));
}
return parent::toHTML() . $managelink;
}
/**
@@ -107,193 +120,17 @@ class MoodleQuickForm_tags extends MoodleQuickForm_group {
*
* @access protected
*/
protected function _load_official_tags() {
protected function load_official_tags() {
global $CFG, $DB;
if (!is_null($this->_officialtags)) {
return;
}
$namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
$this->_officialtags = $DB->get_records_menu('tag', array('tagtype' => 'official'), $namefield, 'id,' . $namefield);
}
/**
* Creates the group's elements.
*/
function _createElements() {
global $CFG, $OUTPUT;
$this->_elements = array();
// Official tags.
$showingofficial = $this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL;
if ($showingofficial) {
$this->_load_official_tags();
// If the user can manage official tags, give them a link to manage them.
$label = get_string('otags', 'tag');
if (has_capability('moodle/tag:manage', context_system::instance())) {
$url = $CFG->wwwroot .'/tag/manage.php';
$label .= ' (' . $OUTPUT->action_link(
$url,
get_string('manageofficialtags', 'tag'),
new popup_action('click', $url, 'managetags'),
array('title'=>get_string('newwindow'))) . ')';
}
// Get the list of official tags.
$noofficial = false;
if (empty($this->_officialtags)) {
$officialtags = array('' => get_string('none'));
$noofficial = true;
} else {
$officialtags = array_combine($this->_officialtags, $this->_officialtags);
}
// Create the element.
$size = min(5, count($officialtags));
// E_STRICT creating elements without forms is nasty because it internally uses $this
$officialtagsselect = @MoodleQuickForm::createElement('select', 'officialtags', $label, $officialtags, array('size' => $size));
$officialtagsselect->setMultiple(true);
if ($noofficial) {
$officialtagsselect->updateAttributes(array('disabled' => 'disabled'));
}
$this->_elements[] = $officialtagsselect;
}
// Other tags.
if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL) {
if ($showingofficial) {
$label = get_string('othertags', 'tag');
} else {
$label = get_string('entertags', 'tag');
}
// E_STRICT creating elements without forms is nasty because it internally uses $this
$othertags = @MoodleQuickForm::createElement('textarea', 'othertags', $label, array('cols'=>'40', 'rows'=>'5'));
$this->_elements[] = $othertags;
}
// Paradoxically, the only way to get labels output is to ask for 'hidden'
// labels, and then override the .accesshide class in the CSS!
foreach ($this->_elements as $element){
if (method_exists($element, 'setHiddenLabel')){
$element->setHiddenLabel(true);
}
}
}
/**
* 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
*/
function onQuickFormEvent($event, $arg, &$caller) {
switch ($event) {
case 'updateValue':
// Get the value we should be setting.
$value = $this->_findValue($caller->_constantValues);
if (null === $value) {
// if no boxes were checked, then there is no value in the array
// yet we don't want to display default value in this case
if ($caller->isSubmitted()) {
$value = $this->_findValue($caller->_submitValues);
} else {
$value = $this->_findValue($caller->_defaultValues);
}
}
if (!empty($value) && !(isset($value['officialtags']) || isset($value['othertags']))) {
// Separate the official and unoffical tags, if necessary.
$official = array();
$other = array();
if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL) {
$this->_load_official_tags();
if (!empty($this->_officialtags)) {
$officaltags = array_combine($this->_officialtags, $this->_officialtags);
} else {
$officaltags = array();
}
foreach ($value as $tag) {
if (isset($officaltags[$tag])) {
$official[] = $tag;
} else {
$other[] = $tag;
}
}
} else {
$other = $value;
}
$value = array('officialtags' => $official, 'othertags' => implode(', ', $other));
}
if (!empty($value)) {
$this->setValue($value);
}
break;
default:
return parent::onQuickFormEvent($event, $arg, $caller);
}
}
/**
* Returns HTML for submitlink form element.
*
* @return string
*/
function toHtml() {
require_once('HTML/QuickForm/Renderer/Default.php');
$renderer = new HTML_QuickForm_Renderer_Default();
$renderer->setElementTemplate('{element}');
parent::accept($renderer);
return $renderer->toHtml();
}
/**
* Accepts a renderer
*
* @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
*/
function accept(&$renderer, $required = false, $error = null)
{
$renderer->renderElement($this, $required, $error);
}
/**
* Output both official and peronal.
*
* @param array $submitValues values submitted.
* @param bool $assoc specifies if returned array is associative
* @return array
*/
function exportValue(&$submitValues, $assoc = false) {
$valuearray = array();
// Get the data out of our child elements.
foreach ($this->_elements as $element){
$thisexport = $element->exportValue($submitValues[$this->getName()], true);
if ($thisexport != null){
$valuearray += $thisexport;
}
}
// Get any manually typed tags.
$records = $DB->get_records('tag', array('tagtype' => 'official'), $namefield, 'id,' . $namefield);
$tags = array();
if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL &&
!empty($valuearray['othertags'])) {
$rawtags = explode(',', clean_param($valuearray['othertags'], PARAM_NOTAGS));
foreach ($rawtags as $tag) {
$tags[] = trim($tag);
}
}
// Add any official tags that were selected.
if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL &&
!empty($valuearray['officialtags'])) {
$tags = array_unique(array_merge($tags, $valuearray['officialtags']));
foreach ($records as $record) {
$tags[$record->$namefield] = $record->$namefield;
}
return array($this->getName() => $tags);
return $tags;
}
}
+13 -9
View File
@@ -1,4 +1,4 @@
@mod @mod_wiki @core_tag
@mod @mod_wiki @core_tag @javascript
Feature: Edited wiki pages handle tags correctly
In order to get wiki pages properly labelled
As a user
@@ -33,13 +33,15 @@ Feature: Edited wiki pages handle tags correctly
And I press "Create page"
When I set the following fields to these values:
| HTML format | Student page contents to be tagged |
| Other tags (enter tags separated by commas) | Example, Page, Cool |
| Tags | Example, Page, Cool |
And I press "Save"
Then I should see "Example" in the ".wiki-tags" "css_element"
And I should see "Page" in the ".wiki-tags" "css_element"
And I should see "Cool" in the ".wiki-tags" "css_element"
And I follow "Edit"
And the field "Other tags (enter tags separated by commas)" matches value "Example, Page, Cool"
Then I should see "Example" in the ".form-autocomplete-selection" "css_element"
Then I should see "Page" in the ".form-autocomplete-selection" "css_element"
Then I should see "Cool" in the ".form-autocomplete-selection" "css_element"
And I press "Cancel"
Scenario: Wiki page edition of official tags works as expected
@@ -54,17 +56,19 @@ Feature: Edited wiki pages handle tags correctly
And I follow "Course 1"
And I follow "Test wiki name"
And I press "Create page"
And the "tags[officialtags][]" select box should contain "OT1"
And the "tags[officialtags][]" select box should contain "OT2"
And the "tags[officialtags][]" select box should contain "OT3"
And I click on ".form-autocomplete-downarrow" "css_element"
And I should see "OT1" in the ".form-autocomplete-suggestions" "css_element"
And I should see "OT2" in the ".form-autocomplete-suggestions" "css_element"
And I should see "OT3" in the ".form-autocomplete-suggestions" "css_element"
When I set the following fields to these values:
| HTML format | Student page contents to be tagged |
| tags[officialtags][] | OT1, OT3 |
| Tags | OT1, OT3 |
And I press "Save"
Then I should see "OT1" in the ".wiki-tags" "css_element"
And I should see "OT3" in the ".wiki-tags" "css_element"
And I should not see "OT2" in the ".wiki-tags" "css_element"
And I follow "Edit"
And the field "tags[officialtags][]" matches value "OT1, OT3"
And the field "tags[officialtags][]" does not match value "OT2"
And I should see "OT1" in the ".form-autocomplete-selection" "css_element"
And I should see "OT3" in the ".form-autocomplete-selection" "css_element"
And I should not see "OT2" in the ".form-autocomplete-selection" "css_element"
And I press "Cancel"