ILIAS  trunk Revision v12.0_alpha-1329-g1094ddb0c33
class.LMHtmlExport.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ilFileUtils;
28
30{
31 protected \ILIAS\DI\UIServices $ui;
33 protected Util $export_util;
34 protected \ilLogger $log;
35 protected string $target_dir = "";
36 protected string $sub_dir = "";
37 protected string $export_dir = "";
38 protected \ilObjLearningModule $lm;
39 protected \ilGlobalTemplateInterface $main_tpl;
40 protected \ilObjUser $user;
41 protected \ilLocatorGUI $locator;
42 protected \ilCOPageHTMLExport $co_page_html_export;
43 protected string $export_format = "";
44 protected \ilLMPresentationGUI $lm_gui;
46 protected string $lang = "";
47 protected \ilSetting $lm_settings;
48 protected array $offline_files = [];
49 protected string $initial_user_language = "";
50 protected string $initial_current_user_language = "";
51 protected \ILIAS\GlobalScreen\Services $global_screen;
52 protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
53
54 public function __construct(
56 string $export_dir,
57 string $sub_dir,
58 string $export_format = "html",
59 string $lang = ""
60 ) {
61 global $DIC;
62
63 $this->locator = $DIC["ilLocator"];
64 $this->user = $DIC->user();
65 $this->lm = $lm;
66 $this->export_dir = $export_dir;
67 $this->sub_dir = $sub_dir;
68 $this->lang = $lang;
69 $this->target_dir = $export_dir . "/" . $sub_dir;
70 $cs = $DIC->contentStyle();
71 $this->ui = $DIC->ui();
72 $this->content_style_domain = $cs->domain()->styleForRefId($this->lm->getRefId());
73 $this->collector = $DIC->export()->domain()->html()->collector($this->lm->getId());
74 $this->collector->init();
75
76 $this->export_util = new Util("", "", $this->collector);
77 $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir, $this->getLinker(), $lm->getRefId(), $this->collector);
78
79 $this->co_page_html_export->setContentStyleId(
80 $this->content_style_domain->getEffectiveStyleId()
81 );
82 $this->export_format = $export_format;
83
84 // get learning module presentation gui class
85 $this->lm_gui = new \ilLMPresentationGUI($export_format, ($lang == "all"), $this->target_dir, false);
86 $this->obj_transl = $lm->getObjectProperties()->getPropertyTranslations();
87
88 $this->lm_settings = new \ilSetting("lm");
89
90 $this->log = \ilLoggerFactory::getLogger("lm");
91
92 $this->initial_user_language = $this->user->getLanguage();
93 $this->initial_current_user_language = $this->user->getCurrentLanguage();
94
95 $this->global_screen = $DIC->globalScreen();
96
98 }
99
100 protected function getLinker(): PageLinker
101 {
102 return new \ilLMPresentationLinker(
103 $this->lm,
104 new \ilLMTree($this->lm->getId()),
105 0,
106 $this->lm->getRefId(),
107 $this->lang,
108 "",
109 "",
110 true,
111 "html",
112 false
113 );
114 }
115
119 protected function setAdditionalContextData(string $key, $data): void
120 {
121 $additional_data = $this->global_screen->tool()->context()->current()->getAdditionalData();
122 if ($additional_data->exists($key)) {
123 $additional_data->replace($key, $data);
124 } else {
125 $additional_data->add($key, $data);
126 }
127 }
128
129 protected function resetUserLanguage(): void
130 {
131 $this->user->setLanguage($this->initial_user_language);
132 $this->user->setCurrentLanguage($this->initial_current_user_language);
133 }
134
135
136 protected function getLanguageIterator(): \Iterator
137 {
138 return new class ($this->lang, $this->obj_transl) implements \Iterator {
139 private int $position = 0;
141 private array $langs = [];
142
143 public function __construct(
144 string $lang,
146 ) {
147 $this->position = 0;
148 if ($lang != "all") {
149 $this->langs = [$lang];
150 } else {
151 foreach ($obj_transl->getLanguages() as $otl) {
152 $this->langs[] = $otl->getLanguageCode();
153 }
154 }
155 }
156
157 public function rewind(): void
158 {
159 $this->position = 0;
160 }
161
162 public function current(): string
163 {
164 return $this->langs[$this->position];
165 }
166
167 public function key(): int
168 {
169 return $this->position;
170 }
171
172 public function next(): void
173 {
174 ++$this->position;
175 }
176
177 public function valid(): bool
178 {
179 return isset($this->langs[$this->position]);
180 }
181 };
182 }
183
184 protected function initLanguage(
187 string $lang
188 ): void {
189 $user_lang = $user->getLanguage();
190
191 if ($lang != "") {
194 } else {
195 $user->setLanguage($user_lang);
196 $user->setCurrentLanguage($user_lang);
197 }
198
199 if ($lang != "") {
200 if ($lang == $this->obj_transl->getBaseLanguage()) {
201 $lm_gui->lang = "";
202 } else {
203 $lm_gui->lang = $lang;
204 }
205 }
206 }
207
208 protected function initGlobalScreen(): void
209 {
210 // set global
211 $this->global_screen->tool()->context()->current()->addAdditionalData(
213 true
214 );
215 }
216
217
221 public function exportHTML(bool $zip = true): void
222 {
223 $this->initGlobalScreen();
224
225 $this->export_util->exportSystemStyle(
226 [
227 "icon_lm.svg"
228 ]
229 );
230 $this->export_util->exportCOPageFiles($this->content_style_domain->getEffectiveStyleId(), "lm");
231
232 $lang_iterator = $this->getLanguageIterator();
233
234 // iterate all languages
235 foreach ($lang_iterator as $lang) {
236 $this->initLanguage($this->user, $this->lm_gui, $lang);
237 $this->exportHTMLPages();
238 }
239
240 $this->resetUserLanguage();
241
242 $this->addSupplyingExportFiles();
243
244 $this->co_page_html_export->exportPageElements();
245
246 }
247
248
252 protected function addSupplyingExportFiles(): void
253 {
254 foreach ($this->getSupplyingExportFiles() as $f) {
255 if ($f["source"] != "") {
256 if ($f["type"] == "js") {
257 $this->global_screen->layout()->meta()->addJs($f["source"]);
258 }
259 if ($f["type"] == "css") {
260 $this->global_screen->layout()->meta()->addCss($f["source"]);
261 }
262 }
263 }
264 }
265
269 protected function getSupplyingExportFiles(string $a_target_dir = "."): array
270 {
271 $scripts = array(
272 array("source" => './components/ILIAS/Accordion/css/accordion.css',
273 "target" => $a_target_dir . '/css/accordion.css',
274 "type" => "css"),
275 array("source" => './components/ILIAS/TestQuestionPool/resources/js/dist/pure_rendering.js',
276 "target" => $a_target_dir . '/js/pure.js',
277 "type" => "js"),
278 array("source" => './components/ILIAS/TestQuestionPool/resources/js/dist/question_handling.js',
279 "target" => $a_target_dir . '/js/question_handling.js',
280 "type" => "js"),
281 array("source" => './components/ILIAS/TestQuestionPool/resources/js/dist/question_handling.css',
282 "target" => $a_target_dir . '/css/question_handling.css',
283 "type" => "css"),
284 array("source" => './components/ILIAS/TestQuestionPool/templates/default/test_javascript.css',
285 "target" => $a_target_dir . '/css/test_javascript.css',
286 "type" => "css"),
288 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalExplorerJsPath(),
289 "type" => "js"),
290 array("source" => \ilExplorerBaseGUI::getLocalJsTreeJsPath(),
291 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalJsTreeJsPath(),
292 "type" => "js"),
293 array("source" => \ilExplorerBaseGUI::getLocalJsTreeCssPath(),
294 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalJsTreeCssPath(),
295 "type" => "css"),
296 array("source" => './components/ILIAS/LearningModule/js/LearningModule.js',
297 "target" => $a_target_dir . '/js/LearningModule.js',
298 "type" => "js")
299 );
300
301 $mathJaxSetting = new \ilSetting("MathJax");
302 $use_mathjax = (bool) $mathJaxSetting->get("enable");
303 if ($use_mathjax) {
304 $scripts[] = array("source" => "",
305 "target" => $mathJaxSetting->get("path_to_mathjax"),
306 "type" => "js");
307 }
308
309 // auto linking js
310 foreach (\ilLinkifyUtil::getLocalJsPaths() as $p) {
311 if (is_int(strpos($p, "ExtLink"))) {
312 $scripts[] = array("source" => $p,
313 "target" => $a_target_dir . '/js/ilExtLink.js',
314 "type" => "js");
315 }
316 if (is_int(strpos($p, "linkify"))) {
317 $scripts[] = array("source" => $p,
318 "target" => $a_target_dir . '/js/linkify.js',
319 "type" => "js");
320 }
321 }
322
323 // check, why these do not come with the gs meta collector
324 $scripts[] = [
325 "source" => "assets/js/mainbar.js",
326 "type" => "js"
327 ];
328 $scripts[] = [
329 "source" => "assets/js/metabar.js",
330 "type" => "js"
331 ];
332 $scripts[] = [
333 "source" => "assets/js/slate.js",
334 "type" => "js"
335 ];
336 $scripts[] = [
337 "source" => "assets/js/stdpage.js",
338 "type" => "js"
339 ];
340 $scripts[] = [
341 "source" => "assets/js/GS.js",
342 "type" => "js"
343 ];
344
345 return $scripts;
346 }
347
348
349
353 public function exportHTMLPages(): void
354 {
355 $lm = $this->lm;
356 $lm_gui = $this->lm_gui;
357 $lang = $lm_gui->lang;
358 $all_languages = ($this->lang == "all");
359 $lm_set = $this->lm_settings;
360 $ilLocator = $this->locator;
361
362 $pages = \ilLMPageObject::getPageList($lm->getId());
363
364 $lm_tree = $lm->getLMTree();
365 $first_page = $lm_tree->fetchSuccessorNode($lm_tree->getRootId(), "pg");
366 $first_page_id = $first_page["child"];
367
368 // iterate all learning module pages
369 $mobs = [];
370 $int_links = [];
371 $this->offline_files = [];
372
373 // get html export id mapping
374
375 $exp_id_map = array();
376
377 if ($lm_set->get("html_export_ids")) {
378 foreach ($pages as $page) {
379 $exp_id = \ilLMPageObject::getExportId($this->lm->getId(), $page["obj_id"]);
380 if (trim($exp_id) != "") {
381 $exp_id_map[$page["obj_id"]] = trim($exp_id);
382 }
383 }
384 }
385
386 if ($lang == "") {
387 $lang = "-";
388 }
389
390 reset($pages);
391 foreach ($pages as $page) {
392 if (\ilLMPage::_exists($this->lm->getType(), $page["obj_id"])) {
393 $ilLocator->clearItems();
394 $this->exportPageHTML($page["obj_id"], ($first_page_id == $page["obj_id"]), $lang, "", $exp_id_map);
395 $this->co_page_html_export->collectPageElements("lm:pg", $page["obj_id"], $lang);
396 }
397 }
398 }
399
401 {
402 global $DIC;
403
404 $tabs = $DIC->tabs();
405
406 $tabs->clearTargets();
407 $tabs->clearSubTabs();
408 $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
409
410 $this->co_page_html_export->getPreparedMainTemplate($tpl);
411
412 return $tpl;
413 }
414
415
419 protected function initScreen(
420 int $lm_page_id,
421 string $frame
422 ): void {
423 $this->export_util->resetGlobalScreen();
424 // load style sheet depending on user's settings
425 $location_stylesheet = \ilUtil::getStyleSheetLocation();
426 $this->global_screen->layout()->meta()->addCss($location_stylesheet);
427
428 $this->addSupplyingExportFiles();
429
430 // template workaround: reset of template
431 $tpl = $this->getInitialisedTemplate();
433
434 $params = [
435 "obj_id" => $lm_page_id,
436 "ref_id" => $this->lm->getRefId(),
437 "frame" => $frame
438 ];
439
440 $this->lm_gui->initByRequest($params);
441
442 $this->setAdditionalContextData(\ilLMGSToolProvider::LM_QUERY_PARAMS, $params);
443 $this->setAdditionalContextData(\ilLMGSToolProvider::LM_OFFLINE, true);
444
445 $this->lm_gui->injectTemplate($tpl);
446 }
447
448
452 public function exportPageHTML(
453 int $lm_page_id,
454 bool $is_first = false,
455 string $lang = "-",
456 string $frame = "",
457 array $exp_id_map = []
458 ): void {
459 $target_dir = $this->target_dir;
460 $lang_suffix = "";
461 if (!in_array($lang, ["-", ""]) && $this->lang === "all") {
462 $lang_suffix = "_" . $lang;
463 }
464 // Init template, lm_gui
465 $this->initScreen($lm_page_id, $frame);
466
467 if ($frame == "") {
468 if (is_array($exp_id_map) && isset($a_exp_id_map[$lm_page_id])) {
469 $file = "lm_pg_" . $exp_id_map[$lm_page_id] . $lang_suffix . ".html";
470 } else {
471 $file = "lm_pg_" . $lm_page_id . $lang_suffix . ".html";
472 }
473 } else {
474 if ($frame != "toc") {
475 $file = "frame_" . $lm_page_id . "_" . $frame . $lang_suffix . ".html";
476 } else {
477 $file = "frame_" . $frame . $lang_suffix . ".html";
478 }
479 }
480
481 // return if file is already existing
482 /*
483 if (is_file($file)) {
484 return;
485 }*/
486
487 $content = $this->lm_gui->layout("main.xml", false);
488
489 $this->collector->addString($content, $file);
490
491 if ($is_first && $frame == "") {
492 $this->collector->addString($content, "index" . $lang_suffix . ".html");
493 }
494 $this->export_util->exportResourceFiles(); // same iteration level as initScreen which calls reset
495 }
496}
Class handles translation mode for an object.
addSupplyingExportFiles()
Add supplying export files.
exportPageHTML(int $lm_page_id, bool $is_first=false, string $lang="-", string $frame="", array $exp_id_map=[])
export single page to file
exportHTMLPages()
export all pages of learning module to html file
__construct(\ilObjLearningModule $lm, string $export_dir, string $sub_dir, string $export_format="html", string $lang="")
initLanguage(\ilObjUser $user, \ilLMPresentationGUI $lm_gui, string $lang)
ILIAS Style Content Object ObjectFacade $content_style_domain
getSupplyingExportFiles(string $a_target_dir=".")
ILIAS GlobalScreen Services $global_screen
initScreen(int $lm_page_id, string $frame)
Init global screen and learning module presentation gui for page.
Util This class is an interim solution for the HTML export handling with 6.0.
Definition: class.Util.php:33
Class ilFileUtils.
static getExportId(int $a_lm_id, int $a_lmobj_id, string $a_type="pg")
static getPageList(int $lm_id)
Class ilLMPresentationGUI GUI class for learning module presentation.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLocalJsPaths()
Get paths of necessary js files.
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
User class.
setLanguage(string $language)
setCurrentLanguage(string $language)
Set current language.
static resetInitialState()
Reset initial state (for exports)
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
static getStyleSheetLocation(string $mode="output", string $a_css_name="")
get full style sheet file name (path inclusive) of current user
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
global $DIC
Definition: shib_login.php:26
$lm_set
getLanguage()