Commit 506068c6 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 <>
parent 30a3e92f
......@@ -1590,17 +1590,35 @@ 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) {
$sql = 'SELECT a.*, i.plugin
FROM {artefact} a
* 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 =
WHERE = ?';
if (!$data = get_record_sql($sql, array($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
......@@ -1122,9 +1122,11 @@ class BlockInstance {
handle_event('deleteblockinstance', $this);
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);
......@@ -1364,8 +1364,10 @@ function delete_user($userid) {
if ($artefactids) {
foreach ($artefactids as $artefactid) {
try {
$a = artefact_instance_from_id($artefactid);
$a = artefact_instance_from_id($artefactid, true);
if ($a) {
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