MDL-46534 course: Remove calls to error_log in activity duplication
[moodle.git] / mod / imscp / locallib.php
blob3ebd1060af0f6dec221359a33f32a4c4e7ec6add
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Private imscp module utility functions
21 * @package mod_imscp
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) {
33 global $PAGE, $CFG;
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);
48 echo '</ul></div>';
49 echo '<div id="imscp_nav" style="display:none"><button id="nav_skipprev">&lt;&lt;</button><button id="nav_prev">&lt;</button><button id="nav_up">^</button><button id="nav_next">&gt;</button><button id="nav_skipnext">&gt;&gt;</button></div>';
50 echo '</div>';
51 echo '</div>';
53 $PAGE->requires->js_init_call('M.mod_imscp.init');
54 return;
57 /**
58 * Internal function - creates htmls structure suitable for YUI tree.
60 function imscp_htmllize_item($item, $imscp, $cm) {
61 global $CFG;
63 if (preg_match('|^https?://|', $item['href'])) {
64 $url = $item['href'];
65 } else {
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']) {
73 $result .= '<ul>';
74 foreach ($item['subitems'] as $subitem) {
75 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
77 $result .= '</ul>';
79 $result .= '</li>';
81 return $result;
84 /**
85 * Parse an IMS content package's manifest file to determine its structure
86 * @param object $imscp
87 * @param object $context
88 * @return array
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')) {
94 return null;
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
103 * @return array
105 function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
106 $doc = new DOMDocument();
107 $oldentities = libxml_disable_entity_loader(true);
108 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
109 return null;
111 libxml_disable_entity_loader($oldentities);
113 // we put this fake URL as base in order to detect path changes caused by xml:base attributes
114 $doc->documentURI = 'http://grrr/';
116 $xmlorganizations = $doc->getElementsByTagName('organizations');
117 if (empty($xmlorganizations->length)) {
118 return null;
120 $default = null;
121 if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) {
122 $default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue;
124 $xmlorganization = $doc->getElementsByTagName('organization');
125 if (empty($xmlorganization->length)) {
126 return null;
128 $organization = null;
129 foreach ($xmlorganization as $org) {
130 if (is_null($organization)) {
131 // use first if default nor found
132 $organization = $org;
134 if (!$org->attributes->getNamedItem('identifier')) {
135 continue;
137 if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) {
138 // found default - use it
139 $organization = $org;
140 break;
144 // load all resources
145 $resources = array();
147 $xmlresources = $doc->getElementsByTagName('resource');
148 foreach ($xmlresources as $res) {
149 if (!$identifier = $res->attributes->getNamedItem('identifier')) {
150 continue;
152 $identifier = $identifier->nodeValue;
153 if ($xmlbase = $res->baseURI) {
154 // undo the fake URL, we are interested in relative links only
155 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
156 $xmlbase = rtrim($xmlbase, '/').'/';
157 } else {
158 $xmlbase = '';
160 if (!$href = $res->attributes->getNamedItem('href')) {
161 // If href not found look for <file href="help.htm"/>
162 $fileresources = $res->getElementsByTagName('file');
163 foreach ($fileresources as $file) {
164 $href = $file->getAttribute('href');
166 if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
167 $href = imscp_recursive_href($href, $imscp, $context);
169 if (empty($href)) {
170 continue;
172 } else {
173 $href = $href->nodeValue;
175 if (strpos($href, 'http://') !== 0) {
176 $href = $xmlbase.$href;
178 // href cleanup - Some packages are poorly done and use \ in urls
179 $href = ltrim(strtr($href, "\\", '/'), '/');
180 $resources[$identifier] = $href;
183 $items = array();
184 foreach ($organization->childNodes as $child) {
185 if ($child->nodeName === 'item') {
186 if (!$item = imscp_recursive_item($child, 0, $resources)) {
187 continue;
189 $items[] = $item;
193 return $items;
196 function imscp_recursive_href($manifestfilename, $imscp, $context) {
197 $fs = get_file_storage();
199 $dirname = dirname($manifestfilename);
200 $filename = basename($manifestfilename);
202 if ($dirname !== '/') {
203 $dirname = "/$dirname/";
206 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, $dirname, $filename)) {
207 return null;
210 $doc = new DOMDocument();
211 $oldentities = libxml_disable_entity_loader(true);
212 if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
213 return null;
215 libxml_disable_entity_loader($oldentities);
217 $xmlresources = $doc->getElementsByTagName('resource');
218 foreach ($xmlresources as $res) {
219 if (!$href = $res->attributes->getNamedItem('href')) {
220 $fileresources = $res->getElementsByTagName('file');
221 foreach ($fileresources as $file) {
222 $href = $file->getAttribute('href');
223 if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
224 $href = imscp_recursive_href($href, $imscp, $context);
227 if (pathinfo($href, PATHINFO_EXTENSION) == 'htm' || pathinfo($href, PATHINFO_EXTENSION) == 'html') {
228 return $href;
234 return $href;
237 function imscp_recursive_item($xmlitem, $level, $resources) {
238 $identifierref = '';
239 if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
240 $identifierref = $identifierref->nodeValue;
243 $title = '?';
244 $subitems = array();
246 foreach ($xmlitem->childNodes as $child) {
247 if ($child->nodeName === 'title') {
248 $title = $child->textContent;
250 } else if ($child->nodeName === 'item') {
251 if ($subitem = imscp_recursive_item($child, $level+1, $resources)) {
252 $subitems[] = $subitem;
257 return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
258 'title' => $title,
259 'level' => $level,
260 'subitems' => $subitems,
265 * File browsing support class
267 class imscp_file_info extends file_info {
268 protected $course;
269 protected $cm;
270 protected $areas;
271 protected $filearea;
273 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
274 parent::__construct($browser, $context);
275 $this->course = $course;
276 $this->cm = $cm;
277 $this->areas = $areas;
278 $this->filearea = $filearea;
282 * Returns list of standard virtual file/directory identification.
283 * The difference from stored_file parameters is that null values
284 * are allowed in all fields
285 * @return array with keys contextid, filearea, itemid, filepath and filename
287 public function get_params() {
288 return array('contextid'=>$this->context->id,
289 'component'=>'mod_imscp',
290 'filearea' =>$this->filearea,
291 'itemid' =>null,
292 'filepath' =>null,
293 'filename' =>null);
297 * Returns localised visible name.
298 * @return string
300 public function get_visible_name() {
301 return $this->areas[$this->filearea];
305 * Can I add new files or directories?
306 * @return bool
308 public function is_writable() {
309 return false;
313 * Is directory?
314 * @return bool
316 public function is_directory() {
317 return true;
321 * Returns list of children.
322 * @return array of file_info instances
324 public function get_children() {
325 return $this->get_filtered_children('*', false, true);
329 * Help function to return files matching extensions or their count
331 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
332 * @param bool|int $countonly if false returns the children, if an int returns just the
333 * count of children but stops counting when $countonly number of children is reached
334 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
335 * @return array|int array of file_info instances or the count
337 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
338 global $DB;
339 $params = array('contextid' => $this->context->id,
340 'component' => 'mod_imscp',
341 'filearea' => $this->filearea);
342 $sql = 'SELECT DISTINCT itemid
343 FROM {files}
344 WHERE contextid = :contextid
345 AND component = :component
346 AND filearea = :filearea';
347 if (!$returnemptyfolders) {
348 $sql .= ' AND filename <> :emptyfilename';
349 $params['emptyfilename'] = '.';
351 list($sql2, $params2) = $this->build_search_files_sql($extensions);
352 $sql .= ' '.$sql2;
353 $params = array_merge($params, $params2);
354 if ($countonly !== false) {
355 $sql .= ' ORDER BY itemid';
358 $rs = $DB->get_recordset_sql($sql, $params);
359 $children = array();
360 foreach ($rs as $record) {
361 if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $record->itemid)) {
362 $children[] = $child;
363 if ($countonly !== false && count($children) >= $countonly) {
364 break;
368 $rs->close();
369 if ($countonly !== false) {
370 return count($children);
372 return $children;
376 * Returns list of children which are either files matching the specified extensions
377 * or folders that contain at least one such file.
379 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
380 * @return array of file_info instances
382 public function get_non_empty_children($extensions = '*') {
383 return $this->get_filtered_children($extensions, false);
387 * Returns the number of children which are either files matching the specified extensions
388 * or folders containing at least one such file.
390 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
391 * @param int $limit stop counting after at least $limit non-empty children are found
392 * @return int
394 public function count_non_empty_children($extensions = '*', $limit = 1) {
395 return $this->get_filtered_children($extensions, $limit);
399 * Returns parent file_info instance
400 * @return file_info or null for root
402 public function get_parent() {
403 return $this->browser->get_file_info($this->context);