Commit 5f30780a authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Use unique names in template file folders; store them in a subfolder

parent 25bc6988
...@@ -666,17 +666,18 @@ class ArtefactTypeBlogPost extends ArtefactType { ...@@ -666,17 +666,18 @@ class ArtefactTypeBlogPost extends ArtefactType {
} }
public static function blogfiles_folder_id($create = true) { public static function blogfiles_folder_id($create = true) {
global $USER;
$name = get_string('blogfilesdirname', 'artefact.blog'); $name = get_string('blogfilesdirname', 'artefact.blog');
$description = get_string('blogfilesdirdescription', 'artefact.blog'); $description = get_string('blogfilesdirdescription', 'artefact.blog');
safe_require('artefact', 'file'); safe_require('artefact', 'file');
return ArtefactTypeFolder::get_folder_id($name, $description, null, null, $create); return ArtefactTypeFolder::get_folder_id($name, $description, null, $create, $USER->get('id'));
} }
// Change the name & description of a user's blogfiles folder when the user changes language pref // Change the name & description of a user's blogfiles folder when the user changes language pref
public static function change_language($userid, $oldlang, $newlang) { public static function change_language($userid, $oldlang, $newlang) {
$oldname = get_string_from_language($oldlang, 'blogfilesdirname', 'artefact.blog'); $oldname = get_string_from_language($oldlang, 'blogfilesdirname', 'artefact.blog');
safe_require('artefact', 'file'); safe_require('artefact', 'file');
$blogfiles = ArtefactTypeFolder::get_folder_by_name($oldname, null, $userid); $blogfiles = ArtefactTypeFolder::get_folder_by_name($oldname, null, $userid, null, null);
if (empty($blogfiles)) { if (empty($blogfiles)) {
return; return;
} }
......
...@@ -395,25 +395,13 @@ abstract class ArtefactTypeFileBase extends ArtefactType { ...@@ -395,25 +395,13 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
// either in adminfiles or with a specific owner. // either in adminfiles or with a specific owner.
public static function file_exists($title, $owner, $folder, $institution=null, $group=null) { public static function file_exists($title, $owner, $folder, $institution=null, $group=null) {
$filetypesql = "('" . join("','", PluginArtefactFile::get_artefact_types()) . "')"; $filetypesql = "('" . join("','", PluginArtefactFile::get_artefact_types()) . "')";
$phvals = array($title); $ownersql = artefact_owner_sql($owner, $group, $institution);
if ($institution) {
$ownersql = 'a.institution = ? AND a.owner IS NULL';
$phvals[] = $institution;
}
else if ($group) {
$ownersql = 'a.group = ? AND a.owner IS NULL';
$phvals[] = $group;
}
else {
$ownersql = 'a.institution IS NULL AND a.owner = ?';
$phvals[] = $owner;
}
return get_field_sql('SELECT a.id FROM {artefact} a return get_field_sql('SELECT a.id FROM {artefact} a
LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id
WHERE a.title = ? WHERE a.title = ?
AND ' . $ownersql . ' AND a.' . $ownersql . '
AND a.parent ' . (empty($folder) ? ' IS NULL' : ' = ' . $folder) . ' AND a.parent ' . (empty($folder) ? ' IS NULL' : ' = ' . $folder) . '
AND a.artefacttype IN ' . $filetypesql, $phvals); AND a.artefacttype IN ' . $filetypesql, array($title));
} }
...@@ -577,7 +565,7 @@ JAVASCRIPT; ...@@ -577,7 +565,7 @@ JAVASCRIPT;
return $javascript; return $javascript;
} }
public static function count_user_files($owner=null, $institution=null, $group=null) { public static function count_user_files($owner=null, $group=null, $institution=null) {
$filetypes = PluginArtefactFile::get_artefact_types(); $filetypes = PluginArtefactFile::get_artefact_types();
foreach ($filetypes as $k => $v) { foreach ($filetypes as $k => $v) {
if ($v == 'folder') { if ($v == 'folder') {
...@@ -586,26 +574,10 @@ JAVASCRIPT; ...@@ -586,26 +574,10 @@ JAVASCRIPT;
} }
$filetypesql = "('" . join("','", $filetypes) . "')"; $filetypesql = "('" . join("','", $filetypes) . "')";
$phvals = array(); $ownersql = artefact_owner_sql($owner, $group, $institution);
if ($institution) {
$ownersql = 'institution = ? AND "group" IS NULL AND owner IS NULL';
$phvals[] = $institution;
}
else if ($group) {
$ownersql = '"group" = ? AND institution IS NULL AND owner IS NULL';
$phvals[] = $group;
}
else {
if (!$owner) {
global $USER;
$owner = $USER->get('id');
}
$ownersql = 'institution IS NULL AND "group" IS NULL AND owner = ?';
$phvals[] = $owner;
}
return (object) array( return (object) array(
'files' => count_records_select('artefact', "artefacttype IN $filetypesql AND $ownersql", $phvals), 'files' => count_records_select('artefact', "artefacttype IN $filetypesql AND $ownersql", array()),
'folders' => count_records_select('artefact', "artefacttype = 'folder' AND $ownersql", $phvals) 'folders' => count_records_select('artefact', "artefacttype = 'folder' AND $ownersql", array())
); );
} }
...@@ -1023,33 +995,28 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase { ...@@ -1023,33 +995,28 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase {
} }
} }
public static function get_folder_by_name($name, $parentfolderid=null, $userid=null) { public static function get_folder_by_name($name, $parentfolderid=null, $userid=null, $groupid=null, $institution=null) {
if (empty($userid)) {
global $USER;
$userid = $USER->id;
}
$parentclause = $parentfolderid ? 'parent = ' . $parentfolderid : 'parent IS NULL'; $parentclause = $parentfolderid ? 'parent = ' . $parentfolderid : 'parent IS NULL';
$ownerclause = artefact_owner_sql($userid, $groupid, $institution);
return get_record_sql('SELECT * FROM {artefact} return get_record_sql('SELECT * FROM {artefact}
WHERE title = ? AND ' . $parentclause . ' AND owner = ' . $userid . " WHERE title = ? AND ' . $parentclause . ' AND ' . $ownerclause . "
AND artefacttype = 'folder'", array($name)); AND artefacttype = 'folder'", array($name));
} }
// Get the id of a folder, creating the folder if necessary // Get the id of a folder, creating the folder if necessary
public static function get_folder_id($name, $description, $parentfolderid=null, $userid=null, $create=true) { public static function get_folder_id($name, $description, $parentfolderid=null, $create=true, $userid=null, $groupid=null, $institution=null) {
if (empty($userid)) { if (!$record = self::get_folder_by_name($name, $parentfolderid, $userid, $groupid, $institution)) {
global $USER;
$userid = $USER->id;
}
if (!$record = self::get_folder_by_name($name, $parentfolderid, $userid)) {
if (!$create) { if (!$create) {
return false; return false;
} }
$data = new StdClass; $data = new StdClass;
$data->title = $name; $data->title = $name;
$data->description = $description; $data->description = $description;
$data->owner = $userid;
$data->group = $groupid;
$data->institution = $institution;
$data->parent = $parentfolderid;
$f = new ArtefactTypeFolder(0, $data); $f = new ArtefactTypeFolder(0, $data);
$f->set('owner', $userid);
$f->set('parent', $parentfolderid);
$f->commit(); $f->commit();
return $f->get('id'); return $f->get('id');
} }
......
...@@ -911,4 +911,17 @@ function artefact_get_descendants($new) { ...@@ -911,4 +911,17 @@ function artefact_get_descendants($new) {
return array_values($seen); return array_values($seen);
} }
function artefact_owner_sql($userid=null, $groupid=null, $institution=null) {
if ($institution) {
return 'institution = ' . db_quote($institution);
}
if ($groupid) {
return '"group" = ' . (int)$groupid;
}
if ($userid) {
return 'owner = ' . (int)$userid;
}
return null;
}
?> ?>
...@@ -72,7 +72,7 @@ else if ($group->jointype == 'open') { ...@@ -72,7 +72,7 @@ else if ($group->jointype == 'open') {
$group->groupjoin = group_get_join_form('joingroup', $group->id); $group->groupjoin = group_get_join_form('joingroup', $group->id);
} }
$filecounts = ArtefactTypeFileBase::count_user_files(null, null, $group->id); $filecounts = ArtefactTypeFileBase::count_user_files(null, $group->id, null);
// Latest forums posts // Latest forums posts
// NOTE: it would be nicer if there was some generic way to get information // NOTE: it would be nicer if there was some generic way to get information
......
...@@ -238,8 +238,9 @@ $string['noaccesstoview'] = 'You do not have permission to access this view'; ...@@ -238,8 +238,9 @@ $string['noaccesstoview'] = 'You do not have permission to access this view';
$string['Template'] = 'Template'; $string['Template'] = 'Template';
$string['templatedescription'] = 'If you would like the people who can see your view to be able to make their own copies of it, check the Template box.'; $string['templatedescription'] = 'If you would like the people who can see your view to be able to make their own copies of it, check the Template box.';
$string['copiedblocksandartefactsfromtemplate'] = 'Copied %d blocks and %d artefacts from %s'; $string['copiedblocksandartefactsfromtemplate'] = 'Copied %d blocks and %d artefacts from %s';
$string['templatefiles'] = 'templatefiles';
$string['filescopiedfromviewtemplate'] = 'Files copied from %s'; $string['filescopiedfromviewtemplate'] = 'Files copied from %s';
$string['viewfilesdirname'] = 'viewfiles';
$string['viewfilesdirdesc'] = 'Files from copied views';
?> ?>
...@@ -1785,12 +1785,28 @@ class View { ...@@ -1785,12 +1785,28 @@ class View {
// Create parent folder for files that have no parent // Create parent folder for files that have no parent
if ($artefactcopies) { if ($artefactcopies) {
safe_require('artefact', 'file'); safe_require('artefact', 'file');
$viewfilesfolder = ArtefactTypeFolder::get_folder_id(get_string('viewfilesdirname', 'view'), get_string('viewfilesdirdesc', 'view'),
null, true, $this->get('owner'), $this->get('group'), $this->get('institution'));
$foldername = $template->get('id');
$existing = get_column_sql("
SELECT title
FROM {artefact}
WHERE parent = ? AND title LIKE ? || '%'", array($viewfilesfolder, $foldername));
$sep = '';
$ext = '';
if ($existing) {
while (in_array($foldername . $sep . $ext, $existing)) {
$sep = '-';
$ext++;
}
}
$data = (object) array( $data = (object) array(
'title' => get_string('templatefiles', 'view') . '.' . $template->get('id'), 'title' => $foldername . $sep . $ext,
'description' => get_string('filescopiedfromviewtemplate', 'view', $template->get('title')), 'description' => get_string('filescopiedfromviewtemplate', 'view', $template->get('title')),
'owner' => $this->get('owner'), 'owner' => $this->get('owner'),
'group' => $this->get('group'), 'group' => $this->get('group'),
'institution' => $this->get('institution'), 'institution' => $this->get('institution'),
'parent' => $viewfilesfolder,
); );
$folder = new ArtefactTypeFolder(0, $data); $folder = new ArtefactTypeFolder(0, $data);
$folder->commit(); $folder->commit();
......
...@@ -63,7 +63,7 @@ $ownerlang = get_user_language($viewdata->owner); ...@@ -63,7 +63,7 @@ $ownerlang = get_user_language($viewdata->owner);
safe_require('artefact', 'file'); safe_require('artefact', 'file');
$folderid = ArtefactTypeFolder::get_folder_id(get_string_from_language($ownerlang, 'feedbackattachdirname', 'view'), $folderid = ArtefactTypeFolder::get_folder_id(get_string_from_language($ownerlang, 'feedbackattachdirname', 'view'),
get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'view'), get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'view'),
null, $viewdata->owner); null, true, $viewdata->owner);
// Create a new file object // Create a new file object
$data = (object) array('owner' => $viewdata->owner, $data = (object) array('owner' => $viewdata->owner,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment