2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Private imscp module utility functions
21 * @copyright 2009 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once("$CFG->dirroot/mod/imscp/lib.php");
28 require_once("$CFG->libdir/filelib.php");
29 require_once("$CFG->libdir/resourcelib.php");
32 * Print IMSCP content to page.
34 * @param stdClass $imscp module instance.
35 * @param stdClass $cm course module.
36 * @param stdClass $course record.
38 function imscp_print_content($imscp, $cm, $course) {
41 $items = unserialize($imscp->structure
);
42 $first = reset($items);
43 $context = context_module
::instance($cm->id
);
44 $urlbase = "$CFG->wwwroot/pluginfile.php";
45 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$first['href'];
46 $firsturl = file_encode_url($urlbase, $path, false);
48 echo '<div id="imscp_layout">';
49 echo '<div id="imscp_toc">';
50 echo '<div id="imscp_tree"><ul>';
51 foreach ($items as $item) {
52 echo imscp_htmllize_item($item, $imscp, $cm);
55 echo '<div id="imscp_nav" style="display:none">';
56 echo '<button id="nav_skipprev"><<</button><button id="nav_prev"><</button><button id="nav_up">^</button>';
57 echo '<button id="nav_next">></button><button id="nav_skipnext">>></button>';
62 $PAGE->requires
->js_init_call('M.mod_imscp.init');
67 * Internal function - creates htmls structure suitable for YUI tree.
69 function imscp_htmllize_item($item, $imscp, $cm) {
73 if (preg_match('|^https?://|', $item['href'])) {
76 $context = context_module
::instance($cm->id
);
77 $urlbase = "$CFG->wwwroot/pluginfile.php";
78 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$item['href'];
79 $url = file_encode_url($urlbase, $path, false);
81 $result = "<li><a href=\"$url\">".$item['title'].'</a>';
83 $result = '<li>'.$item['title'];
85 if ($item['subitems']) {
87 foreach ($item['subitems'] as $subitem) {
88 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
98 * Parse an IMS content package's manifest file to determine its structure
99 * @param object $imscp
100 * @param object $context
103 function imscp_parse_structure($imscp, $context) {
104 $fs = get_file_storage();
106 if (!$manifestfile = $fs->get_file($context->id
, 'mod_imscp', 'content', $imscp->revision
, '/', 'imsmanifest.xml')) {
110 return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context);
114 * Parse the contents of a IMS package's manifest file.
115 * @param string $manifestfilecontents the contents of the manifest file
118 function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
119 $doc = new DOMDocument();
120 $oldentities = libxml_disable_entity_loader(true);
121 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET
)) {
124 libxml_disable_entity_loader($oldentities);
126 // We put this fake URL as base in order to detect path changes caused by xml:base attributes.
127 $doc->documentURI
= 'http://grrr/';
129 $xmlorganizations = $doc->getElementsByTagName('organizations');
130 if (empty($xmlorganizations->length
)) {
134 if ($xmlorganizations->item(0)->attributes
->getNamedItem('default')) {
135 $default = $xmlorganizations->item(0)->attributes
->getNamedItem('default')->nodeValue
;
137 $xmlorganization = $doc->getElementsByTagName('organization');
138 if (empty($xmlorganization->length
)) {
141 $organization = null;
142 foreach ($xmlorganization as $org) {
143 if (is_null($organization)) {
144 // Use first if default nor found.
145 $organization = $org;
147 if (!$org->attributes
->getNamedItem('identifier')) {
150 if ($default === $org->attributes
->getNamedItem('identifier')->nodeValue
) {
151 // Found default - use it.
152 $organization = $org;
157 // Load all resources.
158 $resources = array();
160 $xmlresources = $doc->getElementsByTagName('resource');
161 foreach ($xmlresources as $res) {
162 if (!$identifier = $res->attributes
->getNamedItem('identifier')) {
165 $identifier = $identifier->nodeValue
;
166 if ($xmlbase = $res->baseURI
) {
167 // Undo the fake URL, we are interested in relative links only.
168 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
169 $xmlbase = rtrim($xmlbase, '/').'/';
173 if (!$href = $res->attributes
->getNamedItem('href')) {
174 // If href not found look for <file href="help.htm"/>.
175 $fileresources = $res->getElementsByTagName('file');
176 foreach ($fileresources as $file) {
177 $href = $file->getAttribute('href');
179 if (pathinfo($href, PATHINFO_EXTENSION
) == 'xml') {
180 $href = imscp_recursive_href($href, $imscp, $context);
186 $href = $href->nodeValue
;
188 if (strpos($href, 'http://') !== 0) {
189 $href = $xmlbase.$href;
191 // Item href cleanup - Some packages are poorly done and use \ in urls.
192 $href = ltrim(strtr($href, "\\", '/'), '/');
193 $resources[$identifier] = $href;
197 foreach ($organization->childNodes
as $child) {
198 if ($child->nodeName
=== 'item') {
199 if (!$item = imscp_recursive_item($child, 0, $resources)) {
209 function imscp_recursive_href($manifestfilename, $imscp, $context) {
210 $fs = get_file_storage();
212 $dirname = dirname($manifestfilename);
213 $filename = basename($manifestfilename);
215 if ($dirname !== '/') {
216 $dirname = "/$dirname/";
219 if (!$manifestfile = $fs->get_file($context->id
, 'mod_imscp', 'content', $imscp->revision
, $dirname, $filename)) {
223 $doc = new DOMDocument();
224 $oldentities = libxml_disable_entity_loader(true);
225 if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET
)) {
228 libxml_disable_entity_loader($oldentities);
230 $xmlresources = $doc->getElementsByTagName('resource');
231 foreach ($xmlresources as $res) {
232 if (!$href = $res->attributes
->getNamedItem('href')) {
233 $fileresources = $res->getElementsByTagName('file');
234 foreach ($fileresources as $file) {
235 $href = $file->getAttribute('href');
236 if (pathinfo($href, PATHINFO_EXTENSION
) == 'xml') {
237 $href = imscp_recursive_href($href, $imscp, $context);
240 if (pathinfo($href, PATHINFO_EXTENSION
) == 'htm' ||
pathinfo($href, PATHINFO_EXTENSION
) == 'html') {
250 function imscp_recursive_item($xmlitem, $level, $resources) {
252 if ($identifierref = $xmlitem->attributes
->getNamedItem('identifierref')) {
253 $identifierref = $identifierref->nodeValue
;
259 foreach ($xmlitem->childNodes
as $child) {
260 if ($child->nodeName
=== 'title') {
261 $title = $child->textContent
;
263 } else if ($child->nodeName
=== 'item') {
264 if ($subitem = imscp_recursive_item($child, $level +
1, $resources)) {
265 $subitems[] = $subitem;
270 return array('href' => isset($resources[$identifierref]) ?
$resources[$identifierref] : '',
273 'subitems' => $subitems,
278 * File browsing support class
280 * @copyright 2009 Petr Skoda {@link http://skodak.org}
281 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
283 class imscp_file_info
extends file_info
{
289 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
290 parent
::__construct($browser, $context);
291 $this->course
= $course;
293 $this->areas
= $areas;
294 $this->filearea
= $filearea;
298 * Returns list of standard virtual file/directory identification.
299 * The difference from stored_file parameters is that null values
300 * are allowed in all fields
301 * @return array with keys contextid, filearea, itemid, filepath and filename
303 public function get_params() {
304 return array('contextid' => $this->context
->id
,
305 'component' => 'mod_imscp',
306 'filearea' => $this->filearea
,
313 * Returns localised visible name.
316 public function get_visible_name() {
317 return $this->areas
[$this->filearea
];
321 * Can I add new files or directories?
324 public function is_writable() {
332 public function is_directory() {
337 * Returns list of children.
338 * @return array of file_info instances
340 public function get_children() {
341 return $this->get_filtered_children('*', false, true);
345 * Help function to return files matching extensions or their count
347 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
348 * @param bool|int $countonly if false returns the children, if an int returns just the
349 * count of children but stops counting when $countonly number of children is reached
350 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
351 * @return array|int array of file_info instances or the count
353 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
355 $params = array('contextid' => $this->context
->id
,
356 'component' => 'mod_imscp',
357 'filearea' => $this->filearea
);
358 $sql = 'SELECT DISTINCT itemid
360 WHERE contextid = :contextid
361 AND component = :component
362 AND filearea = :filearea';
363 if (!$returnemptyfolders) {
364 $sql .= ' AND filename <> :emptyfilename';
365 $params['emptyfilename'] = '.';
367 list($sql2, $params2) = $this->build_search_files_sql($extensions);
369 $params = array_merge($params, $params2);
370 if ($countonly !== false) {
371 $sql .= ' ORDER BY itemid';
374 $rs = $DB->get_recordset_sql($sql, $params);
376 foreach ($rs as $record) {
377 if ($child = $this->browser
->get_file_info($this->context
, 'mod_imscp', $this->filearea
, $record->itemid
)) {
378 $children[] = $child;
379 if ($countonly !== false && count($children) >= $countonly) {
385 if ($countonly !== false) {
386 return count($children);
392 * Returns list of children which are either files matching the specified extensions
393 * or folders that contain at least one such file.
395 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
396 * @return array of file_info instances
398 public function get_non_empty_children($extensions = '*') {
399 return $this->get_filtered_children($extensions, false);
403 * Returns the number of children which are either files matching the specified extensions
404 * or folders containing at least one such file.
406 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
407 * @param int $limit stop counting after at least $limit non-empty children are found
410 public function count_non_empty_children($extensions = '*', $limit = 1) {
411 return $this->get_filtered_children($extensions, $limit);
415 * Returns parent file_info instance
416 * @return file_info or null for root
418 public function get_parent() {
419 return $this->browser
->get_file_info($this->context
);