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 | |
83891eda |
23 | if (!isset($CFG->resource_popup)) { |
24 | set_config("resource_popup", ""); |
25 | } |
26 | |
3d30a455 |
27 | $RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location", |
28 | "menubar", "toolbar", "status", "height", "width"); |
29 | |
83891eda |
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 | |
6da4b261 |
115 | print_heading_with_help(get_string("resourcetype$form->type", 'resource'), $form->type, 'resource/type'); |
d18830fe |
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)) { |
7c990f68 |
139 | if ($resource->windowpopup) { |
140 | $optionlist = array(); |
141 | foreach ($RESOURCE_WINDOW_OPTIONS as $option) { |
142 | if (isset($resource->$option)) { |
143 | $optionlist[] = $option."=".$resource->$option; |
144 | } |
86aa7ccf |
145 | } |
7c990f68 |
146 | $resource->popup = implode(',', $optionlist); |
d18830fe |
147 | $resource->options = ""; |
7c990f68 |
148 | } else { |
149 | if (isset($resource->framepage)) { |
150 | $resource->options = "frame"; |
151 | } else { |
152 | $resource->options = ""; |
153 | } |
154 | $resource->popup = ""; |
d18830fe |
155 | } |
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)) { |
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 | |
cccb016a |
192 | return update_record("resource", $resource); |
193 | } |
194 | |
195 | |
d18830fe |
196 | function delete_instance($id) { |
cccb016a |
197 | // Given an ID of an instance of this module, |
198 | // this function will permanently delete the instance |
199 | // and any data that depends on it. |
200 | |
201 | if (! $resource = get_record("resource", "id", "$id")) { |
202 | return false; |
2a439ba7 |
203 | } |
204 | |
cccb016a |
205 | $result = true; |
206 | |
207 | if (! delete_records("resource", "id", "$resource->id")) { |
208 | $result = false; |
2a439ba7 |
209 | } |
210 | |
cccb016a |
211 | return $result; |
2a439ba7 |
212 | } |
cccb016a |
213 | |
2a439ba7 |
214 | |
d18830fe |
215 | |
216 | } /// end of class definition |
217 | |
218 | |
219 | |
220 | function resource_add_instance($resource) { |
221 | global $CFG; |
222 | |
223 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
224 | $resourceclass = "resource_$resource->type"; |
225 | $res = new $resourceclass(); |
d18830fe |
226 | |
227 | return $res->add_instance($resource); |
228 | } |
229 | |
230 | function resource_update_instance($resource) { |
231 | global $CFG; |
232 | |
233 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
234 | $resourceclass = "resource_$resource->type"; |
235 | $res = new $resourceclass(); |
d18830fe |
236 | |
237 | return $res->update_instance($resource); |
238 | } |
239 | |
240 | function resource_delete_instance($id) { |
241 | global $CFG; |
242 | |
243 | if (! $resource = get_record("resource", "id", "$id")) { |
244 | return false; |
245 | } |
246 | |
247 | require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php"); |
1aef6fb7 |
248 | $resourceclass = "resource_$resource->type"; |
249 | $res = new $resourceclass(); |
d18830fe |
250 | |
251 | return $res->delete_instance($id); |
252 | } |
253 | |
254 | |
2a439ba7 |
255 | function resource_user_outline($course, $user, $mod, $resource) { |
ebc3bd2b |
256 | if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' |
257 | AND action='view' AND info='$resource->id'", "time ASC")) { |
2a439ba7 |
258 | |
259 | $numviews = count($logs); |
260 | $lastlog = array_pop($logs); |
261 | |
262 | $result->info = get_string("numviews", "", $numviews); |
263 | $result->time = $lastlog->time; |
264 | |
265 | return $result; |
266 | } |
267 | return NULL; |
268 | } |
269 | |
270 | |
271 | function resource_user_complete($course, $user, $mod, $resource) { |
272 | global $CFG, $THEME; |
273 | |
ebc3bd2b |
274 | if ($logs = get_records_select("log", "userid='$user->id' AND module='resource' |
275 | AND action='view' AND info='$resource->id'", "time ASC")) { |
2a439ba7 |
276 | $numviews = count($logs); |
277 | $lastlog = array_pop($logs); |
278 | |
279 | $strmostrecently = get_string("mostrecently"); |
280 | $strnumviews = get_string("numviews", "", $numviews); |
281 | |
282 | echo "$strnumviews - $strmostrecently ".userdate($lastlog->time); |
283 | |
284 | } else { |
4282d7dd |
285 | print_string("neverseen", "resource"); |
2a439ba7 |
286 | } |
287 | } |
288 | |
84caf038 |
289 | function resource_get_participants($resourceid) { |
290 | //Returns the users with data in one resource |
291 | //(NONE, byt must exists on EVERY mod !!) |
292 | |
293 | return false; |
294 | } |
2a439ba7 |
295 | |
8dddba42 |
296 | function resource_get_coursemodule_info($coursemodule) { |
297 | /// Given a course_module object, this function returns any |
298 | /// "extra" information that may be needed when printing |
299 | /// this activity in a course listing. |
300 | /// |
301 | /// See get_array_of_activities() in course/lib.php |
302 | /// |
303 | |
9d361034 |
304 | global $CFG; |
305 | |
306 | $info = NULL; |
307 | |
8dddba42 |
308 | if ($resource = get_record("resource", "id", $coursemodule->instance)) { |
85e8239e |
309 | if (!empty($resource->popup)) { |
9d361034 |
310 | $info->extra = urlencode("target=\"resource$resource->id\" onClick=\"return ". |
8dddba42 |
311 | "openpopup('/mod/resource/view.php?inpopup=true&id=". |
312 | $coursemodule->id. |
d18830fe |
313 | "','resource$resource->id','$resource->popup');\""); |
8dddba42 |
314 | } |
9d361034 |
315 | |
316 | require_once("$CFG->dirroot/files/mimetypes.php"); |
317 | |
85e8239e |
318 | if ($resource->type == 'file') { |
9d361034 |
319 | $icon = mimeinfo("icon", $resource->reference); |
320 | if ($icon != 'unknown.gif') { |
321 | $info->icon ="f/$icon"; |
85e8239e |
322 | } else { |
99a9b2d4 |
323 | $info->icon ="f/web.gif"; |
9d361034 |
324 | } |
d18830fe |
325 | } else if ($resource->type == 'directory') { |
7e62329a |
326 | $info->icon ="f/folder.gif"; |
9d361034 |
327 | } |
8dddba42 |
328 | } |
329 | |
9d361034 |
330 | return $info; |
8dddba42 |
331 | } |
af65e103 |
332 | |
8367faba |
333 | function resource_fetch_remote_file ($cm, $url, $headers = "" ) { |
3bfe3922 |
334 | /// Snoopy is an HTTP client in PHP |
335 | |
336 | global $CFG; |
337 | |
338 | require_once("$CFG->libdir/snoopy/Snoopy.class.inc"); |
339 | |
b2b8471e |
340 | $client = new Snoopy(); |
bf46cd22 |
341 | $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org'; |
342 | if ( $CFG->resource_usecache ) { |
343 | $ua = $ua . ')'; |
344 | } else { |
345 | $ua = $ua . '; No cache)'; |
346 | } |
347 | $client->agent = $ua; |
348 | $client->read_timeout = 5; |
349 | $client->use_gzip = true; |
b2b8471e |
350 | if (is_array($headers) ) { |
351 | $client->rawheaders = $headers; |
352 | } |
353 | |
354 | @$client->fetch($url); |
bf46cd22 |
355 | if ( $client->status >= 200 && $client->status < 300 ) { |
356 | $tags = array("A" => "href=", |
357 | "IMG" => "src=", |
358 | "LINK" => "href=", |
359 | "AREA" => "href=", |
360 | "FRAME" => "src=", |
361 | "IFRAME" => "src=", |
362 | "FORM" => "action="); |
af65e103 |
363 | |
bf46cd22 |
364 | foreach ($tags as $tag => $key) { |
365 | $prefix = "fetch.php?id=$cm->id&url="; |
366 | if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") { |
367 | $prefix = ""; |
368 | } |
369 | $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix); |
370 | } |
371 | } else { |
372 | if ( $client->status >= 400 && $client->status < 500) { |
373 | $client->results = get_string("fetchclienterror","resource"); // Client error |
374 | } elseif ( $client->status >= 500 && $client->status < 600) { |
375 | $client->results = get_string("fetchservererror","resource"); // Server error |
376 | } else { |
377 | $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error. |
af65e103 |
378 | } |
af65e103 |
379 | } |
b2b8471e |
380 | return $client; |
af65e103 |
381 | } |
382 | |
383 | function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) { |
bf46cd22 |
384 | $valid = 1; |
af65e103 |
385 | if ( strpos($url,"?") == FALSE ) { |
386 | $valid = 1; |
387 | } |
388 | if ( $valid ) { |
389 | $lastpoint = strrpos($url,"."); |
390 | $lastslash = strrpos($url,"/"); |
391 | if ( $lastpoint > $lastslash ) { |
392 | $root = substr($url,0,$lastslash+1); |
393 | } else { |
394 | $root = $url; |
395 | } |
396 | if ( $root == "http://" or |
397 | $root == "https://") { |
398 | $root = $url; |
399 | } |
400 | if ( substr($root,strlen($root)-1) == '/' ) { |
401 | $root = substr($root,0,-1); |
402 | } |
403 | |
404 | $mainroot = $root; |
405 | $lastslash = strrpos($mainroot,"/"); |
406 | while ( $lastslash > 9) { |
407 | $mainroot = substr($mainroot,0,$lastslash); |
408 | |
409 | $lastslash = strrpos($mainroot,"/"); |
410 | } |
8dddba42 |
411 | |
af65e103 |
412 | $regex = "/<$tagtoparse (.+?)>/is"; |
413 | $count = preg_match_all($regex, $text, $hrefs); |
414 | for ( $i = 0; $i < $count; $i++) { |
415 | $tag = $hrefs[1][$i]; |
416 | |
417 | $poshref = strpos(strtolower($tag),strtolower($keytoparse)); |
418 | $start = $poshref + strlen($keytoparse); |
419 | $left = substr($tag,0,$start); |
420 | if ( $tag[$start] == '"' ) { |
421 | $left .= '"'; |
422 | $start++; |
423 | } |
424 | $posspace = strpos($tag," ", $start+1); |
425 | $right = ""; |
426 | if ( $posspace != FALSE) { |
427 | $right = substr($tag, $posspace); |
428 | } |
429 | $end = strlen($tag)-1; |
430 | if ( $tag[$end] == '"' ) { |
431 | $right = '"' . $right; |
432 | } |
433 | $finalurl = substr($tag,$start,$end-$start+$diff); |
434 | // Here, we could have these possible values for $finalurl: |
435 | // file.ext Add current root dir |
436 | // http://(domain) don't care |
437 | // http://(domain)/ don't care |
438 | // http://(domain)/folder don't care |
439 | // http://(domain)/folder/ don't care |
440 | // http://(domain)/folder/file.ext don't care |
441 | // folder/ Add current root dir |
442 | // folder/file.ext Add current root dir |
443 | // /folder/ Add main root dir |
444 | // /folder/file.ext Add main root dir |
445 | |
446 | // Special case: If finalurl contains a ?, it won't be parsed |
bf46cd22 |
447 | $valid = 1; |
af65e103 |
448 | |
449 | if ( strpos($finalurl,"?") == FALSE ) { |
450 | $valid = 1; |
451 | } |
452 | if ( $valid ) { |
453 | if ( $finalurl[0] == "/" ) { |
454 | $finalurl = $mainroot . $finalurl; |
455 | } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and |
456 | strtolower(substr($finalurl,0,8)) != "https://") { |
457 | if ( $finalurl[0] == "/") { |
458 | $finalurl = $mainroot . $finalurl; |
459 | } else { |
460 | $finalurl = "$root/$finalurl"; |
461 | } |
462 | } |
463 | |
464 | $text = str_replace($tag,"$left$prefix$finalurl$right",$text); |
465 | } |
466 | } |
467 | } |
468 | return $text; |
469 | } |
8dddba42 |
470 | |
d18830fe |
471 | function resource_is_url($path) { |
427c8ccb |
472 | if (strpos($path, '://')) { // eg http:// https:// ftp:// etc |
d18830fe |
473 | return true; |
474 | } |
427c8ccb |
475 | if (strpos($path, '/') === 0) { // Starts with slash |
476 | return true; |
477 | } |
478 | return false; |
d18830fe |
479 | } |
480 | |
6da4b261 |
481 | function resource_get_resource_types() { |
482 | /// Returns a menu of current resource types, in standard order |
483 | global $resource_standard_order; |
484 | |
485 | $resources = array(); |
486 | |
487 | /// Standard resource types |
3d30a455 |
488 | $standardresources = array('text','html','file','directory'); |
6da4b261 |
489 | foreach ($standardresources as $resourcetype) { |
490 | $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource'); |
491 | } |
492 | |
493 | /// Drop-in extra resource types |
494 | $resourcetypes = get_list_of_plugins('mod/resource/type'); |
495 | foreach ($resourcetypes as $resourcetype) { |
496 | if (!in_array($resourcetype, $resources)) { |
497 | $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource'); |
498 | } |
499 | } |
500 | return $resources; |
501 | } |
2a439ba7 |
502 | ?> |