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 {
}
public static function blogfiles_folder_id($create = true) {
global $USER;
$name = get_string('blogfilesdirname', 'artefact.blog');
$description = get_string('blogfilesdirdescription', 'artefact.blog');
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
public static function change_language($userid, $oldlang, $newlang) {
$oldname = get_string_from_language($oldlang, 'blogfilesdirname', 'artefact.blog');
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)) {
return;
}
......
......@@ -395,25 +395,13 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
// either in adminfiles or with a specific owner.
public static function file_exists($title, $owner, $folder, $institution=null, $group=null) {
$filetypesql = "('" . join("','", PluginArtefactFile::get_artefact_types()) . "')";
$phvals = array($title);
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;
}
$ownersql = artefact_owner_sql($owner, $group, $institution);
return get_field_sql('SELECT a.id FROM {artefact} a
LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id
WHERE a.title = ?
AND ' . $ownersql . '
AND a.' . $ownersql . '
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;
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();
foreach ($filetypes as $k => $v) {
if ($v == 'folder') {
......@@ -586,26 +574,10 @@ JAVASCRIPT;
}
$filetypesql = "('" . join("','", $filetypes) . "')";
$phvals = array();
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;
}
$ownersql = artefact_owner_sql($owner, $group, $institution);
return (object) array(
'files' => count_records_select('artefact', "artefacttype IN $filetypesql AND $ownersql", $phvals),
'folders' => count_records_select('artefact', "artefacttype = 'folder' AND $ownersql", $phvals)
'files' => count_records_select('artefact', "artefacttype IN $filetypesql AND $ownersql", array()),
'folders' => count_records_select('artefact', "artefacttype = 'folder' AND $ownersql", array())
);
}
......@@ -1023,33 +995,28 @@ class ArtefactTypeFolder extends ArtefactTypeFileBase {
}
}
public static function get_folder_by_name($name, $parentfolderid=null, $userid=null) {
if (empty($userid)) {
global $USER;
$userid = $USER->id;
}
public static function get_folder_by_name($name, $parentfolderid=null, $userid=null, $groupid=null, $institution=null) {
$parentclause = $parentfolderid ? 'parent = ' . $parentfolderid : 'parent IS NULL';
$ownerclause = artefact_owner_sql($userid, $groupid, $institution);
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));
}
// 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) {
if (empty($userid)) {
global $USER;
$userid = $USER->id;
}
if (!$record = self::get_folder_by_name($name, $parentfolderid, $userid)) {
public static function get_folder_id($name, $description, $parentfolderid=null, $create=true, $userid=null, $groupid=null, $institution=null) {
if (!$record = self::get_folder_by_name($name, $parentfolderid, $userid, $groupid, $institution)) {
if (!$create) {
return false;
}
$data = new StdClass;
$data->title = $name;
$data->description = $description;
$data->owner = $userid;
$data->group = $groupid;
$data->institution = $institution;
$data->parent = $parentfolderid;
$f = new ArtefactTypeFolder(0, $data);
$f->set('owner', $userid);
$f->set('parent', $parentfolderid);
$f->commit();
return $f->get('id');
}
......
......@@ -911,4 +911,17 @@ function artefact_get_descendants($new) {
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') {
$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
// 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';
$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['copiedblocksandartefactsfromtemplate'] = 'Copied %d blocks and %d artefacts from %s';
$string['templatefiles'] = 'templatefiles';
$string['filescopiedfromviewtemplate'] = 'Files copied from %s';
$string['viewfilesdirname'] = 'viewfiles';
$string['viewfilesdirdesc'] = 'Files from copied views';
?>
......@@ -1785,12 +1785,28 @@ class View {
// Create parent folder for files that have no parent
if ($artefactcopies) {
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(
'title' => get_string('templatefiles', 'view') . '.' . $template->get('id'),
'title' => $foldername . $sep . $ext,
'description' => get_string('filescopiedfromviewtemplate', 'view', $template->get('title')),
'owner' => $this->get('owner'),
'group' => $this->get('group'),
'institution' => $this->get('institution'),
'parent' => $viewfilesfolder,
);
$folder = new ArtefactTypeFolder(0, $data);
$folder->commit();
......
......@@ -63,7 +63,7 @@ $ownerlang = get_user_language($viewdata->owner);
safe_require('artefact', 'file');
$folderid = ArtefactTypeFolder::get_folder_id(get_string_from_language($ownerlang, 'feedbackattachdirname', 'view'),
get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'view'),
null, $viewdata->owner);
null, true, $viewdata->owner);
// Create a new file object
$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