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