From a69be0d8613d80a1b95076a388b5dadd7ec63f05 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 25 Sep 2006 14:12:46 +0000 Subject: [PATCH] improved handling of resource file/directory references when renaming and deleting files in file manager. It is now fully configurable too. MDL-6132 --- files/index.php | 72 ++++++------------------- lang/en_utf8/resource.php | 5 ++ mod/resource/config.html | 28 ++++++++++ mod/resource/lib.php | 108 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 56 deletions(-) diff --git a/files/index.php b/files/index.php index 0c5e131a33..b78af78f64 100644 --- a/files/index.php +++ b/files/index.php @@ -223,27 +223,19 @@ print_simple_box_end(); echo "
"; - $resourcelist = false; - foreach ($USER->filelist as $file) { - // If file is specified in a resource, then delete that too. - $clean_name = substr($file, 1); - if (record_exists('resource', 'reference', $clean_name)) { - if (!$resourcelist) { - print_simple_box_start("center"); - $resourcelist = true; - } - $resource_id = files_get_cm_from_resource_name($clean_name); - echo '

'.get_string('warningdeleteresource', '', $file)." ".get_string('update')."

"; - } - } - if ($resourcelist) { - print_simple_box_end(); - echo "
"; + require_once($CFG->dirroot.'/mod/resource/lib.php'); + $block = resource_delete_warning($course, $USER->filelist); + + if (empty($CFG->resource_blockdeletingfile) or $block == '') { + $optionsyes = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'choose'=>$choose); + $optionsno = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose); + notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get'); + } else { + + notify(get_string('warningblockingdelete', 'resource')); + $options = array('id'=>$id, 'wdir'=>$wdir, 'action'=>'cancel', 'choose'=>$choose); + print_continue("index.php?id=$id&wdir=$wdir&action=cancel&choose=$choose"); } - - notice_yesno (get_string("deletecheckfiles"), - "index.php?id=$id&wdir=$wdir&action=delete&confirm=1&sesskey=$USER->sesskey&choose=$choose", - "index.php?id=$id&wdir=$wdir&action=cancel&choose=$choose"); } else { displaydir($wdir); } @@ -289,23 +281,10 @@ echo "
Error: $name already exists!
"; } else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) { echo "

Error: could not rename $oldname to $name

"; - } - - //if file is part of resource then update resource table as well - //this line only catch the root directory - if (record_exists('resource', 'reference', $oldname)) { - set_field('resource', 'reference', $name, 'reference', $oldname); - } - - if (get_dir_name_from_resource($oldname)) { - $resources = get_dir_name_from_resource($oldname); - print_simple_box_start("center"); - echo "The following files might be referenced as a resource :
"; - foreach ($resources as $resource) { - $resource_id = files_get_cm_from_resource_name($name); - echo '

