Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Delete related resources on QtiTestDeletedEvent #632

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions models/classes/Command/DeleteItemCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA.
*/

declare(strict_types=1);

namespace oat\taoItems\model\Command;

use core_kernel_classes_Resource;

class DeleteItemCommand
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
{
private core_kernel_classes_Resource $resource;
private bool $deleteRelatedAssets;

public function __construct(core_kernel_classes_Resource $resource, bool $deleteRelatedAssets = false)
{
$this->resource = $resource;
$this->deleteRelatedAssets = $deleteRelatedAssets;
}

public function getResource(): core_kernel_classes_Resource
{
return $this->resource;
}

public function mustDeleteRelatedAssets(): bool
{
return $this->deleteRelatedAssets;
}
}
50 changes: 39 additions & 11 deletions models/classes/class.ItemsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
* (under the project TAO-TRANSFER);
* 2009-2012 (update and modification) Public Research Centre Henri Tudor
* (under the project TAO-SUSTAIN & TAO-DEV);
* 2012-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT)
* 2012-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT)
*/

use oat\taoItems\model\Command\DeleteItemCommand;
use oat\taoItems\model\TaoItemOntology;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\oatbox\filesystem\Directory;
Expand Down Expand Up @@ -132,6 +133,37 @@ public function isItemClass(core_kernel_classes_Class $clazz)
return $clazz->equals($this->getRootClass()) || $clazz->isSubClassOf($this->getRootClass());
}

public function delete(DeleteItemCommand $command): void
{
$resource = $command->getResource();

if (LockManager::getImplementation()->isLocked($resource)) {
$userId = common_session_SessionManager::getSession()->getUser()->getIdentifier();
LockManager::getImplementation()->releaseLock($resource, $userId);
}

$result = $this->deleteItemContent($resource) && parent::deleteResource($resource);

if (!$result) {
throw new Exception(
sprintf(
'Error deleting item content for resource "%s" [%s]',
$resource->getLabel(),
$resource->getUri()
)
);
}

$this->getEventManager()->trigger(
new ItemRemovedEvent(
$resource->getUri(),
[
ItemRemovedEvent::PAYLOAD_KEY_DELETE_RELATED_ASSETS => $command->mustDeleteRelatedAssets(),
]
)
);
}

/**
* please call deleteResource() instead
* @deprecated
Expand All @@ -146,21 +178,17 @@ public function deleteItem(core_kernel_classes_Resource $item)
* @param core_kernel_classes_Resource $resource
* @throws common_exception_Unauthorized
* @return boolean
* @deprecated use self::delete()
*/
public function deleteResource(core_kernel_classes_Resource $resource)
{
if (LockManager::getImplementation()->isLocked($resource)) {
$userId = common_session_SessionManager::getSession()->getUser()->getIdentifier();
LockManager::getImplementation()->releaseLock($resource, $userId);
}

$result = $this->deleteItemContent($resource) && parent::deleteResource($resource);
try {
$this->delete(new DeleteItemCommand($resource));

if ($result) {
$this->getEventManager()->trigger(new ItemRemovedEvent($resource->getUri()));
return true;
} catch (Throwable $exception) {
return false;
}

return $result;
}

/**
Expand Down
38 changes: 14 additions & 24 deletions models/classes/event/ItemRemovedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,39 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2016 (original work) Open Assessment Technologies SA
*
* Copyright (c) 2016-2023 (original work) Open Assessment Technologies SA.
*/

namespace oat\taoItems\model\event;

use JsonSerializable;
use oat\oatbox\event\Event;
use JsonSerializable;

class ItemRemovedEvent implements Event, JsonSerializable
{
/** @var string */
protected $itemUri;
public const PAYLOAD_KEY_DELETE_RELATED_ASSETS = 'deleteRelatedAssets';
public const PAYLOAD_KEY_ITEM_URI = 'itemUri';

/**
* @param String $itemUri
*/
public function __construct($itemUri)
protected string $itemUri;

private array $payload;

public function __construct(string $itemUri, array $payload = [])
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->itemUri = $itemUri;
$this->payload = $payload;
}


/**
* Return a unique name for this event
* @see \oat\oatbox\event\Event::getName()
*/
public function getName()
{
return get_class($this);
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return [
'itemUri' => $this->itemUri
];
return array_merge(
[self::PAYLOAD_KEY_ITEM_URI => $this->itemUri],
$this->payload
);
}
}
Loading