aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Block.php
blob: d311e0fc4911429bc8ff310f2b96451f929ef784 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php

namespace Courseware;

use User;

/**
 * Courseware's blocks.
 *
 * @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                              $container_id    database column
 * @property string                           $owner_id        database column
 * @property string                           $editor_id       database column
 * @property string                           $edit_blocker_id database column
 * @property int                              $position        database column
 * @property string                           $block_type      database column
 * @property int                              $visible         database column
 * @property string                           $payload         database column
 * @property int                              $mkdate          database column
 * @property int                              $chdate          database column
 * @property \Courseware\BlockTypes\BlockType $type            computed column read/write
 * @property string                           $files           computed column
 * @property \SimpleORMapCollection           $data_fields     has_many Courseware\UserDataField
 * @property \SimpleORMapCollection           $comments        has_many Courseware\BlockComment
 * @property \SimpleORMapCollection           $block_feedback  has_many Courseware\BlockFeedback
 * @property \SimpleORMapCollection           $progresses      has_many Courseware\UserProgress
 * @property \User                            $owner           belongs_to User
 * @property \User                            $editor          belongs_to User
 * @property \User                            $edit_blocker    belongs_to User
 * @property \Courseware\Container            $container       belongs_to Courseware\Container
 */
class Block extends \SimpleORMap implements \PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'cw_blocks';

        $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']['container'] = [
            'class_name' => Container::class,
            'foreign_key' => 'container_id',
        ];

        $config['has_many']['data_fields'] = [
            'class_name' => UserDataField::class,
            'assoc_foreign_key' => 'block_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY chdate',
        ];

        $config['has_many']['comments'] = [
            'class_name' => BlockComment::class,
            'assoc_foreign_key' => 'block_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY chdate',
        ];

        $config['has_many']['block_feedback'] = [
            'class_name' => BlockFeedback::class,
            'assoc_foreign_key' => 'block_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY chdate',
        ];

        $config['has_many']['progresses'] = [
            'class_name' => UserProgress::class,
            'assoc_foreign_key' => 'block_id',
            'on_delete' => 'delete',
            'on_store' => 'store',
            'order_by' => 'ORDER BY chdate',
        ];

        $config['additional_fields']['type'] = [
            'get' => function ($block) {
                return BlockTypes\BlockType::factory($block);
            },
            'set' => false,
        ];

        $config['additional_fields']['files'] = [
            'get' => function ($block) {
                return array_filter($block->type->getFiles(), function ($file_ref) {
                    if ($file_ref) {
                        $file = $file_ref->getFileType();
                        return $file->isDownloadable();
                    } else {
                        return false;
                    }
                });
            },
        ];

        parent::configure($config);
    }

    /**
     * Returns whether this block is blocked from editing.
     */
    public function isBlocked(): bool
    {
        return null != $this->edit_blocker;
    }

    /**
     * Returns who is blocking this block from editing if at all.
     *
     * @return ?string either the blocker's ID or null if this block is not blocked
     */
    public function getBlockerUserId(): ?string
    {
        if ($this->isBlocked()) {
            return $this->edit_blocker->id;
        } else {
            return null;
        }
    }

    /**
     * Activates the edit block for a user.
     *
     * @param string $userId the ID of the user blocking this block
     */
    public function setBlockerId(string $userId): void
    {
        $this->edit_blocker_id = $userId;
    }

    /**
     * Returns the full name of the user currently blocking this block.
     *
     * @return string the full name of the user currently blocking this block
     */
    public function getBlockerName(): string
    {
        /** @var \User $user */
        $user = User::find($this->edit_blocker_id);

        return $user->getFullName();
    }

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

        return json_encode($block, true);
    }

    /**
     * Copies this block into another container 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 Container $container the container this block will be copied into
     *
     * @return Block the copy of this block
     */
    public function copy(User $user, Container $container, $sectionIndex = null): Block
    {
        /** @var StructuralElement $struct */
        $struct = $container->structural_element;
        $rangeId = $struct->getRangeId();

        $block = self::create([
            'container_id' => $container->id,
            'owner_id' => $user->id,
            'editor_id' => $user->id,
            'edit_blocker_id' => null,
            'position' => $container->countBlocks(),
            'block_type' => $this->type->getType(),
            'payload' => json_encode($this->type->copyPayload($rangeId)),
            'visible' => 1,
        ]);

        //update Container payload
        $container->type->addBlock($block, $sectionIndex);
        $container->store();

        return $block;
    }

    public static function createFromData(User $user, $data, Container $container, $sectionIndex = null): Block
    {
        $struct = $container->structural_element;
        $rangeId = $struct->getRangeId();

        $block = self::create([
            'container_id' => $container->id,
            'owner_id' => $user->id,
            'editor_id' => $user->id,
            'edit_blocker_id' => null,
            'position' => $container->countBlocks(),
            'block_type' => $data->attributes->{'block-type'},
            'visible' => 1,
        ]);

        $dataPayload = (array)$data->attributes->payload;
        $block->payload = json_encode($block->type->copyPayload('', $dataPayload), true);
        $block->store();

        //update Container payload
        $container->type->addBlock($block, $sectionIndex);
        $container->store();

        return $block;
    }

    public function getBlockType(): ?string
    {
        if ($this->type->findBlockType($this->block_type)) {
            return $this->block_type;
        } else {
            $this->payload = json_encode(array(
                'original_block_type' => $this->block_type
            ));
            return 'error';
        }
    }

    public function getStructuralElement(): ?StructuralElement
    {
        $sql = 'SELECT se.*
                FROM cw_blocks b
                JOIN cw_containers c ON c.id = b.container_id
                JOIN cw_structural_elements se ON se.id = c.structural_element_id
                WHERE  b.id = ?';
        $structuralElement = \DBManager::get()->fetchOne($sql, [$this->getId()]);
        if (!count($structuralElement)) {
            return null;
        }

        return StructuralElement::build($structuralElement, false);
    }

    /**
     * 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)
    {
        $blocks = \DBManager::get()->fetchAll(
            'SELECT * FROM cw_blocks WHERE owner_id = ? OR editor_id = ?',
            [$storage->user_id, $storage->user_id]
        );
        if ($blocks) {
            $storage->addTabularData(_('Courseware Blöcke'), 'cw_blocks', $blocks);
        }
        
    }
}