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