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