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());
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) {
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');
169 $href = $href->nodeValue;
171 if (strpos($href, 'http://') !== 0) {
172 $href = $xmlbase.$href;
174 // href cleanup - Some packages are poorly done and use \ in urls
175 $href = ltrim(strtr($href, "\\", '/'), '/');
176 $resources[$identifier] = $href;
180 foreach ($organization->childNodes as $child) {
181 if ($child->nodeName === 'item') {
182 if (!$item = imscp_recursive_item($child, 0, $resources)) {
192 function imscp_recursive_item($xmlitem, $level, $resources) {
194 if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
195 $identifierref = $identifierref->nodeValue;
201 foreach ($xmlitem->childNodes as $child) {
202 if ($child->nodeName === 'title') {
203 $title = $child->textContent;
205 } else if ($child->nodeName === 'item') {
206 if ($subitem = imscp_recursive_item($child, $level+1, $resources)) {
207 $subitems[] = $subitem;
212 return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
215 'subitems' => $subitems,
220 * File browsing support class
222 class imscp_file_info extends file_info {
228 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
229 parent::__construct($browser, $context);
230 $this->course = $course;
232 $this->areas = $areas;
233 $this->filearea = $filearea;
237 * Returns list of standard virtual file/directory identification.
238 * The difference from stored_file parameters is that null values
239 * are allowed in all fields
240 * @return array with keys contextid, filearea, itemid, filepath and filename
242 public function get_params() {
243 return array('contextid'=>$this->context->id,
244 'component'=>'mod_imscp',
245 'filearea' =>$this->filearea,
252 * Returns localised visible name.
255 public function get_visible_name() {
256 return $this->areas[$this->filearea];
260 * Can I add new files or directories?
263 public function is_writable() {
271 public function is_directory() {
276 * Returns list of children.
277 * @return array of file_info instances
279 public function get_children() {
280 return $this->get_filtered_children('*', false, true);
284 * Help function to return files matching extensions or their count
286 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
287 * @param bool|int $countonly if false returns the children, if an int returns just the
288 * count of children but stops counting when $countonly number of children is reached
289 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
290 * @return array|int array of file_info instances or the count
292 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
294 $params = array('contextid' => $this->context->id,
295 'component' => 'mod_imscp',
296 'filearea' => $this->filearea);
297 $sql = 'SELECT DISTINCT itemid
299 WHERE contextid = :contextid
300 AND component = :component
301 AND filearea = :filearea';
302 if (!$returnemptyfolders) {
303 $sql .= ' AND filename <> :emptyfilename';
304 $params['emptyfilename'] = '.';
306 list($sql2, $params2) = $this->build_search_files_sql($extensions);
308 $params = array_merge($params, $params2);
309 if ($countonly !== false) {
310 $sql .= ' ORDER BY itemid';
313 $rs = $DB->get_recordset_sql($sql, $params);
315 foreach ($rs as $record) {
316 if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $record->itemid)) {
317 $children[] = $child;
318 if ($countonly !== false && count($children) >= $countonly) {
324 if ($countonly !== false) {
325 return count($children);
331 * Returns list of children which are either files matching the specified extensions
332 * or folders that contain at least one such file.
334 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
335 * @return array of file_info instances
337 public function get_non_empty_children($extensions = '*') {
338 return $this->get_filtered_children($extensions, false);
342 * Returns the number of children which are either files matching the specified extensions
343 * or folders containing at least one such file.
345 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
346 * @param int $limit stop counting after at least $limit non-empty children are found
349 public function count_non_empty_children($extensions = '*', $limit = 1) {
350 return $this->get_filtered_children($extensions, $limit);
354 * Returns parent file_info instance
355 * @return file_info or null for root
357 public function get_parent() {
358 return $this->browser->get_file_info($this->context);