From 06147c5ab3256e707bd81df57495c8d0a7e07478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Fri, 12 May 2017 13:00:39 +0200 Subject: [PATCH] MDL-58920 questions: Calculated question name passing multilang. In "Shared wild cards" table calculated question name do pass format_string to process multilang tags. Thus, not breaking the layout any more. --- question/type/calculated/questiontype.php | 38 ++++++++++++------- .../calculated/tests/questiontype_test.php | 21 ++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index e5f5c669fc6..22f56e86337 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -1623,13 +1623,8 @@ class qtype_calculated extends question_type { "; foreach ($datasetdef->questions as $qu) { // Limit the name length displayed. - if (!empty($qu->name)) { - $qu->name = (strlen($qu->name) > $lnamemax) ? - substr($qu->name, 0, $lnamemax).'...' : $qu->name; - } else { - $qu->name = ''; - } - $text .= "    {$qu->name}
"; + $questionname = $this->get_short_question_name($qu->name, $lnamemax); + $text .= "    {$questionname}
"; } $text .= ""; } @@ -1640,6 +1635,26 @@ class qtype_calculated extends question_type { return $text; } + /** + * This function shortens a question name if it exceeds the character limit. + * + * @param string $stringtoshorten the string to be shortened. + * @param int $characterlimit the character limit. + * @return string + */ + public function get_short_question_name($stringtoshorten, $characterlimit) + { + if (!empty($stringtoshorten)) { + $returnstring = format_string($stringtoshorten); + if (strlen($returnstring) > $characterlimit) { + $returnstring = shorten_text($returnstring, $characterlimit, true); + } + return $returnstring; + } else { + return ''; + } + } + /** * This function build a table showing the available category shareable * wild cards, their name, their definition (Min, Max, Decimal) , the item count @@ -1705,17 +1720,12 @@ class qtype_calculated extends question_type { $line = 0; foreach ($datasetdef->questions as $qu) { // Limit the name length displayed. - if (!empty($qu->name)) { - $qu->name = (strlen($qu->name) > $lnamemax) ? - substr($qu->name, 0, $lnamemax).'...' : $qu->name; - } else { - $qu->name = ''; - } + $questionname = $this->get_short_question_name($qu->name, $lnamemax); if ($line) { $text .= ""; } $line++; - $text .= "{$qu->name}"; + $text .= "{$questionname}"; // TODO MDL-43779 should not have quiz-specific code here. $nbofquiz = $DB->count_records('quiz_slots', array('questionid' => $qu->id)); $nbofattempts = $DB->count_records_sql(" diff --git a/question/type/calculated/tests/questiontype_test.php b/question/type/calculated/tests/questiontype_test.php index a233c2f3a24..01ba94a115f 100644 --- a/question/type/calculated/tests/questiontype_test.php +++ b/question/type/calculated/tests/questiontype_test.php @@ -106,4 +106,25 @@ class qtype_calculated_test extends advanced_testcase { ), ), $this->qtype->get_possible_responses($q)); } + + public function test_get_short_question_name() { + $this->resetAfterTest(); + + // Enable multilang filter to on content and heading. + filter_set_global_state('multilang', TEXTFILTER_ON); + filter_set_applies_to_strings('multilang', 1); + $filtermanager = filter_manager::instance(); + $filtermanager->reset_caches(); + + $context = context_system::instance(); + + $longmultilangquestionname = "Lorem ipsum dolor sit amet, consetetur sadipscing elitrLorem ipsum dolor sit amet, consetetur sadipscing elitr"; + $shortmultilangquestionname = "Lorem ipsumLorem ipsum"; + $longquestionname = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr"; + $shortquestionname = "Lorem ipsum"; + $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longmultilangquestionname, 20)); + $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortmultilangquestionname, 20)); + $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longquestionname, 20)); + $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortquestionname, 20)); + } }