MDL-7861, fixing broken xhtml
[moodle.git] / mod / resource / lib.php
CommitLineData
d1290cec 1<?php // $Id$
2a439ba7 2
5925d0ef 3if (!isset($CFG->resource_framesize)) {
4 set_config("resource_framesize", 130);
ec81373f 5}
92a419a2 6
66c25030 7if (!isset($CFG->resource_websearch)) {
8 set_config("resource_websearch", "http://google.com/");
ec81373f 9}
66c25030 10
5e91dd3f 11if (!isset($CFG->resource_defaulturl)) {
12 set_config("resource_defaulturl", "http://");
ec81373f 13}
3bfe3922 14
15if (!isset($CFG->resource_filterexternalpages)) {
16 set_config("resource_filterexternalpages", false);
ec81373f 17}
5e91dd3f 18
d18830fe 19if (!isset($CFG->resource_secretphrase)) {
20 set_config("resource_secretphrase", random_string(20));
ec81373f 21}
d18830fe 22
83891eda 23if (!isset($CFG->resource_popup)) {
24 set_config("resource_popup", "");
ec81373f 25}
83891eda 26
33935242 27if (!isset($CFG->resource_windowsettings)) {
28 set_config("resource_windowsettings", "0");
ec81373f 29}
33935242 30
31if (!isset($CFG->resource_parametersettings)) {
32 set_config("resource_parametersettings", "0");
ec81373f 33}
33935242 34
713d78ea 35if (!isset($CFG->resource_allowlocalfiles)) {
36 set_config("resource_allowlocalfiles", "0");
37}
38
37147357 39if (!isset($CFG->resource_hide_repository)) {
40 set_config("resource_hide_repository", "1");
41}
42
a69be0d8 43if (!isset($CFG->resource_autofilerename)) {
44 set_config("resource_autofilerename", "1");
45}
46
47if (!isset($CFG->resource_blockdeletingfile)) {
48 set_config("resource_blockdeletingfile", "1");
49}
50
713d78ea 51define('RESOURCE_LOCALPATH', 'LOCALPATH');
52
ec81373f 53$RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location',
2e708fa0 54 'menubar', 'toolbar', 'status', 'height', 'width');
3d30a455 55
83891eda 56foreach ($RESOURCE_WINDOW_OPTIONS as $popupoption) {
57 $popupoption = "resource_popup$popupoption";
58 if (!isset($CFG->$popupoption)) {
2e708fa0 59 if ($popupoption == 'resource_popupheight') {
83891eda 60 set_config($popupoption, 450);
2e708fa0 61 } else if ($popupoption == 'resource_popupwidth') {
83891eda 62 set_config($popupoption, 620);
63 } else {
2e708fa0 64 set_config($popupoption, 'checked');
83891eda 65 }
ec81373f 66 }
83891eda 67}
68
d18830fe 69/**
70* resource_base is the base class for resource types
71*
72* This class provides all the functionality for a resource
73*/
74
75class resource_base {
76
77var $cm;
78var $course;
79var $resource;
80
81
82/**
83* Constructor for the base resource class
84*
85* Constructor for the base resource class.
86* If cmid is set create the cm, course, resource objects.
6aac6eef 87* and do some checks to make sure people can be here, and so on.
d18830fe 88*
89* @param cmid integer, the current course module id - not set for new resources
90*/
91function resource_base($cmid=0) {
92
6aac6eef 93 global $CFG;
3cc63301 94 global $course; // Ugly hack, needed for course language ugly hack
6aac6eef 95
d18830fe 96 if ($cmid) {
f9d5371b 97 if (! $this->cm = get_coursemodule_from_id('resource', $cmid)) {
d18830fe 98 error("Course Module ID was incorrect");
99 }
100
101 if (! $this->course = get_record("course", "id", $this->cm->course)) {
102 error("Course is misconfigured");
103 }
104
3cc63301 105 $course = $this->course; // Make it a global so we can see it later
106
ec81373f 107 require_course_login($this->course, true, $this->cm);
6aac6eef 108
d18830fe 109 if (! $this->resource = get_record("resource", "id", $this->cm->instance)) {
110 error("Resource ID was incorrect");
111 }
6aac6eef 112
113 $this->strresource = get_string("modulename", "resource");
114 $this->strresources = get_string("modulenameplural", "resource");
115
1936c10e 116 if ($this->course->id != SITEID) {
6aac6eef 117 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id={$this->course->id}\">{$this->course->shortname}</a> -> ".
118 "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strresources</a> ->";
119 } else {
ec81373f 120 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strresources</a> ->";
6aac6eef 121 }
122
d02eeded 123 if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $this->cm->id))) {
6aac6eef 124 $pagetitle = strip_tags($this->course->shortname.': '.$this->strresource);
125 print_header($pagetitle, $this->course->fullname, "$this->navigation $this->strresource", "", "", true, '', navmenu($this->course, $this->cm));
126 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
127 }
ec81373f 128 }
d18830fe 129}
130
131
6aac6eef 132/**
133* Display function does nothing in the base class
134*/
d18830fe 135function display() {
6aac6eef 136
d18830fe 137}
138
139
0440482f 140/**
141* Display the resource with the course blocks.
142*/
143function display_course_blocks_start() {
144
145 global $CFG;
146 global $USER;
147
148 require_once($CFG->libdir.'/blocklib.php');
149 require_once($CFG->libdir.'/pagelib.php');
dbfa9ccd 150 require_once($CFG->dirroot.'/course/lib.php'); //required by some blocks
0440482f 151
152 $PAGE = page_create_object(PAGE_COURSE_VIEW, $this->course->id);
153 $this->PAGE = $PAGE;
154 $pageblocks = blocks_setup($PAGE);
155
156 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
157
158/// Print the page header
159
d2b23346 160 $edit = optional_param('edit', -1, PARAM_BOOL);
161
162 if (($edit != -1) and $PAGE->user_allowed_editing()) {
163 $USER->editing = $edit;
0440482f 164 }
d2b23346 165
0440482f 166 $morebreadcrumbs = array($this->strresources => 'index.php?id='.$this->course->id,
167 $this->resource->name => '');
168
169 $PAGE->print_header($this->course->shortname.': %fullname%', $morebreadcrumbs);
170
171 echo '<table id="layout-table"><tr>';
172
173 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
174 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
175 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
176 echo '</td>';
177 }
178
179 echo '<td id="middle-column">';
180 echo '<div id="resource">';
181
182}
183
184
185/**
186 * Finish displaying the resource with the course blocks
187 */
188function display_course_blocks_end() {
189
190 global $CFG;
191
192 $PAGE = $this->PAGE;
193 $pageblocks = blocks_setup($PAGE);
194 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
195
196 echo '</div>';
197 echo '</td>';
198
199 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
200 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
201 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
202 echo '</td>';
203 }
204
205 echo '</tr></table>';
206
207 print_footer($this->course);
208
209}
210
211
1aef6fb7 212function setup(&$form) {
d18830fe 213 global $CFG, $usehtmleditor;
214
215 if (! empty($form->course)) {
216 if (! $this->course = get_record("course", "id", $form->course)) {
217 error("Course is misconfigured");
218 }
219 }
220
221 if (empty($form->name)) {
222 $form->name = "";
223 }
224 if (empty($form->type)) {
225 $form->type = "";
226 }
227 if (empty($form->summary)) {
228 $form->summary = "";
229 }
230 if (empty($form->reference)) {
231 $form->reference = "";
232 }
233 if (empty($form->alltext)) {
234 $form->alltext = "";
235 }
1aef6fb7 236 if (empty($form->options)) {
237 $form->options = "";
238 }
d18830fe 239 $nohtmleditorneeded = true;
240
6da4b261 241 print_heading_with_help(get_string("resourcetype$form->type", 'resource'), $form->type, 'resource/type');
d18830fe 242
243 include("$CFG->dirroot/mod/resource/type/common.html");
244}
245
246
247function setup_end() {
248 global $CFG;
249
250 include("$CFG->dirroot/mod/resource/type/common_end.html");
251}
252
253
254function add_instance($resource) {
ec81373f 255// Given an object containing all the necessary data,
256// (defined by the form in mod.html) this function
257// will create a new instance and return the id number
cccb016a 258// of the new instance.
2a439ba7 259
86aa7ccf 260 global $RESOURCE_WINDOW_OPTIONS;
261
cccb016a 262 $resource->timemodified = time();
2a439ba7 263
d18830fe 264 if (isset($resource->windowpopup)) {
7c990f68 265 if ($resource->windowpopup) {
266 $optionlist = array();
267 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
268 if (isset($resource->$option)) {
269 $optionlist[] = $option."=".$resource->$option;
270 }
86aa7ccf 271 }
7c990f68 272 $resource->popup = implode(',', $optionlist);
d18830fe 273 $resource->options = "";
7c990f68 274 } else {
275 if (isset($resource->framepage)) {
276 $resource->options = "frame";
277 } else {
278 $resource->options = "";
279 }
280 $resource->popup = "";
d18830fe 281 }
86aa7ccf 282 }
283
33935242 284 if (isset($resource->parametersettingspref)) {
285 set_user_preference('resource_parametersettingspref', $resource->parametersettingspref);
286 }
287 if (isset($resource->windowsettingspref)) {
288 set_user_preference('resource_windowsettingspref', $resource->windowsettingspref);
289 }
e22482a7 290
291 // fix for MDL-7300
292 if (!isset($resource->alltext)) {
293 $resource->alltext = '';
294 }
33935242 295
636069a3 296 if (!isset($resource->popup)) {
e22482a7 297 $resource->popup = '';
298 }
299
cccb016a 300 return insert_record("resource", $resource);
301}
2a439ba7 302
cccb016a 303
d18830fe 304function update_instance($resource) {
ec81373f 305// Given an object containing all the necessary data,
306// (defined by the form in mod.html) this function
cccb016a 307// will update an existing instance with new data.
308
86aa7ccf 309 global $RESOURCE_WINDOW_OPTIONS;
310
cccb016a 311 $resource->id = $resource->instance;
312 $resource->timemodified = time();
313
d18830fe 314 if (isset($resource->windowpopup)) {
7c990f68 315 if ($resource->windowpopup) {
316 $optionlist = array();
317 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
318 if (isset($resource->$option)) {
319 $optionlist[] = $option."=".$resource->$option;
320 }
86aa7ccf 321 }
7c990f68 322 $resource->popup = implode(',', $optionlist);
d18830fe 323 $resource->options = "";
7c990f68 324 } else {
325 if (isset($resource->framepage)) {
326 $resource->options = "frame";
327 } else {
328 $resource->options = "";
329 }
330 $resource->popup = "";
d18830fe 331 }
86aa7ccf 332 }
333
33935242 334 if (isset($resource->parametersettingspref)) {
335 set_user_preference('resource_parametersettingspref', $resource->parametersettingspref);
336 }
337 if (isset($resource->windowsettingspref)) {
338 set_user_preference('resource_windowsettingspref', $resource->windowsettingspref);
339 }
340
cccb016a 341 return update_record("resource", $resource);
342}
343
344
f0c6abbf 345function delete_instance($resource) {
346// Given an object containing the resource data
ec81373f 347// this function will permanently delete the instance
348// and any data that depends on it.
cccb016a 349
cccb016a 350 $result = true;
351
352 if (! delete_records("resource", "id", "$resource->id")) {
353 $result = false;
2a439ba7 354 }
355
cccb016a 356 return $result;
2a439ba7 357}
cccb016a 358
2a439ba7 359
d18830fe 360
361} /// end of class definition
362
363
364
365function resource_add_instance($resource) {
366 global $CFG;
ec81373f 367
1b1d3422 368 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 369
d18830fe 370 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 371 $resourceclass = "resource_$resource->type";
372 $res = new $resourceclass();
d18830fe 373
374 return $res->add_instance($resource);
375}
376
377function resource_update_instance($resource) {
378 global $CFG;
ec81373f 379
1b1d3422 380 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 381
d18830fe 382 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 383 $resourceclass = "resource_$resource->type";
384 $res = new $resourceclass();
d18830fe 385
386 return $res->update_instance($resource);
387}
388
389function resource_delete_instance($id) {
390 global $CFG;
ec81373f 391
d18830fe 392 if (! $resource = get_record("resource", "id", "$id")) {
393 return false;
394 }
79035d46 395
1b1d3422 396 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
ec81373f 397
d18830fe 398 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 399 $resourceclass = "resource_$resource->type";
400 $res = new $resourceclass();
d18830fe 401
f0c6abbf 402 return $res->delete_instance($resource);
d18830fe 403}
404
405
2a439ba7 406function resource_user_outline($course, $user, $mod, $resource) {
ec81373f 407 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
ebc3bd2b 408 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 409
410 $numviews = count($logs);
411 $lastlog = array_pop($logs);
412
413 $result->info = get_string("numviews", "", $numviews);
414 $result->time = $lastlog->time;
415
416 return $result;
417 }
418 return NULL;
419}
420
421
422function resource_user_complete($course, $user, $mod, $resource) {
9fad2dec 423 global $CFG;
2a439ba7 424
ec81373f 425 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
ebc3bd2b 426 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 427 $numviews = count($logs);
428 $lastlog = array_pop($logs);
429
430 $strmostrecently = get_string("mostrecently");
431 $strnumviews = get_string("numviews", "", $numviews);
432
433 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
434
435 } else {
4282d7dd 436 print_string("neverseen", "resource");
2a439ba7 437 }
438}
439
84caf038 440function resource_get_participants($resourceid) {
441//Returns the users with data in one resource
442//(NONE, byt must exists on EVERY mod !!)
443
444 return false;
445}
2a439ba7 446
8dddba42 447function resource_get_coursemodule_info($coursemodule) {
ec81373f 448/// Given a course_module object, this function returns any
8dddba42 449/// "extra" information that may be needed when printing
450/// this activity in a course listing.
451///
452/// See get_array_of_activities() in course/lib.php
453///
454
9d361034 455 global $CFG;
456
457 $info = NULL;
458
8dddba42 459 if ($resource = get_record("resource", "id", $coursemodule->instance)) {
85e8239e 460 if (!empty($resource->popup)) {
657e7903 461 $info->extra = urlencode("target=\"resource$resource->id\" onclick=\"return ".
839f2456 462 "openpopup('/mod/resource/view.php?inpopup=true&amp;id=".
8dddba42 463 $coursemodule->id.
d18830fe 464 "','resource$resource->id','$resource->popup');\"");
8dddba42 465 }
9d361034 466
f1e0649c 467 require_once($CFG->libdir.'/filelib.php');
9d361034 468
85e8239e 469 if ($resource->type == 'file') {
ec81373f 470 $icon = mimeinfo("icon", $resource->reference);
9d361034 471 if ($icon != 'unknown.gif') {
ec81373f 472 $info->icon ="f/$icon";
85e8239e 473 } else {
ec81373f 474 $info->icon ="f/web.gif";
9d361034 475 }
d18830fe 476 } else if ($resource->type == 'directory') {
ec81373f 477 $info->icon ="f/folder.gif";
9d361034 478 }
8dddba42 479 }
480
9d361034 481 return $info;
8dddba42 482}
ec81373f 483
8367faba 484function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
3bfe3922 485/// Snoopy is an HTTP client in PHP
486
487 global $CFG;
488
489 require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
490
b2b8471e 491 $client = new Snoopy();
ec81373f 492 $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org';
bf46cd22 493 if ( $CFG->resource_usecache ) {
494 $ua = $ua . ')';
495 } else {
496 $ua = $ua . '; No cache)';
497 }
498 $client->agent = $ua;
499 $client->read_timeout = 5;
500 $client->use_gzip = true;
b2b8471e 501 if (is_array($headers) ) {
502 $client->rawheaders = $headers;
503 }
ec81373f 504
b2b8471e 505 @$client->fetch($url);
bf46cd22 506 if ( $client->status >= 200 && $client->status < 300 ) {
507 $tags = array("A" => "href=",
508 "IMG" => "src=",
509 "LINK" => "href=",
510 "AREA" => "href=",
511 "FRAME" => "src=",
512 "IFRAME" => "src=",
513 "FORM" => "action=");
ec81373f 514
bf46cd22 515 foreach ($tags as $tag => $key) {
839f2456 516 $prefix = "fetch.php?id=$cm->id&amp;url=";
bf46cd22 517 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
518 $prefix = "";
519 }
520 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
521 }
522 } else {
523 if ( $client->status >= 400 && $client->status < 500) {
524 $client->results = get_string("fetchclienterror","resource"); // Client error
525 } elseif ( $client->status >= 500 && $client->status < 600) {
526 $client->results = get_string("fetchservererror","resource"); // Server error
527 } else {
528 $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
af65e103 529 }
af65e103 530 }
b2b8471e 531 return $client;
af65e103 532}
533
534function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
bf46cd22 535 $valid = 1;
af65e103 536 if ( strpos($url,"?") == FALSE ) {
537 $valid = 1;
538 }
539 if ( $valid ) {
540 $lastpoint = strrpos($url,".");
541 $lastslash = strrpos($url,"/");
542 if ( $lastpoint > $lastslash ) {
543 $root = substr($url,0,$lastslash+1);
544 } else {
545 $root = $url;
546 }
ec81373f 547 if ( $root == "http://" or
af65e103 548 $root == "https://") {
549 $root = $url;
550 }
551 if ( substr($root,strlen($root)-1) == '/' ) {
552 $root = substr($root,0,-1);
553 }
ec81373f 554
af65e103 555 $mainroot = $root;
556 $lastslash = strrpos($mainroot,"/");
557 while ( $lastslash > 9) {
558 $mainroot = substr($mainroot,0,$lastslash);
ec81373f 559
af65e103 560 $lastslash = strrpos($mainroot,"/");
561 }
8dddba42 562
ec81373f 563 $regex = "/<$tagtoparse (.+?)>/is";
564 $count = preg_match_all($regex, $text, $hrefs);
af65e103 565 for ( $i = 0; $i < $count; $i++) {
566 $tag = $hrefs[1][$i];
ec81373f 567
af65e103 568 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
569 $start = $poshref + strlen($keytoparse);
570 $left = substr($tag,0,$start);
571 if ( $tag[$start] == '"' ) {
572 $left .= '"';
573 $start++;
574 }
575 $posspace = strpos($tag," ", $start+1);
576 $right = "";
577 if ( $posspace != FALSE) {
578 $right = substr($tag, $posspace);
579 }
580 $end = strlen($tag)-1;
581 if ( $tag[$end] == '"' ) {
582 $right = '"' . $right;
583 }
584 $finalurl = substr($tag,$start,$end-$start+$diff);
585 // Here, we could have these possible values for $finalurl:
586 // file.ext Add current root dir
587 // http://(domain) don't care
588 // http://(domain)/ don't care
589 // http://(domain)/folder don't care
590 // http://(domain)/folder/ don't care
591 // http://(domain)/folder/file.ext don't care
592 // folder/ Add current root dir
593 // folder/file.ext Add current root dir
594 // /folder/ Add main root dir
595 // /folder/file.ext Add main root dir
596
597 // Special case: If finalurl contains a ?, it won't be parsed
bf46cd22 598 $valid = 1;
af65e103 599
600 if ( strpos($finalurl,"?") == FALSE ) {
601 $valid = 1;
602 }
603 if ( $valid ) {
604 if ( $finalurl[0] == "/" ) {
605 $finalurl = $mainroot . $finalurl;
ec81373f 606 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
af65e103 607 strtolower(substr($finalurl,0,8)) != "https://") {
608 if ( $finalurl[0] == "/") {
609 $finalurl = $mainroot . $finalurl;
610 } else {
611 $finalurl = "$root/$finalurl";
612 }
613 }
ec81373f 614
af65e103 615 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
616 }
617 }
618 }
619 return $text;
620}
8dddba42 621
d18830fe 622function resource_is_url($path) {
427c8ccb 623 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
d18830fe 624 return true;
625 }
427c8ccb 626 if (strpos($path, '/') === 0) { // Starts with slash
627 return true;
628 }
629 return false;
d18830fe 630}
631
6da4b261 632function resource_get_resource_types() {
633/// Returns a menu of current resource types, in standard order
37147357 634 global $resource_standard_order, $CFG;
6da4b261 635
636 $resources = array();
637
638 /// Standard resource types
3d30a455 639 $standardresources = array('text','html','file','directory');
6da4b261 640 foreach ($standardresources as $resourcetype) {
641 $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
642 }
643
644 /// Drop-in extra resource types
645 $resourcetypes = get_list_of_plugins('mod/resource/type');
646 foreach ($resourcetypes as $resourcetype) {
37147357 647 if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
648 continue;
649 }
6da4b261 650 if (!in_array($resourcetype, $resources)) {
651 $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
652 }
653 }
654 return $resources;
655}
f3221af9 656
657function resource_get_view_actions() {
658 return array('view','view all');
659}
660
661function resource_get_post_actions() {
662 return array();
663}
664
a69be0d8 665function resource_renamefiles($course, $wdir, $oldname, $name) {
666 global $CFG;
667
668 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
669 $updates = false;
670
671 $old = trim($wdir.'/'.$oldname, '/');
672 $new = trim($wdir.'/'.$name, '/');
673
674 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
675 FROM {$CFG->prefix}resource r,
676 {$CFG->prefix}course_modules cm,
677 {$CFG->prefix}modules m
678 WHERE r.course = '{$course->id}'
679 AND m.name = 'resource'
680 AND cm.module = m.id
681 AND cm.instance = r.id
682 AND (r.type = 'file' OR r.type = 'directory')
683 AND (r.reference LIKE '{$old}/%' OR r.reference = '{$old}')";
684 if ($resources = get_records_sql($sql)) {
685 foreach ($resources as $resource) {
686 $r = new object();
687 $r->id = $resource->id;
688 $r->reference = '';
689 if ($resource->reference == $old) {
690 $r->reference = addslashes($new);
691 } else {
692 $r->reference = addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference));
693 }
694 if ($r->reference !== '') {
695 $updates = true;
696 $status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
697 if (!empty($CFG->resource_autofilerename)) {
698 if (!update_record('resource', $r)) {
699 error("Error updating resource with ID $r->id.");
700 }
701 }
702 }
703 }
704 }
705 $status .= '</ul></p>';
706
707 if ($updates) {
708 echo $status;
709 if (empty($CFG->resource_autofilerename)) {
710 notify(get_string('warningdisabledrename', 'resource'));
711 }
712 }
713}
714
715function resource_delete_warning($course, $files) {
716 global $CFG;
717
718 $found = array();
719
720 foreach($files as $key=>$file) {
721 $files[$key] = trim($file, '/');
722 }
723 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
724 FROM {$CFG->prefix}resource r,
725 {$CFG->prefix}course_modules cm,
726 {$CFG->prefix}modules m
727 WHERE r.course = '{$course->id}'
728 AND m.name = 'resource'
729 AND cm.module = m.id
730 AND cm.instance = r.id
731 AND (r.type = 'file' OR r.type = 'directory')";
732 if ($resources = get_records_sql($sql)) {
733 foreach ($resources as $resource) {
734 if ($resource->reference == '') {
735 continue; // top shared directory does not prevent anything
736 }
737 if (in_array($resource->reference, $files)) {
738 $found[$resource->id] = $resource;
739 } else {
740 foreach($files as $file) {
741 if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference)) {
742 $found[$resource->id] = $resource;
743 }
744 }
745 }
746 }
747 }
748
749 if (!empty($found)) {
750
751 print_simple_box_start("center");
752 echo '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
753 foreach($found as $resource) {
754 echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
755 }
756 echo '</ul></p>';
757 print_simple_box_end();
758
759 return true;
760 } else {
761 return false;
762 }
763}
764
2a439ba7 765?>