improved handling of resource file/directory references when renaming and deleting files in file manager. It is now fully configurable too. MDL-6132

This commit is contained in:
skodak 2006-09-25 14:12:46 +00:00
parent 7c900d5d62
commit a69be0d861
4 changed files with 157 additions and 56 deletions

View File

@ -223,27 +223,19 @@
print_simple_box_end();
echo "<br />";
$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 '<p>'.get_string('warningdeleteresource', '', $file)." <a href='$CFG->wwwroot/course/mod.php?update=$resource_id&sesskey=$USER->sesskey'>".get_string('update')."</a></p>";
}
}
if ($resourcelist) {
print_simple_box_end();
echo "<br />";
}
require_once($CFG->dirroot.'/mod/resource/lib.php');
$block = resource_delete_warning($course, $USER->filelist);
notice_yesno (get_string("deletecheckfiles"),
"index.php?id=$id&amp;wdir=$wdir&amp;action=delete&amp;confirm=1&amp;sesskey=$USER->sesskey&amp;choose=$choose",
"index.php?id=$id&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
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&amp;wdir=$wdir&amp;action=cancel&amp;choose=$choose");
}
} else {
displaydir($wdir);
}
@ -289,23 +281,10 @@
echo "<center>Error: $name already exists!</center>";
} else if (!rename($basedir.$wdir."/".$oldname, $basedir.$wdir."/".$name)) {
echo "<p align=\"center\">Error: could not rename $oldname to $name</p>";
}
//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 "<b>The following files might be referenced as a resource :</b><br>";
foreach ($resources as $resource) {
$resource_id = files_get_cm_from_resource_name($name);
echo '<p align=\"center\">'. "$resource->reference :"."</align><a href='$CFG->wwwroot/course/mod.php?update=$resource_id&sesskey=$USER->sesskey'> ".get_string('update')."</a>";
}
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;
}
?>

View File

@ -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.';
?>

View File

@ -125,6 +125,34 @@
<?php print_string("configparametersettings", "resource") ?>
</td>
</tr>
<tr valign="top">
<td align="right">resource_autofilerename:</td>
<td>
<?php
unset($choices);
$choices["0"] = get_string("no");
$choices["1"] = get_string("yes");
choose_from_menu ($choices, "resource_autofilerename", $CFG->resource_autofilerename, "");
?>
</td>
<td>
<?php print_string("configautofilerenamesettings", "resource") ?>
</td>
</tr>
<tr valign="top">
<td align="right">resource_blockdeletingfile:</td>
<td>
<?php
unset($choices);
$choices["0"] = get_string("no");
$choices["1"] = get_string("yes");
choose_from_menu ($choices, "resource_blockdeletingfile", $CFG->resource_blockdeletingfile, "");
?>
</td>
<td>
<?php print_string("configblockdeletingfilesettings", "resource") ?>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="<?php print_string("savechanges") ?>" /></td>

View File

@ -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 = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
$updates = false;
$old = trim($wdir.'/'.$oldname, '/');
$new = trim($wdir.'/'.$name, '/');
$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')
AND (r.reference LIKE '{$old}/%' OR r.reference = '{$old}')";
if ($resources = get_records_sql($sql)) {
foreach ($resources as $resource) {
$r = new object();
$r->id = $resource->id;
$r->reference = '';
if ($resource->reference == $old) {
$r->reference = addslashes($new);
} else {
$r->reference = addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference));
}
if ($r->reference !== '') {
$updates = true;
$status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
if (!empty($CFG->resource_autofilerename)) {
if (!update_record('resource', $r)) {
error("Error updating resource with ID $r->id.");
}
}
}
}
}
$status .= '</ul></p>';
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 '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
foreach($found as $resource) {
echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
}
echo '</ul></p>';
print_simple_box_end();
return true;
} else {
return false;
}
}
?>