This commit is contained in:
Huong Nguyen
2024-12-16 09:50:12 +07:00
7 changed files with 84 additions and 16 deletions
@@ -0,0 +1,21 @@
issueNumber: MDL-75075
notes:
core:
- message: >-
The `core_renderer::tag_list` function now has a new parameter named `displaylink`.
When `displaylink` is set to `true`, the tag name will be displayed as a clickable hyperlink.
Otherwise, it will be rendered as plain text.
type: changed
core_tag:
- message: >-
The `core_tag\taglist` class now includes a new property called `displaylink`,
which has a default value of `true`. When `displaylink` is set to `true`,
the tag name will be displayed as a clickable hyperlink. If `displaylink` is set to `false`,
the tag name will be rendered as plain text instead.
type: changed
mod_quiz:
- message: >-
The `quiz_question_tostring` method now includes a new boolean parameter, `displaytaglink`.
This parameter specifies whether the tag name in the question bank should be displayed
as a clickable hyperlink (`true`) or as plain text (`false`).
type: changed
+4 -2
View File
@@ -4553,6 +4553,7 @@ EOD;
* will be appended to the end, JS will toggle the rest of the tags
* @param context $pagecontext specify if needed to overwrite the current page context for the view tag link
* @param bool $accesshidelabel if true, the label should have class="accesshide" added.
* @param bool $displaylink Indicates whether the tag should be displayed as a link.
* @return string
*/
public function tag_list(
@@ -4561,9 +4562,10 @@ EOD;
$classes = '',
$limit = 10,
$pagecontext = null,
$accesshidelabel = false
$accesshidelabel = false,
$displaylink = true,
) {
$list = new taglist($tags, $label, $classes, $limit, $pagecontext, $accesshidelabel);
$list = new taglist($tags, $label, $classes, $limit, $pagecontext, $accesshidelabel, $displaylink);
return $this->render_from_template('core_tag/taglist', $list->export_for_template($this));
}
@@ -45,7 +45,8 @@ class question_name_text_column extends question_name_column {
if ($labelfor) {
echo \html_writer::start_tag('label', ['for' => $labelfor]);
}
echo quiz_question_tostring($question, false, true, true, $question->tags);
echo quiz_question_tostring($question, false, true, true,
$question->tags, false);
if ($labelfor) {
echo \html_writer::end_tag('label');
}
+3 -2
View File
@@ -1637,10 +1637,11 @@ function quiz_get_js_module() {
* @param bool $showidnumber If true, show the question's idnumber, if any. False by default.
* @param core_tag_tag[]|bool $showtags if array passed, show those tags. Else, if true, get and show tags,
* else, don't show tags (which is the default).
* @param bool $displaytaglink Indicates whether the tag should be displayed as a link.
* @return string HTML fragment.
*/
function quiz_question_tostring($question, $showicon = false, $showquestiontext = true,
$showidnumber = false, $showtags = false) {
$showidnumber = false, $showtags = false, $displaytaglink = true) {
global $OUTPUT;
$result = '';
@@ -1667,7 +1668,7 @@ function quiz_question_tostring($question, $showicon = false, $showquestiontext
$tags = [];
}
if ($tags) {
$result .= $OUTPUT->tag_list($tags, null, 'd-inline', 0, null, true);
$result .= $OUTPUT->tag_list($tags, null, 'd-inline', 0, null, true, $displaytaglink);
}
// Question text.
+26
View File
@@ -121,6 +121,32 @@ final class locallib_test extends \advanced_testcase {
'<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]' . "\n" . '</span>', $summary);
}
/**
* Test the method quiz_question_to_string with the tag display.
*
* @covers ::quiz_question_tostring
*/
public function test_quiz_question_tostring_with_tags(): void {
$this->resetAfterTest();
$context = \context_coursecat::instance($this->getDataGenerator()->create_category()->id);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$questioncat = $questiongenerator->create_question_category(['contextid' => $context->id]);
// Create a question.
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $questioncat->id]);
// Add tag to question.
\core_tag_tag::set_item_tags('core_question', 'question', $question->id,
$context, ['Banana']);
// Retrieve the question text to display, including the tag, with the tag displayed as a link.
$summary = quiz_question_tostring(question: $question, showtags: true);
// Ensure the tag is enclosed within a link.
$this->assertMatchesRegularExpression('/<a[^>]*>\s*Banana\s*<\/a>/', $summary);
// Retrieve the question text to display, including the tag, but ensure the tag is not displayed as a link.
$summary = quiz_question_tostring(question: $question, showtags: true, displaytaglink: false);
$this->assertMatchesRegularExpression('/<span[^>]*>\s*Banana\s*<\/span>/', $summary);
}
/**
* @covers ::quiz_question_tostring
*/
+11 -3
View File
@@ -54,6 +54,9 @@ class taglist implements templatable {
/** @var int */
protected $limit;
/** @var bool */
protected $displaylink;
/**
* Constructor
*
@@ -65,15 +68,17 @@ class taglist implements templatable {
* will be appended to the end, JS will toggle the rest of the tags. 0 means no limit.
* @param context $pagecontext specify if needed to overwrite the current page context for the view tag link
* @param bool $accesshidelabel if true, the label should have class="accesshide" added.
* @param bool $displaylink Indicates whether the tag should be displayed as a link.
*/
public function __construct($tags, $label = null, $classes = '',
$limit = 10, $pagecontext = null, $accesshidelabel = false) {
$limit = 10, $pagecontext = null, $accesshidelabel = false, $displaylink = true) {
global $PAGE;
$canmanagetags = has_capability('moodle/tag:manage', \context_system::instance());
$this->label = ($label === null) ? get_string('tags') : $label;
$this->accesshidelabel = $accesshidelabel;
$this->classes = $classes;
$this->displaylink = $displaylink;
$fromctx = $pagecontext ? $pagecontext->id :
(($PAGE->context->contextlevel == CONTEXT_SYSTEM) ? 0 : $PAGE->context->id);
@@ -87,8 +92,10 @@ class taglist implements templatable {
$this->tags[$idx]->flag = 1;
}
$viewurl = core_tag_tag::make_url($tag->tagcollid, $tag->rawname, 0, $fromctx);
$this->tags[$idx]->viewurl = $viewurl->out(false);
if ($displaylink) {
$viewurl = core_tag_tag::make_url($tag->tagcollid, $tag->rawname, 0, $fromctx);
$this->tags[$idx]->viewurl = $viewurl->out(false);
}
if (isset($tag->isstandard)) {
$this->tags[$idx]->isstandard = $tag->isstandard ? 1 : 0;
@@ -116,6 +123,7 @@ class taglist implements templatable {
'tagscount' => $cnt,
'overflow' => ($this->limit && $cnt > $this->limit) ? 1 : 0,
'classes' => $this->classes,
'displaylink' => $this->displaylink,
);
}
}
+17 -8
View File
@@ -41,7 +41,8 @@
"accesshidelabel": false,
"tagscount": 3,
"overflow": 1,
"classes": "someadditionalclass"
"classes": "someadditionalclass",
"displaylink": false
}
}}
@@ -53,13 +54,21 @@
<ul class="inline-list">
{{#tags}}
<li {{#overlimit}}class="overlimit"{{/overlimit}}>
<a href="{{viewurl}}" class="badge bg-info text-white {{#isstandard}}standardtag{{/isstandard}}" >
{{#flag}}
<span class="flagged-tag">{{name}}</span></a>
{{/flag}}
{{^flag}}
{{name}}</a>
{{/flag}}
{{#displaylink}}
<a href="{{viewurl}}" class="badge bg-info text-white {{#isstandard}}standardtag{{/isstandard}}">
{{#flag}}
<span class="flagged-tag">{{name}}</span>
{{/flag}}
{{^flag}}
{{name}}
{{/flag}}
</a>
{{/displaylink}}
{{^displaylink}}
<span class="badge bg-info {{#flag}}flagged-tag{{/flag}} {{^flag}}text-white{{/flag}} {{#isstandard}}standardtag{{/isstandard}}">
{{name}}
</span>
{{/displaylink}}
</li>
{{/tags}}
{{#overflow}}