forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
295 lines (260 loc) · 9.19 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Private imscp module utility functions
*
* @package mod
* @subpackage imscp
* @copyright 2009 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->dirroot/mod/imscp/lib.php");
require_once("$CFG->libdir/filelib.php");
require_once("$CFG->libdir/resourcelib.php");
function imscp_print_content($imscp, $cm, $course) {
global $PAGE, $CFG;
$items = unserialize($imscp->structure);
$first = reset($items);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$urlbase = "$CFG->wwwroot/pluginfile.php";
$path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$first['href'];
$firsturl = file_encode_url($urlbase, $path, false);
echo '<div id="imscp_layout">';
echo '<div id="imscp_toc">';
echo '<div id="imscp_tree"><ul>';
foreach ($items as $item) {
echo imscp_htmllize_item($item, $imscp, $cm);
}
echo '</ul></div>';
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>';
echo '</div>';
echo '</div>';
$PAGE->requires->js_init_call('M.mod_imscp.init');
return;
}
/**
* Internal function - creates htmls structure suitable for YUI tree.
*/
function imscp_htmllize_item($item, $imscp, $cm) {
global $CFG;
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$urlbase = "$CFG->wwwroot/pluginfile.php";
$path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$item['href'];
$url = file_encode_url($urlbase, $path, false);
$result = "<li><a href=\"$url\">".$item['title'].'</a>';
if ($item['subitems']) {
$result .= '<ul>';
foreach ($item['subitems'] as $subitem) {
$result .= imscp_htmllize_item($subitem, $imscp, $cm);
}
$result .= '</ul>';
}
$result .= '</li>';
return $result;
}
/**
* Parse an IMS content package's manifest file to determine its structure
* @param object $imscp
* @param object $context
* @return array
*/
function imscp_parse_structure($imscp, $context) {
$fs = get_file_storage();
if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) {
return null;
}
return imscp_parse_manifestfile($manifestfile->get_content());
}
/**
* Parse the contents of a IMS package's manifest file
* @param string $manifestfilecontents the contents of the manifest file
* @return array
*/
function imscp_parse_manifestfile($manifestfilecontents) {
$doc = new DOMDocument();
if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
return null;
}
// we put this fake URL as base in order to detect path changes caused by xml:base attributes
$doc->documentURI = 'http://grrr/';
$xmlorganizations = $doc->getElementsByTagName('organizations');
if (empty($xmlorganizations->length)) {
return null;
}
$default = null;
if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) {
$default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue;
}
$xmlorganization = $doc->getElementsByTagName('organization');
if (empty($xmlorganization->length)) {
return null;
}
$organization = null;
foreach ($xmlorganization as $org) {
if (is_null($organization)) {
// use first if default nor found
$organization = $org;
}
if (!$org->attributes->getNamedItem('identifier')) {
continue;
}
if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) {
// found default - use it
$organization = $org;
break;
}
}
// load all resources
$resources = array();
$xmlresources = $doc->getElementsByTagName('resource');
foreach ($xmlresources as $res) {
if (!$identifier = $res->attributes->getNamedItem('identifier')) {
continue;
}
$identifier = $identifier->nodeValue;
if ($xmlbase = $res->baseURI) {
// undo the fake URL, we are interested in relative links only
$xmlbase = str_replace('http://grrr/', '/', $xmlbase);
$xmlbase = rtrim($xmlbase, '/').'/';
} else {
$xmlbase = '';
}
if (!$href = $res->attributes->getNamedItem('href')) {
// If href not found look for <file href="help.htm"/>
$fileresources = $res->getElementsByTagName('file');
foreach ($fileresources as $file) {
$href = $file->getAttribute('href');
}
if (empty($href)) {
continue;
}
} else {
$href = $href->nodeValue;
}
if (strpos($href, 'http://') !== 0) {
$href = $xmlbase.$href;
}
// href cleanup - Some packages are poorly done and use \ in urls
$href = ltrim(strtr($href, "\\", '/'), '/');
$resources[$identifier] = $href;
}
$items = array();
foreach ($organization->childNodes as $child) {
if ($child->nodeName === 'item') {
if (!$item = imscp_recursive_item($child, 0, $resources)) {
continue;
}
$items[] = $item;
}
}
return $items;
}
function imscp_recursive_item($xmlitem, $level, $resources) {
$identifierref = '';
if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
$identifierref = $identifierref->nodeValue;
}
$title = '?';
$subitems = array();
foreach ($xmlitem->childNodes as $child) {
if ($child->nodeName === 'title') {
$title = $child->textContent;
} else if ($child->nodeName === 'item') {
if ($subitem = imscp_recursive_item($child, $level+1, $resources)) {
$subitems[] = $subitem;
}
}
}
return array('href' => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
'title' => $title,
'level' => $level,
'subitems' => $subitems,
);
}
/**
* File browsing support class
*/
class imscp_file_info extends file_info {
protected $course;
protected $cm;
protected $areas;
protected $filearea;
public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
parent::__construct($browser, $context);
$this->course = $course;
$this->cm = $cm;
$this->areas = $areas;
$this->filearea = $filearea;
}
/**
* Returns list of standard virtual file/directory identification.
* The difference from stored_file parameters is that null values
* are allowed in all fields
* @return array with keys contextid, filearea, itemid, filepath and filename
*/
public function get_params() {
return array('contextid'=>$this->context->id,
'component'=>'mod_imscp',
'filearea' =>$this->filearea,
'itemid' =>null,
'filepath' =>null,
'filename' =>null);
}
/**
* Returns localised visible name.
* @return string
*/
public function get_visible_name() {
return $this->areas[$this->filearea];
}
/**
* Can I add new files or directories?
* @return bool
*/
public function is_writable() {
return false;
}
/**
* Is directory?
* @return bool
*/
public function is_directory() {
return true;
}
/**
* Returns list of children.
* @return array of file_info instances
*/
public function get_children() {
global $DB;
$children = array();
$itemids = $DB->get_records('files', array('contextid'=>$this->context->id, 'component'=>'mod_imscp', 'filearea'=>$this->filearea), 'itemid', "DISTINCT itemid");
foreach ($itemids as $itemid=>$unused) {
if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $itemid)) {
$children[] = $child;
}
}
return $children;
}
/**
* Returns parent file_info instance
* @return file_info or null for root
*/
public function get_parent() {
return $this->browser->get_file_info($this->context);
}
}