'. "$resource->reference :"." ".get_string('update').""; - } - print_simple_box_end(); + } else { + //file was renamed now update resources if needed + require_once($CFG->dirroot.'/mod/resource/lib.php'); + resource_renamefiles($course, $wdir, $oldname, $name); } displaydir($wdir); @@ -867,23 +846,4 @@ function displaydir ($wdir) { } -function files_get_cm_from_resource_name($clean_name) { - global $CFG; - - $SQL = 'SELECT a.id FROM '.$CFG->prefix.'course_modules a, '.$CFG->prefix.'resource b - WHERE a.instance = b.id AND b.reference = "'.$clean_name.'"'; - $resource = get_record_sql($SQL); - return $resource->id; -} - -function get_dir_name_from_resource($clean_name) { - global $CFG; - - $LIKE = sql_ilike(); - - $SQL = 'SELECT * FROM '.$CFG->prefix.'resource WHERE reference '.$LIKE. "\"%$clean_name%\""; - $resource = get_records_sql($SQL); - return $resource; -} - ?> diff --git a/lang/en_utf8/resource.php b/lang/en_utf8/resource.php index 01bb57ca9a..852c9dd3c7 100644 --- a/lang/en_utf8/resource.php +++ b/lang/en_utf8/resource.php @@ -3,12 +3,15 @@ $string['addresource'] = 'Add a resource'; +$string['affectedresources'] = 'Affected resources'; $string['back'] = 'Back'; $string['browserepository'] = 'Browse repository'; $string['choose'] = 'Choose'; $string['chooseafile'] = 'Choose or upload a file'; $string['chooseparameter'] = 'Choose parameter'; $string['configallowlocalfiles'] = 'When creating a new file resource, allow links to files on a local file system such as a CD drive or a hard drive. This may be useful in a classroom where all students have access to a common network drive or where files on a CD need to be accessed. Use of this feature may require changes to your browser\'s security settings.'; +$string['configautofilerenamesettings'] = 'Automatically update references to files and directories when renamed in file manager.'; +$string['configblockdeletingfilesettings'] = 'Block deleting of files and directories referenced by resources. Please note that images and other files referenced from html are not protected.'; $string['configdefaulturl'] = 'This value is used to prefill the URL form when creating a new URL-based resource.'; $string['configfilterexternalpages'] = 'Enabling this setting will cause all external resources (web pages, uploaded HTML files) to be processed by the currently defined site filters (such as glossary autolinks, for example). Turning this option on may cause your course pages to slow down significantly - use with caution and only if you really need it.'; $string['configframesize'] = 'When a web page or an uploaded file is displayed within a frame, this value is the size (in pixels) of the top frame (which contains the navigation).'; @@ -116,5 +119,7 @@ $string['upbutton'] = 'Up'; $string['variablename'] = 'Variable name'; $string['viewims'] = 'View IMS Content Package'; $string['vol'] = 'Vol'; +$string['warningblockingdelete'] = 'Warning: Delete operation is blocked by existing resource. Either update your resources first or disable this safety mechanism in global Resource configuration.'; +$string['warningdisabledrename'] = 'Warning: The automatic renaming of references in resources is disabled. Please update affected resources manually if needed. Automatic renaming can be enabled in global Resource configuration.'; ?> diff --git a/mod/resource/config.html b/mod/resource/config.html index 0bf8ea37e6..1f4d91806b 100644 --- a/mod/resource/config.html +++ b/mod/resource/config.html @@ -125,6 +125,34 @@ + + resource_autofilerename: + + resource_autofilerename, ""); + ?> + + + + + + + resource_blockdeletingfile: + + resource_blockdeletingfile, ""); + ?> + + + + + " /> diff --git a/mod/resource/lib.php b/mod/resource/lib.php index 3a5d81abcc..bc9a032bbf 100644 --- a/mod/resource/lib.php +++ b/mod/resource/lib.php @@ -40,6 +40,14 @@ if (!isset($CFG->resource_hide_repository)) { set_config("resource_hide_repository", "1"); } +if (!isset($CFG->resource_autofilerename)) { + set_config("resource_autofilerename", "1"); +} + +if (!isset($CFG->resource_blockdeletingfile)) { + set_config("resource_blockdeletingfile", "1"); +} + define('RESOURCE_LOCALPATH', 'LOCALPATH'); $RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location', @@ -645,4 +653,104 @@ function resource_get_post_actions() { return array(); } +function resource_renamefiles($course, $wdir, $oldname, $name) { + global $CFG; + + $status = '

'.get_string('affectedresources', 'resource').':

'; + + if ($updates) { + echo $status; + if (empty($CFG->resource_autofilerename)) { + notify(get_string('warningdisabledrename', 'resource')); + } + } +} + +function resource_delete_warning($course, $files) { + global $CFG; + + $found = array(); + + foreach($files as $key=>$file) { + $files[$key] = trim($file, '/'); + } + $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid + FROM {$CFG->prefix}resource r, + {$CFG->prefix}course_modules cm, + {$CFG->prefix}modules m + WHERE r.course = '{$course->id}' + AND m.name = 'resource' + AND cm.module = m.id + AND cm.instance = r.id + AND (r.type = 'file' OR r.type = 'directory')"; + if ($resources = get_records_sql($sql)) { + foreach ($resources as $resource) { + if ($resource->reference == '') { + continue; // top shared directory does not prevent anything + } + if (in_array($resource->reference, $files)) { + $found[$resource->id] = $resource; + } else { + foreach($files as $file) { + if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference)) { + $found[$resource->id] = $resource; + } + } + } + } + } + + if (!empty($found)) { + + print_simple_box_start("center"); + echo '

'.get_string('affectedresources', 'resource').':

'; + print_simple_box_end(); + + return true; + } else { + return false; + } +} + ?> -- 2.39.5