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