Commit ccd5e53d authored by Nigel Cunningham's avatar Nigel Cunningham
Browse files

(Bug1364691) Add artefact_instances_from_ids function.



Add support for getting multiple artefacts from an array
of IDs, all at once. The main saving is that we then do
one SQL query for the whole array instead of one for each
element in the array.

In testing admin/users/bulkexport.php, this reduced
execution time for an export of 1000 users by 12 seconds
(approx 10 percent).

Change-Id: I51867cd4693a7572dd609b98b6503251e473078a
Signed-off-by: default avatarNigel Cunningham <nigelc@catalyst-au.net>
parent e20841c0
......@@ -1565,6 +1565,34 @@ function artefact_can_render_to($type, $format) {
return in_array($format, call_static_method(generate_artefact_class_name($type), 'get_render_list'));
}
/**
* Get artefact instances from ids
* @param array of int $ids of the artefacts
*
* @result mixed Either the artefact object or false if plugin has gone.
*/
function artefact_instances_from_ids($ids) {
$result = array();
if (empty($ids)) {
return $result;
}
$sql = 'SELECT a.*, i.plugin
FROM {artefact} a
JOIN {artefact_installed_type} i ON a.artefacttype = i.name
WHERE a.id IN (' . join(',', $ids) . ')';
if (!$data = get_records_sql_array($sql, NULL)) {
throw new ArtefactNotFoundException(get_string('artefactsnotfound', 'mahara', implode(', ', $ids)));
}
foreach ($data as $item) {
$classname = generate_artefact_class_name($item->artefacttype);
safe_require('artefact', $item->plugin);
$result[$item->id] = new $classname($item->id, $item);
}
return $result;
}
/**
* Get artefact instance from id
* @param int $id of the artefact
......
......@@ -279,16 +279,29 @@ abstract class PluginExport extends Plugin implements IPluginExport {
}
}
$typestoplugins = get_records_assoc('artefact_installed_type');
$ids_to_get = array();
foreach ($tmpartefacts as $a) {
if ($a instanceof ArtefactType) {
continue;
}
else if (is_object($a) && isset($a->id)) {
$ids_to_get[] = $a->id;
}
else if (is_numeric($a)) {
$ids_to_get[] = $a;
}
}
$artefacts = artefact_instances_from_ids($ids_to_get);
foreach ($tmpartefacts as $a) {
$artefact = null;
if ($a instanceof ArtefactType) {
$artefact = $a;
}
else if (is_object($a) && isset($a->id)) {
$artefact = artefact_instance_from_id($a->id);
$artefact = $artefacts[$a->id];
}
else if (is_numeric($a)) {
$artefact = artefact_instance_from_id($a);
$artefact = $artefacts[$a];
}
if (is_null($artefact)) {
throw new ParamOutOfRangeException("Invalid artefact $a");
......
......@@ -100,6 +100,7 @@ $string['previewimagegenerationfailed'] = 'Sorry, there was a problem generating
$string['artefactnotfoundmaybedeleted'] = "Artefact with id %s not found (maybe it has been deleted already?)";
$string['artefactnotfound'] = 'Artefact with id %s not found';
$string['artefactsnotfound'] = 'No artefact(s) found with the id(s): %s';
$string['artefactnotinview'] = 'Artefact %s not in page %s';
$string['artefactonlyviewableinview'] = 'Artefacts of this type are only viewable within a page.';
$string['notartefactowner'] = 'You do not own this artefact.';
......
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