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