Commit 0f7251b3 authored by Robert Lyon's avatar Robert Lyon
Browse files

Checking to make sure artefact/block library still exist (Bug #1319634)



Sometimes views will have artefacts/block instances on them but the
plugin has been deleted. This should not stop a user being deleted.

To test:
Add a plugin to the site (eg openbadgesdisplayer block plugin, or cpds
artefact plugin) and install it.
Add a page and add a block to the page.
Delete the dir from the site for the plugin
Try to bulk delete the user

Change-Id: Ib23ad79d9f1ffa0bca56ad7ccb6083333527e64c
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 2a1b9c77
......@@ -1520,7 +1520,16 @@ function artefact_can_render_to($type, $format) {
return in_array($format, call_static_method(generate_artefact_class_name($type), 'get_render_list'));
}
function artefact_instance_from_id($id) {
/**
* Get artefact instance from id
* @param int $id of the artefact
* @param int $deleting If we are wanting to delete the artefact we need
* to check that the artefact plugin still exists on
* the server.
*
* @result mixed Either the artefact object or false if plugin has gone.
*/
function artefact_instance_from_id($id, $deleting = false) {
$sql = 'SELECT a.*, i.plugin
FROM {artefact} a
JOIN {artefact_installed_type} i ON a.artefacttype = i.name
......@@ -1529,8 +1538,17 @@ function artefact_instance_from_id($id) {
throw new ArtefactNotFoundException(get_string('artefactnotfound', 'mahara', $id));
}
$classname = generate_artefact_class_name($data->artefacttype);
safe_require('artefact', $data->plugin);
return new $classname($id, $data);
if ($deleting) {
safe_require('artefact', $data->plugin, 'lib.php', 'require_once', true);
if (is_callable($classname . '::delete')) {
return new $classname($id, $data);
}
return false;
}
else {
safe_require('artefact', $data->plugin);
return new $classname($id, $data);
}
}
/**
* This function returns the current title of an artefact's blockinstance
......
......@@ -1139,9 +1139,11 @@ class BlockInstance {
handle_event('deleteblockinstance', $this);
db_begin();
safe_require('blocktype', $this->get('blocktype'));
call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'delete_instance', $this);
safe_require('blocktype', $this->get('blocktype'), 'lib.php', 'require_once', true);
$classname = generate_class_name('blocktype', $this->get('blocktype'));
if (is_callable($classname . '::delete_instance')) {
call_static_method($classname, 'delete_instance', $this);
}
delete_records('view_artefact', 'block', $this->id);
delete_records('block_instance', 'id', $this->id);
db_commit();
......
......@@ -1437,8 +1437,10 @@ function delete_user($userid) {
if ($artefactids) {
foreach ($artefactids as $artefactid) {
try {
$a = artefact_instance_from_id($artefactid);
$a->delete();
$a = artefact_instance_from_id($artefactid, true);
if ($a) {
$a->delete();
}
}
catch (ArtefactNotFoundException $e) {
// Awesome, it's already gone.
......
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