lp = $lp;
$this->courseInfo = api_get_course_info();
$this->domDocument = new DOMDocument();
$this->generateHtml();
}
public function generate(): string
{
$this->generateToc();
$indexHtml = @$this->domDocument->saveHTML();
return api_utf8_decode_xml($indexHtml);
}
private function generateHtml()
{
$iso = api_get_language_isocode();
$title = api_utf8_encode($this->lp->get_name());
$this->domDocument->loadHTML(
'
'.$title.'
'
);
}
private function generateToc()
{
$ulNode = $this->domDocument->getElementById('toc__ul');
$folderName = 'document';
$pathToRemove = '';
$pathToReplace = '';
$result = $this->lp->generate_lp_folder($this->courseInfo);
if (isset($result['dir']) && strpos($result['dir'], 'learning_path')) {
$pathToRemove = 'document'.$result['dir'];
$pathToReplace = $folderName;
}
if ($this->lp->ref === 'chamilo_scorm_export') {
$pathToRemove = 'scorm/'.$this->lp->path.'/document/';
}
foreach ($this->lp->ordered_items as $itemId) {
$item = $this->lp->items[$itemId];
if (!in_array($item->type, [TOOL_QUIZ, TOOL_FORUM, TOOL_THREAD, TOOL_LINK, TOOL_STUDENTPUBLICATION])) {
$myFilePath = $item->get_file_path('scorm/'.$this->lp->path.'/');
$itemFilePath = $myFilePath;
if (!empty($pathToRemove)) {
$itemFilePath = str_replace($pathToRemove, $pathToReplace, $myFilePath);
if ($this->lp->ref === 'chamilo_scorm_export') {
$pathToRemove = 'scorm/'.$this->lp->path.'/';
$itemFilePath = 'document/'.str_replace($pathToRemove, '', $myFilePath);
}
}
} elseif (TOOL_LINK === $item->type) {
$itemFilePath = "link_{$item->get_id()}.html";
} elseif (TOOL_QUIZ === $item->type) {
$itemFilePath = "quiz_{$item->get_id()}.html";
} else {
continue;
}
$itemText = htmlspecialchars(api_utf8_encode($item->get_title()), ENT_QUOTES);
$liNode = $this->domDocument->createElement('li');
$liNode->setAttribute('id', "item_{$item->get_id()}");
if (!empty($item->parent) && $item->parent != 0) {
$possibleItemParent = $this->lp->get_scorm_xml_node(
$ulNode->childNodes,
"item_$item->parent",
'li',
'id'
);
if ($possibleItemParent instanceof DOMElement) {
$innerUlNode = $possibleItemParent->getElementsByTagName('ul')->item(0)
?: $this->domDocument->createElement('ul');
$innerUlNode->appendChild($liNode);
$possibleItemParent->appendChild($innerUlNode);
}
} else {
$ulNode->appendChild($liNode);
}
if (!empty($itemFilePath) && $itemFilePath !== 'document/') {
$aNode = $this->domDocument->createElement('a', $itemText);
$aNode->setAttribute('href', $itemFilePath);
$aNode->setAttribute('target', 'content-frame');
$liNode->appendChild($aNode);
} else {
$liNode->appendChild(
$this->domDocument->createTextNode($itemText)
);
}
}
}
}