ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCOPageHTMLExport.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected \ILIAS\COPage\Xsl\XslManager $xsl;
27  protected string $mp3_dir = "";
28  protected string $flv_dir = "";
29  protected string $css_dir = "";
30  protected string $js_yahoo_dir = "";
31  protected string $js_dir = "";
32  protected string $media_service_dir = "";
33  protected string $services_dir = "";
34  protected string $content_style_img_dir = "";
35  protected string $content_style_dir = "";
36  protected string $tex_dir = "";
37  protected string $files_dir = "";
38  protected string $mobs_dir = "";
39  protected array $mobs = [];
40  protected array $glossary_terms = [];
41  protected array $files = [];
42  protected array $files_direct = [];
43  protected array $int_links = [];
44  protected array $q_ids = [];
45  protected string $exp_dir = "";
46  protected int $content_style_id = 0;
47  protected ilObjUser $user;
48  protected ilLogger $log;
49  protected \ILIAS\GlobalScreen\Services $global_screen;
50  protected \ILIAS\Skill\Service\SkillTreeService $skill_tree_service;
51  protected \ILIAS\Skill\Service\SkillPersonalService $skill_personal_service;
52  protected \ILIAS\COPage\PageLinker $page_linker;
53  protected int $ref_id;
54 
55  public function __construct(
56  string $a_exp_dir,
57  \ILIAS\COPage\PageLinker $linker = null,
58  int $ref_id = 0
59  ) {
60  global $DIC;
61 
62  $this->log = ilLoggerFactory::getLogger('copg');
63  $this->user = $DIC->user();
64  $this->global_screen = $DIC->globalScreen();
65  $this->skill_tree_service = $DIC->skills()->tree();
66  $this->skill_personal_service = $DIC->skills()->personal();
67  $this->page_linker = is_null($linker)
68  ? new ilPageLinker("", true)
69  : $linker;
70  $this->ref_id = $ref_id;
71 
72  $this->exp_dir = $a_exp_dir;
73  $this->mobs_dir = $a_exp_dir . "/mobs";
74  $this->files_dir = $a_exp_dir . "/files";
75  $this->tex_dir = $a_exp_dir . "/teximg";
76  $this->content_style_dir = $a_exp_dir . "/content_style";
77  $this->content_style_img_dir = $a_exp_dir . "/content_style/images";
78 
79  $this->services_dir = $a_exp_dir . "/Services";
80  $this->media_service_dir = $this->services_dir . "/MediaObjects";
81  $this->flv_dir = $a_exp_dir . "/" . ilPlayerUtil::getFlashVideoPlayerDirectory();
82  $this->mp3_dir = $this->media_service_dir . "/flash_mp3_player";
83 
84  $this->js_dir = $a_exp_dir . '/js';
85  $this->js_yahoo_dir = $a_exp_dir . '/js/yahoo';
86  $this->css_dir = $a_exp_dir . '/css';
87  $this->xsl = $DIC->copage()->internal()->domain()->xsl();
88  }
89 
90  public function setContentStyleId(int $a_val): void
91  {
92  $this->content_style_id = $a_val;
93  }
94 
95  public function getContentStyleId(): int
96  {
98  }
99 
100  public function createDirectories(): void
101  {
102  ilFileUtils::makeDir($this->mobs_dir);
103  ilFileUtils::makeDir($this->files_dir);
104  ilFileUtils::makeDir($this->tex_dir);
105  ilFileUtils::makeDir($this->content_style_dir);
106  ilFileUtils::makeDir($this->content_style_img_dir);
107  ilFileUtils::makeDir($this->services_dir);
108  ilFileUtils::makeDir($this->media_service_dir);
109  ilFileUtils::makeDirParents($this->flv_dir);
110  ilFileUtils::makeDirParents($this->mp3_dir);
111 
112  ilFileUtils::makeDirParents($this->js_dir);
113  ilFileUtils::makeDirParents($this->js_yahoo_dir);
114  ilFileUtils::makeDirParents($this->css_dir);
115  ilFileUtils::makeDirParents($this->css_dir . "/yahoo");
116  }
117 
124  public function exportStyles(): void
125  {
126  $this->log->debug("export styles");
127 
128  // export content style sheet
129  if ($this->getContentStyleId() < 1) { // basic style
132  $this->exp_dir . "/" . ilObjStyleSheet::getBasicImageDir()
133  );
134  ilFileUtils::makeDirParents($this->exp_dir . "/Services/COPage/css");
135  copy("Services/COPage/css/content.css", $this->exp_dir . "/Services/COPage/css/content.css");
136  } else {
137  $style = new ilObjStyleSheet($this->getContentStyleId());
138  $style->copyImagesToDir($this->exp_dir . "/" . $style->getImagesDirectory());
139  $this->exportResourceFile(
140  $this->exp_dir,
142  );
143  }
144 
145  // export syntax highlighting style
146  $syn_stylesheet = ilObjStyleSheet::getSyntaxStylePath();
147  $this->exportResourceFile($this->exp_dir, $syn_stylesheet);
148 
149  // export print style
150  $print_stylesheet = ilObjStyleSheet::getContentPrintStyle();
151  $this->exportResourceFile($this->exp_dir, $print_stylesheet);
152  }
153 
159  public function exportSupportScripts(): void
160  {
161  $this->log->debug("export scripts");
162 
163  $collector = new \ILIAS\COPage\ResourcesCollector(ilPageObjectGUI::OFFLINE);
164 
165  foreach ($collector->getJavascriptFiles() as $js) {
166  $this->exportResourceFile($this->exp_dir, $js);
167  }
168 
169  foreach ($collector->getCssFiles() as $css) {
170  $this->exportResourceFile($this->exp_dir, $css);
171  }
172  // mediaelement.js
174  }
175 
176  protected function exportResourceFile(
177  string $target_dir,
178  string $file
179  ): void {
180  if (is_int(strpos($file, "?"))) {
181  $file = substr($file, 0, strpos($file, "?"));
182  }
183  if (is_file($file)) {
184  $dir = dirname($file);
185  ilFileUtils::makeDirParents($target_dir . "/" . $dir);
186  if (!is_file($target_dir . "/" . $file)) {
187  copy($file, $target_dir . "/" . $file);
188  }
189  }
190  }
191 
192  public function getPreparedMainTemplate(
193  ilGlobalTemplateInterface $a_tpl = null
195  global $DIC;
196  $this->log->debug("get main template");
197 
198 
199  $resource_collector = new \ILIAS\COPage\ResourcesCollector(ilPageObjectGUI::OFFLINE);
200  $resource_injector = new \ILIAS\COPage\ResourcesInjector($resource_collector);
201 
202  if (!is_null($a_tpl)) {
203  $tpl = $a_tpl;
204  } else {
205  // template workaround: reset of template
206  $tpl = new ilGlobalPageTemplate($DIC->globalScreen(), $DIC->ui(), $DIC->http());
207  }
208 
209  // scripts needed
210  /* @todo check
211  $scripts = [];
212  $scripts = array_merge($scripts, ilPlayerUtil::getJsFilePaths());
213 
214  $mathJaxSetting = new ilSetting("MathJax");
215  $use_mathjax = $mathJaxSetting->get("enable");
216  if ($use_mathjax) {
217  $scripts[] = $mathJaxSetting->get("path_to_mathjax");
218  }
219  */
220 
221  $tpl->addCss(\ilUtil::getStyleSheetLocation());
223  $tpl->addCss(ilObjStyleSheet::getSyntaxStylePath());
224 
225  $resource_injector->inject($tpl);
226 
227  return $tpl;
228  }
229 
233  public function collectPageElements(
234  string $a_type,
235  int $a_id,
236  string $lang = ""
237  ): void {
238  $this->log->debug("collect page elements");
239 
240  // collect all dependent pages (only one level deep)
241  $pages[] = [
242  "type" => $a_type,
243  "id" => $a_id
244  ];
245 
246  // ... content includes
247  $pcs = ilPageContentUsage::getUsagesOfPage($a_id, $a_type, 0, false, $lang);
248  foreach ($pcs as $pc) {
249  // content includes
250  if ($pc["type"] == "incl") {
251  $pages[] = [
252  "type" => "mep:pg",
253  "id" => $pc["id"]
254  ];
255  }
256  }
257 
258  // ... internal links
259  $pg_links = \ilInternalLink::_getTargetsOfSource($a_type, $a_id, $lang);
260  $this->int_links = array_merge($this->int_links, $pg_links);
261  $this->glossary_terms = [];
262 
263  // ... glossary pages of internal links
264  foreach ($this->int_links as $int_link) {
265  if ($int_link["type"] == "git") {
266  $this->glossary_terms[] = $int_link["id"];
267  // store linked/embedded media objects of glosssary term
268  $pages[] = [
269  "type" => "term:pg",
270  "id" => $int_link["id"]
271  ];
272  }
273  }
274 
275  // resources of pages
276  foreach ($pages as $page) {
277  $page_id = $page["id"];
278  $page_type = $page["type"];
279 
280  // collect media objects
281  $pg_mobs = ilObjMediaObject::_getMobsOfObject($page_type, $page_id, 0, $lang);
282  foreach ($pg_mobs as $pg_mob) {
283  $this->mobs[$pg_mob] = $pg_mob;
284  $this->log->debug("HTML Export: Add media object $pg_mob (" . \ilObject::_lookupTitle($pg_mob) . ") " .
285  " due to page $page_id, $page_type ).");
286  }
287 
288  // collect all files
289  $files = ilObjFile::_getFilesOfObject($page_type, $page_id, 0, $lang);
290  foreach ($files as $f) {
291  $this->files[$f] = $f;
292  }
293 
294  // collect all questions
295  $q_ids = \ilPCQuestion::_getQuestionIdsForPage($a_type, $a_id, $lang);
296  foreach ($q_ids as $q_id) {
297  $this->q_ids[$q_id] = $q_id;
298  }
299  }
300 
301  // collect page content items
302  $skill_tree = $ws_tree = null;
303 
304  // skills
305  foreach ($pcs as $pc) {
306  if ($pc["type"] == "skmg") {
307  $skill_id = $pc["id"];
308 
309  // trying to find user id
310  $user_id = null;
311  switch ($a_type) {
312  case "prtf:pg":
313  $page = new ilPortfolioPage($a_id);
314  $user_id = $page->getCreationUserId();
315  break;
316 
317  default:
318  // :TODO:
319  break;
320  }
321 
322  if ($user_id) {
323  // we only need 1 instance each
324  if (!$skill_tree) {
325  $skill_tree = $this->skill_tree_service->getGlobalSkillTree();
326 
327  $ws_tree = new ilWorkspaceTree($user_id);
328  }
329 
330  // walk skill tree
331  $skill_id = (int) $skill_id;
332  $vtree = $this->skill_tree_service->getVirtualSkillTreeForNodeId($skill_id);
333  $tref_id = 0;
334  if (ilSkillTreeNode::_lookupType($skill_id) == "sktr") {
335  $tref_id = $skill_id;
336  $skill_id = ilSkillTemplateReference::_lookupTemplateId($skill_id);
337  }
338  $b_skills = $vtree->getSubTreeForCSkillId($skill_id . ":" . $tref_id, true);
339 
340  foreach ($b_skills as $bs) {
341  $skill = ilSkillTreeNodeFactory::getInstance($bs["skill_id"]);
342  $level_data = $skill->getLevelData();
343  foreach ($level_data as $k => $v) {
344  // get assigned materials from personal skill
345  $mat = $this->skill_personal_service->getAssignedMaterials($user_id, $bs["tref_id"], $v["id"]);
346  if (count($mat)) {
347  foreach ($mat as $item) {
348  $wsp_id = $item->getWorkspaceId();
349  $obj_id = $ws_tree->lookupObjectId($wsp_id);
350 
351  // all possible material types for now
352  switch (ilObject::_lookupType($obj_id)) {
353  case "file":
354  $this->files[$obj_id] = $obj_id;
355  break;
356 
357  case "tstv":
358  $obj = new ilObjTestVerification($obj_id, false);
359  $this->files_direct[$obj_id] = array($obj->getFilePath(),
360  $obj->getOfflineFilename());
361  break;
362 
363  case "excv":
364  $obj = new ilObjExerciseVerification($obj_id, false);
365  $this->files_direct[$obj_id] = array($obj->getFilePath(),
366  $obj->getOfflineFilename());
367  break;
368 
369  case "crsv":
370  $obj = new ilObjCourseVerification($obj_id, false);
371  $this->files_direct[$obj_id] = array($obj->getFilePath(),
372  $obj->getOfflineFilename());
373  break;
374 
375  case "cmxv":
376  $obj = new ilObjCmiXapiVerification($obj_id, false);
377  $this->files_direct[$obj_id] = array($obj->getFilePath(),
378  $obj->getOfflineFilename());
379  break;
380 
381  case "ltiv":
382  $obj = new ilObjLTIConsumerVerification($obj_id, false);
383  $this->files_direct[$obj_id] = array($obj->getFilePath(),
384  $obj->getOfflineFilename());
385  break;
386 
387  case "scov":
388  $obj = new ilObjSCORMVerification($obj_id, false);
389  $this->files_direct[$obj_id] = array($obj->getFilePath(),
390  $obj->getOfflineFilename());
391  break;
392  }
393  }
394  }
395  }
396  }
397  }
398  }
399  }
400  }
401 
405  public function exportPageElements(
406  callable $a_update_callback = null
407  ): void {
408  $this->log->debug("export page elements");
409 
410  $total = count($this->mobs) + count($this->files) + count($this->files_direct);
411  $cnt = 0;
412  // export all media objects
413  $linked_mobs = array();
414  foreach ($this->mobs as $mob) {
415  if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
416  $this->exportHTMLMOB($mob, $linked_mobs);
417  }
418  if (is_callable($a_update_callback)) {
419  $cnt++;
420  $a_update_callback($total, $cnt);
421  }
422  }
423  $linked_mobs2 = array(); // mobs linked in link areas
424  foreach ($linked_mobs as $mob) {
425  if (ilObject::_exists($mob)) {
426  $this->exportHTMLMOB($mob, $linked_mobs2);
427  }
428  }
429 
430  // export all file objects
431  foreach ($this->files as $file) {
432  $this->exportHTMLFile($file);
433  if (is_callable($a_update_callback)) {
434  $cnt++;
435  $a_update_callback($total, $cnt);
436  }
437  }
438 
439  // export all files (which are not objects
440  foreach ($this->files_direct as $file_id => $attr) {
441  $this->exportHTMLFileDirect($file_id, $attr[0], $attr[1]);
442  if (is_callable($a_update_callback)) {
443  $cnt++;
444  $a_update_callback($total, $cnt);
445  }
446  }
447 
448  $this->exportQuestionFiles();
449 
450  // export all glossary terms
451  $this->exportHTMLGlossaryTerms();
452  }
453 
457  protected function initResourceTemplate(
458  string $template_file
460  $this->global_screen->layout()->meta()->reset();
461  $tpl = new ilGlobalTemplate($template_file, true, true, "Services/COPage");
462  $this->getPreparedMainTemplate($tpl);
463  $tpl->addCss(\ilUtil::getStyleSheetLocation());
465  $tpl->addCss(ilObjStyleSheet::getSyntaxStylePath());
466  return $tpl;
467  }
468 
472  public function exportHTMLMOB(
473  int $a_mob_id,
474  array &$a_linked_mobs
475  ): void {
476  $this->log->debug("export html mobs");
477 
478  $source_dir = ilFileUtils::getWebspaceDir() . "/mobs/mm_" . $a_mob_id;
479  if (is_dir($source_dir)) {
480  ilFileUtils::makeDir($this->mobs_dir . "/mm_" . $a_mob_id);
481  ilFileUtils::rCopy($source_dir, $this->mobs_dir . "/mm_" . $a_mob_id);
482  }
483 
484  $mob_obj = new ilObjMediaObject($a_mob_id);
485 
486  $tpl = $this->initResourceTemplate("tpl.fullscreen.html");
487  $med_links = ilMediaItem::_getMapAreasIntLinks($a_mob_id);
488  $link_xml = $this->page_linker->getLinkXML($med_links);
489 
490  $params = [
491  "mode" => "media",
492  'enlarge_path' => ilUtil::getImagePath("media/enlarge.svg", false, "output", true),
493  'enable_html_mob' => ilObjMediaObject::isTypeAllowed("html") ? "y" : "n",
494  'fullscreen_link' => $this->page_linker->getFullScreenLink()
495  ];
496  if ($this->ref_id > 0) {
497  $params["ref_id"] = $this->ref_id;
498  $params["link_params"] = "ref_id=" . $this->ref_id;
499  }
500 
501  $tpl->setVariable("MEDIA_CONTENT", $this->renderMob($mob_obj, $link_xml, $params));
502  $html = $tpl->printToString();
503  $file = $this->exp_dir . "/media_" . $a_mob_id . ".html";
504  $fp = fopen($file, "w+");
505  fwrite($fp, $html);
506  fclose($fp);
507  unset($fp);
508 
509  if ($mob_obj->hasFullscreenItem()) {
510  $tpl = $this->initResourceTemplate("tpl.fullscreen.html");
511  $params["mode"] = "fullscreen";
512  $tpl->setVariable("MEDIA_CONTENT", $this->renderMob($mob_obj, $link_xml, $params));
513  $html = $tpl->printToString();
514  $file = $this->exp_dir . "/fullscreen_" . $a_mob_id . ".html";
515  $fp = fopen($file, "w+");
516  fwrite($fp, $html);
517  fclose($fp);
518  unset($fp);
519  }
520 
521  $linked_mobs = $mob_obj->getLinkedMediaObjects();
522  $a_linked_mobs = array_merge($a_linked_mobs, $linked_mobs);
523  }
524 
528  protected function renderMob(
529  \ilObjMediaObject $mob_obj,
530  string $link_xml,
531  array $params
532  ): string {
533  $xml = "<dummy>" .
534  $mob_obj->getXML(IL_MODE_ALIAS) .
535  $mob_obj->getXML(IL_MODE_OUTPUT) .
536  $link_xml .
537  "</dummy>";
538 
539  return $this->xsl->process($xml, $params);
540  }
541 
542 
546  public function exportHTMLFile(string $a_file_id): void
547  {
548  $file_dir = $this->files_dir . "/file_" . $a_file_id;
549  ilFileUtils::makeDir($file_dir);
550 
551  $file_obj = new ilObjFile($a_file_id, false);
552  $source_file = $file_obj->getFile($file_obj->getVersion());
553  if (!is_file($source_file)) {
554  $source_file = $file_obj->getFile();
555  }
556  if (is_file($source_file)) {
557  copy($source_file, $file_dir . "/" . $file_obj->getFileName());
558  }
559  }
560 
564  public function exportHTMLFileDirect(
565  string $a_file_id,
566  string $a_source_file,
567  string $a_file_name
568  ): void {
569  $file_dir = $this->files_dir . "/file_" . $a_file_id;
570  ilFileUtils::makeDir($file_dir);
571 
572  if (is_file($a_source_file)) {
573  copy(
574  $a_source_file,
575  $file_dir . "/" . ilFileUtils::getASCIIFilename($a_file_name)
576  );
577  }
578  }
579 
583  protected function exportQuestionFiles(): void
584  {
585  // export questions (images)
586  if (count($this->q_ids) > 0) {
587  foreach ($this->q_ids as $q_id) {
588  ilFileUtils::makeDirParents($this->exp_dir . "/assessment/0/" . $q_id . "/images");
590  ilFileUtils::getWebspaceDir() . "/assessment/0/" . $q_id . "/images",
591  $this->exp_dir . "/assessment/0/" . $q_id . "/images"
592  );
593  }
594  }
595  }
596 
597  public function exportHTMLGlossaryTerms(): void
598  {
599  foreach ($this->glossary_terms as $term_id) {
600  if (!ilGlossaryTerm::_exists($term_id)) {
601  continue;
602  }
603  $tpl = $this->initResourceTemplate("tpl.glossary_term_output.html");
604 
605  $term_gui = new ilGlossaryTermGUI($term_id);
606  $term_gui->setPageLinker($this->page_linker);
607  $term_gui->setOfflineDirectory($this->exp_dir);
608  $term_gui->output(true, $tpl);
609 
610  // write file
611  $html = $tpl->printToString();
612  $file = $this->exp_dir . "/term_" . $term_id . ".html";
613  $fp = fopen($file, "w+");
614  fwrite($fp, $html);
615  fclose($fp);
616  }
617  }
618 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
renderMob(\ilObjMediaObject $mob_obj, string $link_xml, array $params)
Render Mob.
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.
exportHTMLMOB(int $a_mob_id, array &$a_linked_mobs)
Export media object to html.
ILIAS COPage PageLinker $page_linker
special template class to simplify handling of ITX/PEAR
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
static _exists(int $a_id)
checks whether a glossary term with specified id exists or not
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
exportPageElements(callable $a_update_callback=null)
Export page elements.
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static getUsagesOfPage(int $a_usage_id, string $a_usage_type, int $a_hist_nr=0, bool $a_all_hist_nrs=false, string $a_lang="-")
Get page content usages for page.
collectPageElements(string $a_type, int $a_id, string $lang="")
Collect page elements (that need to be exported separately)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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 makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static _lookupType(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getASCIIFilename(string $a_filename)
global $DIC
Definition: feed.php:28
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
ILIAS Skill Service SkillTreeService $skill_tree_service
static _lookupTitle(int $obj_id)
exportHTMLFile(string $a_file_id)
Export file object.
static isTypeAllowed(string $a_type)
Class ilObjFile.
getPreparedMainTemplate(ilGlobalTemplateInterface $a_tpl=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getFlashVideoPlayerDirectory()
static _getMapAreasIntLinks(int $a_mob_id)
get all internal links of map areas of a mob
exportSupportScripts()
Export support scripts.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
const IL_MODE_ALIAS
static getContentStylePath(int $a_style_id, bool $add_random=true, bool $add_token=true)
get content style path static (to avoid full reading)
ILIAS GlobalScreen Services $global_screen
exportHTMLFileDirect(string $a_file_id, string $a_source_file, string $a_file_name)
Export file from path.
$lang
Definition: xapiexit.php:26
exportResourceFile(string $target_dir, string $file)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(string $a_exp_dir, \ILIAS\COPage\PageLinker $linker=null, int $ref_id=0)
ILIAS Skill Service SkillPersonalService $skill_personal_service
static copyPlayerFilesToTargetDirectory(string $a_target_dir)
const IL_MODE_OUTPUT
exportStyles()
Export content style.
exportQuestionFiles()
Export question images.
getXML(int $a_mode=IL_MODE_FULL, int $a_inst=0, bool $a_sign_locals=false)
get MediaObject XLM Tag
static _lookupType(int $id, bool $reference=false)
initResourceTemplate(string $template_file)
Get resource template.
static _getQuestionIdsForPage(string $a_parent_type, int $a_page_id, string $a_lang="-")
ILIAS COPage Xsl XslManager $xsl
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...