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));
+ }
}