Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / resource / type / ims / preview.php
blob3ed5f3dbbbb9608404d9f9ba69a6108f3dadd46a
1 <?php // $Id$
3 require_once('../../../../config.php');
4 require_once('../../lib.php');
5 require_once('resource.class.php');
6 require_once('../../../../backup/lib.php');
7 require_once('../../../../lib/filelib.php');
8 require_once('../../../../lib/xmlize.php');
10 require_once('repository_config.php');
12 $directory = required_param ('directory', PARAM_PATH);
13 $choose = optional_param ('choose', 'id_reference_value', PARAM_FILE);
14 $page = optional_param ('page', 0, PARAM_INT);
16 /// Calculate the path of the IMS CP to be displayed
17 $deploydir = $CFG->repository . '/' . $directory;
19 /// Confirm that the IMS package has been deployed. Hash not generated
20 /// for repository ones.
21 if (!file_exists($deploydir.'/moodle_inx.ser')) {
22 $errortext = "Not Deployed";
23 print_header();
24 print_simple_box_start('center', '60%');
25 echo '<p align="center">'.$errortext.'</p>';
26 print_footer();
27 exit;
30 /// Load serialized IMS CP index to memory only once.
31 if (empty($items)) {
32 if (!$items = ims_load_serialized_file($deploydir.'/moodle_inx.ser')) {
33 error (get_string('errorreadingfile', 'error', 'moodle_inx.ser'));
37 /// fast forward to first non-index page
38 while (empty($items[$page]->href)) $page++;
40 /// Select direction
41 if (get_string('thisdirection') == 'rtl') {
42 $direction = ' dir="rtl"';
43 } else {
44 $direction = ' dir="ltr"';
47 /// Conditional argument to pass to IMS JavaScript. Need to be global to retrieve it from our custom javascript! :-(
48 global $jsarg;
49 $jsarg = 'true';
50 /// Define $CFG->javascript to use our custom javascript. Save the original one to add it from ours. Global too! :-(
51 global $standard_javascript;
52 $standard_javascript = $CFG->javascript; // Save original javascript file
53 $CFG->javascript = $CFG->dirroot.'/mod/resource/type/ims/javascript.php'; //Use our custom IMS javascript code
55 /// The output here
57 /// moodle header
58 print_header();
59 /// content - this produces everything else
61 /// adds side navigation bar if needed. must also adjust width of iframe to accomodate
62 echo "<div id=\"ims-menudiv\">";
63 preview_buttons($directory, $items['title'], $choose);
64 echo preview_ims_generate_toc($items, $directory, 0, $page); echo "</div>";
66 $fullurl = "$CFG->repositorywebroot/$directory/".$items[$page]->href;
67 /// prints iframe filled with $fullurl ;width:".$iframewidth." missing also height=\"420px\"
68 echo "<iframe id=\"ims-contentframe\" name=\"ims-contentframe\" src=\"{$fullurl}\"></iframe>"; //Content frame
69 /// moodle footer
70 echo "</div></div><script type=\"text/javascript\">resizeiframe($jsarg);</script></body></html>";
72 /*** This function will generate the TOC file for the package
73 * from an specified parent to be used in the view of the IMS
75 function preview_ims_generate_toc($items, $directory, $page=0, $selected_page) {
76 global $CFG;
78 $contents = '';
80 /// Configure links behaviour
81 $fullurl = '?directory='.$directory.'&amp;page=';
83 /// Iterate over items to build the menu
84 $currlevel = 0;
85 $currorder = 0;
86 $endlevel = 0;
87 $openlielement = false;
88 foreach ($items as $item) {
89 if (!is_object($item)) {
90 continue;
92 /// Skip pages until we arrive to $page
93 if ($item->id < $page) {
94 continue;
96 /// Arrive to page, we store its level
97 if ($item->id == $page) {
98 $endlevel = $item->level;
99 continue;
101 /// We are after page and inside it (level > endlevel)
102 if ($item->id > $page && $item->level > $endlevel) {
103 /// Start Level
104 if ($item->level > $currlevel) {
105 $contents .= '<ol class="listlevel_'.$item->level.'">';
106 $openlielement = false;
108 /// End Level
109 if ($item->level < $currlevel) {
110 $contents .= '</li>';
111 $contents .= '</ol>';
113 /// If we have some openlielement, just close it
114 if ($openlielement) {
115 $contents .= '</li>';
117 /// Add item
118 $contents .= '<li>';
119 if (!empty($item->href)) {
120 if ($item->id == $selected_page) $contents .= '<div id="ims-toc-selected">';
121 $contents .= '<a href="'.$fullurl.$item->id.'" target="_parent">'.$item->title.'</a>';
122 if ($item->id == $selected_page) $contents .= '</div>';
123 } else {
124 $contents .= $item->title;
126 $currlevel = $item->level;
127 $openlielement = true;
128 continue;
130 /// We have reached endlevel, exit
131 if ($item->id > $page && $item->level <= $endlevel) {
132 break;
135 /// Close up to $endlevel
136 for ($i=$currlevel;$i>$endlevel;$i--) {
137 $contents .= '</li>';
138 $contents .= '</ol>';
141 return $contents;
144 function preview_buttons($directory, $name, $choose='') {
145 $strchoose = get_string('choose','resource');
146 $strback = get_string('back','resource');
148 $path = $directory;
149 $arr = explode('/', $directory);
150 array_pop($arr);
151 $directory = implode('/', $arr);
153 <script type="text/javascript">
154 //<![CDATA[
155 function set_value(txt) {
156 opener.document.getElementById('<?php echo $choose ?>').value = txt;
157 window.close();
159 //]]>
160 </script>
161 <?php
162 echo "<div id=\"ims_preview_buttons\" style=\"padding:10px;\">".
163 "(<a href=\"finder.php?directory=$directory&amp;choose=$choose\">$strback</a>) ".
164 "(<a onclick=\"return set_value('#$path')\" href=\"#\">$strchoose</a>)</div>";