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