aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Filesystem/CustomFile.php
blob: 7a6d4b8ce6878eba02b4ea87cca0f6e9143d324d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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'];
    }
}