[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 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. 14 // 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/>. 17 18 /** 19 * Private imscp module utility functions 20 * 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 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 require_once("$CFG->dirroot/mod/imscp/lib.php"); 29 require_once("$CFG->libdir/filelib.php"); 30 require_once("$CFG->libdir/resourcelib.php"); 31 32 function imscp_print_content($imscp, $cm, $course) { 33 global $PAGE, $CFG; 34 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); 41 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); 47 } 48 echo '</ul></div>'; 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>'; 50 echo '</div>'; 51 echo '</div>'; 52 53 $PAGE->requires->js_init_call('M.mod_imscp.init'); 54 return; 55 } 56 57 /** 58 * Internal function - creates htmls structure suitable for YUI tree. 59 */ 60 function imscp_htmllize_item($item, $imscp, $cm) { 61 global $CFG; 62 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); 70 } 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); 76 } 77 $result .= '</ul>'; 78 } 79 $result .= '</li>'; 80 81 return $result; 82 } 83 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 89 */ 90 function imscp_parse_structure($imscp, $context) { 91 $fs = get_file_storage(); 92 93 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) { 94 return null; 95 } 96 97 return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context); 98 } 99 100 /** 101 * Parse the contents of a IMS package's manifest file 102 * @param string $manifestfilecontents the contents of the manifest file 103 * @return array 104 */ 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; 110 } 111 libxml_disable_entity_loader($oldentities); 112 113 // we put this fake URL as base in order to detect path changes caused by xml:base attributes 114 $doc->documentURI = 'http://grrr/'; 115 116 $xmlorganizations = $doc->getElementsByTagName('organizations'); 117 if (empty($xmlorganizations->length)) { 118 return null; 119 } 120 $default = null; 121 if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) { 122 $default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue; 123 } 124 $xmlorganization = $doc->getElementsByTagName('organization'); 125 if (empty($xmlorganization->length)) { 126 return null; 127 } 128 $organization = null; 129 foreach ($xmlorganization as $org) { 130 if (is_null($organization)) { 131 // use first if default nor found 132 $organization = $org; 133 } 134 if (!$org->attributes->getNamedItem('identifier')) { 135 continue; 136 } 137 if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) { 138 // found default - use it 139 $organization = $org; 140 break; 141 } 142 } 143 144 // load all resources 145 $resources = array(); 146 147 $xmlresources = $doc->getElementsByTagName('resource'); 148 foreach ($xmlresources as $res) { 149 if (!$identifier = $res->attributes->getNamedItem('identifier')) { 150 continue; 151 } 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 = ''; 159 } 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'); 165 } 166 if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') { 167 $href = imscp_recursive_href($href, $imscp, $context); 168 } 169 if (empty($href)) { 170 continue; 171 } 172 } else { 173 $href = $href->nodeValue; 174 } 175 if (strpos($href, 'http://') !== 0) { 176 $href = $xmlbase.$href; 177 } 178 // href cleanup - Some packages are poorly done and use \ in urls 179 $href = ltrim(strtr($href, "\\", '/'), '/'); 180 $resources[$identifier] = $href; 181 } 182 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; 188 } 189 $items[] = $item; 190 } 191 } 192 193 return $items; 194 } 195 196 function imscp_recursive_href($manifestfilename, $imscp, $context) { 197 $fs = get_file_storage(); 198 199 $dirname = dirname($manifestfilename); 200 $filename = basename($manifestfilename); 201 202 if ($dirname !== '/') { 203 $dirname = "/$dirname/"; 204 } 205 206 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, $dirname, $filename)) { 207 return null; 208 } 209 210 $doc = new DOMDocument(); 211 $oldentities = libxml_disable_entity_loader(true); 212 if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) { 213 return null; 214 } 215 libxml_disable_entity_loader($oldentities); 216 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); 225 } 226 227 if (pathinfo($href, PATHINFO_EXTENSION) == 'htm' || pathinfo($href, PATHINFO_EXTENSION) == 'html') { 228 return $href; 229 } 230 } 231 } 232 } 233 234 return $href; 235 } 236 237 function imscp_recursive_item($xmlitem, $level, $resources) { 238 $identifierref = ''; 239 if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) { 240 $identifierref = $identifierref->nodeValue; 241 } 242 243 $title = '?'; 244 $subitems = array(); 245 246 foreach ($xmlitem->childNodes as $child) { 247 if ($child->nodeName === 'title') { 248 $title = $child->textContent; 249 250 } else if ($child->nodeName === 'item') { 251 if ($subitem = imscp_recursive_item($child, $level+1, $resources)) { 252 $subitems[] = $subitem; 253 } 254 } 255 } 256 257 return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '', 258 'title' => $title, 259 'level' => $level, 260 'subitems' => $subitems, 261 ); 262 } 263 264 /** 265 * File browsing support class 266 */ 267 class imscp_file_info extends file_info { 268 protected $course; 269 protected $cm; 270 protected $areas; 271 protected $filearea; 272 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; 279 } 280 281 /** 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 286 */ 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); 294 } 295 296 /** 297 * Returns localised visible name. 298 * @return string 299 */ 300 public function get_visible_name() { 301 return $this->areas[$this->filearea]; 302 } 303 304 /** 305 * Can I add new files or directories? 306 * @return bool 307 */ 308 public function is_writable() { 309 return false; 310 } 311 312 /** 313 * Is directory? 314 * @return bool 315 */ 316 public function is_directory() { 317 return true; 318 } 319 320 /** 321 * Returns list of children. 322 * @return array of file_info instances 323 */ 324 public function get_children() { 325 return $this->get_filtered_children('*', false, true); 326 } 327 328 /** 329 * Help function to return files matching extensions or their count 330 * 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 336 */ 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'] = '.'; 350 } 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'; 356 } 357 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; 365 } 366 } 367 } 368 $rs->close(); 369 if ($countonly !== false) { 370 return count($children); 371 } 372 return $children; 373 } 374 375 /** 376 * Returns list of children which are either files matching the specified extensions 377 * or folders that contain at least one such file. 378 * 379 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg') 380 * @return array of file_info instances 381 */ 382 public function get_non_empty_children($extensions = '*') { 383 return $this->get_filtered_children($extensions, false); 384 } 385 386 /** 387 * Returns the number of children which are either files matching the specified extensions 388 * or folders containing at least one such file. 389 * 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 393 */ 394 public function count_non_empty_children($extensions = '*', $limit = 1) { 395 return $this->get_filtered_children($extensions, $limit); 396 } 397 398 /** 399 * Returns parent file_info instance 400 * @return file_info or null for root 401 */ 402 public function get_parent() { 403 return $this->browser->get_file_info($this->context); 404 } 405 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |