aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/ContainerTypes/ContainerType.php
blob: 1081eecd1bdf1797301762c23900b5e8a8552099 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php

namespace Courseware\ContainerTypes;

use Courseware\CoursewarePlugin;
use Opis\JsonSchema\Validator;

/**
 * This class represents the content of a Courseware container stored in payload.
 *
 * @author  Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @SuppressWarnings(PHPMD.StaticAccess)
 *
 * @since   Stud.IP 5.0
 */
abstract class ContainerType
{
    /**
     * Returns the initial payload of every instance of this type of container.
     *
     * @return array<mixed> the initial payload of an instance of this type of container
     */
    abstract public function initialPayload(): array;

    /**
     * Returns the JSON schema which is used to validate the payload of
     * instances of this type of container.
     *
     * @return string the JSON schema to be used
     */
    abstract public static function getJsonSchema(): string;

    /**
     * Returns a short string describing this type of container.
     *
     * @return string the short string describing this type
     */
    abstract public static function getType(): string;

    /**
     * Returns the title of this type of containers suitable to display it to the user.
     *
     * @return string the title of this type of containers
     */
    abstract public static function getTitle(): string;

    /**
     * Returns the description of this type of containers suitable to display it to the user.
     *
     * @return string the description of this type of containers
     */
    abstract public static function getDescription(): string;

    /**
     * Adds a block id to payload of the container
     *
     *  @param /Courseware/Block $block - block to be added to the container
     */
    abstract public function addBlock($block, $sectionIndex): void;

    /**
     * Returns all known types of containers: core types and plugin types as well.
     *
     * @return array<string> a list of all known types of containers;
     *                       each one a fully qualified class name
     */
    public static function getContainerTypes(): array
    {
        $containerTypes = [AccordionContainer::class, ListContainer::class, TabsContainer::class];

        try {
            foreach (\PluginEngine::getPlugins(CoursewarePlugin::class) as $plugin) {
                $containerTypes = $plugin->registerContainerTypes($containerTypes);
            }
        } catch (\Exception $e) {
            // there is nothing we can do here other than absorbing exceptions
        }

        return $containerTypes;
    }

    /**
     * Return all container types that are activated in this Stud.IP installation.
     *
     * @return iterable<string> an iterable of all activated `ContainerType` classes
     */
    public static function getActivatedContainerTypes(): iterable
    {
        return ContainerTypeState::getActivatedContainerTypes();
    }
    /**
     * @param string $containerType a short string describing a type of container; see `getType`
     *
     * @return bool true, if the given type of container is valid; false otherwise
     */
    public static function isContainerType(string $containerType): bool
    {
        return null !== self::findContainerType($containerType);
    }

    /**
     * Returns the classname of a container type whose `type` equals the given one.
     *
     * @param string $containerType a short string describing a type of container; see `getType`
     *
     * @return ?string either the classname if the given type was valid; null otherwise
     */
    public static function findContainerType(string $containerType): ?string
    {
        static $mapping;

        // memorize mapping of types to classes
        if (!$mapping) {
            $mapping = array_reduce(
                self::getContainerTypes(),
                function ($memo, $typeClass) {
                    $memo[$typeClass::getType()] = $typeClass;

                    return $memo;
                },
                []
            );
        }

        foreach ($mapping as $class) {
            if ($class::getType() === $containerType) {
                return $class;
            }
        }

        return null;
    }

    /**
     * Creates an instance of ContainerType for a given container.
     *
     * @param \Courseware\Container $container the container whose ContainerType is returned
     *
     * @return ContainerType the ContainerType associated with the given container
     */
    public static function factory(\Courseware\Container $container): ContainerType
    {
        if (!($class = self::findContainerType($container['container_type']))) {
            // TODO: Hier müsste es eine weniger allgemeine Exception geben.
            throw new \RuntimeException('Invalid `container_type` attribute in database.');
        }

        return new $class($container);
    }

    /**
     * @return bool `true`, if this `ContainerType` is activated, otherwise `false`
     */
    public static function isActivated(): bool
    {
        return in_array(static::class, ContainerTypeState::getActivatedContainerTypes());
    }

    /**
     * Activates a `ContainerType`.
     *
     * @return `true`, if this `ContainerType` was activated, otherwise `false`
     */
    public static function activate(): bool
    {
        $state = static::findContainerTypeStateOrNew();
        return $state->activate();
    }

