Removed some tabs
[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();
176 $client->agent = MAGPIE_USER_AGENT;
177 $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
178 $client->use_gzip = MAGPIE_USE_GZIP;
179 if (is_array($headers) ) {
180 $client->rawheaders = $headers;
181 }
182
183 @$client->fetch($url);
af65e103 184
185 $tags = array("A" => "href=",
186 "IMG" => "src=",
187 "LINK" => "href=",
188 "AREA" => "href=",
189 "FRAME" => "src=",
190 "IFRAME" => "src=",
191 "FORM" => "action=");
192
193 foreach ($tags as $tag => $key) {
194 $prefix = "fetch.php?id=$cm->id&url=";
195 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
196 $prefix = "";
197 }
198 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
199 }
b2b8471e 200 return $client;
af65e103 201}
202
203function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
204 $valid = 0;
205 if ( strpos($url,"?") == FALSE ) {
206 $valid = 1;
207 }
208 if ( $valid ) {
209 $lastpoint = strrpos($url,".");
210 $lastslash = strrpos($url,"/");
211 if ( $lastpoint > $lastslash ) {
212 $root = substr($url,0,$lastslash+1);
213 } else {
214 $root = $url;
215 }
216 if ( $root == "http://" or
217 $root == "https://") {
218 $root = $url;
219 }
220 if ( substr($root,strlen($root)-1) == '/' ) {
221 $root = substr($root,0,-1);
222 }
223
224 $mainroot = $root;
225 $lastslash = strrpos($mainroot,"/");
226 while ( $lastslash > 9) {
227 $mainroot = substr($mainroot,0,$lastslash);
228
229 $lastslash = strrpos($mainroot,"/");
230 }
8dddba42 231
af65e103 232 $regex = "/<$tagtoparse (.+?)>/is";
233 $count = preg_match_all($regex, $text, $hrefs);
234 for ( $i = 0; $i < $count; $i++) {
235 $tag = $hrefs[1][$i];
236
237 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
238 $start = $poshref + strlen($keytoparse);
239 $left = substr($tag,0,$start);
240 if ( $tag[$start] == '"' ) {
241 $left .= '"';
242 $start++;
243 }
244 $posspace = strpos($tag," ", $start+1);
245 $right = "";
246 if ( $posspace != FALSE) {
247 $right = substr($tag, $posspace);
248 }
249 $end = strlen($tag)-1;
250 if ( $tag[$end] == '"' ) {
251 $right = '"' . $right;
252 }
253 $finalurl = substr($tag,$start,$end-$start+$diff);
254 // Here, we could have these possible values for $finalurl:
255 // file.ext Add current root dir
256 // http://(domain) don't care
257 // http://(domain)/ don't care
258 // http://(domain)/folder don't care
259 // http://(domain)/folder/ don't care
260 // http://(domain)/folder/file.ext don't care
261 // folder/ Add current root dir
262 // folder/file.ext Add current root dir
263 // /folder/ Add main root dir
264 // /folder/file.ext Add main root dir
265
266 // Special case: If finalurl contains a ?, it won't be parsed
267 $valid = 0;
268
269 if ( strpos($finalurl,"?") == FALSE ) {
270 $valid = 1;
271 }
272 if ( $valid ) {
273 if ( $finalurl[0] == "/" ) {
274 $finalurl = $mainroot . $finalurl;
275 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
276 strtolower(substr($finalurl,0,8)) != "https://") {
277 if ( $finalurl[0] == "/") {
278 $finalurl = $mainroot . $finalurl;
279 } else {
280 $finalurl = "$root/$finalurl";
281 }
282 }
283
284 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
285 }
286 }
287 }
288 return $text;
289}
8dddba42 290
2a439ba7 291?>