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