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
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once("$CFG->dirroot/mod/imscp/lib.php");
29 require_once("$CFG->libdir/filelib.php");
30 require_once("$CFG->libdir/resourcelib.php");
32 function imscp_print_content($imscp, $cm, $course) {
35 $items = unserialize($imscp->structure
);
36 $first = reset($items);
37 $context = context_module
::instance($cm->id
);
38 $urlbase = "$CFG->wwwroot/pluginfile.php";
39 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$first['href'];
40 $firsturl = file_encode_url($urlbase, $path, false);
42 echo '<div id="imscp_layout">';
43 echo '<div id="imscp_toc">';
44 echo '<div id="imscp_tree"><ul>';
45 foreach ($items as $item) {
46 echo imscp_htmllize_item($item, $imscp, $cm);
49 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>';
53 $PAGE->requires
->js_init_call('M.mod_imscp.init');
58 * Internal function - creates htmls structure suitable for YUI tree.
60 function imscp_htmllize_item($item, $imscp, $cm) {
63 if (preg_match('|^https?://|', $item['href'])) {
66 $context = context_module
::instance($cm->id
);
67 $urlbase = "$CFG->wwwroot/pluginfile.php";
68 $path = '/'.$context->id
.'/mod_imscp/content/'.$imscp->revision
.'/'.$item['href'];
69 $url = file_encode_url($urlbase, $path, false);
71 $result = "<li><a href=\"$url\">".$item['title'].'</a>';
72 if ($item['subitems']) {
74 foreach ($item['subitems'] as $subitem) {
75 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
85 * Parse an IMS content package's manifest file to determine its structure
86 * @param object $imscp
87 * @param object $context
90 function imscp_parse_structure($imscp, $context) {
91 $fs = get_file_storage();
93 if (!$manifestfile = $fs->get_file($context->id
, 'mod_imscp', 'content', $imscp->revision
, '/', 'imsmanifest.xml')) {
97 return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context);
101 * Parse the contents of a IMS package's manifest file
102 * @param string $manifestfilecontents the contents of the manifest file
105 function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
106 $doc = new DOMDocument();
107 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET
)) {
111 // we put this fake URL as base in order to detect path changes caused by xml:base attributes
112 $doc->documentURI
= 'http://grrr/';
114 $xmlorganizations = $doc->getElementsByTagName('organizations');
115 if (empty($xmlorganizations->length
)) {
119 if ($xmlorganizations->item(0)->attributes
->getNamedItem('default')) {
120 $default = $xmlorganizations->item(0)->attributes
->getNamedItem('default')->nodeValue
;
122 $xmlorganization = $doc->getElementsByTagName('organization');
123 if (empty($xmlorganization->length
)) {
126 $organization = null;
127 foreach ($xmlorganization as $org) {
128 if (is_null($organization)) {
129 // use first if default nor found
130 $organization = $org;
132 if (!$org->attributes
->getNamedItem('identifier')) {
135 if ($default === $org->attributes
->getNamedItem('identifier')->nodeValue
) {
136 // found default - use it
137 $organization = $org;
142 // load all resources
143 $resources = array();
145 $xmlresources = $doc->getElementsByTagName('resource');
146 foreach ($xmlresources as $res) {
147 if (!$identifier = $res->attributes
->getNamedItem('identifier')) {
150 $identifier = $identifier->nodeValue
;
151 if ($xmlbase = $res->baseURI
) {
152 // undo the fake URL, we are interested in relative links only
153 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
154 $xmlbase = rtrim($xmlbase, '/').'/';
158 if (!$href = $res->attributes
->getNamedItem('href')) {
159 // If href not found look for <file href="help.htm"/>
160 $fileresources = $res->getElementsByTagName('file');
161 foreach ($fileresources as $file) {
162 $href = $file->getAttribute('href');
164 if (pathinfo($href, PATHINFO_EXTENSION
) == 'xml') {
165 $href = imscp_recursive_href($href, $imscp, $context);
171 $href = $href->nodeValue
;
173 if (strpos($href, 'http://') !== 0) {
174 $href = $xmlbase.$href;
176 // href cleanup - Some packages are poorly done and use \ in urls
177 $href = ltrim(strtr($href, "\\", '/'), '/');
178 $resources[$identifier] = $href;
182 foreach ($organization->childNodes
as $child) {
183 if ($child->nodeName
=== 'item') {
184 if (!$item = imscp_recursive_item($child, 0, $resources)) {
194 function imscp_recursive_href($manifestfilename, $imscp, $context) {
195 $fs = get_file_storage();
197 $dirname = dirname($manifestfilename);
198 $filename = basename($manifestfilename);
200 if ($dirname !== '/') {
201 $dirname = "/$dirname/";
204 if (!$manifestfile = $fs->get_file($context->id
, 'mod_imscp', 'content', $imscp->revision
, $dirname, $filename)) {
207 $doc = new DOMDocument();
208 if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET
)) {
211 $xmlresources = $doc->getElementsByTagName('resource');
212 foreach ($xmlresources as $res) {
213 if (!$href = $res->attributes
->getNamedItem('href')) {
214 $fileresources = $res->getElementsByTagName('file');
215 foreach ($fileresources as $file) {
216 $href = $file->getAttribute('href');
217 if (pathinfo($href, PATHINFO_EXTENSION
) == 'xml') {
218 $href = imscp_recursive_href($href, $imscp, $context);
221 if (pathinfo($href, PATHINFO_EXTENSION
) == 'htm' ||
pathinfo($href, PATHINFO_EXTENSION
) == 'html') {
231 function imscp_recursive_item($xmlitem, $level, $resources) {
233 if ($identifierref = $xmlitem->attributes
->getNamedItem('identifierref')) {
234 $identifierref = $identifierref->nodeValue
;
240 foreach ($xmlitem->childNodes
as $child) {
241 if ($child->nodeName
=== 'title') {
242 $title = $child->textContent
;
244 } else if ($child->nodeName
=== 'item') {
245 if ($subitem = imscp_recursive_item($child, $level+
1, $resources)) {
246 $subitems[] = $subitem;
251 return array('href' => isset($resources[$identifierref]) ?
$resources[$identifierref] : '',
254 'subitems' => $subitems,
259 * File browsing support class
261 class imscp_file_info
extends file_info
{
267 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
268 parent
::__construct($browser, $context);
269 $this->course
= $course;
271 $this->areas
= $areas;
272 $this->filearea
= $filearea;
276 * Returns list of standard virtual file/directory identification.
277 * The difference from stored_file parameters is that null values
278 * are allowed in all fields
279 * @return array with keys contextid, filearea, itemid, filepath and filename
281 public function get_params() {
282 return array('contextid'=>$this->context
->id
,
283 'component'=>'mod_imscp',
284 'filearea' =>$this->filearea
,
291 * Returns localised visible name.
294 public function get_visible_name() {
295 return $this->areas
[$this->filearea
];
299 * Can I add new files or directories?
302 public function is_writable() {
310 public function is_directory() {
315 * Returns list of children.
316 * @return array of file_info instances
318 public function get_children() {
319 return $this->get_filtered_children('*', false, true);
323 * Help function to return files matching extensions or their count
325 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
326 * @param bool|int $countonly if false returns the children, if an int returns just the
327 * count of children but stops counting when $countonly number of children is reached
328 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
329 * @return array|int array of file_info instances or the count
331 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
333 $params = array('contextid' => $this->context
->id
,
334 'component' => 'mod_imscp',
335 'filearea' => $this->filearea
);
336 $sql = 'SELECT DISTINCT itemid
338 WHERE contextid = :contextid
339 AND component = :component
340 AND filearea = :filearea';
341 if (!$returnemptyfolders) {
342 $sql .= ' AND filename <> :emptyfilename';
343 $params['emptyfilename'] = '.';
345 list($sql2, $params2) = $this->build_search_files_sql($extensions);
347 $params = array_merge($params, $params2);
348 if ($countonly !== false) {
349 $sql .= ' ORDER BY itemid';
352 $rs = $DB->get_recordset_sql($sql, $params);
354 foreach ($rs as $record) {
355 if ($child = $this->browser
->get_file_info($this->context
, 'mod_imscp', $this->filearea
, $record->itemid
)) {
356 $children[] = $child;
357 if ($countonly !== false && count($children) >= $countonly) {
363 if ($countonly !== false) {
364 return count($children);
370 * Returns list of children which are either files matching the specified extensions
371 * or folders that contain at least one such file.
373 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
374 * @return array of file_info instances
376 public function get_non_empty_children($extensions = '*') {
377 return $this->get_filtered_children($extensions, false);
381 * Returns the number of children which are either files matching the specified extensions
382 * or folders containing at least one such file.
384 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
385 * @param int $limit stop counting after at least $limit non-empty children are found
388 public function count_non_empty_children($extensions = '*', $limit = 1) {
389 return $this->get_filtered_children($extensions, $limit);
393 * Returns parent file_info instance
394 * @return file_info or null for root
396 public function get_parent() {
397 return $this->browser
->get_file_info($this->context
);