Merged in changed from defunct resource branch
[moodle.git] / mod / resource / lib.php
CommitLineData
2a439ba7 1<?PHP // $Id$
2
3define("REFERENCE", "1");
4define("WEBPAGE", "2");
5define("UPLOADEDFILE","3");
6define("PLAINTEXT", "4");
7define("WEBLINK", "5");
8define("HTML", "6");
f7a5c8fa 9define("PROGRAM", "7");
3e9ca9fb 10define("WIKITEXT", "8");
7ba54e1b 11define("DIRECTORY", "9");
2a439ba7 12
13$RESOURCE_TYPE = array (REFERENCE => get_string("resourcetype1", "resource"),
14 WEBPAGE => get_string("resourcetype2", "resource"),
15 UPLOADEDFILE => get_string("resourcetype3", "resource"),
16 PLAINTEXT => get_string("resourcetype4", "resource"),
17 WEBLINK => get_string("resourcetype5", "resource"),
f7a5c8fa 18 HTML => get_string("resourcetype6", "resource"),
3e9ca9fb 19 PROGRAM => get_string("resourcetype7", "resource"),
7ba54e1b 20 WIKITEXT => get_string("resourcetype8", "resource"),
21 DIRECTORY => get_string("resourcetype9", "resource") );
2a439ba7 22
5925d0ef 23if (!isset($CFG->resource_framesize)) {
24 set_config("resource_framesize", 130);
25}
92a419a2 26
66c25030 27if (!isset($CFG->resource_websearch)) {
28 set_config("resource_websearch", "http://google.com/");
29}
30
5e91dd3f 31if (!isset($CFG->resource_defaulturl)) {
32 set_config("resource_defaulturl", "http://");
3bfe3922 33}
34
35if (!isset($CFG->resource_filterexternalpages)) {
36 set_config("resource_filterexternalpages", false);
37}
5e91dd3f 38
86aa7ccf 39$RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location",
40 "menubar", "toolbar", "status", "height", "width");
92a419a2 41
83891eda 42if (!isset($CFG->resource_popup)) {
43 set_config("resource_popup", "");
44}
45
46foreach ($RESOURCE_WINDOW_OPTIONS as $popupoption) {
47 $popupoption = "resource_popup$popupoption";
48 if (!isset($CFG->$popupoption)) {
49 if ($popupoption == "resource_popupheight") {
50 set_config($popupoption, 450);
51 } else if ($popupoption == "resource_popupwidth") {
52 set_config($popupoption, 620);
53 } else {
54 set_config($popupoption, "checked");
55 }
56 }
57}
58
cccb016a 59function resource_add_instance($resource) {
60// Given an object containing all the necessary data,
61// (defined by the form in mod.html) this function
62// will create a new instance and return the id number
63// of the new instance.
2a439ba7 64
86aa7ccf 65 global $RESOURCE_WINDOW_OPTIONS;
66
cccb016a 67 $resource->timemodified = time();
2a439ba7 68
86aa7ccf 69 if (isset($resource->setnewwindow)) {
70 $optionlist = array();
71 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
72 if (isset($resource->$option)) {
73 $optionlist[] = $option."=".$resource->$option;
74 }
75 }
76 $resource->alltext = implode(',', $optionlist);
77 }
78
cccb016a 79 return insert_record("resource", $resource);
80}
2a439ba7 81
cccb016a 82
83function resource_update_instance($resource) {
84// Given an object containing all the necessary data,
85// (defined by the form in mod.html) this function
86// will update an existing instance with new data.
87
86aa7ccf 88 global $RESOURCE_WINDOW_OPTIONS;
89
cccb016a 90 $resource->id = $resource->instance;
91 $resource->timemodified = time();
92
86aa7ccf 93 if (isset($resource->setnewwindow)) {
94 $optionlist = array();
95 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
96 if (isset($resource->$option)) {
97 $optionlist[] = $option."=".$resource->$option;
98 }
99 }
100 $resource->alltext = implode(',', $optionlist);
101 }
102
cccb016a 103 return update_record("resource", $resource);
104}
105
106
107function resource_delete_instance($id) {
108// Given an ID of an instance of this module,
109// this function will permanently delete the instance
110// and any data that depends on it.
111
112 if (! $resource = get_record("resource", "id", "$id")) {
113 return false;
2a439ba7 114 }
115
cccb016a 116 $result = true;
117
118 if (! delete_records("resource", "id", "$resource->id")) {
119 $result = false;
2a439ba7 120 }
121
cccb016a 122 return $result;
2a439ba7 123}
cccb016a 124
2a439ba7 125
126function resource_user_outline($course, $user, $mod, $resource) {
ebc3bd2b 127 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
128 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 129
130 $numviews = count($logs);
131 $lastlog = array_pop($logs);
132
133 $result->info = get_string("numviews", "", $numviews);
134 $result->time = $lastlog->time;
135
136 return $result;
137 }
138 return NULL;
139}
140
141
142function resource_user_complete($course, $user, $mod, $resource) {
143 global $CFG, $THEME;
144
ebc3bd2b 145 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
146 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 147 $numviews = count($logs);
148 $lastlog = array_pop($logs);
149
150 $strmostrecently = get_string("mostrecently");
151 $strnumviews = get_string("numviews", "", $numviews);
152
153 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
154
155 } else {
4282d7dd 156 print_string("neverseen", "resource");
2a439ba7 157 }
158}
159
84caf038 160function resource_get_participants($resourceid) {
161//Returns the users with data in one resource
162//(NONE, byt must exists on EVERY mod !!)
163
164 return false;
165}
2a439ba7 166
8dddba42 167function resource_get_coursemodule_info($coursemodule) {
168/// Given a course_module object, this function returns any
169/// "extra" information that may be needed when printing
170/// this activity in a course listing.
171///
172/// See get_array_of_activities() in course/lib.php
173///
174
9d361034 175 global $CFG;
176
177 $info = NULL;
178
8dddba42 179 if ($resource = get_record("resource", "id", $coursemodule->instance)) {
180 if (($resource->type == UPLOADEDFILE or $resource->type == WEBLINK) and !empty($resource->alltext)) {
9d361034 181 $info->extra = urlencode("target=\"resource$resource->id\" onClick=\"return ".
8dddba42 182 "openpopup('/mod/resource/view.php?inpopup=true&id=".
183 $coursemodule->id.
184 "','resource$resource->id','$resource->alltext');\"");
185 }
9d361034 186
187 require_once("$CFG->dirroot/files/mimetypes.php");
188
7e1500ac 189 if ($resource->type == UPLOADEDFILE or $resource->type == WEBLINK or $resource->type == WEBPAGE) {
9d361034 190 $icon = mimeinfo("icon", $resource->reference);
191 if ($icon != 'unknown.gif') {
192 $info->icon ="f/$icon";
99a9b2d4 193 } else if ($resource->type == WEBLINK or $resource->type == WEBPAGE) {
194 $info->icon ="f/web.gif";
9d361034 195 }
7e62329a 196 } else if ($resource->type == DIRECTORY) {
197 $info->icon ="f/folder.gif";
9d361034 198 }
8dddba42 199 }
200
9d361034 201 return $info;
8dddba42 202}
af65e103 203
8367faba 204function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
3bfe3922 205/// Snoopy is an HTTP client in PHP
206
207 global $CFG;
208
209 require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
210
b2b8471e 211 $client = new Snoopy();
bf46cd22 212 $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org';
213 if ( $CFG->resource_usecache ) {
214 $ua = $ua . ')';
215 } else {
216 $ua = $ua . '; No cache)';
217 }
218 $client->agent = $ua;
219 $client->read_timeout = 5;
220 $client->use_gzip = true;
b2b8471e 221 if (is_array($headers) ) {
222 $client->rawheaders = $headers;
223 }
224
225 @$client->fetch($url);
bf46cd22 226 if ( $client->status >= 200 && $client->status < 300 ) {
227 $tags = array("A" => "href=",
228 "IMG" => "src=",
229 "LINK" => "href=",
230 "AREA" => "href=",
231 "FRAME" => "src=",
232 "IFRAME" => "src=",
233 "FORM" => "action=");
af65e103 234
bf46cd22 235 foreach ($tags as $tag => $key) {
236 $prefix = "fetch.php?id=$cm->id&url=";
237 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
238 $prefix = "";
239 }
240 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
241 }
242 } else {
243 if ( $client->status >= 400 && $client->status < 500) {
244 $client->results = get_string("fetchclienterror","resource"); // Client error
245 } elseif ( $client->status >= 500 && $client->status < 600) {
246 $client->results = get_string("fetchservererror","resource"); // Server error
247 } else {
248 $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
af65e103 249 }
af65e103 250 }
b2b8471e 251 return $client;
af65e103 252}
253
254function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
bf46cd22 255 $valid = 1;
af65e103 256 if ( strpos($url,"?") == FALSE ) {
257 $valid = 1;
258 }
259 if ( $valid ) {
260 $lastpoint = strrpos($url,".");
261 $lastslash = strrpos($url,"/");
262 if ( $lastpoint > $lastslash ) {
263 $root = substr($url,0,$lastslash+1);
264 } else {
265 $root = $url;
266 }
267 if ( $root == "http://" or
268 $root == "https://") {
269 $root = $url;
270 }
271 if ( substr($root,strlen($root)-1) == '/' ) {
272 $root = substr($root,0,-1);
273 }
274
275 $mainroot = $root;
276 $lastslash = strrpos($mainroot,"/");
277 while ( $lastslash > 9) {
278 $mainroot = substr($mainroot,0,$lastslash);
279
280 $lastslash = strrpos($mainroot,"/");
281 }
8dddba42 282
af65e103 283 $regex = "/<$tagtoparse (.+?)>/is";
284 $count = preg_match_all($regex, $text, $hrefs);
285 for ( $i = 0; $i < $count; $i++) {
286 $tag = $hrefs[1][$i];
287
288 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
289 $start = $poshref + strlen($keytoparse);
290 $left = substr($tag,0,$start);
291 if ( $tag[$start] == '"' ) {
292 $left .= '"';
293 $start++;
294 }
295 $posspace = strpos($tag," ", $start+1);
296 $right = "";
297 if ( $posspace != FALSE) {
298 $right = substr($tag, $posspace);
299 }
300 $end = strlen($tag)-1;
301 if ( $tag[$end] == '"' ) {
302 $right = '"' . $right;
303 }
304 $finalurl = substr($tag,$start,$end-$start+$diff);
305 // Here, we could have these possible values for $finalurl:
306 // file.ext Add current root dir
307 // http://(domain) don't care
308 // http://(domain)/ don't care
309 // http://(domain)/folder don't care
310 // http://(domain)/folder/ don't care
311 // http://(domain)/folder/file.ext don't care
312 // folder/ Add current root dir
313 // folder/file.ext Add current root dir
314 // /folder/ Add main root dir
315 // /folder/file.ext Add main root dir
316
317 // Special case: If finalurl contains a ?, it won't be parsed
bf46cd22 318 $valid = 1;
af65e103 319
320 if ( strpos($finalurl,"?") == FALSE ) {
321 $valid = 1;
322 }
323 if ( $valid ) {
324 if ( $finalurl[0] == "/" ) {
325 $finalurl = $mainroot . $finalurl;
326 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
327 strtolower(substr($finalurl,0,8)) != "https://") {
328 if ( $finalurl[0] == "/") {
329 $finalurl = $mainroot . $finalurl;
330 } else {
331 $finalurl = "$root/$finalurl";
332 }
333 }
334
335 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
336 }
337 }
338 }
339 return $text;
340}
8dddba42 341
2a439ba7 342?>