ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables 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 
33  require_once 'Services/RTE/classes/class.ilRTE.php';
34  $string = ilRTE::_replaceMediaObjectImageSrc($string, 1);
35 
36  $string = str_replace("</li><br />", "</li>", $string);
37  $string = str_replace("</li><br>", "</li>", $string);
38 
39  require_once 'Services/MathJax/classes/class.ilMathJax.php';
40  $string = ilMathJax::getInstance()->insertLatexImages($string, "\[tex\]", "\[\/tex\]");
41  $string = ilMathJax::getInstance()->insertLatexImages($string, "<span class\=\"latex\">", "<\/span>");
42 
43  $string = str_replace('{', '&#123;', $string);
44  $string = str_replace('}', '&#125;', $string);
45 
46  return $string;
47  }
48 
53  protected function handleLineBreaks($string): string
54  {
55  if (!ilUtil::isHTML($string)) {
56  $string = nl2br($string);
57  }
58 
59  return $string;
60  }
61 
66  protected function convertLatexSpanToTex($string): string
67  {
68  // we try to save all latex tags
69  $try = true;
70  $ls = '<span class="latex">';
71  $le = '</span>';
72  while ($try) {
73  // search position of start tag
74  $pos1 = strpos($string, $ls);
75  if (is_int($pos1)) {
76  $pos2 = strpos($string, $le, $pos1);
77  if (is_int($pos2)) {
78  // both found: replace end tag
79  $string = substr($string, 0, $pos2) . "[/tex]" . substr($string, $pos2 + 7);
80  $string = substr($string, 0, $pos1) . "[tex]" . substr($string, $pos1 + 20);
81  } else {
82  $try = false;
83  }
84  } else {
85  $try = false;
86  }
87  }
88 
89  return $string;
90  }
91 
96  protected function stripHtmlExceptSelfAssessmentTags($string): string
97  {
98  $tags = self::getSelfAssessmentTags();
99 
100  $tstr = "";
101 
102  foreach ($tags as $t) {
103  $tstr .= "<" . $t . ">";
104  }
105 
106  $string = ilUtil::secureString($string, true, $tstr);
107 
108  return $string;
109  }
110 
115  public function migrateToLmContent($string): string
116  {
117  $string = $this->convertLatexSpanToTex($string);
118  $string = $this->stripHtmlExceptSelfAssessmentTags($string);
119  return $string;
120  }
121 
125  public static function prepareQuestionForLearningModule(assQuestion $question): void
126  {
127  $question->migrateContentForLearningModule(new self());
128  }
129 
134  public static function getSelfAssessmentTags(): array
135  {
136  // set tags we allow in self assessment mode
137  $st = ilUtil::getSecureTags();
138 
139  $not_supported = ['img'];
140  $tags = ['br', 'table', 'td', 'tr', 'th'] + array_diff($st, $not_supported);
141 
142  return $tags;
143  }
144 }
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.
Abstract basic class which is to be extended by the concrete assessment question type classes...
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...