ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAssSelfAssessmentQuestionFormatter.php
Go to the documentation of this file.
1 <?php
2 
23 {
29  public function format($string): string
30  {
31  $string = $this->handleLineBreaks($string);
32  $string = ilRTE::_replaceMediaObjectImageSrc($string, 1);
33  $string = str_replace("</li><br />", "</li>", $string);
34  $string = str_replace("</li><br>", "</li>", $string);
35  $string = ilMathJax::getInstance()->insertLatexImages($string, "\[tex\]", "\[\/tex\]");
36  $string = ilMathJax::getInstance()->insertLatexImages($string, "<span class\=\"latex\">", "<\/span>");
37  $string = str_replace('{', '&#123;', $string);
38  $string = str_replace('}', '&#125;', $string);
39 
40  return $string;
41  }
42 
47  protected function handleLineBreaks($string): string
48  {
49  if (!ilUtil::isHTML($string)) {
50  $string = nl2br($string);
51  }
52 
53  return $string;
54  }
55 
60  protected function convertLatexSpanToTex($string): string
61  {
62  // we try to save all latex tags
63  $try = true;
64  $ls = '<span class="latex">';
65  $le = '</span>';
66  while ($try) {
67  // search position of start tag
68  $pos1 = strpos($string, $ls);
69  if (is_int($pos1)) {
70  $pos2 = strpos($string, $le, $pos1);
71  if (is_int($pos2)) {
72  // both found: replace end tag
73  $string = substr($string, 0, $pos2) . "[/tex]" . substr($string, $pos2 + 7);
74  $string = substr($string, 0, $pos1) . "[tex]" . substr($string, $pos1 + 20);
75  } else {
76  $try = false;
77  }
78  } else {
79  $try = false;
80  }
81  }
82 
83  return $string;
84  }
85 
90  protected function stripHtmlExceptSelfAssessmentTags($string): string
91  {
92  $tags = self::getSelfAssessmentTags();
93 
94  $tstr = "";
95 
96  foreach ($tags as $t) {
97  $tstr .= "<" . $t . ">";
98  }
99 
100  $string = ilUtil::secureString($string, true, $tstr);
101 
102  return $string;
103  }
104 
109  public function migrateToLmContent($string): string
110  {
111  $string = $this->convertLatexSpanToTex($string);
112  $string = $this->stripHtmlExceptSelfAssessmentTags($string);
113  return $string;
114  }
115 
119  public static function prepareQuestionForLearningModule(assQuestion $question): void
120  {
121  $question->migrateContentForLearningModule(new self());
122  }
123 
128  public static function getSelfAssessmentTags(): array
129  {
130  // set tags we allow in self assessment mode
131  $st = ilUtil::getSecureTags();
132 
133  $not_supported = ['img'];
134  $tags = ['br', 'table', 'td', 'tr', 'th'] + array_diff($st, $not_supported);
135 
136  return $tags;
137  }
138 }
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static getSelfAssessmentTags()
Get tags allowed in question tags in self assessment mode.
migrateContentForLearningModule(ilAssSelfAssessmentMigrator $migrator)
format($string)
Original code copied from ::formatSAQuestion (author: akill)
static isHTML(string $a_text)
Checks if a given string contains HTML or not.
static secureString(string $a_str, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getSecureTags()
static getInstance()
Singleton: get instance for use in ILIAS requests with a config loaded from the settings.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...