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