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