ILIAS  release_8 Revision v8.24
class.LMHtmlExport.php
Go to the documentation of this file.
1<?php
2
20
22use ilFileUtils;
23
30{
31 protected \ILIAS\Services\Export\HTML\Util $export_util;
32 protected \ilLogger $log;
33 protected string $target_dir = "";
34 protected string $sub_dir = "";
35 protected string $export_dir = "";
36 protected \ilObjLearningModule $lm;
37 protected \ilGlobalTemplateInterface $main_tpl;
38 protected \ilObjUser $user;
39 protected \ilLocatorGUI $locator;
40 protected \ilCOPageHTMLExport $co_page_html_export;
41 protected string $export_format = "";
42 protected \ilLMPresentationGUI $lm_gui;
43 protected \ilObjectTranslation $obj_transl;
44 protected string $lang = "";
45 protected \ilSetting $lm_settings;
46 protected array $offline_files = [];
47 protected string $initial_user_language = "";
48 protected string $initial_current_user_language = "";
49 protected \ILIAS\GlobalScreen\Services $global_screen;
50 protected \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
51
52 public function __construct(
54 string $export_dir,
55 string $sub_dir,
56 string $export_format = "html",
57 string $lang = ""
58 ) {
59 global $DIC;
60
61 $this->locator = $DIC["ilLocator"];
62 $this->user = $DIC->user();
63 $this->lm = $lm;
64 $this->export_dir = $export_dir;
65 $this->sub_dir = $sub_dir;
66 $this->lang = $lang;
67 $this->target_dir = $export_dir . "/" . $sub_dir;
68 $cs = $DIC->contentStyle();
69 $this->content_style_domain = $cs->domain()->styleForRefId($this->lm->getRefId());
70 $this->co_page_html_export = new \ilCOPageHTMLExport($this->target_dir, $this->getLinker(), $lm->getRefId());
71 $this->co_page_html_export->setContentStyleId(
72 $this->content_style_domain->getEffectiveStyleId()
73 );
74 $this->export_format = $export_format;
75
76 // get learning module presentation gui class
77 $this->lm_gui = new \ilLMPresentationGUI($export_format, ($lang == "all"), $this->target_dir, false);
78 $this->obj_transl = \ilObjectTranslation::getInstance($lm->getId());
79
80 $this->lm_settings = new \ilSetting("lm");
81
82 $this->log = \ilLoggerFactory::getLogger("lm");
83
84 $this->initial_user_language = $this->user->getLanguage();
85 $this->initial_current_user_language = $this->user->getCurrentLanguage();
86
87 $this->global_screen = $DIC->globalScreen();
88 $this->export_util = new \ILIAS\Services\Export\HTML\Util($export_dir, $sub_dir);
89
91 }
92
93 protected function getLinker(): PageLinker
94 {
95 return new \ilLMPresentationLinker(
96 $this->lm,
97 new \ilLMTree($this->lm->getId()),
98 0,
99 $this->lm->getRefId(),
100 $this->lang,
101 "",
102 "",
103 true,
104 "html",
105 false
106 );
107 }
108
112 protected function setAdditionalContextData(string $key, $data): void
113 {
114 $additional_data = $this->global_screen->tool()->context()->current()->getAdditionalData();
115 if ($additional_data->exists($key)) {
116 $additional_data->replace($key, $data);
117 } else {
118 $additional_data->add($key, $data);
119 }
120 }
121
122 protected function resetUserLanguage(): void
123 {
124 $this->user->setLanguage($this->initial_user_language);
125 $this->user->setCurrentLanguage($this->initial_current_user_language);
126 }
127
128
132 protected function initDirectories(): void
133 {
134 // initialize temporary target directory
135 ilFileUtils::delDir($this->target_dir);
136 ilFileUtils::makeDir($this->target_dir);
137 foreach (["mobs", "files", "textimg", "style",
138 "style/images", "content_style", "content_style", "content_style/images"] as $dir) {
139 ilFileUtils::makeDir($this->target_dir . "/" . $dir);
140 }
141 }
142
143 protected function getLanguageIterator(): \Iterator
144 {
145 return new class ($this->lang, $this->obj_transl) implements \Iterator {
146 private int $position = 0;
148 private array $langs = [];
149
150 public function __construct(
151 string $lang,
153 ) {
154 $this->position = 0;
155 if ($lang != "all") {
156 $this->langs = [$lang];
157 } else {
158 foreach ($obj_transl->getLanguages() as $otl) {
159 $this->langs[] = $otl->getLanguageCode();
160 }
161 }
162 }
163
164 public function rewind(): void
165 {
166 $this->position = 0;
167 }
168
169 public function current(): string
170 {
171 return $this->langs[$this->position];
172 }
173
174 public function key(): int
175 {
176 return $this->position;
177 }
178
179 public function next(): void
180 {
181 ++$this->position;
182 }
183
184 public function valid(): bool
185 {
186 return isset($this->langs[$this->position]);
187 }
188 };
189 }
190
191 protected function initLanguage(
194 string $lang
195 ): void {
196 $user_lang = $user->getLanguage();
197
198 if ($lang != "") {
201 } else {
202 $user->setLanguage($user_lang);
203 $user->setCurrentLanguage($user_lang);
204 }
205
206 if ($lang != "") {
207 if ($lang == $this->obj_transl->getMasterLanguage()) {
208 $lm_gui->lang = "";
209 } else {
210 $lm_gui->lang = $lang;
211 }
212 }
213 }
214
215 protected function initGlobalScreen(): void
216 {
217 // set global
218 $this->global_screen->tool()->context()->current()->addAdditionalData(
220 true
221 );
222 }
223
224
228 public function exportHTML(bool $zip = true): void
229 {
230 $this->initGlobalScreen();
231 $this->initDirectories();
232
233 $this->export_util->exportSystemStyle();
234 $this->export_util->exportCOPageFiles($this->content_style_domain->getEffectiveStyleId(), "lm");
235
236 $lang_iterator = $this->getLanguageIterator();
237
238 // iterate all languages
239 foreach ($lang_iterator as $lang) {
240 $this->initLanguage($this->user, $this->lm_gui, $lang);
241 $this->exportHTMLPages();
242 }
243
244 // keep this for some time for reference...
245 /*
246 $services_dir = $a_target_dir."/Services";
247 ilUtil::makeDir($services_dir);
248 $media_service_dir = $services_dir."/MediaObjects";
249 ilUtil::makeDir($media_service_dir);
250 $flv_dir = $a_target_dir."/".ilPlayerUtil::getFlashVideoPlayerDirectory();
251 ilUtil::makeDirParents($flv_dir);
252 $mp3_dir = $media_service_dir."/flash_mp3_player";
253 ilUtil::makeDir($mp3_dir);
254 ilPlayerUtil::copyPlayerFilesToTargetDirectory($flv_dir);
255 ilExplorerBaseGUI::createHTMLExportDirs($a_target_dir);
256 ilPlayerUtil::copyPlayerFilesToTargetDirectory($flv_dir);
257
258 // js files
259 ilUtil::makeDir($a_target_dir.'/js');
260 ilUtil::makeDir($a_target_dir.'/js/yahoo');
261 ilUtil::makeDir($a_target_dir.'/css');
262 foreach (self::getSupplyingExportFiles($a_target_dir) as $f)
263 {
264 if ($f["source"] != "")
265 {
266 ilUtil::makeDirParents(dirname($f["target"]));
267 copy($f["source"], $f["target"]);
268 }
269 }*/
270
271 // template workaround: reset of template
272 /*
273 $tpl = new ilGlobalTemplate("tpl.main.html", true, true);
274 $tpl->setVariable("LOCATION_STYLESHEET",$location_stylesheet);
275 $tpl->addBlockFile("CONTENT", "content", "tpl.adm_content.html");
276 */
277
278 $this->resetUserLanguage();
279
280 $this->addSupplyingExportFiles();
281
282 $this->export_util->exportResourceFiles();
283
284 $this->co_page_html_export->exportPageElements();
285
286 // zip everything
287 if ($zip) {
288 $this->zipPackage();
289 }
290 }
291
296 protected function zipPackage(): void
297 {
298 if ($this->lang == "") {
299 $zip_target_dir = $this->lm->getExportDirectory("html");
300 } else {
301 $zip_target_dir = $this->lm->getExportDirectory("html_" . $this->lang);
302 ilFileUtils::makeDir($zip_target_dir);
303 }
304
305 // zip it all
306 $date = time();
307 $zip_file = $zip_target_dir . "/" . $date . "__" . IL_INST_ID . "__" .
308 $this->lm->getType() . "_" . $this->lm->getId() . ".zip";
309 ilFileUtils::zip($this->target_dir, $zip_file);
310 ilFileUtils::delDir($this->target_dir);
311 }
312
313
317 protected function addSupplyingExportFiles(): void
318 {
319 foreach ($this->getSupplyingExportFiles() as $f) {
320 if ($f["source"] != "") {
321 if ($f["type"] == "js") {
322 $this->global_screen->layout()->meta()->addJs($f["source"]);
323 }
324 if ($f["type"] == "css") {
325 $this->global_screen->layout()->meta()->addCss($f["source"]);
326 }
327 }
328 }
329 }
330
334 protected function getSupplyingExportFiles(string $a_target_dir = "."): array
335 {
336 $scripts = array(
337 array("source" => \ilYuiUtil::getLocalPath('yahoo/yahoo-min.js'),
338 "target" => $a_target_dir . '/js/yahoo/yahoo-min.js',
339 "type" => "js"),
340 array("source" => \ilYuiUtil::getLocalPath('yahoo-dom-event/yahoo-dom-event.js'),
341 "target" => $a_target_dir . '/js/yahoo/yahoo-dom-event.js',
342 "type" => "js"),
343 array("source" => \ilYuiUtil::getLocalPath('animation/animation-min.js'),
344 "target" => $a_target_dir . '/js/yahoo/animation-min.js',
345 "type" => "js"),
346 array("source" => './Services/Accordion/js/accordion.js',
347 "target" => $a_target_dir . '/js/accordion.js',
348 "type" => "js"),
349 array("source" => './Services/Accordion/css/accordion.css',
350 "target" => $a_target_dir . '/css/accordion.css',
351 "type" => "css"),
352 array("source" => './Modules/Scorm2004/scripts/questions/pure.js',
353 "target" => $a_target_dir . '/js/pure.js',
354 "type" => "js"),
355 array("source" => './Modules/Scorm2004/scripts/questions/question_handling.js',
356 "target" => $a_target_dir . '/js/question_handling.js',
357 "type" => "js"),
358 array("source" => './Modules/Scorm2004/templates/default/question_handling.css',
359 "target" => $a_target_dir . '/css/question_handling.css',
360 "type" => "css"),
361 array("source" => './Modules/TestQuestionPool/templates/default/test_javascript.css',
362 "target" => $a_target_dir . '/css/test_javascript.css',
363 "type" => "css"),
365 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalExplorerJsPath(),
366 "type" => "js"),
367 array("source" => \ilExplorerBaseGUI::getLocalJsTreeJsPath(),
368 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalJsTreeJsPath(),
369 "type" => "js"),
370 array("source" => \ilExplorerBaseGUI::getLocalJsTreeCssPath(),
371 "target" => $a_target_dir . "/" . \ilExplorerBaseGUI::getLocalJsTreeCssPath(),
372 "type" => "css"),
373 array("source" => './Modules/LearningModule/js/LearningModule.js',
374 "target" => $a_target_dir . '/js/LearningModule.js',
375 "type" => "js")
376 );
377
378 $mathJaxSetting = new \ilSetting("MathJax");
379 $use_mathjax = (bool) $mathJaxSetting->get("enable");
380 if ($use_mathjax) {
381 $scripts[] = array("source" => "",
382 "target" => $mathJaxSetting->get("path_to_mathjax"),
383 "type" => "js");
384 }
385
386 // auto linking js
387 foreach (\ilLinkifyUtil::getLocalJsPaths() as $p) {
388 if (is_int(strpos($p, "ExtLink"))) {
389 $scripts[] = array("source" => $p,
390 "target" => $a_target_dir . '/js/ilExtLink.js',
391 "type" => "js");
392 }
393 if (is_int(strpos($p, "linkify"))) {
394 $scripts[] = array("source" => $p,
395 "target" => $a_target_dir . '/js/linkify.js',
396 "type" => "js");
397 }
398 }
399
400 // check, why these do not come with the gs meta collector
401 $scripts[] = [
402 "source" => "src/UI/templates/js/MainControls/dist/mainbar.js",
403 "type" => "js"
404 ];
405 $scripts[] = [
406 "source" => "src/UI/templates/js/MainControls/metabar.js",
407 "type" => "js"
408 ];
409 $scripts[] = [
410 "source" => "src/UI/templates/js/MainControls/slate.js",
411 "type" => "js"
412 ];
413 $scripts[] = [
414 "source" => "src/UI/templates/js/Page/stdpage.js",
415 "type" => "js"
416 ];
417 $scripts[] = [
418 "source" => "src/GlobalScreen/Client/dist/GS.js",
419 "type" => "js"
420 ];
421
422 return $scripts;
423 }
424
425
426
430 public function exportHTMLPages(): void
431 {
432 $lm = $this->lm;
433 $lm_gui = $this->lm_gui;
434 $lang = $lm_gui->lang;
435 $all_languages = ($this->lang == "all");
436 $lm_set = $this->lm_settings;
437 $ilLocator = $this->locator;
438
439 $pages = \ilLMPageObject::getPageList($lm->getId());
440
441 $lm_tree = $lm->getLMTree();
442 $first_page = $lm_tree->fetchSuccessorNode($lm_tree->getRootId(), "pg");
443 $first_page_id = $first_page["child"];
444
445 // iterate all learning module pages
446 $mobs = [];
447 $int_links = [];
448 $this->offline_files = [];
449
450 // get html export id mapping
451
452 $exp_id_map = array();
453
454 if ($lm_set->get("html_export_ids")) {
455 foreach ($pages as $page) {
456 $exp_id = \ilLMPageObject::getExportId($this->lm->getId(), $page["obj_id"]);
457 if (trim($exp_id) != "") {
458 $exp_id_map[$page["obj_id"]] = trim($exp_id);
459 }
460 }
461 }
462
463 if ($lang == "") {
464 $lang = "-";
465 }
466
467 reset($pages);
468 foreach ($pages as $page) {
469 if (\ilLMPage::_exists($this->lm->getType(), $page["obj_id"])) {
470 $ilLocator->clearItems();
471 $this->exportPageHTML($page["obj_id"], ($first_page_id == $page["obj_id"]), $lang, "", $exp_id_map);
472 $this->co_page_html_export->collectPageElements("lm:pg", $page["obj_id"], $lang);
473 }
474 }
475 }
476
478 {
479 global $DIC;
480
481 $tabs = $DIC->tabs();
482
483 $tabs->clearTargets();
484 $tabs->clearSubTabs();
485 $tpl = new \ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
486
487 $this->co_page_html_export->getPreparedMainTemplate($tpl);
488
489 return $tpl;
490 }
491
492
496 protected function initScreen(
497 int $lm_page_id,
498 string $frame
499 ): void {
500 $this->global_screen->layout()->meta()->reset();
501
502 // load style sheet depending on user's settings
503 $location_stylesheet = \ilUtil::getStyleSheetLocation();
504 $this->global_screen->layout()->meta()->addCss($location_stylesheet);
505
506 $this->addSupplyingExportFiles();
507
508 // template workaround: reset of template
509 $tpl = $this->getInitialisedTemplate();
511
512 $params = [
513 "obj_id" => $lm_page_id,
514 "ref_id" => $this->lm->getRefId(),
515 "frame" => $frame
516 ];
517
518 $this->lm_gui->initByRequest($params);
519
520 $this->setAdditionalContextData(\ilLMGSToolProvider::LM_QUERY_PARAMS, $params);
521 $this->setAdditionalContextData(\ilLMGSToolProvider::LM_OFFLINE, true);
522
523 $this->lm_gui->injectTemplate($tpl);
524 }
525
526
530 public function exportPageHTML(
531 int $lm_page_id,
532 bool $is_first = false,
533 string $lang = "-",
534 string $frame = "",
535 array $exp_id_map = []
536 ): void {
537 $target_dir = $this->target_dir;
538 $lang_suffix = "";
539 if (!in_array($lang, ["-", ""]) && $this->lang === "all") {
540 $lang_suffix = "_" . $lang;
541 }
542
543 // Init template, lm_gui
544 $this->initScreen($lm_page_id, $frame);
545
546 if ($frame == "") {
547 if (is_array($exp_id_map) && isset($a_exp_id_map[$lm_page_id])) {
548 $file = $target_dir . "/lm_pg_" . $exp_id_map[$lm_page_id] . $lang_suffix . ".html";
549 } else {
550 $file = $target_dir . "/lm_pg_" . $lm_page_id . $lang_suffix . ".html";
551 }
552 } else {
553 if ($frame != "toc") {
554 $file = $target_dir . "/frame_" . $lm_page_id . "_" . $frame . $lang_suffix . ".html";
555 } else {
556 $file = $target_dir . "/frame_" . $frame . $lang_suffix . ".html";
557 }
558 }
559
560 // return if file is already existing
561 if (is_file($file)) {
562 return;
563 }
564
565 $content = $this->lm_gui->layout("main.xml", false);
566
567 // write xml data into the file
568 $fp = fopen($file, "w+");
569 fwrite($fp, $content);
570
571 // close file
572 fclose($fp);
573
574 if ($is_first && $frame == "") {
575 copy($file, $target_dir . "/index" . $lang_suffix . ".html");
576 }
577 }
578}
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=".")
zipPackage()
Zip everything, zip file will be in $this->export_dir, $this->target_dir (sub-dir in export dir) will...
ILIAS GlobalScreen Services $global_screen
initScreen(int $lm_page_id, string $frame)
Init global screen and learning module presentation gui for page.
ILIAS Services Export HTML Util $export_util
Class ilFileUtils.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getExportId(int $a_lm_id, int $a_lmobj_id, string $a_type="pg")
static getPageList(int $lm_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
setCurrentLanguage(string $a_val)
Set current language.
setLanguage(string $a_str)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstance(int $obj_id)
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="", string $a_css_location="")
get full style sheet file name (path inclusive) of current user
static getLocalPath(string $a_name="")
Get local path of a YUI js file.
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$mobs
Definition: imgupload.php:70
if(strpos( $jquery_path, './')===0) elseif(strpos($jquery_path, '.')===0) $mathJaxSetting
Definition: latex.php:54
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lm_set
$lang
Definition: xapiexit.php:26