aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Container.php
blob: 5e4a49a47c08b1e89b401aba06454e0f55f3b4e6 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php

namespace Courseware;

use JSONArrayObject;
use User;

/**
 * Courseware's containers.
 *
 * @author  Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>
 * @author  Till Glöggler <gloeggler@elan-ev.de>
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 *
 * @property int $id database column
 * @property int $structural_element_id database column
 * @property string $owner_id database column
 * @property string $editor_id database column
 * @property string|null $edit_blocker_id database column
 * @property int $position database column
 * @property int $site database column
 * @property string $container_type database column
 * @property int $visible database column
 * @property \JSONArrayObject $payload database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property \SimpleORMapCollection|Block[] $blocks has_many Block
 * @property \User $owner belongs_to \User
 * @property \User $editor belongs_to \User
 * @property \User|null $edit_blocker belongs_to \User
 * @property StructuralElement $structural_element belongs_to StructuralElement
 * @property mixed $type additional field
 */
class Container extends \SimpleORMap implements \PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_containers';

        $config['serialized_fields']['payload'] = JSONArrayObject::class;

        $config['has_many']['blocks'] = [
            'class_name' => Block::class,
            'assoc_foreign_key' => 'container_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY position',
        ];

        $config['belongs_to']['owner'] = [
            'class_name' => User::class,
            'foreign_key' => 'owner_id',
        ];

        $config['belongs_to']['editor'] = [
            'class_name' => User::class,
            'foreign_key' => 'editor_id',
        ];
        $config['belongs_to']['edit_blocker'] = [
            'class_name' => User::class,
            'foreign_key' => 'edit_blocker_id',
        ];

        $config['belongs_to']['structural_element'] = [
            'class_name' => StructuralElement::class,
            'foreign_key' => 'structural_element_id',
        ];

        $config['additional_fields']['type'] = [
            'get' => function ($container) {
                return ContainerTypes\ContainerType::factory($container);
            },
        ];

        parent::configure($config);
    }

    /**
     * Returns the structural element this container belongs to.
     *
     * @return StructuralElement the structural element
     */
    public function getStructuralElement(): StructuralElement
    {
        return $this->structural_element;
    }

    /**
     * Returns the number of blocks contained in this.
     *
     * @return int the number of blocks contained in this
     */
    public function countBlocks(): int
    {
        return Block::countBySql('container_id = ?', [$this->id]);
    }

    public function getClipboardBackup(): string
    {
        $container = [
            'type' => 'courseware-containers',
            'id' => $this->id,
            'attributes' => [
                'position' => $this->position,
                'site' => $this->site,
                'container-type' => $this->type->getType(),
                'title' => $this->type->getTitle(),
                'visible' => $this->visible,
                'payload' => $this->type->getPayload(),
                'mkdate' => $this->mkdate,
                'chdate' => $this->chdate
            ],
            'blocks' => $this->getClipboardBackupBlocks()
        ];
        return json_encode($container);
    }

    public function getClipboardBackupBlocks(): array
    {
        return $this->blocks->map(function (Block $block) {
            return json_decode($block->getClipboardBackup(), true);
        });
    }

    /**
     * Copies this block into another structural element such that the given user is the owner of the copy.
     *
     * @param User              $user    the owner and editor of the new copy of this block
     * @param StructuralElement $element the structural element this block will be copied into
     *
     * @return array an array containing the container object and the block maps
     */
    public function copy(User $user, StructuralElement $element): array
    {
        $container = self::create([
            'structural_element_id' => $element->id,
            'owner_id' => $user->id,
            'editor_id' => $user->id,
            'edit_blocker_id' => null,
            'position' => $element->countContainers(),
            'container_type' => $this->type->getType(),
            'payload' => $this['payload'],
        ]);

        [$blockMapIds, $blockMapObjs] = $this->copyBlocks($user, $container);

        $container['payload'] = $container->type->copyPayload($blockMapIds);

        $container->store();

        return [$container, $blockMapObjs];
    }

    private function copyBlocks(User $user, Container $newContainer): array
    {
        $blockMap = [];
        $newBlockList = [];

        foreach ($this->blocks as $block) {
            $newBlock = $block->copy($user, $newContainer);
            $blockMap[$block->id] = $newBlock->id;
            $newBlockList[$block->id] = $newBlock;
        }

        return [$blockMap, $newBlockList];
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(\StoredUserData $storage)
    {
        $containers = \DBManager::get()->fetchAll(
            'SELECT * FROM cw_containers WHERE owner_id = ? OR editor_id = ?',
            [$storage->user_id, $storage->user_id]
        );
        if ($containers) {
            $storage->addTabularData(_('Courseware Abschnitte'), 'cw_containers', $containers);
        }

    }

    public static function createFromData(User $user, $data, StructuralElement $element): Container
    {
        $container = self::create([
            'structural_element_id' => $element->id,
            'owner_id' => $user->id,
            'editor_id' => $user->id,
            'edit_blocker_id' => null,
            'position' => $element->countContainers(),
            'container_type' => $data->attributes->{'container-type'},
            'payload' => $data->attributes->payload,
        ]);

        $blockMap = self::createBlocksFromData($user, $container, $data);
        $container->payload = $container->type->copyPayload($blockMap);
        $container->store();

        return $container;
    }

    private static function createBlocksFromData($user, $container, $data): array
    {
        $blockMap = [];

        foreach ($data->blocks as $block) {
            $newBlock = Block::createFromData($user, $block, $container);
            $blockMap[$block->id] = $newBlock->id;
        }

        return $blockMap;
    }
}