Commit 9cecf19d authored by Lisa Seeto's avatar Lisa Seeto Committed by Robert Lyon
Browse files

Bug 1867822: Tidy up the artefact attachments_from_id_list() function



- alter files to use attachments_from_id_list()
- alter attachments_from_id_list func params

Change-Id: I32b26586deb2f2a1b6a624f2c7afc4520a1a57ff
Signed-off-by: default avatarLisa Seeto <lisaseeto@catalyst.net.nz>
parent 8bb6d4d3
...@@ -127,20 +127,11 @@ class PluginBlocktypeRecentposts extends MaharaCoreBlocktype { ...@@ -127,20 +127,11 @@ class PluginBlocktypeRecentposts extends MaharaCoreBlocktype {
$data->tags = $tags; $data->tags = $tags;
} }
$data->owner = $blog->get('owner'); $data->owner = $blog->get('owner');
$sql = 'SELECT a.title, a.id, a.artefacttype, af.size, a.description if ($attachments = ArtefactType::attachments_from_id_list(array($data->id), $instance->get('view'))) {
FROM {artefact} a foreach ($attachments as $attachment) {
JOIN {artefact_file_files} af ON af.artefact = a.id $attachment->id = $attachment->attachment;
JOIN {artefact_attachment} at ON at.attachment = a.id $attachment->iconpath = $attachment->icon;
WHERE at.artefact = ? $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->attachment;
ORDER BY a.title';
if ($attachments = get_records_sql_array($sql, array($data->id))) {
foreach ($attachments as &$attachment) {
$f = artefact_instance_from_id($attachment->id);
$attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0));
$attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
if (isset($options['viewid'])) {
$attachment->downloadpath .= '&view=' . $options['viewid'];
}
} }
$data->attachments = $attachments; $data->attachments = $attachments;
} }
......
...@@ -1048,15 +1048,9 @@ class ArtefactTypeBlogPost extends ArtefactType { ...@@ -1048,15 +1048,9 @@ class ArtefactTypeBlogPost extends ArtefactType {
// Get the attached files. // Get the attached files.
$postids = array_map(function ($a) { return $a->id; }, $data); $postids = array_map(function ($a) { return $a->id; }, $data);
$files = ArtefactType::attachments_from_id_list($postids); $viewid = isset($viewoptions['viewid']) ? $viewoptions['viewid'] : null;
if ($files) { if ($files = ArtefactType::attachments_from_id_list($postids, $viewid)) {
safe_require('artefact', 'file');
foreach ($files as &$file) { foreach ($files as &$file) {
$params = array('id' => $file->attachment);
if (!empty($viewoptions['viewid'])) {
$params['viewid'] = $viewoptions['viewid'];
}
$file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', $params);
$data[$file->artefact]->files[] = $file; $data[$file->artefact]->files[] = $file;
} }
} }
......
...@@ -56,7 +56,7 @@ class PluginBlocktypeTextbox extends MaharaCoreBlocktype { ...@@ -56,7 +56,7 @@ class PluginBlocktypeTextbox extends MaharaCoreBlocktype {
foreach ($attachments as &$attachment) { foreach ($attachments as &$attachment) {
$f = artefact_instance_from_id($attachment->id); $f = artefact_instance_from_id($attachment->id);
$attachment->size = $f->describe_size(); $attachment->size = $f->describe_size();
$attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => isset($options['viewid']) ? $options['viewid'] : 0)); $attachment->iconpath = $f->get_icon(array('id' => $attachment->id, 'viewid' => $instance->get('view')));
$attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id; $attachment->downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $attachment->id;
if (isset($viewid)) { if (isset($viewid)) {
$attachment->downloadpath .= '&view=' . $viewid; $attachment->downloadpath .= '&view=' . $viewid;
......
...@@ -164,11 +164,8 @@ $noteids = array(); ...@@ -164,11 +164,8 @@ $noteids = array();
if ($data) { if ($data) {
$noteids = array_keys($data); $noteids = array_keys($data);
} }
$files = ArtefactType::attachments_from_id_list($noteids); if ($files = ArtefactType::attachments_from_id_list($noteids)) {
if ($files) {
safe_require('artefact', 'file');
foreach ($files as $file) { foreach ($files as $file) {
$file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', array('id' => $file->attachment));
$data[$file->artefact]->files[] = $file; $data[$file->artefact]->files[] = $file;
} }
} }
......
...@@ -158,20 +158,17 @@ if ($data) { ...@@ -158,20 +158,17 @@ if ($data) {
$n->deleteform = pieform(deletenote_form($id, $n)); $n->deleteform = pieform(deletenote_form($id, $n));
} }
} }
// Get the attached files. // Get the attached files.
$noteids = array(); $noteids = array();
if ($data) { if ($data) {
$noteids = array_keys($data); $noteids = array_keys($data);
} }
$files = ArtefactType::attachments_from_id_list($noteids); if ($files = ArtefactType::attachments_from_id_list($noteids)) {
if ($files) {
safe_require('artefact', 'file');
foreach ($files as $file) { foreach ($files as $file) {
$file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', array('id' => $file->attachment));
$data[$file->artefact]->files[] = $file; $data[$file->artefact]->files[] = $file;
} }
} }
// Add Attachments count for each Note // Add Attachments count for each Note
if ($data) { if ($data) {
foreach ($data as $item) { foreach ($data as $item) {
......
...@@ -1210,7 +1210,7 @@ abstract class ArtefactType implements IArtefactType { ...@@ -1210,7 +1210,7 @@ abstract class ArtefactType implements IArtefactType {
return array(); return array();
} }
public function attachments_from_id_list($artefactids) { public function attachments_from_id_list($artefactids, $viewid=null, $itemid=null) {
if (empty($artefactids)) { if (empty($artefactids)) {
return array(); return array();
} }
...@@ -1219,19 +1219,43 @@ abstract class ArtefactType implements IArtefactType { ...@@ -1219,19 +1219,43 @@ abstract class ArtefactType implements IArtefactType {
// move all these attachment functions to the artefact file // move all these attachment functions to the artefact file
// plugin, or we could allow artefact plugins to add stuff // plugin, or we could allow artefact plugins to add stuff
// to this query. // to this query.
$attachments = get_records_sql_array(' $sql = '
SELECT SELECT
aa.artefact, aa.attachment, a.artefacttype, a.title, a.description, f.size aa.artefact, aa.attachment, a.artefacttype, a.title, a.description, f.size
FROM {artefact_attachment} aa FROM {artefact_attachment} aa
INNER JOIN {artefact} a ON aa.attachment = a.id INNER JOIN {artefact} a ON aa.attachment = a.id
LEFT JOIN {artefact_file_files} f ON a.id = f.artefact LEFT JOIN {artefact_file_files} f ON a.id = f.artefact
WHERE aa.artefact IN (' . join(', ', array_map('intval', $artefactids)) . ')', array()); WHERE aa.artefact IN (' . join(', ', array_map('intval', $artefactids)) . ')';
$values = array();
if ($itemid) {
$sql .= ' AND aa.item = ?';
array_push($values, $itemid);
}
$attachments = get_records_sql_array($sql, $values);
if (!$attachments) { if (!$attachments) {
return array(); return array();
} }
$options = array();
if ($itemid) {
$options['itemid'] = $itemid;
}
if ($viewid) {
$options['viewid'] = $viewid;
}
safe_require('artefact', 'file');
foreach ($attachments as &$file) {
$options['id'] = $file->attachment;
$file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', $options);
}
return $attachments; return $attachments;
} }
public function tags_from_id_list($artefactids) { public function tags_from_id_list($artefactids) {
if (empty($artefactids)) { if (empty($artefactids)) {
return array(); return array();
......
...@@ -54,14 +54,9 @@ foreach ($data as &$row) { ...@@ -54,14 +54,9 @@ foreach ($data as &$row) {
// Add artefact attachments it there are any // Add artefact attachments it there are any
$datawithattachments = array(); $datawithattachments = array();
foreach ($data as $record) { foreach ($data as $record) {
$sql = 'SELECT a.title, a.id, af.size if ($attachments = ArtefactType::attachments_from_id_list(array($record->artefact), null, $record->id)) {
FROM {artefact} a $record->attachments = $attachments;
JOIN {artefact_file_files} af ON af.artefact = a.id }
JOIN {artefact_attachment} at ON at.attachment = a.id
WHERE at.artefact = ? AND at.item = ?
ORDER BY a.title';
$attachments = get_records_sql_array($sql, array($record->artefact, $record->id));
$record->attachments = $attachments;
if (!is_array($attachments)) { if (!is_array($attachments)) {
$record->clipcount = 0; $record->clipcount = 0;
} }
......
...@@ -2078,11 +2078,8 @@ class ArtefactTypeResumeGoalAndSkill extends ArtefactTypeResume { ...@@ -2078,11 +2078,8 @@ class ArtefactTypeResumeGoalAndSkill extends ArtefactTypeResume {
if ($record) { if ($record) {
$record->exists = 1; $record->exists = 1;
// Add attachments // Add attachments
$files = ArtefactType::attachments_from_id_list(array($record->id)); if ($files = ArtefactType::attachments_from_id_list(array($record->id))) {
if ($files) { foreach ($files as $file) {
safe_require('artefact', 'file');
foreach ($files as &$file) {
$file->icon = call_static_method(generate_artefact_class_name($file->artefacttype), 'get_icon', array('id' => $file->attachment));
$record->files[] = $file; $record->files[] = $file;
} }
$record->count = count($files); $record->count = count($files);
......
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