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