MDL-51283 core_tag: Allow each tag area to set 'showstandard'

This commit is contained in:
Marina Glancy
2016-02-02 17:31:17 +08:00
parent e11d7380c2
commit 4be9c7ad99
11 changed files with 160 additions and 28 deletions
+2 -1
View File
@@ -1977,10 +1977,11 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="itemtype" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="enabled" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="tagcollid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="callback" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="callbackfile" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="showstandard" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+3
View File
@@ -28,6 +28,8 @@
* any other tag areas to this collection nor move this tag area elsewhere
* - searchable (only if collection is specified) - wether the tag collection
* should be searchable on /tag/search.php
* - showstandard - default value for the "Standard tags" attribute of the area,
* this is only respected when new tag area is added and ignored during upgrade
* - customurl (only if collection is specified) - custom url to use instead of
* /tag/search.php to display information about one tag
* - callback - name of the function that returns items tagged with this tag,
@@ -56,6 +58,7 @@ $tagareas = array(
'component' => 'core',
'callback' => 'user_get_tagged_users',
'callbackfile' => '/user/lib.php',
'showstandard' => core_tag_tag::HIDE_STANDARD,
),
array(
'itemtype' => 'course', // Courses.
+26
View File
@@ -4890,5 +4890,31 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2016020200.00);
}
if ($oldversion < 2016020201.00) {
// Define field showstandard to be added to tag_area.
$table = new xmldb_table('tag_area');
$field = new xmldb_field('showstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'callbackfile');
// Conditionally launch add field showstandard.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// By default set user area to hide standard tags. 2 = core_tag_tag::HIDE_STANDARD (can not use constant here).
$DB->execute("UPDATE {tag_area} SET showstandard = ? WHERE itemtype = ? AND component = ?",
array(2, 'user', 'core'));
// Changing precision of field enabled on table tag_area to (1).
$table = new xmldb_table('tag_area');
$field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'itemtype');
// Launch change of precision for field enabled.
$dbman->change_field_precision($table, $field);
// Main savepoint reached.
upgrade_main_savepoint(true, 2016020201.00);
}
return true;
}
+32 -21
View File
@@ -18,7 +18,7 @@
/**
* Tag autocomplete field.
*
* Contains HTML class for editing tags, both official and personal.
* Contains HTML class for editing tags, both standard and not.
*
* @package core_form
* @copyright 2009 Tim Hunt
@@ -31,7 +31,7 @@ require_once($CFG->libdir . '/form/autocomplete.php');
/**
* Form field type for editing tags.
*
* HTML class for editing tags, both official and personal.
* HTML class for editing tags, both standard and not.
*
* @package core_form
* @copyright 2009 Tim Hunt
@@ -41,12 +41,14 @@ 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.
* @deprecated since 3.1
* @var int
*/
const DEFAULTUI = 'defaultui';
/**
* Indicates that the user should only be allowed to select official tags.
* @deprecated since 3.1
* @var int
*/
const ONLYOFFICIAL = 'onlyofficial';
@@ -54,14 +56,15 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
/**
* Indicates that the user should just be given a text box to type in (they
* can still type official tags though.
* @deprecated since 3.1
* @var int
*/
const NOOFFICIAL = 'noofficial';
/**
* @var boolean $showingofficial Official tags shown? (if not, then don't show link to manage official tags).
* @var boolean $showstandard Standard tags suggested? (if not, then don't show link to manage standard tags).
*/
protected $showingofficial = false;
protected $showstandard = false;
/**
* Options passed when creating an element.
@@ -83,25 +86,33 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
if (!empty($options)) {
// Only execute it when the element was created and $options has values set by user.
// In onQuickFormEvent() we make sure that $options is not empty even if developer left it empty.
if (empty($options['display'])) {
$options['display'] = self::DEFAULTUI;
$showstandard = core_tag_tag::BOTH_STANDARD_AND_NOT;
if (isset($options['showstandard'])) {
$showstandard = $options['showstandard'];
} else if (isset($options['display'])) {
debugging('Option "display" is deprecated, each tag area can be configured to show standard tags or not ' .
'by admin or manager. If it is necessary for the developer to override it, please use "showstandard" option',
DEBUG_DEVELOPER);
if ($options['display'] === self::NOOFFICIAL) {
$showstandard = core_tag_tag::HIDE_STANDARD;
} else if ($options['display'] === self::ONLYOFFICIAL) {
$showstandard = core_tag_tag::STANDARD_ONLY;
}
} else if (!empty($options['component']) && !empty($options['itemtype'])) {
$showstandard = core_tag_area::get_showstandard($options['component'], $options['itemtype']);
}
$this->tagsoptions = $options;
$this->showingofficial = $options['display'] != self::NOOFFICIAL;
if ($this->showingofficial) {
$validoptions = $this->load_official_tags();
$this->showstandard = ($showstandard != core_tag_tag::HIDE_STANDARD);
if ($this->showstandard) {
$validoptions = $this->load_standard_tags();
}
// Option 'tags' allows us to type new tags.
if ($options['display'] == self::ONLYOFFICIAL) {
$attributes['tags'] = false;
} else {
$attributes['tags'] = true;
}
$attributes['tags'] = ($showstandard != core_tag_tag::STANDARD_ONLY);
$attributes['multiple'] = 'multiple';
$attributes['placeholder'] = get_string('entertags', 'tag');
$attributes['showsuggestions'] = $this->showingofficial;
$attributes['showsuggestions'] = $this->showstandard;
}
parent::__construct($elementName, $elementLabel, $validoptions, $attributes);
@@ -149,7 +160,7 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
}
/**
* Finds the tag collection to use for official tag selector
* Finds the tag collection to use for standard tag selector
*
* @return int
*/
@@ -181,7 +192,7 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
global $OUTPUT;
$managelink = '';
if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showingofficial) {
if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showstandard) {
$url = new moodle_url('/tag/manage.php', array('tc' => $this->get_tag_collection()));
$managelink = ' ' . $OUTPUT->action_link($url, get_string('managestandardtags', 'tag'));
}
@@ -205,16 +216,16 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
}
/**
* Internal function to load official tags
* Internal function to load standard tags
*/
protected function load_official_tags() {
protected function load_standard_tags() {
global $CFG, $DB;
if (!$this->is_tagging_enabled()) {
return array();
}
$namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
$tags = $DB->get_records_menu('tag',
array('tagtype' => core_tag_tag::TAGTYPE_OFFICIAL, 'tagcollid' => $this->get_tag_collection()),
array('isstandard' => 1, 'tagcollid' => $this->get_tag_collection()),
$namefield, 'id,' . $namefield);
return array_combine($tags, $tags);
}