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 = context_module::instance($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 if (preg_match('|^https?://|', $item['href'])) {
67 $context = context_module::instance($cm->id);
68 $urlbase = "$CFG->wwwroot/pluginfile.php";
69 $path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$item['href'];
70 $url = file_encode_url($urlbase, $path, false);
72 $result = "<li><a href=\"$url\">".$item['title'].'</a>';
73 if ($item['subitems']) {
75 foreach ($item['subitems'] as $subitem) {
76 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
86 * Parse an IMS content package's manifest file to determine its structure
87 * @param object $imscp
88 * @param object $context
91 function imscp_parse_structure($imscp, $context) {
92 $fs = get_file_storage();
94 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) {
98 return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context);
102 * Parse the contents of a IMS package's manifest file
103 * @param string $manifestfilecontents the contents of the manifest file
106 function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
107 $doc = new DOMDocument();
108 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
112 // we put this fake URL as base in order to detect path changes caused by xml:base attributes
113 $doc->documentURI = 'http://grrr/';
115 $xmlorganizations = $doc->getElementsByTagName('organizations');
116 if (empty($xmlorganizations->length)) {
120 if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) {
121 $default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue;
123 $xmlorganization = $doc->getElementsByTagName('organization');
124 if (empty($xmlorganization->length)) {
127 $organization = null;
128 foreach ($xmlorganization as $org) {
129 if (is_null($organization)) {
130 // use first if default nor found
131 $organization = $org;
133 if (!$org->attributes->getNamedItem('identifier')) {
136 if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) {
137 // found default - use it
138 $organization = $org;
143 // load all resources
144 $resources = array();
146 $xmlresources = $doc->getElementsByTagName('resource');
147 foreach ($xmlresources as $res) {
148 if (!$identifier = $res->attributes->getNamedItem('identifier')) {
151 $identifier = $identifier->nodeValue;
152 if ($xmlbase = $res->baseURI) {
153 // undo the fake URL, we are interested in relative links only
154 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
155 $xmlbase = rtrim($xmlbase, '/').'/';
159 if (!$href = $res->attributes->getNamedItem('href')) {
160 // If href not found look for <file href="help.htm"/>
161 $fileresources = $res->getElementsByTagName('file');
162 foreach ($fileresources as $file) {
163 $href = $file->getAttribute('href');
165 if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
166 $href = imscp_recursive_href($href, $imscp, $context);
172 $href = $href->nodeValue;
174 if (strpos($href, 'http://') !== 0) {
175 $href = $xmlbase.$href;
177 // href cleanup - Some packages are poorly done and use \ in urls
178 $href = ltrim(strtr($href, "\\", '/'), '/');
179 $resources[$identifier] = $href;
183 foreach ($organization->childNodes as $child) {
184 if ($child->nodeName === 'item') {
185 if (!$item = imscp_recursive_item($child, 0, $resources)) {
195 function imscp_recursive_href($manifestfilename, $imscp, $context) {
196 $fs = get_file_storage();
198 $dirname = dirname($manifestfilename);
199 $filename = basename($manifestfilename);
201 if ($dirname !== '/') {
202 $dirname = "/$dirname/";
205 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, $dirname, $filename)) {
208 $doc = new DOMDocument();
209 if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
212 $xmlresources = $doc->getElementsByTagName('resource');
213 foreach ($xmlresources as $res) {
214 if (!$href = $res->attributes->getNamedItem('href')) {
215 $fileresources = $res->getElementsByTagName('file');
216 foreach ($fileresources as $file) {
217 $href = $file->getAttribute('href');
218 if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
219 $href = imscp_recursive_href($href, $imscp, $context);
222 if (pathinfo($href, PATHINFO_EXTENSION) == 'htm' || pathinfo($href, PATHINFO_EXTENSION) == 'html') {
232 function imscp_recursive_item($xmlitem, $level, $resources) {
234 if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
235 $identifierref = $identifierref->nodeValue;
241 foreach ($xmlitem->childNodes as $child) {
242 if ($child->nodeName === 'title') {
243 $title = $child->textContent;
245 } else if ($child->nodeName === 'item') {
246 if ($subitem = imscp_recursive_item($child, $level+1, $resources)) {
247 $subitems[] = $subitem;
252 return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
255 'subitems' => $subitems,
260 * File browsing support class
262 class imscp_file_info extends file_info {
268 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
269 parent::__construct($browser, $context);
270 $this->course = $course;
272 $this->areas = $areas;
273 $this->filearea = $filearea;
277 * Returns list of standard virtual file/directory identification.
278 * The difference from stored_file parameters is that null values
279 * are allowed in all fields
280 * @return array with keys contextid, filearea, itemid, filepath and filename
282 public function get_params() {
283 return array('contextid'=>$this->context->id,
284 'component'=>'mod_imscp',
285 'filearea' =>$this->filearea,
292 * Returns localised visible name.
295 public function get_visible_name() {
296 return $this->areas[$this->filearea];
300 * Can I add new files or directories?
303 public function is_writable() {
311 public function is_directory() {
316 * Returns list of children.
317 * @return array of file_info instances
319 public function get_children() {
320 return $this->get_filtered_children('*', false, true);
324 * Help function to return files matching extensions or their count
326 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
327 * @param bool|int $countonly if false returns the children, if an int returns just the
328 * count of children but stops counting when $countonly number of children is reached
329 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
330 * @return array|int array of file_info instances or the count
332 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
334 $params = array('contextid' => $this->context->id,
335 'component' => 'mod_imscp',
336 'filearea' => $this->filearea);
337 $sql = 'SELECT DISTINCT itemid
339 WHERE contextid = :contextid
340 AND component = :component
341 AND filearea = :filearea';
342 if (!$returnemptyfolders) {
343 $sql .= ' AND filename <> :emptyfilename';
344 $params['emptyfilename'] = '.';
346 list($sql2, $params2) = $this->build_search_files_sql($extensions);
348 $params = array_merge($params, $params2);
349 if ($countonly !== false) {
350 $sql .= ' ORDER BY itemid';
353 $rs = $DB->get_recordset_sql($sql, $params);
355 foreach ($rs as $record) {
356 if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $record->itemid)) {
357 $children[] = $child;
358 if ($countonly !== false && count($children) >= $countonly) {
364 if ($countonly !== false) {
365 return count($children);
371 * Returns list of children which are either files matching the specified extensions
372 * or folders that contain at least one such file.
374 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
375 * @return array of file_info instances
377 public function get_non_empty_children($extensions = '*') {
378 return $this->get_filtered_children($extensions, false);
382 * Returns the number of children which are either files matching the specified extensions
383 * or folders containing at least one such file.
385 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
386 * @param int $limit stop counting after at least $limit non-empty children are found
389 public function count_non_empty_children($extensions = '*', $limit = 1) {
390 return $this->get_filtered_children($extensions, $limit);
394 * Returns parent file_info instance
395 * @return file_info or null for root
397 public function get_parent() {
398 return $this->browser->get_file_info($this->context);