    /**
     * Deactivates a `ContainerType`.
     *
     * @return `true`, if this `ContainerType` was deactivated, otherwise `false`
     */
    public static function deactivate(): bool
    {
        $state = static::findContainerTypeStateOrNew();
        return $state->deactivate();
    }

    private static function findContainerTypeStateOrNew(): ContainerTypeState
    {
        $state = ContainerTypeState::findOneBySql('container_type = ?', [static::class]);
        if (!$state) {
            $state = new ContainerTypeState();
            $state->container_type = static::class;
        }

        return $state;
    }

    /**
     * Validates a given payload according to the JSON schema of this type of container.
     *
     * @param mixed $payload the payload to be validated
     *
     * @return bool true, if the given payload is valid; false otherwise
     */
    public function validatePayload($payload): bool
    {
        $validator = new Validator();
        $result = $validator->validate($payload, static::getJsonSchema());
        return $result->isValid();
    }

    /** @var \Courseware\Container */
    protected $container;

    /**
     * @param \Courseware\Container $container the container associated to this type
     */
    public function __construct(\Courseware\Container $container)
    {
        $this->container = $container;
    }

    /**
     * Returns the decoded payload of the block associated with this instance.
     *
     * @return mixed the decoded payload
     */
    public function getPayload()
    {
        $decoded = $this->decodePayloadString($this->container['payload']);

        return $decoded;
    }

    /**
     * Encodes the payload and sets it in the associated block.
     *
     * @param mixed $payload the payload to be encoded
     */
    public function setPayload($payload): void
    {
        $this->container['payload'] = null === $payload ? '' : json_encode($payload, true);
    }

    // TODO: (tgloeggl) DocBlock ergänzen
    public function copyPayload(array $block_map = []): array
    {
        $payload = $this->getPayload();

        foreach ($payload['sections'] as &$section) {
            foreach ($section['blocks'] as &$block) {
                $block = (is_string($block) || is_int($block)) ? ($block_map[$block] ?? null) : null;
            }
            $section['blocks'] = array_values(array_filter($section['blocks']));
        }

        return $payload;
    }

    /**
     * Decode a given payload.
     *
     * @param string $payload the payload to be decoded
     *
     * @return mixed the decoded payload
     */
    protected function decodePayloadString(string $payload)
    {
        if ('' === $payload) {
            $decoded = $this->initialPayload();
        } else {
            $decoded = json_decode($payload, true);
            if (JSON_ERROR_NONE !== json_last_error()) {
                throw new \RuntimeException('TODO');
            }
        }

        return $decoded;
    }

    /**
     * Returns a containers width as a description.
     *
     * @return string the description
     */
    public function getContainerWidth(): string
    {
        $width = [
            'full' => _('Volle Breite'),
            'half' => _('Halbe Breite'),
            'half-center' => _('Halbe Breite (zentriert)'),
        ];
        $payload = $this->getPayload();
        if (array_key_exists($payload['colspan'], $width)) {
            return $width[$payload['colspan']];
        } else {
            return _('unbekannter Courseware-Container');
        }
    }

    /**
     * Gets the related container's html template if exists otherwise a default one, to be exported as pdf if exists.
     *
     * It turns the classname into snakecase in order to find the
     * template file in templates/courseware/container_types.
     *
     * @return \Flexi\Template|null the \Flexi\Template instance if exists, otherwise null.
     */
    public function getPdfHtmlTemplate(): ?\Flexi\Template
    {
        if (!$this->isActivated()) {
            return null;
        }

        $template = null;
        try {
            $template_name = strtosnakecase((new \ReflectionClass($this))->getShortName());
            $template_path = $GLOBALS['template_factory']->get_path() . "courseware/container_types/{$template_name}.php";
            if (file_exists($template_path)) {
                $template = $GLOBALS['template_factory']->open("courseware/container_types/{$template_name}");
            } else {
                $template = $GLOBALS['template_factory']->open("courseware/container_types/default");
            }
            $template->set_attributes([
                'title' => $this->getTitle(),
                'payload' => $this->getPayload(),
                'container' => $this->container
            ]);
        } catch (\Exception $e) {
            // it catches the exception mostly because the template file could not be found.
        }
        return $template;
    }
}