2a439ba7 |
1 | <?PHP // $Id$ |
2 | |
5925d0ef |
3 | if (!isset($CFG->resource_framesize)) { |
4 | set_config("resource_framesize", 130); |
5 | } |
92a419a2 |
6 | |
66c25030 |
7 | if (!isset($CFG->resource_websearch)) { |
8 | set_config("resource_websearch", "http://google.com/"); |
9 | } |
10 | |
5e91dd3f |
11 | if (!isset($CFG->resource_defaulturl)) { |
12 | set_config("resource_defaulturl", "http://"); |
3bfe3922 |
13 | } |
14 | |
15 | if (!isset($CFG->resource_filterexternalpages)) { |
16 | set_config("resource_filterexternalpages", false); |
17 | } |
5e91dd3f |
18 | |
d18830fe |
19 | if (!isset($CFG->resource_secretphrase)) { |
20 | set_config("resource_secretphrase", random_string(20)); |
21 | } |
22 | |
86aa7ccf |
23 | $RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location", |
24 | "menubar", "toolbar", "status", "height", "width"); |
92a419a2 |
25 | |
83891eda |
26 | if (!isset($CFG->resource_popup)) { |
27 | set_config("resource_popup", ""); |
28 | } |
29 | |
30 | foreach ($RESOURCE_WINDOW_OPTIONS as $popupoption) { |
31 | $popupoption = "resource_popup$popupoption"; |
32 | if (!isset($CFG->$popupoption)) { |
33 | if ($popupoption == "resource_popupheight") { |
34 | set_config($popupoption, 450); |
35 | } else if ($popupoption == "resource_popupwidth") { |
36 | set_config($popupoption, 620); |
37 | } else { |
38 | set_config($popupoption, "checked"); |
39 | } |
40 | } |
41 | } |
42 | |
d18830fe |
43 | /** |
44 | * resource_base is the base class for resource types |
45 | * |
46 | * This class provides all the functionality for a resource |
47 | */ |
48 | |
49 | class resource_base { |
50 | |
51 | var $cm; |
52 | var $course; |
53 | var $resource; |
54 | |
55 | |
56 | /** |
57 | * Constructor for the base resource class |
58 | * |
59 | * Constructor for the base resource class. |
60 | * If cmid is set create the cm, course, resource objects. |
61 | * |
62 | * @param cmid integer, the current course module id - not set for new resources |
63 | */ |
64 | function resource_base($cmid=0) { |
65 | |
66 | if ($cmid) { |
67 | if (! $this->cm = get_record("course_modules", "id", $cmid)) { |
68 | error("Course Module ID was incorrect"); |
69 | } |
70 | |
71 | if (! $this->course = get_record("course", "id", $this->cm->course)) { |
72 | error("Course is misconfigured"); |
73 | } |
74 | |
75 | if (! $this->resource = get_record("resource", "id", $this->cm->instance)) { |
76 | error("Resource ID was incorrect"); |
77 | } |
78 | } |
79 | } |
80 | |
81 | |
82 | function display() { |
83 | } |
84 | |
85 | |
1aef6fb7 |
86 | function setup(&$form) { |
d18830fe |
87 | global $CFG, $usehtmleditor; |
88 | |
89 | if (! empty($form->course)) { |
90 | if (! $this->course = get_record("course", "id", $form->course)) { |
91 | error("Course is misconfigured"); |
92 | } |
93 | } |
94 | |
95 | if (empty($form->name)) { |
96 | $form->name = ""; |
97 | } |
98 | if (empty($form->type)) { |
99 | $form->type = ""; |
100 | } |
101 | if (empty($form->summary)) { |
102 | $form->summary = ""; |
103 | } |
104 | if (empty($form->reference)) { |
105 | $form->reference = ""; |
106 | } |
107 | if (empty($form->alltext)) { |
108 | $form->alltext = ""; |
109 | } |
1aef6fb7 |
110 | if (empty($form->options)) { |
111 | $form->options = ""; |
112 | } |
d18830fe |
113 | $nohtmleditorneeded = true; |
114 | |
115 | print_heading_with_help(get_string("resourcetype$form->type", 'resource'), $form->type, 'resource'); |
116 | |
117 | include("$CFG->dirroot/mod/resource/type/common.html"); |
118 | } |
119 | |
120 | |
121 | function setup_end() { |
122 | global $CFG; |
123 | |
124 | include("$CFG->dirroot/mod/resource/type/common_end.html"); |
125 | } |
126 | |
127 | |
128 | function add_instance($resource) { |
cccb016a |
129 | // Given an object containing all the necessary data, |
130 | // (defined by the form in mod.html) this function |
131 | // will create a new instance and return the id number |
132 | // of the new instance. |
2a439ba7 |
133 | |
86aa7ccf |
134 | global $RESOURCE_WINDOW_OPTIONS; |
135 | |
cccb016a |
136 | $resource->timemodified = time(); |
2a439ba7 |
137 | |
d18830fe |
138 | if (isset($resource->windowpopup)) { |
86aa7ccf |
139 | $optionlist = array(); |
140 | foreach ($RESOURCE_WINDOW_OPTIONS as $option) { |
141 | if (isset($resource->$option)) { |
142 | $optionlist[] = $option."=".$resource->$option; |
143 | } |
144 | } |
d18830fe |
145 | $resource->popup = implode(',', $optionlist); |
146 | $resource->options = ""; |
147 | |
148 | } else if (isset($resource->windowpage)) { |
149 | |
150 | if (isset($resource->framepage)) { |
151 | $resource->options = "frame"; |
152 | } else { |
153 | $resource->options = ""; |
154 | } |
155 | $resource->popup = ""; |
86aa7ccf |
156 | } |
157 | |
cccb016a |
158 | return insert_record("resource", $resource); |
159 | } |
2a439ba7 |
160 | |
cccb016a |
161 | |
d18830fe |
162 | function update_instance($resource) { |
cccb016a |
163 | // Given an object containing all the necessary data, |
164 | // (defined by the form in mod.html) this function |
165 | // will update an existing instance with new data. |
166 | |
86aa7ccf |
167 | global $RESOURCE_WINDOW_OPTIONS; |
168 | |
cccb016a |
169 | $resource->id = $resource->instance; |
170 | $resource->timemodified = time(); |
171 | |
d18830fe |
172 | if (isset($resource->windowpopup)) { |
86aa7ccf |
173 | $optionlist = array(); |
174 | foreach ($RESOURCE_WINDOW_OPTIONS as $option) { |
175 | if (isset($resource->$option)) { |
176 | $optionlist[] = $option."=".$resource->$option; |
177 | } |
178 | } |
d18830fe |
179 | $resource->popup = implode(',', $optionlist); |
180 | $resource->options = ""; |
181 | |
182 | } else if (isset($resource->windowpage)) { |
183 | if (isset($resource->framepage)) { |
184 | $resource->options = "frame"; |
185 | } else { |
186 | $resource->options = ""; |
187 | } |
188 | $resource->popup = ""; |
86aa7ccf |
189 | } |
190 | |
cccb016a |
191 | return update_record("resource", $resource); |
192 | } |
193 | |
194 | |
d18830fe |
195 | function delete_instance($id) { |
cccb016a |
196 | // Given an ID of an instance of this module, |
197 | // this function will permanently delete the instance |
198 | // and any data that depends on it. |
199 | |
200 | if (! $resource = get_record("resource", "id", "$id")) { |
201 | return false; |
2a439ba7 |
202 | } |
203 | |
cccb016a |
204 | $result = true; |
205 | |
206 | if (! delete_records("resource", "id", "$resource->id")) { |
207 | $result = false; |
2a439ba7 |
208 | } |
209 | |
cccb016a |
210 | return $result; |
2a439ba7 |
211 | } |
cccb016a |
212 | |
2a439ba7 |
213 | |
d18830fe |
214 | |
215 | } /// end of class definition |
216 | |
217 | |
218 | |
219 | function resource_add_instance($resource) { |
220 | global $CFG; |
221 | |
222 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
223 | $resourceclass = "resource_$resource->type"; |
224 | $res = new $resourceclass(); |
d18830fe |
225 | |
226 | return $res->add_instance($resource); |
227 | } |
228 | |
229 | function resource_update_instance($resource) { |
230 | global $CFG; |
231 | |
232 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
233 | $resourceclass = "resource_$resource->type"; |
234 | $res = new $resourceclass(); |
d18830fe |
235 | |
236 | return $res->update_instance($resource); |
237 | } |
238 | |
239 | function resource_delete_instance($id) { |
240 | global $CFG; |
241 | |
242 | if (! $resource = get_record("resource", "id", "$id")) { |
243 | return false; |
244 | } |
245 | |
246 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
247 | $resourceclass = "resource_$resource->type"; |
248 | $res = new $resourceclass(); |
d18830fe |
249 | |
250 | return $res->delete_instance($id); |
251 | } |
252 | |
253 | |
2a439ba7 |
254 | function resource_user_outline($course, $user, $mod, $resource) { |
ebc3bd2b |
255 | if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' |
256 | AND action='view' AND info='$resource->id'", "time ASC")) { |
2a439ba7 |
257 | |
258 | $numviews = count($logs); |
259 | $lastlog = array_pop($logs); |
260 | |
261 | $result->info = get_string("numviews", "", $numviews); |
262 | $result->time = $lastlog->time; |
263 | |
264 | return $result; |
265 | } |
266 | return NULL; |
267 | } |
268 | |
269 | |
270 | function resource_user_complete($course, $user, $mod, $resource) { |
271 | global $CFG, $THEME; |
272 | |
ebc3bd2b |
273 | if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' |
274 | AND action='view' AND info='$resource->id'", "time ASC")) { |
2a439ba7 |
275 | $numviews = count($logs); |
276 | $lastlog = array_pop($logs); |
277 | |
278 | $strmostrecently = get_string("mostrecently"); |
279 | $strnumviews = get_string("numviews", "", $numviews); |
280 | |
281 | echo "$strnumviews - $strmostrecently ".userdate($lastlog->time); |
282 | |
283 | } else { |
4282d7dd |
284 | print_string("neverseen", "resource"); |
2a439ba7 |
285 | } |
286 | } |
287 | |
84caf038 |
288 | function resource_get_participants($resourceid) { |
289 | //Returns the users with data in one resource |
290 | //(NONE, byt must exists on EVERY mod !!) |
291 | |
292 | return false; |
293 | } |
2a439ba7 |
294 | |
8dddba42 |
295 | function resource_get_coursemodule_info($coursemodule) { |
296 | /// Given a course_module object, this function returns any |
297 | /// "extra" information that may be needed when printing |
298 | /// this activity in a course listing. |
299 | /// |
300 | /// See get_array_of_activities() in course/lib.php |
301 | /// |
302 | |
9d361034 |
303 | global $CFG; |
304 | |
305 | $info = NULL; |
306 | |
8dddba42 |
307 | if ($resource = get_record("resource", "id", $coursemodule->instance)) { |
85e8239e |
308 | if (!empty($resource->popup)) { |
9d361034 |
309 | $info->extra = urlencode("target=\"resource$resource->id\" onClick=\"return ". |
8dddba42 |
310 | "openpopup('/mod/resource/view.php?inpopup=true&id=". |
311 | $coursemodule->id. |
d18830fe |
312 | "','resource$resource->id','$resource->popup');\""); |
8dddba42 |
313 | } |
9d361034 |
314 | |
315 | require_once("$CFG->dirroot/files/mimetypes.php"); |
316 | |
85e8239e |
317 | if ($resource->type == 'file') { |
9d361034 |
318 | $icon = mimeinfo("icon", $resource->reference); |
319 | if ($icon != 'unknown.gif') { |
320 | $info->icon ="f/$icon"; |
85e8239e |
321 | } else { |
99a9b2d4 |
322 | $info->icon ="f/web.gif"; |
9d361034 |
323 | } |
d18830fe |
324 | } else if ($resource->type == 'directory') { |
7e62329a |
325 | $info->icon ="f/folder.gif"; |
9d361034 |
326 | } |
8dddba42 |
327 | } |
328 | |
9d361034 |
329 | return $info; |
8dddba42 |
330 | } |
af65e103 |
331 | |
8367faba |
332 | function resource_fetch_remote_file ($cm, $url, $headers = "" ) { |
3bfe3922 |
333 | /// Snoopy is an HTTP client in PHP |
334 | |
335 | global $CFG; |
336 | |
337 | require_once("$CFG->libdir/snoopy/Snoopy.class.inc"); |
338 | |
b2b8471e |
339 | $client = new Snoopy(); |
bf46cd22 |
340 | $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org'; |
341 | if ( $CFG->resource_usecache ) { |
342 | $ua = $ua . ')'; |
343 | } else { |
344 | $ua = $ua . '; No cache)'; |
345 | } |
346 | $client->agent = $ua; |
347 | $client->read_timeout = 5; |
348 | $client->use_gzip = true; |
b2b8471e |
349 | if (is_array($headers) ) { |
350 | $client->rawheaders = $headers; |
351 | } |
352 | |
353 | @$client->fetch($url); |
bf46cd22 |
354 | if ( $client->status >= 200 && $client->status < 300 ) { |
355 | $tags = array("A" => "href=", |
356 | "IMG" => "src=", |
357 | "LINK" => "href=", |
358 | "AREA" => "href=", |
359 | "FRAME" => "src=", |
360 | "IFRAME" => "src=", |
361 | "FORM" => "action="); |
af65e103 |
362 | |
bf46cd22 |
363 | foreach ($tags as $tag => $key) { |
364 | $prefix = "fetch.php?id=$cm->id&url="; |
365 | if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") { |
366 | $prefix = ""; |
367 | } |
368 | $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix); |
369 | } |
370 | } else { |
371 | if ( $client->status >= 400 && $client->status < 500) { |
372 | $client->results = get_string("fetchclienterror","resource"); // Client error |
373 | } elseif ( $client->status >= 500 && $client->status < 600) { |
374 | $client->results = get_string("fetchservererror","resource"); // Server error |
375 | } else { |
376 | $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error. |
af65e103 |
377 | } |
af65e103 |
378 | } |
b2b8471e |
379 | return $client; |
af65e103 |
380 | } |
381 | |
382 | function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) { |
bf46cd22 |
383 | $valid = 1; |
af65e103 |
384 | if ( strpos($url,"?") == FALSE ) { |
385 | $valid = 1; |
386 | } |
387 | if ( $valid ) { |
388 | $lastpoint = strrpos($url,"."); |
389 | $lastslash = strrpos($url,"/"); |
390 | if ( $lastpoint > $lastslash ) { |
391 | $root = substr($url,0,$lastslash+1); |
392 | } else { |
393 | $root = $url; |
394 | } |
395 | if ( $root == "http://" or |
396 | $root == "https://") { |
397 | $root = $url; |
398 | } |
399 | if ( substr($root,strlen($root)-1) == '/' ) { |
400 | $root = substr($root,0,-1); |
401 | } |
402 | |
403 | $mainroot = $root; |
404 | $lastslash = strrpos($mainroot,"/"); |
405 | while ( $lastslash > 9) { |
406 | $mainroot = substr($mainroot,0,$lastslash); |
407 | |
408 | $lastslash = strrpos($mainroot,"/"); |
409 | } |
8dddba42 |
410 | |
af65e103 |
411 | $regex = "/<$tagtoparse (.+?)>/is"; |
412 | $count = preg_match_all($regex, $text, $hrefs); |
413 | for ( $i = 0; $i < $count; $i++) { |
414 | $tag = $hrefs[1][$i]; |
415 | |
416 | $poshref = strpos(strtolower($tag),strtolower($keytoparse)); |
417 | $start = $poshref + strlen($keytoparse); |
418 | $left = substr($tag,0,$start); |
419 | if ( $tag[$start] == '"' ) { |
420 | $left .= '"'; |
421 | $start++; |
422 | } |
423 | $posspace = strpos($tag," ", $start+1); |
424 | $right = ""; |
425 | if ( $posspace != FALSE) { |
426 | $right = substr($tag, $posspace); |
427 | } |
428 | $end = strlen($tag)-1; |
429 | if ( $tag[$end] == '"' ) { |
430 | $right = '"' . $right; |
431 | } |
432 | $finalurl = substr($tag,$start,$end-$start+$diff); |
433 | // Here, we could have these possible values for $finalurl: |
434 | // file.ext Add current root dir |
435 | // http://(domain) don't care |
436 | // http://(domain)/ don't care |
437 | // http://(domain)/folder don't care |
438 | // http://(domain)/folder/ don't care |
439 | // http://(domain)/folder/file.ext don't care |
440 | // folder/ Add current root dir |
441 | // folder/file.ext Add current root dir |
442 | // /folder/ Add main root dir |
443 | // /folder/file.ext Add main root dir |
444 | |
445 | // Special case: If finalurl contains a ?, it won't be parsed |
bf46cd22 |
446 | $valid = 1; |
af65e103 |
447 | |
448 | if ( strpos($finalurl,"?") == FALSE ) { |
449 | $valid = 1; |
450 | } |
451 | if ( $valid ) { |
452 | if ( $finalurl[0] == "/" ) { |
453 | $finalurl = $mainroot . $finalurl; |
454 | } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and |
455 | strtolower(substr($finalurl,0,8)) != "https://") { |
456 | if ( $finalurl[0] == "/") { |
457 | $finalurl = $mainroot . $finalurl; |
458 | } else { |
459 | $finalurl = "$root/$finalurl"; |
460 | } |
461 | } |
462 | |
463 | $text = str_replace($tag,"$left$prefix$finalurl$right",$text); |
464 | } |
465 | } |
466 | } |
467 | return $text; |
468 | } |
8dddba42 |
469 | |
d18830fe |
470 | function resource_is_url($path) { |
427c8ccb |
471 | if (strpos($path, '://')) { // eg http:// https:// ftp:// etc |
d18830fe |
472 | return true; |
473 | } |
427c8ccb |
474 | if (strpos($path, '/') === 0) { // Starts with slash |
475 | return true; |
476 | } |
477 | return false; |
d18830fe |
478 | } |
479 | |
2a439ba7 |
480 | ?> |