File: preview.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (166 lines) | stat: -rw-r--r-- 6,321 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
    require_once('../../../../config.php');
    require_once('../../lib.php');
    require_once('resource.class.php');
    require_once('../../../../backup/lib.php');
    require_once('../../../../lib/filelib.php');
    require_once('../../../../lib/xmlize.php');
    
    require_once('repository_config.php');
    
    $directory = required_param ('directory', PARAM_PATH);
    $page = optional_param ('page', 0, PARAM_INT);

/// Calculate the path of the IMS CP to be displayed
    $deploydir = $CFG->repository . '/' . $directory;

/// Confirm that the IMS package has been deployed. Hash not generated
/// for repository ones.
    if (!file_exists($deploydir.'/moodle_inx.ser')) {
            $errortext = "Not Deployed";
            print_header();
            print_simple_box_start('center', '60%');
            echo '<p align="center">'.$errortext.'</p>';
            print_footer();
            exit;
    }               

/// Load serialized IMS CP index to memory only once.
    if (empty($items)) {
        if (!$items = ims_load_serialized_file($deploydir.'/moodle_inx.ser')) {
            error (get_string('errorreadingfile', 'error', 'moodle_inx.ser'));
        }
    }

/// fast forward to first non-index page
    while (empty($items[$page]->href)) $page++;
    
/// Select encoding
    $encoding = current_charset();

/// Select direction
    if (get_string('thisdirection') == 'rtl') {
        $direction = ' dir="rtl"';
    } else {
        $direction = ' dir="ltr"';
    }

/// Conditional argument to pass to IMS JavaScript. Need to be global to retrieve it from our custom javascript! :-(
    global $jsarg;
    $jsarg = 'true';
/// Define $CFG->javascript to use our custom javascript. Save the original one to add it from ours. Global too! :-(
    global $standard_javascript;
    $standard_javascript = $CFG->javascript;  // Save original javascript file
    $CFG->javascript = $CFG->dirroot.'/mod/resource/type/ims/javascript.php';  //Use our custom IMS javascript code

/// The output here

/// moodle header
    print_header();
/// content - this produces everything else

/// adds side navigation bar if needed. must also adjust width of iframe to accomodate 
    echo "<div id=\"ims-menudiv\">";  preview_buttons($directory, $items['title']); echo preview_ims_generate_toc($items, $directory, 0, $page); echo "</div>";
    
    $fullurl = "$CFG->repositorywebroot/$directory/".$items[$page]->href;
/// prints iframe filled with $fullurl ;width:".$iframewidth." missing also height=\"420px\"
    echo "<iframe id=\"ims-contentframe\" name=\"ims-contentframe\" src=\"{$fullurl}\"></iframe>"; //Content frame 
/// moodle footer
    echo "</div></div><script type=\"text/javascript\">resizeiframe($jsarg);</script></body></html>";
    
    
    /*** This function will generate the TOC file for the package
     *   from an specified parent to be used in the view of the IMS
     */
    function preview_ims_generate_toc($items, $directory, $page=0, $selected_page) {
        global $CFG;

        $contents = '';

    /// Configure links behaviour
        $fullurl = '?directory='.$directory.'&amp;page=';

    /// Iterate over items to build the menu
        $currlevel = 0;
        $currorder = 0;
        $endlevel  = 0;
        $openlielement = false;
        foreach ($items as $item) {
            if (!is_object($item)) {
                continue;
            }
        /// Convert text from UTF-8 to current charset if needed
            if (empty($CFG->unicodedb)) {
                $textlib = textlib_get_instance();
                $item->title = $textlib->convert($item->title, 'UTF-8', current_charset());
            }
        /// Skip pages until we arrive to $page
            if ($item->id < $page) {
                continue;
            }
        /// Arrive to page, we store its level
            if ($item->id == $page) {
                $endlevel = $item->level;
                continue;
            }
        /// We are after page and inside it (level > endlevel)
            if ($item->id > $page && $item->level > $endlevel) {
            /// Start Level 
                if ($item->level > $currlevel) {
                    $contents .= '<ol class="listlevel_'.$item->level.'">';
                    $openlielement = false;
                }
            /// End Level
                if ($item->level < $currlevel) {
                    $contents .= '</li>';
                    $contents .= '</ol>';
                }
            /// If we have some openlielement, just close it
                if ($openlielement) {
                    $contents .= '</li>';
                }
            /// Add item
                $contents .= '<li>';
                if (!empty($item->href)) {
                    if ($item->id == $selected_page) $contents .= '<div id="ims-toc-selected">';
                    $contents .= '<a href="'.$fullurl.$item->id.'" target="_parent">'.$item->title.'</a>';
                    if ($item->id == $selected_page) $contents .= '</div>';
                } else {
                    $contents .= $item->title;
                }
                $currlevel = $item->level;
                $openlielement = true;
                continue;
            }
        /// We have reached endlevel, exit
            if ($item->id > $page && $item->level <= $endlevel) {
                break;
            }
        }
    /// Close up to $endlevel
        for ($i=$currlevel;$i>$endlevel;$i--) {
            $contents .= '</li>';
            $contents .= '</ol>';
        }

        return $contents;
    }
    
    function preview_buttons($directory, $name) {
        $strchoose = get_string('choose','resource');
        $strback = get_string('back','resource');
        
        $path = $directory;
        $arr = explode('/', $directory);
        array_pop($arr);
        $directory = implode('/', $arr);
        echo "<div id=\"ims_preview_buttons\" style=\"padding:10px;\">
              (<a href='finder.php?directory=$directory'>$strback</a>) 
              (<a href=\"javascript:
                        opener.document.forms['form'].reference.value = '#$path'; 
                        opener.document.forms['form'].name.value = '$name'; 
                        window.close();
              \">$strchoose</a>)</div>";
    }
    
?>