3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Private imscp module utility functions
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
29 require_once("$CFG->dirroot/mod/imscp/lib.php");
30 require_once("$CFG->libdir/filelib.php");
31 require_once("$CFG->libdir/resourcelib.php");
33 function imscp_print_content($imscp, $cm, $course) {
36 $items = unserialize($imscp->structure
);
37 $first = reset($items);
38 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
39 $urlbase = "$CFG->wwwroot/pluginfile.php";
40 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$first['href'];
41 $firsturl = file_encode_url($urlbase, $path, false);
43 echo '<div id="imscp_layout">';
44 echo '<div id="imscp_toc">';
45 echo '<div id="imscp_tree"><ul>';
46 foreach ($items as $item) {
47 echo imscp_htmllize_item($item, $imscp, $cm);
50 echo '<div id="imscp_nav" style="display:none"><button id="nav_skipprev"><<</button><button id="nav_prev"><</button><button id="nav_up">^</button><button id="nav_next">></button><button id="nav_skipnext">>></button></div>';
54 $PAGE->requires
->js_init_call('M.mod_imscp.init');
59 * Internal function - creates htmls structure suitable for YUI tree.
61 function imscp_htmllize_item($item, $imscp, $cm) {
64 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
65 $urlbase = "$CFG->wwwroot/pluginfile.php";
66 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$item['href'];
67 $url = file_encode_url($urlbase, $path, false);
68 $result = "<li><a href=\"$url\">".$item['title'].'</a>';
69 if ($item['subitems']) {
71 foreach ($item['subitems'] as $subitem) {
72 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
82 * Parse an IMS content package's manifest file to determine its structure
83 * @param object $imscp
84 * @param object $context
87 function imscp_parse_structure($imscp, $context) {
88 $fs = get_file_storage();
90 if (!$manifestfile = $fs->get_file($context->id
, 'mod_imscp', 'content', $imscp->revision
, '/', 'imsmanifest.xml')) {
94 return imscp_parse_manifestfile($manifestfile->get_content());
98 * Parse the contents of a IMS package's manifest file
99 * @param string $manifestfilecontents the contents of the manifest file
102 function imscp_parse_manifestfile($manifestfilecontents) {
103 $doc = new DOMDocument();
104 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET
)) {
108 // we put this fake URL as base in order to detect path changes caused by xml:base attributes
109 $doc->documentURI
= 'http://grrr/';
111 $xmlorganizations = $doc->getElementsByTagName('organizations');
112 if (empty($xmlorganizations->length
)) {
116 if ($xmlorganizations->item(0)->attributes
->getNamedItem('default')) {
117 $default = $xmlorganizations->item(0)->attributes
->getNamedItem('default')->nodeValue
;
119 $xmlorganization = $doc->getElementsByTagName('organization');
120 if (empty($xmlorganization->length
)) {
123 $organization = null;
124 foreach ($xmlorganization as $org) {
125 if (is_null($organization)) {
126 // use first if default nor found
127 $organization = $org;
129 if (!$org->attributes
->getNamedItem('identifier')) {
132 if ($default === $org->attributes
->getNamedItem('identifier')->nodeValue
) {
133 // found default - use it
134 $organization = $org;
139 // load all resources
140 $resources = array();
142 $xmlresources = $doc->getElementsByTagName('resource');
143 foreach ($xmlresources as $res) {
144 if (!$identifier = $res->attributes
->getNamedItem('identifier')) {
147 $identifier = $identifier->nodeValue
;
148 if ($xmlbase = $res->baseURI
) {
149 // undo the fake URL, we are interested in relative links only
150 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
151 $xmlbase = rtrim($xmlbase, '/').'/';
155 if (!$href = $res->attributes
->getNamedItem('href')) {
156 // If href not found look for <file href="help.htm"/>
157 $fileresources = $res->getElementsByTagName('file');
158 foreach ($fileresources as $file) {
159 $href = $file->getAttribute('href');
165 $href = $href->nodeValue
;
167 if (strpos($href, 'http://') !== 0) {
168 $href = $xmlbase.$href;
170 // href cleanup - Some packages are poorly done and use \ in urls
171 $href = ltrim(strtr($href, "\\", '/'), '/');
172 $resources[$identifier] = $href;
176 foreach ($organization->childNodes
as $child) {
177 if ($child->nodeName
=== 'item') {
178 if (!$item = imscp_recursive_item($child, 0, $resources)) {
188 function imscp_recursive_item($xmlitem, $level, $resources) {
190 if ($identifierref = $xmlitem->attributes
->getNamedItem('identifierref')) {
191 $identifierref = $identifierref->nodeValue
;
197 foreach ($xmlitem->childNodes
as $child) {
198 if ($child->nodeName
=== 'title') {
199 $title = $child->textContent
;
201 } else if ($child->nodeName
=== 'item') {
202 if ($subitem = imscp_recursive_item($child, $level+
1, $resources)) {
203 $subitems[] = $subitem;
208 return array('href' => isset($resources[$identifierref]) ?
$resources[$identifierref] : '',
211 'subitems' => $subitems,
216 * File browsing support class
218 class imscp_file_info
extends file_info
{
224 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
225 parent
::__construct($browser, $context);
226 $this->course
= $course;
228 $this->areas
= $areas;
229 $this->filearea
= $filearea;
233 * Returns list of standard virtual file/directory identification.
234 * The difference from stored_file parameters is that null values
235 * are allowed in all fields
236 * @return array with keys contextid, filearea, itemid, filepath and filename
238 public function get_params() {
239 return array('contextid'=>$this->context
->id
,
240 'component'=>'mod_imscp',
241 'filearea' =>$this->filearea
,
248 * Returns localised visible name.
251 public function get_visible_name() {
252 return $this->areas
[$this->filearea
];
256 * Can I add new files or directories?
259 public function is_writable() {
267 public function is_directory() {
272 * Returns list of children.
273 * @return array of file_info instances
275 public function get_children() {
279 $itemids = $DB->get_records('files', array('contextid'=>$this->context
->id
, 'component'=>'mod_imscp', 'filearea'=>$this->filearea
), 'itemid', "DISTINCT itemid");
280 foreach ($itemids as $itemid=>$unused) {
281 if ($child = $this->browser
->get_file_info($this->context
, 'mod_imscp', $this->filearea
, $itemid)) {
282 $children[] = $child;
289 * Returns parent file_info instance
290 * @return file_info or null for root
292 public function get_parent() {
293 return $this->browser
->get_file_info($this->context
);