MDL-32572 fix notice when changing internal auth_db passwords
[moodle.git] / mod / imscp / locallib.php
blob6eb6ebb9650300f89b7bfbb6b6d04f0926138f70
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
22 * @subpackage imscp
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) {
34 global $PAGE, $CFG;
36 $items = unserialize($imscp->structure);
37 $first = reset($items);
38 $context = get_context_instance(CONTEXT_MODULE, $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);
49 echo '</ul></div>';
50 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>';
51 echo '</div>';
52 echo '</div>';
54 $PAGE->requires->js_init_call('M.mod_imscp.init');
55 return;
58 /**
59 * Internal function - creates htmls structure suitable for YUI tree.
61 function imscp_htmllize_item($item, $imscp, $cm) {
62 global $CFG;
64 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
65 $urlbase = "$CFG->wwwroot/pluginfile.php";
66 $path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$item['href'];
67 $url = file_encode_url($urlbase, $path, false);
68 $result = "<li><a href=\"$url\">".$item['title'].'</a>';
69 if ($item['subitems']) {
70 $result .= '<ul>';
71 foreach ($item['subitems'] as $subitem) {
72 $result .= imscp_htmllize_item($subitem, $imscp, $cm);
74 $result .= '</ul>';
76 $result .= '</li>';
78 return $result;
81 /**
82 * Parse an IMS content package's manifest file to determine its structure
83 * @param object $imscp
84 * @param object $context
85 * @return array
87 function imscp_parse_structure($imscp, $context) {
88 $fs = get_file_storage();
90 if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) {
91 return null;
94 return imscp_parse_manifestfile($manifestfile->get_content());
97 /**
98 * Parse the contents of a IMS package's manifest file
99 * @param string $manifestfilecontents the contents of the manifest file
100 * @return array
102 function imscp_parse_manifestfile($manifestfilecontents) {
103 $doc = new DOMDocument();
104 if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
105 return null;
108 // we put this fake URL as base in order to detect path changes caused by xml:base attributes
109 $doc->documentURI = 'http://grrr/';
111 $xmlorganizations = $doc->getElementsByTagName('organizations');
112 if (empty($xmlorganizations->length)) {
113 return null;
115 $default = null;
116 if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) {
117 $default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue;
119 $xmlorganization = $doc->getElementsByTagName('organization');
120 if (empty($xmlorganization->length)) {
121 return null;
123 $organization = null;
124 foreach ($xmlorganization as $org) {
125 if (is_null($organization)) {
126 // use first if default nor found
127 $organization = $org;
129 if (!$org->attributes->getNamedItem('identifier')) {
130 continue;
132 if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) {
133 // found default - use it
134 $organization = $org;
135 break;
139 // load all resources
140 $resources = array();
142 $xmlresources = $doc->getElementsByTagName('resource');
143 foreach ($xmlresources as $res) {
144 if (!$identifier = $res->attributes->getNamedItem('identifier')) {
145 continue;
147 $identifier = $identifier->nodeValue;
148 if ($xmlbase = $res->baseURI) {
149 // undo the fake URL, we are interested in relative links only
150 $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
151 $xmlbase = rtrim($xmlbase, '/').'/';
152 } else {
153 $xmlbase = '';
155 if (!$href = $res->attributes->getNamedItem('href')) {
156 // If href not found look for <file href="help.htm"/>
157 $fileresources = $res->getElementsByTagName('file');
158 foreach ($fileresources as $file) {
159 $href = $file->getAttribute('href');
161 if (empty($href)) {
162 continue;
164 } else {
165 $href = $href->nodeValue;
167 if (strpos($href, 'http://') !== 0) {
168 $href = $xmlbase.$href;
170 // href cleanup - Some packages are poorly done and use \ in urls
171 $href = ltrim(strtr($href, "\\", '/'), '/');
172 $resources[$identifier] = $href;
175 $items = array();
176 foreach ($organization->childNodes as $child) {
177 if ($child->nodeName === 'item') {
178 if (!$item = imscp_recursive_item($child, 0, $resources)) {
179 continue;
181 $items[] = $item;
185 return $items;
188 function imscp_recursive_item($xmlitem, $level, $resources) {
189 $identifierref = '';
190 if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
191 $identifierref = $identifierref->nodeValue;
194 $title = '?';
195 $subitems = array();
197 foreach ($xmlitem->childNodes as $child) {
198 if ($child->nodeName === 'title') {
199 $title = $child->textContent;
201 } else if ($child->nodeName === 'item') {
202 if ($subitem = imscp_recursive_item($child, $level+1, $resources)) {
203 $subitems[] = $subitem;
208 return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
209 'title' => $title,
210 'level' => $level,
211 'subitems' => $subitems,
216 * File browsing support class
218 class imscp_file_info extends file_info {
219 protected $course;
220 protected $cm;
221 protected $areas;
222 protected $filearea;
224 public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
225 parent::__construct($browser, $context);
226 $this->course = $course;
227 $this->cm = $cm;
228 $this->areas = $areas;
229 $this->filearea = $filearea;
233 * Returns list of standard virtual file/directory identification.
234 * The difference from stored_file parameters is that null values
235 * are allowed in all fields
236 * @return array with keys contextid, filearea, itemid, filepath and filename
238 public function get_params() {
239 return array('contextid'=>$this->context->id,
240 'component'=>'mod_imscp',
241 'filearea' =>$this->filearea,
242 'itemid' =>null,
243 'filepath' =>null,
244 'filename' =>null);
248 * Returns localised visible name.
249 * @return string
251 public function get_visible_name() {
252 return $this->areas[$this->filearea];
256 * Can I add new files or directories?
257 * @return bool
259 public function is_writable() {
260 return false;
264 * Is directory?
265 * @return bool
267 public function is_directory() {
268 return true;
272 * Returns list of children.
273 * @return array of file_info instances
275 public function get_children() {
276 global $DB;
278 $children = array();
279 $itemids = $DB->get_records('files', array('contextid'=>$this->context->id, 'component'=>'mod_imscp', 'filearea'=>$this->filearea), 'itemid', "DISTINCT itemid");
280 foreach ($itemids as $itemid=>$unused) {
281 if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $itemid)) {
282 $children[] = $child;
285 return $children;
289 * Returns parent file_info instance
290 * @return file_info or null for root
292 public function get_parent() {
293 return $this->browser->get_file_info($this->context);