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