diff options
Diffstat (limited to 'lib/models')
| -rw-r--r-- | lib/models/Courseware/CustomFiles.php | 69 | ||||
| -rw-r--r-- | lib/models/Courseware/Filesystem/CustomFile.php | 123 |
2 files changed, 192 insertions, 0 deletions
diff --git a/lib/models/Courseware/CustomFiles.php b/lib/models/Courseware/CustomFiles.php new file mode 100644 index 0000000..21a0ea6 --- /dev/null +++ b/lib/models/Courseware/CustomFiles.php @@ -0,0 +1,69 @@ +<?php + +namespace Courseware; + +use Courseware\Filesystem\CustomFile; + +/** + * This interface enables a courseware-block to have a user defined representation + * of files. This enables a block to have its own internal representation for + * arbitrary content as well allowing the import and export of said content in + * a defined and coherent way. + */ +interface CustomFiles +{ + /** + * Returns an array of CustomFile objects belongig to this block + * + * @return array<int, CustomFile> + */ + public function getCustomFiles() : array; + + /** + * create a new custom file, the contents have to be set by updateCustomFilesContent afterwards + * + * @param array $metadata any additional metadata needed, like id + * @param string $content the files contets + * + * @return CustomFile the newly created custom file + */ + public function createCustomFile(CustomFile $custom_file) : CustomFile; + + /** + * returns the contents for the custom file with the passed id + * + * @param string $id the id for the custom file + * + * @return string + */ + public function readCustomFile($id) : string; + + /** + * update the attributes of the custom file for the passed id + * + * @param string $id + * @param array $metadata + * + * @return CustomFile + */ + public function updateCustomFileMetadata($id, CustomFile $custom_file) : CustomFile; + + /** + * update the contents of the customf ile for the passed id + * + * @param string $id + * @param string $content + * + * @return CustomFile + */ + public function updateCustomFileContent($id, $content) : CustomFile; + + /** + * delete the custom file for the passed id + * + * @param string $id + * + * @return bool + */ + public function deleteCustomFile($id) : bool; +} diff --git a/lib/models/Courseware/Filesystem/CustomFile.php b/lib/models/Courseware/Filesystem/CustomFile.php new file mode 100644 index 0000000..7a6d4b8 --- /dev/null +++ b/lib/models/Courseware/Filesystem/CustomFile.php @@ -0,0 +1,123 @@ +<?php + +namespace Courseware\Filesystem; + +/** + * This class represents the metdata for a custom file in a courseware block + */ +class CustomFile +{ + /** + * The payload for this custom file, containt id, block_id and arbitrary attributes + * @var [type] + */ + protected + $payload; + + /** + * create a new custom file object, containing a self assigned id (make it unique!), + * the blocks id this custom file is referenced to and some attributes of + * your choice + * + * @param string $id an unique id for this custom file + * @param int $block_id the id of the related block + * @param array $attributes [description] + */ + public function __construct($id = null, $block_id, $attributes = []) + { + $this->payload = [ + 'id' => $id, + 'block_id' => $block_id, + 'attributes' => $attributes + ]; + } + + /** + * returns the payload: [ + * 'id' => ..., + * 'block_id' => ..., + * 'attributes' => [ ... ] + * ] + * + * @return array the payload + */ + public function getPayload() + { + return $this->payload; + } + + /** + * get id for this custom file + * + * @return string custom file id + */ + public function getId() + { + return $this->payload['id']; + } + + /** + * get id of related block + * + * @return int related block id + */ + public function getBlockId() + { + return $this->payload['block_id']; + } + + /** + * Overwrite the complete payload for this block. The payload MUST have the + * following structure: [ + * 'id' => ..., + * 'block_id' => ..., + * 'attributes' => [ ... ] + * ] + * + * @param array $payload the payload of appropriate structure + */ + public function setPayload($payload): void + { + if (!$payload['id']) { + throw new InvalidArgumentException(); + } + + if (!$payload['block_id']) { + throw new InvalidArgumentException(); + } + + $this->payload = $payload; + } + + /** + * Set the unique id for this custom file + * + * @param string $id + */ + public function setId($id): void + { + $this->payload['id'] = $id; + } + + /** + * Set the id for the related block + * + * @param int $block_id + */ + public function setBlockId($block_id): void + { + $this->payload['block_id'] = $block_id; + } + + /** + * Get the download url for this custom file + * + * @return string the download url + */ + public function getDownloadUrl() : string + { + return rtrim(\URLHelper::getUrl('jsonapi.php/v1'), '/') + . '/courseware-blocks/' . $this->payload['block_id'] + . '/custom-files/'. $this->payload['id']; + } +} |
