diff --git a/lib/accesslib.php b/lib/accesslib.php index 5e01fa175e6..b2d18f55124 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -5521,9 +5521,11 @@ abstract class context extends stdClass implements IteratorAggregate { * type of context, e.g. User, Course, Forum, etc. * @param boolean $short whether to use the short name of the thing. Only applies * to course contexts + * @param boolean $escape Whether the returned name of the thing is to be + * HTML escaped or not. * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { // must be implemented in all context levels throw new coding_exception('can not get name of abstract context'); } @@ -6225,9 +6227,10 @@ class context_system extends context { * * @param boolean $withprefix does not apply to system context * @param boolean $short does not apply to system context + * @param boolean $escape does not apply to system context * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { return self::get_level_name(); } @@ -6484,9 +6487,10 @@ class context_user extends context { * * @param boolean $withprefix whether to prefix the name of the context with User * @param boolean $short does not apply to user context + * @param boolean $escape does not apply to user context * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { global $DB; $name = ''; @@ -6671,9 +6675,10 @@ class context_coursecat extends context { * * @param boolean $withprefix whether to prefix the name of the context with Category * @param boolean $short does not apply to course categories + * @param boolean $escape Whether the returned name of the context is to be HTML escaped or not. * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { global $DB; $name = ''; @@ -6681,7 +6686,11 @@ class context_coursecat extends context { if ($withprefix){ $name = get_string('category').': '; } - $name .= format_string($category->name, true, array('context' => $this)); + if (!$escape) { + $name .= format_string($category->name, true, array('context' => $this, 'escape' => false)); + } else { + $name .= format_string($category->name, true, array('context' => $this)); + } } return $name; } @@ -6903,9 +6912,10 @@ class context_course extends context { * * @param boolean $withprefix whether to prefix the name of the context with Course * @param boolean $short whether to use the short name of the thing. + * @param bool $escape Whether the returned category name is to be HTML escaped or not. * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { global $DB; $name = ''; @@ -6917,9 +6927,18 @@ class context_course extends context { $name = get_string('course').': '; } if ($short){ - $name .= format_string($course->shortname, true, array('context' => $this)); + if (!$escape) { + $name .= format_string($course->shortname, true, array('context' => $this, 'escape' => false)); + } else { + $name .= format_string($course->shortname, true, array('context' => $this)); + } } else { - $name .= format_string(get_course_display_name_for_list($course)); + if (!$escape) { + $name .= format_string(get_course_display_name_for_list($course), true, array('context' => $this, + 'escape' => false)); + } else { + $name .= format_string(get_course_display_name_for_list($course), true, array('context' => $this)); + } } } } @@ -7126,9 +7145,10 @@ class context_module extends context { * @param boolean $withprefix whether to prefix the name of the context with the * module name, e.g. Forum, Glossary, etc. * @param boolean $short does not apply to module context + * @param boolean $escape Whether the returned name of the context is to be HTML escaped or not. * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { global $DB; $name = ''; @@ -7140,7 +7160,11 @@ class context_module extends context { if ($withprefix){ $name = get_string('modulename', $cm->modname).': '; } - $name .= format_string($mod->name, true, array('context' => $this)); + if (!$escape) { + $name .= format_string($mod->name, true, array('context' => $this, 'escape' => false)); + } else { + $name .= format_string($mod->name, true, array('context' => $this)); + } } } return $name; @@ -7401,9 +7425,10 @@ class context_block extends context { * * @param boolean $withprefix whether to prefix the name of the context with Block * @param boolean $short does not apply to block context + * @param boolean $escape does not apply to block context * @return string the human readable context name. */ - public function get_context_name($withprefix = true, $short = false) { + public function get_context_name($withprefix = true, $short = false, $escape = true) { global $DB, $CFG; $name = ''; diff --git a/lib/form/questioncategory.php b/lib/form/questioncategory.php index 8ebe1d0fd7f..32455d58e59 100644 --- a/lib/form/questioncategory.php +++ b/lib/form/questioncategory.php @@ -60,7 +60,7 @@ class MoodleQuickForm_questioncategory extends MoodleQuickForm_selectgroups { $this->_options = $options + $this->_options; $this->loadArrayOptGroups( question_category_options($this->_options['contexts'], $this->_options['top'], $this->_options['currentcat'], - false, $this->_options['nochildrenof'])); + false, $this->_options['nochildrenof'], false)); } } diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index b6277cb1cf8..2cee5ad463d 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1853,6 +1853,8 @@ class html_writer { /** * Generates a simple select form field * + * Note this function does HTML escaping on the optgroup labels, but not on the choice labels. + * * @param array $options associative array value=>label ex.: * array(1=>'One, 2=>Two) * it is also possible to specify optgroup as complex label array ex.: diff --git a/lib/questionlib.php b/lib/questionlib.php index 8c89e043a86..b703fe71096 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1222,7 +1222,7 @@ function add_indented_names($categories, $nochildrenof = -1) { function question_category_select_menu($contexts, $top = false, $currentcat = 0, $selected = "", $nochildrenof = -1) { $categoriesarray = question_category_options($contexts, $top, $currentcat, - false, $nochildrenof); + false, $nochildrenof, false); if ($selected) { $choose = ''; } else { @@ -1386,10 +1386,11 @@ function get_categories_for_contexts($contexts, $sortorder = 'parent, sortorder, * @param int $currentcat * @param bool $popupform * @param int $nochildrenof + * @param boolean $escapecontextnames Whether the returned name of the thing is to be HTML escaped or not. * @return array */ function question_category_options($contexts, $top = false, $currentcat = 0, - $popupform = false, $nochildrenof = -1) { + $popupform = false, $nochildrenof = -1, $escapecontextnames = true) { global $CFG; $pcontexts = array(); foreach ($contexts as $context) { @@ -1410,7 +1411,7 @@ function question_category_options($contexts, $top = false, $currentcat = 0, $categoriesarray = array(); foreach ($pcontexts as $contextid) { $context = context::instance_by_id($contextid); - $contextstring = $context->get_context_name(true, true); + $contextstring = $context->get_context_name(true, true, $escapecontextnames); foreach ($categories as $category) { if ($category->contextid == $contextid) { $cid = $category->id; @@ -1468,14 +1469,15 @@ function question_add_context_in_key($categories) { * Finds top categories in the given categories hierarchy and replace their name with a proper localised string. * * @param array $categories An array of question categories. + * @param boolean $escape Whether the returned name of the thing is to be HTML escaped or not. * @return array The same question category list given to the function, with the top category names being translated. */ -function question_fix_top_names($categories) { +function question_fix_top_names($categories, $escape = true) { foreach ($categories as $id => $category) { if ($category->parent == 0) { $context = context::instance_by_id($category->contextid); - $categories[$id]->name = get_string('topfor', 'question', $context->get_context_name(false)); + $categories[$id]->name = get_string('topfor', 'question', $context->get_context_name(false, false, $escape)); } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 95d16e8785f..a97b146615e 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,12 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 3.9.7 === +* The signature of the get_context_name() function in the abstract class context and all extending classes (such as context_course) + has been extended. The new parameter allows the to get the name without escaped characters. +* The signature of the question_category_options() has been extended. The new parameter allows the to get the categories name + in the returned array without escaped characters. + === 3.9.6 === * The behat transformation 'string time to timestamp' no longer supports datetime format. If provided, the format must be strftime compatible. Example: diff --git a/question/classes/bank/search/category_condition.php b/question/classes/bank/search/category_condition.php index c2a40088854..18e275690cd 100644 --- a/question/classes/bank/search/category_condition.php +++ b/question/classes/bank/search/category_condition.php @@ -131,7 +131,7 @@ class category_condition extends condition { */ protected function display_category_form($contexts, $pageurl, $current) { echo \html_writer::start_div('choosecategory'); - $catmenu = question_category_options($contexts, true, 0, true); + $catmenu = question_category_options($contexts, true, 0, true, -1, false); echo \html_writer::label(get_string('selectacategory', 'question'), 'id_selectacategory', true, array("class" => "mr-1")); echo \html_writer::select($catmenu, 'category', $current, array(), array('class' => 'searchoptions custom-select', 'id' => 'id_selectacategory'));